Improve tests for exception handling
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / rest / ExceptionHandlerTest.java
index 6281892..df6ca3f 100644 (file)
@@ -3,6 +3,7 @@
  * org.onap.aai
  * ================================================================================
  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 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.
@@ -25,7 +26,10 @@ import static org.mockito.Mockito.when;
 
 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;
@@ -44,11 +48,14 @@ import org.junit.Test;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
+import org.onap.aai.entities.AAIErrorResponse;
 
 public class ExceptionHandlerTest {
 
     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
 
+    private static final ObjectMapper objectMapper = new ObjectMapper();
+
     @Mock
     private HttpHeaders httpHeaders;
 
@@ -62,7 +69,7 @@ public class ExceptionHandlerTest {
     public void setup() {
         MockitoAnnotations.initMocks(this);
 
-        MultivaluedHashMap headersMultiMap = new MultivaluedHashMap<>();
+        MultivaluedHashMap<String, String> headersMultiMap = new MultivaluedHashMap<>();
 
         headersMultiMap.add("X-FromAppId", "JUNIT");
         headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
@@ -74,6 +81,7 @@ public class ExceptionHandlerTest {
         outputMediaTypes.add(APPLICATION_JSON);
         when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
         when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
+        when(request.getMethod()).thenReturn("PUT");
     }
 
     @Test
@@ -94,34 +102,94 @@ public class ExceptionHandlerTest {
         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 {
 
-        JsonLocation jsonLocation = mock(JsonLocation.class);
-        Exception exception = new JsonParseException("", jsonLocation);
+        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 {
+        JsonParser jsonParser = mock(JsonParser.class);
+        Exception exception = JsonMappingException.from(jsonParser,"");
+        Response response = handler.toResponse(exception);
+        AAIErrorResponse responseEntity = objectMapper.readValue(response.getEntity().toString(), AAIErrorResponse.class);
 
-        JsonLocation jsonLocation = mock(JsonLocation.class);
-        Exception exception = new JsonMappingException("", jsonLocation);
+        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
@@ -135,7 +203,6 @@ public class ExceptionHandlerTest {
         assertNotNull(response);
         assertNotNull(response.getEntity());
         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
-
     }
 
     @Test