* 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.
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;
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;
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());
outputMediaTypes.add(APPLICATION_JSON);
when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
+ when(request.getMethod()).thenReturn("PUT");
}
@Test
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
assertNotNull(response);
assertNotNull(response.getEntity());
assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
-
}
@Test