diff options
Diffstat (limited to 'aai-traversal/src/test')
7 files changed, 182 insertions, 378 deletions
diff --git a/aai-traversal/src/test/java/org/onap/aai/entities/AAIErrorResponse.java b/aai-traversal/src/test/java/org/onap/aai/entities/AAIErrorResponse.java deleted file mode 100644 index 6d85ed9..0000000 --- a/aai-traversal/src/test/java/org/onap/aai/entities/AAIErrorResponse.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2023 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; - -public class AAIErrorResponse { - private RequestError requestError; - - public RequestError getRequestError() { - return requestError; - } - - public void setRequestError(RequestError requestError) { - this.requestError = requestError; - } -} diff --git a/aai-traversal/src/test/java/org/onap/aai/entities/RequestError.java b/aai-traversal/src/test/java/org/onap/aai/entities/RequestError.java deleted file mode 100644 index 4de0398..0000000 --- a/aai-traversal/src/test/java/org/onap/aai/entities/RequestError.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2023 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; - -public class RequestError { - private ServiceException serviceException; - - public ServiceException getServiceException() { - return serviceException; - } - - public void setServiceException(ServiceException serviceException) { - this.serviceException = serviceException; - } -} diff --git a/aai-traversal/src/test/java/org/onap/aai/entities/ServiceException.java b/aai-traversal/src/test/java/org/onap/aai/entities/ServiceException.java deleted file mode 100644 index b9cb000..0000000 --- a/aai-traversal/src/test/java/org/onap/aai/entities/ServiceException.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2023 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; - -public class ServiceException { - private String messageId; - private String text; - private List<String> variables; - - public String getMessageId() { - return messageId; - } - - public void setMessageId(String messageId) { - this.messageId = messageId; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public List<String> getVariables() { - return variables; - } - - public void setVariables(List<String> variables) { - this.variables = variables; - } -} diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/DslConsumerTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/DslConsumerTest.java index ac6b749..a4fb0a7 100644 --- a/aai-traversal/src/test/java/org/onap/aai/rest/DslConsumerTest.java +++ b/aai-traversal/src/test/java/org/onap/aai/rest/DslConsumerTest.java @@ -29,8 +29,11 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; @@ -199,9 +202,26 @@ public class DslConsumerTest extends AbstractSpringRestTest { responseEntity.getStatusCode()); // Make sure that there are no two result <result><result> - assertThat(responseEntity.getBody().toString(), + assertThat(responseEntity.getBody(), is(not(containsString("<result><result>")))); - assertThat(responseEntity.getBody().toString(), is(containsString("<results><result>"))); + assertThat(responseEntity.getBody(), is(containsString("<results><result>"))); + } + + @Test + public void thatWildcardContentTypeCanBeUsed() throws Exception { + + String endpoint = "/aai/v14/dsl?format=console"; + Map<String, String> dslQueryMap = new HashMap<>(); + dslQueryMap.put("dsl-query", "pserver*('hostname','test-pserver-dsl')"); + String payload = PayloadUtil.getTemplatePayload("dsl-query.json", dslQueryMap); + headers.add("X-Dsl-Version", "V1"); + headers.setAccept(Arrays.asList(MediaType.ALL)); + httpEntity = new HttpEntity<String>(payload, headers); + ResponseEntity<String> responseEntity = + restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); + + JsonObject result = JsonParser.parseString(responseEntity.getBody()).getAsJsonObject(); + assertEquals(1, result.get("results").getAsJsonArray().size()); } @Test @@ -230,9 +250,9 @@ public class DslConsumerTest extends AbstractSpringRestTest { responseEntity.getStatusCode()); // Make sure that there are no two result <result><result> - assertThat(responseEntity.getBody().toString(), + assertThat(responseEntity.getBody(), is(not(containsString("<result><result>")))); - assertThat(responseEntity.getBody().toString(), is(containsString("<results><result>"))); + assertThat(responseEntity.getBody(), is(containsString("<results><result>"))); } @Test @@ -333,7 +353,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); @@ -361,7 +381,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); @@ -388,7 +408,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); @@ -415,7 +435,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); @@ -443,7 +463,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); @@ -470,7 +490,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); @@ -497,7 +517,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); JsonArray resultsArray = results.get("results").getAsJsonArray(); @@ -540,7 +560,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); JsonArray resultsArray = results.get("results").getAsJsonArray(); @@ -585,7 +605,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); JsonArray resultsArray = results.get("results").getAsJsonArray(); @@ -618,7 +638,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); JsonArray resultsArray = results.get("results").getAsJsonArray(); @@ -645,7 +665,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); JsonArray resultsArray = results.get("results").getAsJsonArray(); @@ -669,7 +689,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); Assert.assertTrue(responseString.contains( "Value ['test'] is not an instance of the expected data type for property key ['number-of-cpus'] and cannot be converted. " + "Expected: class java.lang.Integer, found: class java.lang.String")); @@ -687,7 +707,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); @@ -709,7 +729,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Extract the properties array from the response and compare in assert statements JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); @@ -740,7 +760,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); // pnf should have no results + String responseString = responseEntity.getBody(); // pnf should have no results JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); JsonArray resultsArray = results.get("results").getAsJsonArray(); @@ -778,7 +798,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); JsonObject results = JsonParser.parseString(responseString).getAsJsonObject(); JsonArray resultsArray = results.get("results").getAsJsonArray(); @@ -809,7 +829,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Confirm that the vserver was returned in the response Assert.assertTrue(responseString.contains("\"vserver-id\":\"test-vserver-id-2\"")); @@ -825,7 +845,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - responseString = responseEntity.getBody().toString(); + responseString = responseEntity.getBody(); // Confirm that the vserver was returned in the response Assert.assertTrue(responseString.contains("\"vserver-id\":\"test-vserver-id-2\"")); } @@ -844,7 +864,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Confirm that the l-interface was returned in the response Assert.assertTrue(responseString.contains("\"interface-name\":\"test-interface-name-02\"")); } @@ -863,7 +883,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Confirm that the l-interface was returned in the response Assert.assertTrue(responseString.contains("\"interface-name\":\"test-interface-name-02\"")); } @@ -882,7 +902,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Confirm that the l-interface was returned in the response Assert .assertTrue(!responseString.contains("\"interface-name\":\"test-interface-name-02\"")); @@ -902,7 +922,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Confirm that the l-interface was returned in the response Assert .assertTrue(!responseString.contains("\"interface-name\":\"test-interface-name-02\"")); @@ -922,7 +942,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Confirm that the l-interface was returned in the response Assert .assertTrue(!responseString.contains("\"interface-name\":\"test-interface-name-02\"")); @@ -941,7 +961,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Confirm that the l-interface was returned in the response Assert.assertTrue(responseString.contains("\"interface-name\":\"test-interface-name-02\"")); @@ -956,7 +976,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - responseString = responseEntity.getBody().toString(); + responseString = responseEntity.getBody(); // Confirm that the l-interface was returned in the response Assert.assertTrue(responseString.contains("\"interface-name\":\"test-interface-name-02\"")); @@ -975,7 +995,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Confirm that the oam-network was returned in the response Assert.assertTrue(responseString.contains("\"cvlan-tag\":456")); @@ -989,7 +1009,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - responseString = responseEntity.getBody().toString(); + responseString = responseEntity.getBody(); // Confirm that the oam-network was returned in the response Assert.assertTrue(responseString.contains("\"cvlan-tag\":456")); @@ -1008,7 +1028,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - String responseString = responseEntity.getBody().toString(); + String responseString = responseEntity.getBody(); // Confirm that the pserver was returned in the response Assert.assertTrue(responseString.contains("\"number-of-cpus\":364")); @@ -1022,7 +1042,7 @@ public class DslConsumerTest extends AbstractSpringRestTest { httpEntity = new HttpEntity<String>(payload, headers); responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); - responseString = responseEntity.getBody().toString(); + responseString = responseEntity.getBody(); // Confirm that the pserver was returned in the response Assert.assertTrue(responseString.contains("\"number-of-cpus\":364")); diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/ExceptionHandlerTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/ExceptionHandlerTest.java deleted file mode 100644 index bed964e..0000000 --- a/aai-traversal/src/test/java/org/onap/aai/rest/ExceptionHandlerTest.java +++ /dev/null @@ -1,224 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * 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. - * 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.rest; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -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 java.util.List; -import java.util.UUID; - -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedHashMap; -import javax.ws.rs.core.Response; - -import org.junit.Before; -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; - - @Mock - private HttpServletRequest request; - - @InjectMocks - private ExceptionHandler handler = new ExceptionHandler(); - - @Before - public void setup() { - MockitoAnnotations.initMocks(this); - - MultivaluedHashMap<String, String> headersMultiMap = new MultivaluedHashMap<>(); - - headersMultiMap.add("X-FromAppId", "JUNIT"); - headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString()); - headersMultiMap.add("Real-Time", "true"); - headersMultiMap.add("Accept", "application/json"); - headersMultiMap.add("aai-request-context", ""); - - List<MediaType> outputMediaTypes = new ArrayList<>(); - outputMediaTypes.add(APPLICATION_JSON); - when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes); - when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap); - when(request.getMethod()).thenReturn("PUT"); - when(request.getRequestURI()).thenReturn("/aai/v14/dsl"); - } - - @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 { - - 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("PUT",responseEntity.getRequestError().getServiceException().getVariables().get(0)); - assertEquals("/aai/v14/dsl",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 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("PUT",responseEntity.getRequestError().getServiceException().getVariables().get(0)); - assertEquals("/aai/v14/dsl",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); - - 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("PUT",responseEntity.getRequestError().getServiceException().getVariables().get(0)); - assertEquals("/aai/v14/dsl",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("/aai/v14/dsl",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("/aai/v14/dsl",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); - Response response = handler.toResponse(exception); - - when(request.getMethod()).thenReturn("GET"); - - assertNotNull(response); - assertNotNull(response.getEntity()); - assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); - } - - @Test - public void testConversionWhenUnknownExceptionResultBadRequestForXmlResponseType() - throws Exception { - - List<MediaType> outputMediaTypes = new ArrayList<>(); - outputMediaTypes.add(MediaType.valueOf("application/xml")); - when(request.getMethod()).thenReturn("GET"); - when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes); - - Exception exception = mock(Exception.class); - Response response = handler.toResponse(exception); - - assertNotNull(response); - assertNotNull(response.getEntity()); - assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); - } -} diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/SpringExceptionHandlerTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/SpringExceptionHandlerTest.java new file mode 100644 index 0000000..b3d1ac9 --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/rest/SpringExceptionHandlerTest.java @@ -0,0 +1,127 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2023 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.rest; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import javax.servlet.http.HttpServletRequest; + +import org.janusgraph.core.SchemaViolationException; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.aai.entities.AAIErrorResponse; +import org.onap.aai.exceptions.AAIException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.context.request.WebRequest; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class SpringExceptionHandlerTest { + + private static final ObjectMapper objectMapper = new ObjectMapper(); + + @InjectMocks + private GlobalExceptionHandler springExceptionHandler; + + @Mock RequestContextHolder requestContextHolder; + + @Mock + private WebRequest webRequest; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getMethod()).thenReturn("PUT"); + when(request.getRequestURI()).thenReturn("/aai/v14/dsl"); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + } + + @Test + public void testHandleJsonParseException() throws JsonMappingException, JsonProcessingException { + JsonParser jsonParser = mock(JsonParser.class); + JsonParseException exception = new JsonParseException(jsonParser, ""); + ResponseEntity<String> response = springExceptionHandler.handleJsonException(exception, webRequest); + AAIErrorResponse responseEntity = objectMapper.readValue(response.getBody(), AAIErrorResponse.class); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + + assertEquals("SVC3102",responseEntity.getRequestError().getServiceException().getMessageId()); + assertEquals("Error parsing input performing %1 on %2 (msg=%3) (ec=%4)",responseEntity.getRequestError().getServiceException().getText()); + assertEquals("PUT",responseEntity.getRequestError().getServiceException().getVariables().get(0)); + assertEquals("/aai/v14/dsl",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 testHandleSchemaViolationException() throws JsonMappingException, JsonProcessingException { + SchemaViolationException exception = Mockito.mock(SchemaViolationException.class); + ResponseEntity<String> response = springExceptionHandler.handleSchemaViolationException(exception, webRequest); + AAIErrorResponse responseEntity = objectMapper.readValue(response.getBody(), AAIErrorResponse.class); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + + 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("/aai/v14/dsl",responseEntity.getRequestError().getServiceException().getVariables().get(1)); + assertEquals("ERR.5.4.4020",responseEntity.getRequestError().getServiceException().getVariables().get(3)); + } + + @Test + public void testHandleAAIException() throws JsonMappingException, JsonProcessingException { + AAIException exception = new AAIException("AAI_4009"); + ResponseEntity<String> response = springExceptionHandler.handleAAIException(exception, webRequest); + AAIErrorResponse responseEntity = objectMapper.readValue(response.getBody(), AAIErrorResponse.class); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + assertEquals("SVC3000",responseEntity.getRequestError().getServiceException().getMessageId()); + assertEquals("PUT",responseEntity.getRequestError().getServiceException().getVariables().get(0)); + assertEquals("/aai/v14/dsl",responseEntity.getRequestError().getServiceException().getVariables().get(1)); + assertEquals("Invalid X-FromAppId in header",responseEntity.getRequestError().getServiceException().getVariables().get(2)); + assertEquals("4.0.4009",responseEntity.getRequestError().getServiceException().getVariables().get(3)); + } + + @Test + public void testHandleUnknownException() throws Exception { + Exception exception = new Exception(); + ResponseEntity<String> response = springExceptionHandler.handleUnknownException(exception, webRequest); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + AAIErrorResponse responseEntity = objectMapper.readValue(response.getBody(), AAIErrorResponse.class); + 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("/aai/v14/dsl",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)); + } +} diff --git a/aai-traversal/src/test/resources/application-test.properties b/aai-traversal/src/test/resources/application-test.properties index 422ea30..b5e5398 100644 --- a/aai-traversal/src/test/resources/application-test.properties +++ b/aai-traversal/src/test/resources/application-test.properties @@ -7,7 +7,7 @@ info.build.version=1.1.0 spring.application.name=aai-traversal spring.jersey.type=filter -server.contextPath=/ +server.servlet.context-path=${schema.uri.base.path} spring.autoconfigure.exclude=\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ @@ -15,7 +15,7 @@ spring.autoconfigure.exclude=\ org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\ org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration -spring.jersey.application-path=${schema.uri.base.path} +spring.jersey.application-path=/ spring.profiles.active=production #The max number of active threads in this pool |