aboutsummaryrefslogtreecommitdiffstats
path: root/aai-resources
diff options
context:
space:
mode:
authorLukasz Rajewski <lukasz.rajewski@t-mobile.pl>2024-05-23 13:30:37 +0000
committerGerrit Code Review <gerrit@onap.org>2024-05-23 13:30:37 +0000
commitb74095873ab92c49e88c546f31d0b05b0279b13e (patch)
tree2bc847db9b7b53c9d80a20466dbe2a9252a18ee9 /aai-resources
parent85c9d0e95cd96e4f9f5b390b3807432a52e85479 (diff)
parent843834cb8c2c8184607270e4a01c9c3af2d0313e (diff)
Merge "Improve tests for exception handling"
Diffstat (limited to 'aai-resources')
-rw-r--r--aai-resources/pom.xml11
-rw-r--r--aai-resources/src/main/java/org/onap/aai/entities/AAIErrorResponse.java32
-rw-r--r--aai-resources/src/main/java/org/onap/aai/entities/RequestError.java32
-rw-r--r--aai-resources/src/main/java/org/onap/aai/entities/ServiceException.java36
-rw-r--r--aai-resources/src/test/java/org/onap/aai/rest/ExceptionHandlerTest.java87
5 files changed, 182 insertions, 16 deletions
diff --git a/aai-resources/pom.xml b/aai-resources/pom.xml
index 2efab17..abfd928 100644
--- a/aai-resources/pom.xml
+++ b/aai-resources/pom.xml
@@ -713,6 +713,17 @@
<version>${keycloak.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.projectlombok</groupId>
+ <artifactId>lombok</artifactId>
+ <version>1.18.30</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.dataformat</groupId>
+ <artifactId>jackson-dataformat-xml</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<dependencyManagement>
<dependencies>
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<String> 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,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);