From 843834cb8c2c8184607270e4a01c9c3af2d0313e Mon Sep 17 00:00:00 2001 From: Fiete Ostkamp Date: Thu, 23 May 2024 15:22:07 +0200 Subject: Improve tests for exception handling - add more assertions to ExceptionHandlerTest - add class definitions for error responses to allow the usage of object mappers for that Issue-ID: AAI-3691 Change-Id: I0a3f26c7f3a14bf536cc4f023b567aeb4191e963 Signed-off-by: Fiete Ostkamp --- aai-resources/pom.xml | 11 +++ .../org/onap/aai/entities/AAIErrorResponse.java | 32 ++++++++ .../java/org/onap/aai/entities/RequestError.java | 32 ++++++++ .../org/onap/aai/entities/ServiceException.java | 36 +++++++++ .../org/onap/aai/rest/ExceptionHandlerTest.java | 87 ++++++++++++++++++---- 5 files changed, 182 insertions(+), 16 deletions(-) create mode 100644 aai-resources/src/main/java/org/onap/aai/entities/AAIErrorResponse.java create mode 100644 aai-resources/src/main/java/org/onap/aai/entities/RequestError.java create mode 100644 aai-resources/src/main/java/org/onap/aai/entities/ServiceException.java (limited to 'aai-resources') diff --git a/aai-resources/pom.xml b/aai-resources/pom.xml index f39349a..5049e04 100644 --- a/aai-resources/pom.xml +++ b/aai-resources/pom.xml @@ -713,6 +713,17 @@ ${keycloak.version} test + + org.projectlombok + lombok + 1.18.30 + provided + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + test + diff --git a/aai-resources/src/main/java/org/onap/aai/entities/AAIErrorResponse.java b/aai-resources/src/main/java/org/onap/aai/entities/AAIErrorResponse.java new file mode 100644 index 0000000..eeaecd4 --- /dev/null +++ b/aai-resources/src/main/java/org/onap/aai/entities/AAIErrorResponse.java @@ -0,0 +1,32 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2024 Deutsche Telekom. 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.aai.entities; + +import lombok.Builder; +import lombok.Data; +import lombok.extern.jackson.Jacksonized; + +@Data +@Builder +@Jacksonized +public class AAIErrorResponse { + private final RequestError requestError; +} diff --git a/aai-resources/src/main/java/org/onap/aai/entities/RequestError.java b/aai-resources/src/main/java/org/onap/aai/entities/RequestError.java new file mode 100644 index 0000000..3fb051b --- /dev/null +++ b/aai-resources/src/main/java/org/onap/aai/entities/RequestError.java @@ -0,0 +1,32 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2024 Deutsche Telekom. 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.aai.entities; + +import lombok.Builder; +import lombok.Data; +import lombok.extern.jackson.Jacksonized; + +@Data +@Builder +@Jacksonized +public class RequestError { + private ServiceException serviceException; +} diff --git a/aai-resources/src/main/java/org/onap/aai/entities/ServiceException.java b/aai-resources/src/main/java/org/onap/aai/entities/ServiceException.java new file mode 100644 index 0000000..38075c0 --- /dev/null +++ b/aai-resources/src/main/java/org/onap/aai/entities/ServiceException.java @@ -0,0 +1,36 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2024 Deutsche Telekom. 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.aai.entities; + +import java.util.List; + +import lombok.Builder; +import lombok.Data; +import lombok.extern.jackson.Jacksonized; + +@Data +@Builder +@Jacksonized +public class ServiceException { + private String messageId; + private String text; + private List variables; +} diff --git a/aai-resources/src/test/java/org/onap/aai/rest/ExceptionHandlerTest.java b/aai-resources/src/test/java/org/onap/aai/rest/ExceptionHandlerTest.java index 4b4c87b..0780649 100644 --- a/aai-resources/src/test/java/org/onap/aai/rest/ExceptionHandlerTest.java +++ b/aai-resources/src/test/java/org/onap/aai/rest/ExceptionHandlerTest.java @@ -3,6 +3,7 @@ * org.onap.aai * ================================================================================ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 Deutsche Telekom SA. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +31,9 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonLocation; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.sun.istack.SAXParseException2; import java.util.ArrayList; @@ -49,11 +53,14 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.onap.aai.AAISetup; +import org.onap.aai.entities.AAIErrorResponse; public class ExceptionHandlerTest extends AAISetup { protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json"); + private static final ObjectMapper objectMapper = new ObjectMapper(); + @Mock private HttpHeaders httpHeaders; @@ -79,57 +86,105 @@ public class ExceptionHandlerTest extends AAISetup { outputMediaTypes.add(APPLICATION_JSON); when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes); when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap); + when(request.getMethod()).thenReturn("PUT"); } @Test public void testConversionOfWebApplicationResponse() throws Exception { - Exception exception = new WebApplicationException(); Response response = handler.toResponse(exception); - assertNotNull(response); assertNull(response.getEntity()); assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); } - @Test - public void testConversionOfWebApplicationResponseWhenUmarshalExceptionResultBadRequest() throws Exception { - + public void testConversionOfWebApplicationResponseWhenUmarshalExceptionResultBadRequest() + throws Exception { SAXParseException2 mockSaxParseException = mock(SAXParseException2.class); Exception exception = new WebApplicationException(mockSaxParseException); Response response = handler.toResponse(exception); - + AAIErrorResponse responseEntity = objectMapper.readValue(response.getEntity().toString(), AAIErrorResponse.class); assertNotNull(response); assertNotNull(response.getEntity()); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertEquals("SVC3102",responseEntity.getRequestError().getServiceException().getMessageId()); + assertEquals("Error parsing input performing %1 on %2 (msg=%3) (ec=%4)",responseEntity.getRequestError().getServiceException().getText()); + assertEquals("UnmarshalException",responseEntity.getRequestError().getServiceException().getVariables().get(0)); + assertEquals("null",responseEntity.getRequestError().getServiceException().getVariables().get(1)); + assertEquals("Input parsing error:javax.ws.rs.WebApplicationException: HTTP 500 Internal Server Error",responseEntity.getRequestError().getServiceException().getVariables().get(2)); + assertEquals("ERR.5.4.4007",responseEntity.getRequestError().getServiceException().getVariables().get(3)); } @Test public void testConversionWhenJsonParseExceptionResultBadRequest() throws Exception { - - JsonParser parser = new JsonFactory().createParser(""); - Exception exception = new JsonParseException(parser, ""); + JsonParser jsonParser = mock(JsonParser.class); + Exception exception = new JsonParseException(jsonParser, ""); Response response = handler.toResponse(exception); - + AAIErrorResponse responseEntity = objectMapper.readValue(response.getEntity().toString(), AAIErrorResponse.class); assertNotNull(response); assertNotNull(response.getEntity()); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertEquals("SVC3102",responseEntity.getRequestError().getServiceException().getMessageId()); + assertEquals("Error parsing input performing %1 on %2 (msg=%3) (ec=%4)",responseEntity.getRequestError().getServiceException().getText()); + assertEquals("JsonParseException",responseEntity.getRequestError().getServiceException().getVariables().get(0)); + assertEquals("null",responseEntity.getRequestError().getServiceException().getVariables().get(1)); + assertEquals("Input parsing error:com.fasterxml.jackson.core.JsonParseException: ",responseEntity.getRequestError().getServiceException().getVariables().get(2)); + assertEquals("ERR.5.4.4007",responseEntity.getRequestError().getServiceException().getVariables().get(3)); } @Test public void testConversionWhenJsonMappingExceptionResultBadRequest() throws Exception { - - JsonLocation jsonLocation = mock(JsonLocation.class); - // Exception exception = new JsonMappingException("", jsonLocation); - JsonParser parser = new JsonFactory().createParser(""); - Exception exception = new JsonParseException(parser, ""); + JsonParser jsonParser = mock(JsonParser.class); + Exception exception = JsonMappingException.from(jsonParser,""); Response response = handler.toResponse(exception); - + AAIErrorResponse responseEntity = objectMapper.readValue(response.getEntity().toString(), AAIErrorResponse.class); assertNotNull(response); assertNotNull(response.getEntity()); assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertEquals("SVC3102",responseEntity.getRequestError().getServiceException().getMessageId()); + assertEquals("Error parsing input performing %1 on %2 (msg=%3) (ec=%4)",responseEntity.getRequestError().getServiceException().getText()); + assertEquals("JsonMappingException",responseEntity.getRequestError().getServiceException().getVariables().get(0)); + assertEquals("null",responseEntity.getRequestError().getServiceException().getVariables().get(1)); + assertEquals("Input parsing error:com.fasterxml.jackson.databind.JsonMappingException: ",responseEntity.getRequestError().getServiceException().getVariables().get(2)); + assertEquals("ERR.5.4.4007",responseEntity.getRequestError().getServiceException().getVariables().get(3)); } + @Test + public void testJsonDefaultErrorResponse() + throws Exception { + Exception exception = new Exception(); + Response response = handler.toResponse(exception); + AAIErrorResponse responseEntity = objectMapper.readValue(response.getEntity().toString(), AAIErrorResponse.class); + assertNotNull(response); + assertNotNull(response.getEntity()); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertEquals("SVC3002",responseEntity.getRequestError().getServiceException().getMessageId()); + assertEquals("Error writing output performing %1 on %2 (msg=%3) (ec=%4)",responseEntity.getRequestError().getServiceException().getText()); + assertEquals("PUT",responseEntity.getRequestError().getServiceException().getVariables().get(0)); + assertEquals("unknown",responseEntity.getRequestError().getServiceException().getVariables().get(1)); + assertEquals("Internal Error:java.lang.Exception",responseEntity.getRequestError().getServiceException().getVariables().get(2)); + assertEquals("ERR.5.4.4000",responseEntity.getRequestError().getServiceException().getVariables().get(3)); + } + @Test + public void testXmlDefaultErrorResponse() + throws Exception { + List outputMediaTypes = new ArrayList<>(); + outputMediaTypes.add(MediaType.APPLICATION_XML_TYPE); + when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes); + Exception exception = new Exception(); + Response response = handler.toResponse(exception); + XmlMapper xmlMapper = new XmlMapper(); + AAIErrorResponse responseEntity = xmlMapper.readValue(response.getEntity().toString(), AAIErrorResponse.class); + assertNotNull(response); + assertNotNull(response.getEntity()); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertEquals("SVC3002",responseEntity.getRequestError().getServiceException().getMessageId()); + assertEquals("Error writing output performing %1 on %2 (msg=%3) (ec=%4)",responseEntity.getRequestError().getServiceException().getText()); + assertEquals("PUT",responseEntity.getRequestError().getServiceException().getVariables().get(0)); + assertEquals("unknown",responseEntity.getRequestError().getServiceException().getVariables().get(1)); + assertEquals("Internal Error:java.lang.Exception",responseEntity.getRequestError().getServiceException().getVariables().get(2)); + assertEquals("ERR.5.4.4000",responseEntity.getRequestError().getServiceException().getVariables().get(3)); + } @Test public void testConversionWhenUnknownExceptionResultBadRequest() throws Exception { -- cgit 1.2.3-korg