diff options
Diffstat (limited to 'mso-api-handlers/mso-api-handler-common/src')
6 files changed, 64 insertions, 119 deletions
diff --git a/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandler/common/ResponseHandler.java b/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandler/common/ResponseHandler.java index 095182fe98..bedce185c5 100644 --- a/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandler/common/ResponseHandler.java +++ b/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandler/common/ResponseHandler.java @@ -98,8 +98,9 @@ public class ResponseHandler { if (response != null) { responseBody = response.getResponse(); } else { - ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, - ErrorCode.BusinessProcesssError).targetEntity("Camunda").targetServiceName("parseCamunda").build(); + ErrorLoggerInfo errorLoggerInfo = + new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcessError) + .targetEntity("Camunda").targetServiceName("parseCamunda").build(); BPMNFailureException bpmnFailureException = new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL) .errorInfo(errorLoggerInfo).build(); @@ -123,8 +124,9 @@ public class ResponseHandler { ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build(); } if (status != HttpStatus.SC_ACCEPTED) { - ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, - ErrorCode.BusinessProcesssError).targetEntity("BPEL").targetServiceName("parseBpel").build(); + ErrorLoggerInfo errorLoggerInfo = + new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcessError) + .targetEntity("BPEL").targetServiceName("parseBpel").build(); throw new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL) .errorInfo(errorLoggerInfo).build(); @@ -149,9 +151,9 @@ public class ResponseHandler { .build(); } if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED) { - ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, - ErrorCode.BusinessProcesssError).targetEntity("CAMUNDATASK").targetServiceName("parseCamundaTask") - .build(); + ErrorLoggerInfo errorLoggerInfo = + new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcessError) + .targetEntity("CAMUNDATASK").targetServiceName("parseCamundaTask").build(); throw new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL) .errorInfo(errorLoggerInfo).build(); 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/main/java/org/onap/so/apihandlerinfra/Action.java b/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandlerinfra/Action.java index 6957e1fe73..0653513184 100644 --- a/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandlerinfra/Action.java +++ b/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandlerinfra/Action.java @@ -29,6 +29,7 @@ public enum Action implements Actions { deleteInstance, configureInstance, replaceInstance, + replaceInstanceRetainAssignments, activateInstance, deactivateInstance, enablePort, diff --git a/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandlerinfra/exceptions/ApiExceptionMapper.java b/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandlerinfra/exceptions/ApiExceptionMapper.java index 6a56b58f04..62f39d9148 100644 --- a/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandlerinfra/exceptions/ApiExceptionMapper.java +++ b/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandlerinfra/exceptions/ApiExceptionMapper.java @@ -71,72 +71,33 @@ public class ApiExceptionMapper implements ExceptionMapper<ApiException> { @Override public Response toResponse(ApiException exception) { logger.error("Error During API Call", exception); - return Response.status(exception.getHttpResponseCode()).entity(buildErrorString(exception)).build(); + return Response.status(exception.getHttpResponseCode()).entity(buildError(exception)).build(); } - protected String buildErrorString(ApiException exception) { + protected RequestError buildError(ApiException exception) { String errorText = exception.getMessage(); String messageId = exception.getMessageID(); List<String> variables = exception.getVariables(); - ErrorLoggerInfo errorLoggerInfo = exception.getErrorLoggerInfo(); - - if (errorText.length() > 1999) { errorText = errorText.substring(0, 1999); } - - - - List<MediaType> typeList = Optional.ofNullable(headers.getAcceptableMediaTypes()).orElse(new ArrayList<>()); - List<String> typeListString = typeList.stream().map(item -> item.toString()).collect(Collectors.toList()); - MediaType type; - if (typeListString.stream().anyMatch(item -> item.contains(MediaType.APPLICATION_XML))) { - type = MediaType.APPLICATION_XML_TYPE; - } else if (typeListString.stream().anyMatch(item -> typeListString.contains(MediaType.APPLICATION_JSON))) { - type = MediaType.APPLICATION_JSON_TYPE; - } else { - type = MediaType.APPLICATION_JSON_TYPE; - } - - return buildServiceErrorResponse(errorText, messageId, variables, type); + return buildServiceErrorResponse(errorText, messageId, variables); } - protected String buildServiceErrorResponse(String errorText, String messageId, List<String> variables, - MediaType type) { - RequestError re = new RequestError(); - ServiceException se = new ServiceException(); - se.setMessageId(messageId); - se.setText(errorText); + protected RequestError buildServiceErrorResponse(String errorText, String messageId, List<String> variables) { + RequestError requestError = new RequestError(); + ServiceException serviceException = new ServiceException(); + serviceException.setMessageId(messageId); + serviceException.setText(errorText); if (variables != null) { for (String variable : variables) { - se.getVariables().add(variable); + serviceException.getVariables().add(variable); } } - re.setServiceException(se); - String requestErrorStr; - - ObjectMapper mapper = createObjectMapper(); - - mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); - mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); - try { - if (MediaType.APPLICATION_JSON_TYPE.equals(type)) { - requestErrorStr = mapper.writeValueAsString(re); - } else { - StringWriter sw = new StringWriter(); - this.getMarshaller().marshal(re, sw); - requestErrorStr = sw.toString(); - } - } catch (JsonProcessingException | JAXBException e) { - String errorMsg = - "Exception in buildServiceErrorResponse writing exceptionType to string " + e.getMessage(); - logger.error("BuildServiceErrorResponse", e); - return errorMsg; - } - - return requestErrorStr; + requestError.setServiceException(serviceException); + return requestError; } protected void writeErrorLog(Exception e, String errorText, ErrorLoggerInfo errorLogInfo) { 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..1aa6c3c5d9 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; @@ -70,7 +77,6 @@ public class RequestIdFilterTest { serviceException.setMessageId("SVC0002"); serviceException.setText( "RequestId: 32807a28-1a14-4b88-b7b3-2950918aa769 already exists in the RequestDB InfraActiveRequests table"); - serviceException.setVariables(Collections.emptyList()); requestError.setServiceException(serviceException); return requestError; } @@ -87,12 +93,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-common/src/test/java/org/onap/so/apihandlerinfra/exceptions/ApiExceptionMapperTest.java b/mso-api-handlers/mso-api-handler-common/src/test/java/org/onap/so/apihandlerinfra/exceptions/ApiExceptionMapperTest.java index 2922aaa896..1a846da65a 100644 --- a/mso-api-handlers/mso-api-handler-common/src/test/java/org/onap/so/apihandlerinfra/exceptions/ApiExceptionMapperTest.java +++ b/mso-api-handlers/mso-api-handler-common/src/test/java/org/onap/so/apihandlerinfra/exceptions/ApiExceptionMapperTest.java @@ -73,26 +73,6 @@ public class ApiExceptionMapperTest { @InjectMocks ApiExceptionMapper mapper = new ApiExceptionMapper(); - - @Before - public void setUp() { - when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_JSON_TYPE)); - } - - @Test - public void testObjectMapperError() throws JsonProcessingException { - ObjectMapper mockedMapper = Mockito.mock(ObjectMapper.class); - Mockito.when(mockedMapper.writeValueAsString(anyObject())).thenThrow(JsonProcessingException.class); - ValidateException validateException = new ValidateException.Builder("Test", 0, null).build(); - ApiExceptionMapper mockedException = Mockito.spy(mapper); - Mockito.doReturn(mockedMapper).when(mockedException).createObjectMapper(); - Response resp = mockedException.toResponse((ApiException) validateException); - - /// assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST); - assertThat(resp.getEntity().toString(), - startsWith("Exception in buildServiceErrorResponse writing exceptionType to string")); - } - @Test public void testValidateResponse() { ValidateException validateException = @@ -138,38 +118,4 @@ public class ApiExceptionMapperTest { assertEquals(resp.getStatus(), HttpStatus.SC_BAD_GATEWAY); } - - @Test - public void verifyXMLPath() throws JAXBException { - when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_XML_TYPE)); - BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND, - ErrorNumbers.SVC_BAD_PARAMETER).build(); - ApiExceptionMapper mapperSpy = Mockito.spy(mapper); - doReturn(marshaller).when(mapperSpy).getMarshaller(); - Response resp = mapperSpy.toResponse((ApiException) bpmnException); - verify(marshaller, times(1)).marshal(any(Object.class), any(Writer.class)); - } - - @Test - public void verifyMediaType() { - ApiExceptionMapper mapperSpy = Mockito.spy(mapper); - BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND, - ErrorNumbers.SVC_BAD_PARAMETER).build(); - when(headers.getAcceptableMediaTypes()) - .thenReturn(Arrays.asList(MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8"))); - mapperSpy.toResponse(bpmnException); - verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class), - ArgumentMatchers.isNull(), eq(MediaType.APPLICATION_XML_TYPE)); - when(headers.getAcceptableMediaTypes()) - .thenReturn(Arrays.asList(MediaType.APPLICATION_JSON_TYPE.withCharset("UTF-8"))); - mapperSpy = Mockito.spy(mapper); - mapperSpy.toResponse(bpmnException); - verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class), - ArgumentMatchers.isNull(), eq(MediaType.APPLICATION_JSON_TYPE)); - when(headers.getAcceptableMediaTypes()).thenReturn(null); - mapperSpy = Mockito.spy(mapper); - mapperSpy.toResponse(bpmnException); - verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class), - ArgumentMatchers.isNull(), eq(MediaType.APPLICATION_JSON_TYPE)); - } } |