diff options
Diffstat (limited to 'mso-api-handlers')
10 files changed, 588 insertions, 407 deletions
diff --git a/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandler/filters/RequestIdFilter.java b/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandler/filters/RequestIdFilter.java index 2fd426dec2..b81ee58aa4 100644 --- a/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandler/filters/RequestIdFilter.java +++ b/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandler/filters/RequestIdFilter.java @@ -23,6 +23,7 @@ import java.io.IOException; import javax.annotation.Priority; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.core.UriInfo; import javax.ws.rs.ext.Provider; import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.onap.so.apihandler.common.ErrorNumbers; @@ -52,15 +53,20 @@ public class RequestIdFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext context) throws IOException { String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID); + UriInfo uriInfo = context.getUriInfo(); + String requestURI = uriInfo.getPath(); - logger.info("Checking if requestId: {} already exists in requestDb InfraActiveRequests table", requestId); - InfraActiveRequests infraActiveRequests = infraActiveRequestsClient.getInfraActiveRequestbyRequestId(requestId); + if (!requestURI.contains("orchestrationRequests")) { + logger.info("Checking if requestId: {} already exists in requestDb InfraActiveRequests table", requestId); + InfraActiveRequests infraActiveRequests = + infraActiveRequestsClient.getInfraActiveRequestbyRequestId(requestId); - if (infraActiveRequests != null) { - logger.error( - "RequestId: {} already exists in RequestDB InfraActiveRequests table, throwing DuplicateRequestIdException", - requestId); - throw new DuplicateRequestIdException(createRequestError(requestId, "InfraActiveRequests")); + if (infraActiveRequests != null) { + logger.error( + "RequestId: {} already exists in RequestDB InfraActiveRequests table, throwing DuplicateRequestIdException", + requestId); + throw new DuplicateRequestIdException(createRequestError(requestId, "InfraActiveRequests")); + } } } diff --git a/mso-api-handlers/mso-api-handler-common/src/test/java/org/onap/so/apihandler/filters/RequestIdFilterTest.java b/mso-api-handlers/mso-api-handler-common/src/test/java/org/onap/so/apihandler/filters/RequestIdFilterTest.java index 8716047603..421136d402 100644 --- a/mso-api-handlers/mso-api-handler-common/src/test/java/org/onap/so/apihandler/filters/RequestIdFilterTest.java +++ b/mso-api-handlers/mso-api-handler-common/src/test/java/org/onap/so/apihandler/filters/RequestIdFilterTest.java @@ -23,9 +23,13 @@ package org.onap.so.apihandler.filters; import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import java.io.IOException; +import java.net.URI; import java.util.Collections; import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.core.UriInfo; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -53,6 +57,9 @@ public class RequestIdFilterTest { @Mock private RequestsDbClient requestsDbClient; + @Mock + private UriInfo uriInfo; + @InjectMocks @Spy private RequestIdFilter requestIdFilter; @@ -87,12 +94,35 @@ public class RequestIdFilterTest { doReturn(infraActiveRequests).when(requestsDbClient).getInfraActiveRequestbyRequestId(requestId); doReturn(error).when(requestIdFilter).createRequestError(REQUEST_ID, "InfraActiveRequests"); + doReturn("/onap/so/infra/serviceInstantiation/v7/serviceInstances").when(uriInfo).getPath(); + doReturn(uriInfo).when(mockContext).getUriInfo(); thrown.expect(DuplicateRequestIdException.class); thrown.expectMessage("HTTP 400 Bad Request"); + + requestIdFilter.filter(mockContext); } + + @Test + public void filterTestInfraSkipRequestIdLookup() throws IOException { + String requestId = REQUEST_ID; + MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId); + + // ExpectedRecord InfraActiveRequests + InfraActiveRequests infraActiveRequests = new InfraActiveRequests(); + infraActiveRequests.setRequestId(REQUEST_ID); + + doReturn("onap/so/infra/orchestrationRequests/v7/" + REQUEST_ID).when(uriInfo).getPath(); + doReturn(uriInfo).when(mockContext).getUriInfo(); + + verify(requestsDbClient, never()).getInfraActiveRequestbyRequestId(REQUEST_ID); + + requestIdFilter.filter(mockContext); + } + + @Test public void createRequestErrorTest() throws IOException { RequestError requestError = getRequestError(); diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/CamundaRequestHandler.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/CamundaRequestHandler.java index e56eb422d8..fb7ab3a61e 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/CamundaRequestHandler.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/CamundaRequestHandler.java @@ -7,11 +7,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.bind.DatatypeConverter; -import org.apache.http.HttpStatus; import org.camunda.bpm.engine.impl.persistence.entity.HistoricActivityInstanceEntity; import org.camunda.bpm.engine.impl.persistence.entity.HistoricProcessInstanceEntity; -import org.onap.so.apihandler.common.ErrorNumbers; -import org.onap.so.apihandlerinfra.exceptions.ContactCamundaException; +import org.onap.logging.filter.spring.SpringClientPayloadFilter; +import org.onap.so.logging.jaxrs.filter.SOSpringClientFilter; import org.onap.so.utils.CryptoUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,6 +21,8 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; +import org.springframework.http.client.BufferingClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.retry.policy.SimpleRetryPolicy; import org.springframework.retry.support.RetryTemplate; import org.springframework.stereotype.Component; @@ -33,15 +34,16 @@ import org.springframework.web.client.RestTemplate; public class CamundaRequestHandler { private static Logger logger = LoggerFactory.getLogger(CamundaRequestHandler.class); - - @Autowired - private RestTemplate restTemplate; + private static final String TIMEOUT = "30000"; + private static final String RETRY_TIMEOUT = "15000"; + private static final String TIMEOUT_PROPERTY = "mso.camunda.request.timeout"; + private static final String RETRY_TIMEOUT_PROPERTY = "mso.camunda.request.timeout.retry"; @Autowired private Environment env; - public ResponseEntity<List<HistoricProcessInstanceEntity>> getCamundaProcessInstanceHistory(String requestId) { - RetryTemplate retryTemplate = setRetryTemplate(); + public ResponseEntity<List<HistoricProcessInstanceEntity>> getCamundaProcessInstanceHistory(String requestId, + boolean retry) { String path = env.getProperty("mso.camunda.rest.history.uri") + requestId; String targetUrl = env.getProperty("mso.camundaURL") + path; HttpHeaders headers = @@ -49,86 +51,90 @@ public class CamundaRequestHandler { HttpEntity<?> requestEntity = new HttpEntity<>(headers); - return retryTemplate.execute(context -> { - if (context.getLastThrowable() != null) { - logger.error("Retrying: Last call resulted in exception: ", context.getLastThrowable()); - } - if (context.getRetryCount() == 0) { - logger.info("Querying Camunda for process-instance history for requestId: {}", requestId); - } else { - logger.info("Retry: {} of 3. Querying Camunda for process-instance history for requestId: {}", - context.getRetryCount(), requestId); - } - return restTemplate.exchange(targetUrl, HttpMethod.GET, requestEntity, - new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); - }); - } - - protected ResponseEntity<List<HistoricActivityInstanceEntity>> getCamundaActivityHistory(String processInstanceId, - String requestId) throws ContactCamundaException { - RetryTemplate retryTemplate = setRetryTemplate(); - String path = env.getProperty("mso.camunda.rest.activity.uri") + processInstanceId; - String targetUrl = env.getProperty("mso.camundaURL") + path; - HttpHeaders headers = - setCamundaHeaders(env.getRequiredProperty("mso.camundaAuth"), env.getRequiredProperty("mso.msoKey")); - HttpEntity<?> requestEntity = new HttpEntity<>(headers); - try { + if (retry) { + RestTemplate restTemplate = getRestTemplate(retry); + RetryTemplate retryTemplate = getRetryTemplate(); return retryTemplate.execute(context -> { if (context.getLastThrowable() != null) { logger.error("Retrying: Last call resulted in exception: ", context.getLastThrowable()); } if (context.getRetryCount() == 0) { - logger.info( - "Querying Camunda for activity-instance history for processInstanceId: {}, for requestId: {}", - processInstanceId, requestId); + logger.info("Querying Camunda for process-instance history for requestId: {}", requestId); } else { - logger.info( - "Retry: {} of 3. Querying Camunda for activity-instance history for processInstanceId: {}, for requestId: {}", - context.getRetryCount(), processInstanceId, requestId); + logger.info("Retry: Querying Camunda for process-instance history for requestId: {}", + context.getRetryCount(), requestId); } - return restTemplate.exchange(targetUrl, HttpMethod.GET, requestEntity, - new ParameterizedTypeReference<List<HistoricActivityInstanceEntity>>() {}); + new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); }); - - } catch (RestClientException e) { - logger.error( - "Error querying Camunda for activity-instance history for processInstanceId: {}, for requestId: {}, exception: {}", - processInstanceId, requestId, e.getMessage()); - throw new ContactCamundaException.Builder("activity-instance", requestId, e.toString(), - HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e).build(); + } else { + RestTemplate restTemplate = getRestTemplate(retry); + return restTemplate.exchange(targetUrl, HttpMethod.GET, requestEntity, + new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); } } - protected String getTaskName(String requestId) throws ContactCamundaException { + protected ResponseEntity<List<HistoricActivityInstanceEntity>> getCamundaActivityHistory(String processInstanceId) { + RestTemplate restTemplate = getRestTemplate(false); + String path = env.getProperty("mso.camunda.rest.activity.uri") + processInstanceId; + String targetUrl = env.getProperty("mso.camundaURL") + path; + HttpHeaders headers = + setCamundaHeaders(env.getRequiredProperty("mso.camundaAuth"), env.getRequiredProperty("mso.msoKey")); + HttpEntity<?> requestEntity = new HttpEntity<>(headers); + + return restTemplate.exchange(targetUrl, HttpMethod.GET, requestEntity, + new ParameterizedTypeReference<List<HistoricActivityInstanceEntity>>() {}); + } + + protected String getTaskName(String requestId) { ResponseEntity<List<HistoricProcessInstanceEntity>> response = null; - ResponseEntity<List<HistoricActivityInstanceEntity>> activityResponse = null; - String processInstanceId = null; + + String taskInformation = null; try { - response = getCamundaProcessInstanceHistory(requestId); + response = getCamundaProcessInstanceHistory(requestId, false); } catch (RestClientException e) { - logger.error("Error querying Camunda for process-instance history for requestId: {}, exception: {}", + logger.warn("Error querying Camunda for process-instance history for requestId: {}, exception: {}", requestId, e.getMessage()); - throw new ContactCamundaException.Builder("process-instance", requestId, e.toString(), - HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e).build(); } + if (response != null) { + taskInformation = getTaskInformation(response, requestId); + } + return taskInformation; + } + + protected String getTaskInformation(ResponseEntity<List<HistoricProcessInstanceEntity>> response, + String requestId) { List<HistoricProcessInstanceEntity> historicProcessInstanceList = response.getBody(); + ResponseEntity<List<HistoricActivityInstanceEntity>> activityResponse = null; + String processInstanceId = null; + String taskInformation = null; if (historicProcessInstanceList != null && !historicProcessInstanceList.isEmpty()) { Collections.reverse(historicProcessInstanceList); processInstanceId = historicProcessInstanceList.get(0).getId(); } else { - return "No processInstances returned for requestId: " + requestId; + logger.warn("No processInstances returned for requestId: {} to get TaskInformation", requestId); } if (processInstanceId != null) { - activityResponse = getCamundaActivityHistory(processInstanceId, requestId); + try { + activityResponse = getCamundaActivityHistory(processInstanceId); + } catch (RestClientException e) { + logger.warn( + "Error querying Camunda for activity-instance history for processInstanceId: {}, for requestId: {}, exception: {}", + processInstanceId, requestId, e.getMessage()); + } } else { - return "No processInstanceId returned for requestId: " + requestId; + logger.warn("No processInstanceId returned for requestId: {} to get TaskInformation", requestId); } - return getActivityName(activityResponse.getBody()); + if (activityResponse != null) { + taskInformation = getActivityName(activityResponse.getBody()); + } else { + logger.warn("No activity history information returned for requestId: {} to get TaskInformation", requestId); + } + return taskInformation; } protected String getActivityName(List<HistoricActivityInstanceEntity> activityInstanceList) { @@ -169,12 +175,31 @@ public class CamundaRequestHandler { return headers; } - protected RetryTemplate setRetryTemplate() { + protected RetryTemplate getRetryTemplate() { RetryTemplate retryTemplate = new RetryTemplate(); Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<>(); retryableExceptions.put(ResourceAccessException.class, true); - SimpleRetryPolicy policy = new SimpleRetryPolicy(4, retryableExceptions); + SimpleRetryPolicy policy = new SimpleRetryPolicy(2, retryableExceptions); retryTemplate.setRetryPolicy(policy); return retryTemplate; } + + protected RestTemplate getRestTemplate(boolean retry) { + int timeout; + if (retry) { + timeout = Integer.parseInt(env.getProperty(RETRY_TIMEOUT_PROPERTY, RETRY_TIMEOUT)); + } else { + timeout = Integer.parseInt(env.getProperty(TIMEOUT_PROPERTY, TIMEOUT)); + } + + RestTemplate restTemplate = new RestTemplate(); + HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); + factory.setConnectionRequestTimeout(timeout); + factory.setReadTimeout(timeout); + factory.setConnectTimeout(timeout); + restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory)); + restTemplate.getInterceptors().add(new SOSpringClientFilter()); + restTemplate.getInterceptors().add((new SpringClientPayloadFilter())); + return restTemplate; + } } diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/RequestHandlerUtils.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/RequestHandlerUtils.java index e4ae80da97..557ae6c619 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/RequestHandlerUtils.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/RequestHandlerUtils.java @@ -306,6 +306,22 @@ public class RequestHandlerUtils extends AbstractRestHandler { return requestUri; } + public void checkForDuplicateRequests(Actions action, HashMap<String, String> instanceIdMap, String requestScope, + InfraActiveRequests currentActiveReq, String instanceName) throws ApiException { + InfraActiveRequests dup = null; + boolean inProgress = false; + + dup = duplicateCheck(action, instanceIdMap, instanceName, requestScope, currentActiveReq); + + if (dup != null) { + inProgress = camundaHistoryCheck(dup, currentActiveReq); + } + + if (dup != null && inProgress) { + buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, instanceName, requestScope, dup); + } + } + public InfraActiveRequests duplicateCheck(Actions action, HashMap<String, String> instanceIdMap, String instanceName, String requestScope, InfraActiveRequests currentActiveReq) throws ApiException { InfraActiveRequests dup = null; @@ -333,7 +349,7 @@ public class RequestHandlerUtils extends AbstractRestHandler { String requestId = duplicateRecord.getRequestId(); ResponseEntity<List<HistoricProcessInstanceEntity>> response = null; try { - response = camundaRequestHandler.getCamundaProcessInstanceHistory(requestId); + response = camundaRequestHandler.getCamundaProcessInstanceHistory(requestId, true); } catch (RestClientException e) { logger.error("Error querying Camunda for process-instance history for requestId: {}, exception: {}", requestId, e.getMessage()); diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ServiceInstances.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ServiceInstances.java index 91c62180fc..bb5013085b 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ServiceInstances.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ServiceInstances.java @@ -846,19 +846,9 @@ public class ServiceInstances extends AbstractRestHandler { currentActiveReq.setNetworkType(networkType); } - InfraActiveRequests dup = null; - boolean inProgress = false; - - dup = requestHandlerUtils.duplicateCheck(action, instanceIdMap, instanceName, requestScope, currentActiveReq); - - if (dup != null) { - inProgress = requestHandlerUtils.camundaHistoryCheck(dup, currentActiveReq); - } + requestHandlerUtils.checkForDuplicateRequests(action, instanceIdMap, requestScope, currentActiveReq, + instanceName); - if (dup != null && inProgress) { - requestHandlerUtils.buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, instanceName, - requestScope, dup); - } ServiceInstancesResponse serviceResponse = new ServiceInstancesResponse(); RequestReferences referencesResponse = new RequestReferences(); @@ -1006,18 +996,7 @@ public class ServiceInstances extends AbstractRestHandler { throw validateException; } - InfraActiveRequests dup = - requestHandlerUtils.duplicateCheck(action, instanceIdMap, null, requestScope, currentActiveReq); - boolean inProgress = false; - - if (dup != null) { - inProgress = requestHandlerUtils.camundaHistoryCheck(dup, currentActiveReq); - } - - if (dup != null && inProgress) { - requestHandlerUtils.buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, null, requestScope, - dup); - } + requestHandlerUtils.checkForDuplicateRequests(action, instanceIdMap, requestScope, currentActiveReq, null); ServiceInstancesResponse serviceResponse = new ServiceInstancesResponse(); @@ -1077,18 +1056,8 @@ public class ServiceInstances extends AbstractRestHandler { requestHandlerUtils.setInstanceId(currentActiveReq, requestScope, null, instanceIdMap); String instanceName = sir.getRequestDetails().getRequestInfo().getInstanceName(); - InfraActiveRequests dup = null; - - dup = requestHandlerUtils.duplicateCheck(action, instanceIdMap, instanceName, requestScope, currentActiveReq); - - if (dup != null) { - inProgress = requestHandlerUtils.camundaHistoryCheck(dup, currentActiveReq); - } - - if (instanceIdMap != null && dup != null && inProgress) { - requestHandlerUtils.buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, instanceName, - requestScope, dup); - } + requestHandlerUtils.checkForDuplicateRequests(action, instanceIdMap, requestScope, currentActiveReq, + instanceName); ServiceInstancesResponse serviceResponse = new ServiceInstancesResponse(); RequestReferences referencesResponse = new RequestReferences(); diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/CamundaRequestHandlerTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/CamundaRequestHandlerTest.java index 830f38f98b..4dc281b3fc 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/CamundaRequestHandlerTest.java +++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/CamundaRequestHandlerTest.java @@ -17,324 +17,39 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.onap.so.apihandlerinfra; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import org.camunda.bpm.engine.impl.persistence.entity.HistoricActivityInstanceEntity; -import org.camunda.bpm.engine.impl.persistence.entity.HistoricProcessInstanceEntity; -import org.junit.Before; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import javax.ws.rs.core.MediaType; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Spy; -import org.mockito.junit.MockitoJUnitRunner; -import org.onap.so.apihandlerinfra.exceptions.ContactCamundaException; -import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException; -import org.springframework.core.ParameterizedTypeReference; -import org.springframework.core.env.Environment; -import org.springframework.http.HttpEntity; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.ResourceAccessException; -import org.springframework.web.client.RestTemplate; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; - -@RunWith(MockitoJUnitRunner.class) -public class CamundaRequestHandlerTest { - - @Mock - private RestTemplate restTemplate; - @Mock - private Environment env; +public class CamundaRequestHandlerTest extends BaseTest { - @InjectMocks - @Spy + @Autowired private CamundaRequestHandler camundaRequestHandler; + @Value("${wiremock.server.port}") + private String wiremockPort; + @Rule public ExpectedException thrown = ExpectedException.none(); - private static final String REQUEST_ID = "eca3a1b1-43ab-457e-ab1c-367263d148b4"; - private ResponseEntity<List<HistoricActivityInstanceEntity>> activityInstanceResponse = null; - private ResponseEntity<List<HistoricProcessInstanceEntity>> processInstanceResponse = null; - private List<HistoricActivityInstanceEntity> activityInstanceList = null; - private List<HistoricProcessInstanceEntity> processInstanceList = null; - - - - @Before - public void setup() throws IOException { - ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - activityInstanceList = mapper.readValue( - new String(Files.readAllBytes( - Paths.get("src/test/resources/OrchestrationRequest/ActivityInstanceHistoryResponse.json"))), - new TypeReference<List<HistoricActivityInstanceEntity>>() {}); - processInstanceList = mapper.readValue( - new String(Files.readAllBytes( - Paths.get("src/test/resources/OrchestrationRequest/ProcessInstanceHistoryResponse.json"))), - new TypeReference<List<HistoricProcessInstanceEntity>>() {}); - processInstanceResponse = - new ResponseEntity<List<HistoricProcessInstanceEntity>>(processInstanceList, HttpStatus.ACCEPTED); - activityInstanceResponse = - new ResponseEntity<List<HistoricActivityInstanceEntity>>(activityInstanceList, HttpStatus.ACCEPTED); - - doReturn("/sobpmnengine/history/process-instance?variables=mso-request-id_eq_").when(env) - .getProperty("mso.camunda.rest.history.uri"); - doReturn("/sobpmnengine/history/activity-instance?processInstanceId=").when(env) - .getProperty("mso.camunda.rest.activity.uri"); - doReturn("auth").when(env).getRequiredProperty("mso.camundaAuth"); - doReturn("key").when(env).getRequiredProperty("mso.msoKey"); - doReturn("http://localhost:8089").when(env).getProperty("mso.camundaURL"); - } - - public HttpHeaders setHeaders() { - HttpHeaders headers = new HttpHeaders(); - List<org.springframework.http.MediaType> acceptableMediaTypes = new ArrayList<>(); - acceptableMediaTypes.add(org.springframework.http.MediaType.APPLICATION_JSON); - headers.setAccept(acceptableMediaTypes); - headers.add(HttpHeaders.AUTHORIZATION, "auth"); - - return headers; - } - - @Test - public void getActivityNameTest() { - String expectedActivityName = "Last task executed: BB to Execute"; - String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList); - - assertEquals(expectedActivityName, actualActivityName); - } - - @Test - public void getActivityNameNullActivityNameTest() { - String expectedActivityName = "Task name is null."; - HistoricActivityInstanceEntity activityInstance = new HistoricActivityInstanceEntity(); - List<HistoricActivityInstanceEntity> activityInstanceList = new ArrayList<>(); - activityInstanceList.add(activityInstance); - - String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList); - - assertEquals(expectedActivityName, actualActivityName); - } - - @Test - public void getActivityNameNullListTest() { - String expectedActivityName = "No results returned on activityInstance history lookup."; - List<HistoricActivityInstanceEntity> activityInstanceList = null; - String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList); - - assertEquals(expectedActivityName, actualActivityName); - } - - @Test - public void getActivityNameEmptyListTest() { - String expectedActivityName = "No results returned on activityInstance history lookup."; - List<HistoricActivityInstanceEntity> activityInstanceList = new ArrayList<>(); - String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList); - - assertEquals(expectedActivityName, actualActivityName); - } - - @Test - public void getTaskNameTest() throws ContactCamundaException { - doReturn(processInstanceResponse).when(camundaRequestHandler).getCamundaProcessInstanceHistory(REQUEST_ID); - doReturn(activityInstanceResponse).when(camundaRequestHandler) - .getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b", REQUEST_ID); - doReturn("Last task executed: BB to Execute").when(camundaRequestHandler).getActivityName(activityInstanceList); - String expectedTaskName = "Last task executed: BB to Execute"; - - String actualTaskName = camundaRequestHandler.getTaskName(REQUEST_ID); - - assertEquals(expectedTaskName, actualTaskName); - } - - @Test - public void getTaskNameNullProcessInstanceListTest() throws ContactCamundaException { - ResponseEntity<List<HistoricProcessInstanceEntity>> response = new ResponseEntity<>(null, HttpStatus.OK); - doReturn(response).when(camundaRequestHandler).getCamundaProcessInstanceHistory(REQUEST_ID); - String expected = "No processInstances returned for requestId: " + REQUEST_ID; - - String actual = camundaRequestHandler.getTaskName(REQUEST_ID); - - assertEquals(expected, actual); - } - @Test - public void getTaskNameNullProcessInstanceIdTest() throws ContactCamundaException { - HistoricProcessInstanceEntity processInstance = new HistoricProcessInstanceEntity(); - List<HistoricProcessInstanceEntity> processInstanceList = new ArrayList<>(); - processInstanceList.add(processInstance); - ResponseEntity<List<HistoricProcessInstanceEntity>> response = - new ResponseEntity<>(processInstanceList, HttpStatus.OK); - doReturn(response).when(camundaRequestHandler).getCamundaProcessInstanceHistory(REQUEST_ID); - String expected = "No processInstanceId returned for requestId: " + REQUEST_ID; - - String actual = camundaRequestHandler.getTaskName(REQUEST_ID); - - assertEquals(expected, actual); - } - - @Test - public void getTaskNameEmptyProcessInstanceListTest() throws ContactCamundaException { - ResponseEntity<List<HistoricProcessInstanceEntity>> response = - new ResponseEntity<>(Collections.emptyList(), HttpStatus.OK); - doReturn(response).when(camundaRequestHandler).getCamundaProcessInstanceHistory(REQUEST_ID); - String expected = "No processInstances returned for requestId: " + REQUEST_ID; - - String actual = camundaRequestHandler.getTaskName(REQUEST_ID); - - assertEquals(expected, actual); - } - - @Test - public void getTaskNameProcessInstanceLookupFailureTest() throws ContactCamundaException { - doThrow(HttpClientErrorException.class).when(camundaRequestHandler) - .getCamundaProcessInstanceHistory(REQUEST_ID); - - thrown.expect(ContactCamundaException.class); - camundaRequestHandler.getTaskName(REQUEST_ID); - } - - @Test - public void getCamundaActivityHistoryTest() throws ContactCamundaException { - HttpHeaders headers = setHeaders(); - HttpEntity<?> requestEntity = new HttpEntity<>(headers); - String targetUrl = "http://localhost:8089/sobpmnengine/history/activity-instance?processInstanceId=" - + "c4c6b647-a26e-11e9-b144-0242ac14000b"; - doReturn(activityInstanceResponse).when(restTemplate).exchange(targetUrl, HttpMethod.GET, requestEntity, - new ParameterizedTypeReference<List<HistoricActivityInstanceEntity>>() {}); - doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); - ResponseEntity<List<HistoricActivityInstanceEntity>> actualResponse = - camundaRequestHandler.getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b", REQUEST_ID); - assertEquals(activityInstanceResponse, actualResponse); - } - - @Test - public void getCamundaActivityHistoryErrorTest() { - HttpHeaders headers = setHeaders(); - HttpEntity<?> requestEntity = new HttpEntity<>(headers); - String targetUrl = "http://localhost:8089/sobpmnengine/history/activity-instance?processInstanceId=" - + "c4c6b647-a26e-11e9-b144-0242ac14000b"; - doThrow(new ResourceAccessException("IOException")).when(restTemplate).exchange(targetUrl, HttpMethod.GET, - requestEntity, new ParameterizedTypeReference<List<HistoricActivityInstanceEntity>>() {}); - doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); - - try { - camundaRequestHandler.getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b", REQUEST_ID); - } catch (ContactCamundaException e) { - // Exception thrown after retries are completed - } - - verify(restTemplate, times(4)).exchange(targetUrl, HttpMethod.GET, requestEntity, - new ParameterizedTypeReference<List<HistoricActivityInstanceEntity>>() {}); - } - - @Test - public void getCamundaProccesInstanceHistoryTest() { - HttpHeaders headers = setHeaders(); - HttpEntity<?> requestEntity = new HttpEntity<>(headers); - String targetUrl = - "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID; - doReturn(processInstanceResponse).when(restTemplate).exchange(targetUrl, HttpMethod.GET, requestEntity, - new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); - doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); - - ResponseEntity<List<HistoricProcessInstanceEntity>> actualResponse = - camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID); - assertEquals(processInstanceResponse, actualResponse); - } - - @Test - public void getCamundaProccesInstanceHistoryRetryTest() { - HttpHeaders headers = setHeaders(); - HttpEntity<?> requestEntity = new HttpEntity<>(headers); - String targetUrl = - "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID; - doThrow(new ResourceAccessException("I/O error")).when(restTemplate).exchange(targetUrl, HttpMethod.GET, - requestEntity, new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); - doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); - - try { - camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID); - } catch (ResourceAccessException e) { - // Exception thrown after retries are completed - } - verify(restTemplate, times(4)).exchange(targetUrl, HttpMethod.GET, requestEntity, - new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); - } - - @Test - public void getCamundaProccesInstanceHistoryNoRetryTest() { - HttpHeaders headers = setHeaders(); - HttpEntity<?> requestEntity = new HttpEntity<>(headers); - String targetUrl = - "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID; - doThrow(HttpClientErrorException.class).when(restTemplate).exchange(targetUrl, HttpMethod.GET, requestEntity, - new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); - doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); - - try { - camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID); - } catch (HttpStatusCodeException e) { - // Exception thrown, no retries - } - verify(restTemplate, times(1)).exchange(targetUrl, HttpMethod.GET, requestEntity, - new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); - } - - @Test - public void getCamundaProccesInstanceHistoryFailThenSuccessTest() { - HttpHeaders headers = setHeaders(); - HttpEntity<?> requestEntity = new HttpEntity<>(headers); - String targetUrl = - "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID; - when(restTemplate.exchange(targetUrl, HttpMethod.GET, requestEntity, - new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {})) - .thenThrow(new ResourceAccessException("I/O Exception")).thenReturn(processInstanceResponse); - doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); - - ResponseEntity<List<HistoricProcessInstanceEntity>> actualResponse = - camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID); - assertEquals(processInstanceResponse, actualResponse); - verify(restTemplate, times(2)).exchange(targetUrl, HttpMethod.GET, requestEntity, - new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); - } - - @Test - public void setCamundaHeadersTest() { - String encryptedAuth = "015E7ACF706C6BBF85F2079378BDD2896E226E09D13DC2784BA309E27D59AB9FAD3A5E039DF0BB8408"; // user:password - String key = "07a7159d3bf51a0e53be7a8f89699be7"; - - HttpHeaders headers = camundaRequestHandler.setCamundaHeaders(encryptedAuth, key); - List<org.springframework.http.MediaType> acceptedType = headers.getAccept(); - - String expectedAcceptedType = "application/json"; - assertEquals(expectedAcceptedType, acceptedType.get(0).toString()); - String basicAuth = headers.getFirst(HttpHeaders.AUTHORIZATION); - String expectedBasicAuth = "Basic dXNlcjpwYXNzd29yZA=="; + public void timeoutTest() { + wireMockServer.stubFor(get( + ("/sobpmnengine/history/process-instance?variables=mso-request-id_eq_6718de35-b9a5-4670-b19f-a0f4ac22bfaf")) + .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) + .withBodyFile("Camunda/HistoryCheckResponse.json") + .withStatus(org.apache.http.HttpStatus.SC_OK).withFixedDelay(40000))); - assertEquals(expectedBasicAuth, basicAuth); + thrown.expect(ResourceAccessException.class); + camundaRequestHandler.getCamundaProcessInstanceHistory("6718de35-b9a5-4670-b19f-a0f4ac22bfaf", false); } } diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/CamundaRequestHandlerUnitTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/CamundaRequestHandlerUnitTest.java new file mode 100644 index 0000000000..261b64f2e6 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/CamundaRequestHandlerUnitTest.java @@ -0,0 +1,378 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.apihandlerinfra; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import org.camunda.bpm.engine.impl.persistence.entity.HistoricActivityInstanceEntity; +import org.camunda.bpm.engine.impl.persistence.entity.HistoricProcessInstanceEntity; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.apihandlerinfra.exceptions.ContactCamundaException; +import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.HttpStatusCodeException; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; + +@RunWith(MockitoJUnitRunner.class) +public class CamundaRequestHandlerUnitTest { + + @Mock + private RestTemplate restTemplate; + + @Mock + private RestTemplate restTemplateRetry; + + @Mock + private Environment env; + + @InjectMocks + @Spy + private CamundaRequestHandler camundaRequestHandler; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private static final String REQUEST_ID = "eca3a1b1-43ab-457e-ab1c-367263d148b4"; + private ResponseEntity<List<HistoricActivityInstanceEntity>> activityInstanceResponse = null; + private ResponseEntity<List<HistoricProcessInstanceEntity>> processInstanceResponse = null; + private List<HistoricActivityInstanceEntity> activityInstanceList = null; + private List<HistoricProcessInstanceEntity> processInstanceList = null; + + + + @Before + public void setup() throws IOException { + ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + activityInstanceList = mapper.readValue( + new String(Files.readAllBytes( + Paths.get("src/test/resources/OrchestrationRequest/ActivityInstanceHistoryResponse.json"))), + new TypeReference<List<HistoricActivityInstanceEntity>>() {}); + processInstanceList = mapper.readValue( + new String(Files.readAllBytes( + Paths.get("src/test/resources/OrchestrationRequest/ProcessInstanceHistoryResponse.json"))), + new TypeReference<List<HistoricProcessInstanceEntity>>() {}); + processInstanceResponse = + new ResponseEntity<List<HistoricProcessInstanceEntity>>(processInstanceList, HttpStatus.ACCEPTED); + activityInstanceResponse = + new ResponseEntity<List<HistoricActivityInstanceEntity>>(activityInstanceList, HttpStatus.ACCEPTED); + + doReturn("/sobpmnengine/history/process-instance?variables=mso-request-id_eq_").when(env) + .getProperty("mso.camunda.rest.history.uri"); + doReturn("/sobpmnengine/history/activity-instance?processInstanceId=").when(env) + .getProperty("mso.camunda.rest.activity.uri"); + doReturn("auth").when(env).getRequiredProperty("mso.camundaAuth"); + doReturn("key").when(env).getRequiredProperty("mso.msoKey"); + doReturn("http://localhost:8089").when(env).getProperty("mso.camundaURL"); + + HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); + factory.setReadTimeout(30000); + factory.setConnectTimeout(30000); + restTemplate.setRequestFactory(factory); + doReturn(restTemplate).when(camundaRequestHandler).getRestTemplate(false); + + HttpComponentsClientHttpRequestFactory factoryRetry = new HttpComponentsClientHttpRequestFactory(); + factoryRetry.setReadTimeout(15000); + factoryRetry.setConnectTimeout(15000); + restTemplate.setRequestFactory(factoryRetry); + doReturn(restTemplateRetry).when(camundaRequestHandler).getRestTemplate(true); + } + + public HttpHeaders setHeaders() { + HttpHeaders headers = new HttpHeaders(); + List<org.springframework.http.MediaType> acceptableMediaTypes = new ArrayList<>(); + acceptableMediaTypes.add(org.springframework.http.MediaType.APPLICATION_JSON); + headers.setAccept(acceptableMediaTypes); + headers.add(HttpHeaders.AUTHORIZATION, "auth"); + + return headers; + } + + @Test + public void getActivityNameTest() throws IOException { + String expectedActivityName = "Last task executed: BB to Execute"; + String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList); + + assertEquals(expectedActivityName, actualActivityName); + } + + @Test + public void getActivityNameNullActivityNameTest() throws IOException { + String expectedActivityName = "Task name is null."; + HistoricActivityInstanceEntity activityInstance = new HistoricActivityInstanceEntity(); + List<HistoricActivityInstanceEntity> activityInstanceList = new ArrayList<>(); + activityInstanceList.add(activityInstance); + + String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList); + + assertEquals(expectedActivityName, actualActivityName); + } + + @Test + public void getActivityNameNullListTest() throws IOException { + String expectedActivityName = "No results returned on activityInstance history lookup."; + List<HistoricActivityInstanceEntity> activityInstanceList = null; + String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList); + + assertEquals(expectedActivityName, actualActivityName); + } + + @Test + public void getActivityNameEmptyListTest() throws IOException { + String expectedActivityName = "No results returned on activityInstance history lookup."; + List<HistoricActivityInstanceEntity> activityInstanceList = new ArrayList<>(); + String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList); + + assertEquals(expectedActivityName, actualActivityName); + } + + @Test + public void getCamundActivityHistoryNullTest() throws IOException, ContactCamundaException { + HistoricProcessInstanceEntity processInstance = new HistoricProcessInstanceEntity(); + processInstance.setId("c4c6b647-a26e-11e9-b144-0242ac14000b"); + List<HistoricProcessInstanceEntity> processInstanceList = new ArrayList<>(); + processInstanceList.add(processInstance); + ResponseEntity<List<HistoricProcessInstanceEntity>> response = + new ResponseEntity<>(processInstanceList, HttpStatus.OK); + doReturn(null).when(camundaRequestHandler).getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b"); + + String actualTaskName = camundaRequestHandler.getTaskInformation(response, REQUEST_ID); + + assertNull(actualTaskName); + } + + @Test + public void getCamundActivityHistoryErrorTest() throws IOException, ContactCamundaException { + HistoricProcessInstanceEntity processInstance = new HistoricProcessInstanceEntity(); + processInstance.setId("c4c6b647-a26e-11e9-b144-0242ac14000b"); + List<HistoricProcessInstanceEntity> processInstanceList = new ArrayList<>(); + processInstanceList.add(processInstance); + ResponseEntity<List<HistoricProcessInstanceEntity>> response = + new ResponseEntity<>(processInstanceList, HttpStatus.OK); + doThrow(RestClientException.class).when(camundaRequestHandler) + .getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b"); + + String actualTaskName = camundaRequestHandler.getTaskInformation(response, REQUEST_ID); + + assertNull(actualTaskName); + } + + @Test + public void getTaskName() throws IOException, ContactCamundaException { + doReturn(processInstanceResponse).when(camundaRequestHandler).getCamundaProcessInstanceHistory(REQUEST_ID, + false); + doReturn(activityInstanceResponse).when(camundaRequestHandler) + .getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b"); + doReturn("Last task executed: BB to Execute").when(camundaRequestHandler).getActivityName(activityInstanceList); + String expectedTaskName = "Last task executed: BB to Execute"; + + String actualTaskName = camundaRequestHandler.getTaskName(REQUEST_ID); + + assertEquals(expectedTaskName, actualTaskName); + } + + @Test + public void getTaskNameNullProcessInstanceListTest() throws IOException, ContactCamundaException { + ResponseEntity<List<HistoricProcessInstanceEntity>> response = new ResponseEntity<>(null, HttpStatus.OK); + + String actual = camundaRequestHandler.getTaskInformation(response, REQUEST_ID); + + assertNull(actual); + } + + @Test + public void getTaskNameNullProcessInstanceIdTest() throws IOException, ContactCamundaException { + HistoricProcessInstanceEntity processInstance = new HistoricProcessInstanceEntity(); + List<HistoricProcessInstanceEntity> processInstanceList = new ArrayList<>(); + processInstanceList.add(processInstance); + ResponseEntity<List<HistoricProcessInstanceEntity>> response = + new ResponseEntity<>(processInstanceList, HttpStatus.OK); + + String actual = camundaRequestHandler.getTaskInformation(response, REQUEST_ID); + + assertNull(actual); + } + + @Test + public void getTaskNameEmptyProcessInstanceListTest() throws IOException, ContactCamundaException { + List<HistoricProcessInstanceEntity> processInstanceList = new ArrayList<>(); + ResponseEntity<List<HistoricProcessInstanceEntity>> response = + new ResponseEntity<>(processInstanceList, HttpStatus.OK); + + String actual = camundaRequestHandler.getTaskInformation(response, REQUEST_ID); + + assertNull(actual); + } + + @Test + public void getTaskNameProcessInstanceLookupFailureTest() throws IOException, ContactCamundaException { + doThrow(HttpClientErrorException.class).when(camundaRequestHandler).getCamundaProcessInstanceHistory(REQUEST_ID, + false); + + String result = camundaRequestHandler.getTaskName(REQUEST_ID); + assertNull(result); + } + + @Test + public void getCamundaActivityHistoryTest() throws IOException, ContactCamundaException { + HttpHeaders headers = setHeaders(); + HttpEntity<?> requestEntity = new HttpEntity<>(headers); + String targetUrl = "http://localhost:8089/sobpmnengine/history/activity-instance?processInstanceId=" + + "c4c6b647-a26e-11e9-b144-0242ac14000b"; + doReturn(activityInstanceResponse).when(restTemplate).exchange(targetUrl, HttpMethod.GET, requestEntity, + new ParameterizedTypeReference<List<HistoricActivityInstanceEntity>>() {}); + doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); + ResponseEntity<List<HistoricActivityInstanceEntity>> actualResponse = + camundaRequestHandler.getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b"); + assertEquals(activityInstanceResponse, actualResponse); + } + + @Test + public void getCamundaActivityHistoryErrorTest() { + HttpHeaders headers = setHeaders(); + HttpEntity<?> requestEntity = new HttpEntity<>(headers); + String targetUrl = "http://localhost:8089/sobpmnengine/history/activity-instance?processInstanceId=" + + "c4c6b647-a26e-11e9-b144-0242ac14000b"; + doThrow(new ResourceAccessException("IOException")).when(restTemplate).exchange(targetUrl, HttpMethod.GET, + requestEntity, new ParameterizedTypeReference<List<HistoricActivityInstanceEntity>>() {}); + doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); + + thrown.expect(ResourceAccessException.class); + camundaRequestHandler.getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b"); + + verify(restTemplate, times(1)).exchange(targetUrl, HttpMethod.GET, requestEntity, + new ParameterizedTypeReference<List<HistoricActivityInstanceEntity>>() {}); + } + + @Test + public void getCamundaProccesInstanceHistoryTest() throws IOException, ContactCamundaException { + HttpHeaders headers = setHeaders(); + HttpEntity<?> requestEntity = new HttpEntity<>(headers); + String targetUrl = + "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID; + doReturn(processInstanceResponse).when(restTemplate).exchange(targetUrl, HttpMethod.GET, requestEntity, + new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); + doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); + + ResponseEntity<List<HistoricProcessInstanceEntity>> actualResponse = + camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID, false); + assertEquals(processInstanceResponse, actualResponse); + } + + @Test + public void getCamundaProccesInstanceHistoryRetryTest() { + HttpHeaders headers = setHeaders(); + HttpEntity<?> requestEntity = new HttpEntity<>(headers); + String targetUrl = + "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID; + doThrow(new ResourceAccessException("I/O error")).when(restTemplateRetry).exchange(targetUrl, HttpMethod.GET, + requestEntity, new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); + doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); + + thrown.expect(ResourceAccessException.class); + camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID, true); + + verify(restTemplateRetry, times(2)).exchange(targetUrl, HttpMethod.GET, requestEntity, + new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); + } + + @Test + public void getCamundaProccesInstanceHistoryNoRetryTest() { + HttpHeaders headers = setHeaders(); + HttpEntity<?> requestEntity = new HttpEntity<>(headers); + String targetUrl = + "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID; + doThrow(HttpClientErrorException.class).when(restTemplate).exchange(targetUrl, HttpMethod.GET, requestEntity, + new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); + doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); + + thrown.expect(HttpStatusCodeException.class); + camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID, false); + + verify(restTemplate, times(1)).exchange(targetUrl, HttpMethod.GET, requestEntity, + new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); + } + + @Test + public void getCamundaProccesInstanceHistoryFailThenSuccessTest() throws IOException, ContactCamundaException { + HttpHeaders headers = setHeaders(); + HttpEntity<?> requestEntity = new HttpEntity<>(headers); + String targetUrl = + "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID; + when(restTemplateRetry.exchange(targetUrl, HttpMethod.GET, requestEntity, + new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {})) + .thenThrow(new ResourceAccessException("I/O Exception")).thenReturn(processInstanceResponse); + doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key"); + + ResponseEntity<List<HistoricProcessInstanceEntity>> actualResponse = + camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID, true); + + assertEquals(processInstanceResponse, actualResponse); + verify(restTemplateRetry, times(2)).exchange(targetUrl, HttpMethod.GET, requestEntity, + new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}); + } + + @Test + public void setCamundaHeadersTest() throws ContactCamundaException, RequestDbFailureException { + String encryptedAuth = "015E7ACF706C6BBF85F2079378BDD2896E226E09D13DC2784BA309E27D59AB9FAD3A5E039DF0BB8408"; // user:password + String key = "07a7159d3bf51a0e53be7a8f89699be7"; + + HttpHeaders headers = camundaRequestHandler.setCamundaHeaders(encryptedAuth, key); + List<org.springframework.http.MediaType> acceptedType = headers.getAccept(); + + String expectedAcceptedType = "application/json"; + assertEquals(expectedAcceptedType, acceptedType.get(0).toString()); + String basicAuth = headers.getFirst(HttpHeaders.AUTHORIZATION); + String expectedBasicAuth = "Basic dXNlcjpwYXNzd29yZA=="; + + assertEquals(expectedBasicAuth, basicAuth); + } +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/RequestHandlerUtilsUnitTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/RequestHandlerUtilsUnitTest.java index d4b0c3aad1..d0b16cffe6 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/RequestHandlerUtilsUnitTest.java +++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/RequestHandlerUtilsUnitTest.java @@ -25,10 +25,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.sql.Timestamp; +import java.util.HashMap; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -441,4 +443,28 @@ public class RequestHandlerUtilsUnitTest { assertEquals(expected, result); } + @Test + public void checkForDuplicateRequestsTest() throws ApiException { + InfraActiveRequests currentActiveReq = new InfraActiveRequests(); + currentActiveReq.setAicCloudRegion("testRegion"); + currentActiveReq.setRequestId("792a3158-d9a3-49fd-b3ac-ab09842d6a1a"); + Action action = Action.createInstance; + String requestScope = ModelType.service.toString(); + + InfraActiveRequests duplicate = new InfraActiveRequests(); + + HashMap<String, String> instanceIdMap = new HashMap<String, String>(); + String instanceName = "instanceName"; + + doReturn(duplicate).when(requestHandler).duplicateCheck(action, instanceIdMap, instanceName, requestScope, + currentActiveReq); + doReturn(true).when(requestHandler).camundaHistoryCheck(duplicate, currentActiveReq); + doNothing().when(requestHandler).buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, + instanceName, requestScope, duplicate); + + requestHandler.checkForDuplicateRequests(action, instanceIdMap, requestScope, currentActiveReq, instanceName); + verify(requestHandler).buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, instanceName, + requestScope, duplicate); + } + } diff --git a/mso-api-handlers/mso-requests-db-repositories/src/main/java/org/onap/so/db/request/data/repository/InfraActiveRequestsRepository.java b/mso-api-handlers/mso-requests-db-repositories/src/main/java/org/onap/so/db/request/data/repository/InfraActiveRequestsRepository.java index a14699827d..e2c7aef4f4 100644 --- a/mso-api-handlers/mso-requests-db-repositories/src/main/java/org/onap/so/db/request/data/repository/InfraActiveRequestsRepository.java +++ b/mso-api-handlers/mso-requests-db-repositories/src/main/java/org/onap/so/db/request/data/repository/InfraActiveRequestsRepository.java @@ -25,6 +25,7 @@ import java.util.List; import org.onap.so.db.request.beans.InfraActiveRequests; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @@ -47,4 +48,8 @@ public interface InfraActiveRequestsRepository List<InfraActiveRequests> findByEndTimeLessThan(Date endTime, Pageable request); List<InfraActiveRequests> findByStartTimeLessThanAndEndTime(Date startTime, Date endTime, Pageable request); + + @Query(value = "SELECT * FROM infra_active_requests WHERE request_status = 'IN_PROGRESS' AND (request_scope = 'volumeGroup' OR request_scope = 'vfModule') AND start_time < (NOW() - INTERVAL 2 MINUTE)", + nativeQuery = true) + List<InfraActiveRequests> getInProgressVolumeGroupsAndVfModules(); } diff --git a/mso-api-handlers/mso-requests-db/src/main/java/org/onap/so/db/request/client/RequestsDbClient.java b/mso-api-handlers/mso-requests-db/src/main/java/org/onap/so/db/request/client/RequestsDbClient.java index e17ba54a7c..132ec28e31 100644 --- a/mso-api-handlers/mso-requests-db/src/main/java/org/onap/so/db/request/client/RequestsDbClient.java +++ b/mso-api-handlers/mso-requests-db/src/main/java/org/onap/so/db/request/client/RequestsDbClient.java @@ -115,6 +115,8 @@ public class RequestsDbClient { private String getInfraActiveRequests = "/infraActiveRequests/v1/getInfraActiveRequests"; + private String getInProgressVolumeGroupsAndVfModules = "/infraActiveRequests/getInProgressVolumeGroupsAndVfModules"; + private static final String findBySoRequestIdAndGroupIdAndName = "/requestProcessingData/search/findOneBySoRequestIdAndGroupingIdAndName"; @@ -185,6 +187,15 @@ public class RequestsDbClient { } } + public List<InfraActiveRequests> getInProgressVolumeGroupsAndVfModules() { + HttpHeaders headers = getHttpHeaders(); + URI uri = getUri(endpoint + getInProgressVolumeGroupsAndVfModules); + HttpEntity<Map<String, List<String>>> entity = new HttpEntity<>(headers); + return restTemplate + .exchange(uri, HttpMethod.GET, entity, new ParameterizedTypeReference<List<InfraActiveRequests>>() {}) + .getBody(); + } + public InfraActiveRequests getInfraActiveRequestbyRequestId(String requestId) { try { HttpEntity<?> entity = getHttpEntity(); |