diff options
author | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2024-05-23 15:22:07 +0200 |
---|---|---|
committer | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2024-05-23 15:22:07 +0200 |
commit | 843834cb8c2c8184607270e4a01c9c3af2d0313e (patch) | |
tree | 43f01f5d38a8e83129793b382a46327fe86520dc /aai-resources/src/test/java/org | |
parent | e4ff951e645d131a5f4793a75c595da66cc6a23c (diff) |
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 <Fiete.Ostkamp@telekom.de>
Diffstat (limited to 'aai-resources/src/test/java/org')
-rw-r--r-- | aai-resources/src/test/java/org/onap/aai/rest/ExceptionHandlerTest.java | 87 |
1 files changed, 71 insertions, 16 deletions
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,58 +86,106 @@ 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<MediaType> 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 { Exception exception = mock(Exception.class); |