From 109f5f03539b10121ead67320c333e06eda126f6 Mon Sep 17 00:00:00 2001 From: Fiete Ostkamp Date: Wed, 3 Jan 2024 14:49:42 +0100 Subject: Update spring-boot to 2.4 - update spring-boot to 2.4 - add junit vintage engine dependency to keep supporting junit 4 tests - update mockito to version 3.4.0 Issue-ID: AAI-3681 Change-Id: I8b40529418f9d03179970edf1ab12992db1cb5e8 Signed-off-by: Fiete Ostkamp --- .../ModelBasedProcessingInvalidDataTest.java | 2 +- .../aai/dbgraphgen/ModelBasedProcessingTest.java | 2 +- .../java/org/onap/aai/rest/BadQueryFormatTest.java | 2 +- .../java/org/onap/aai/rest/ConfigurationTest.java | 9 +- .../onap/aai/rest/GlobalExceptionHandlerTest.java | 127 +++++++++++++++++++++ .../onap/aai/rest/SpringExceptionHandlerTest.java | 127 --------------------- .../org/onap/aai/rest/search/OnapQueryTest.java | 2 +- .../java/org/onap/aai/rest/search/QueryTest.java | 2 +- .../org/onap/aai/rest/search/SimpleFormatTest.java | 2 +- 9 files changed, 139 insertions(+), 136 deletions(-) create mode 100644 aai-traversal/src/test/java/org/onap/aai/rest/GlobalExceptionHandlerTest.java delete mode 100644 aai-traversal/src/test/java/org/onap/aai/rest/SpringExceptionHandlerTest.java (limited to 'aai-traversal/src/test') diff --git a/aai-traversal/src/test/java/org/onap/aai/dbgraphgen/ModelBasedProcessingInvalidDataTest.java b/aai-traversal/src/test/java/org/onap/aai/dbgraphgen/ModelBasedProcessingInvalidDataTest.java index d6b43aa..c228d03 100644 --- a/aai-traversal/src/test/java/org/onap/aai/dbgraphgen/ModelBasedProcessingInvalidDataTest.java +++ b/aai-traversal/src/test/java/org/onap/aai/dbgraphgen/ModelBasedProcessingInvalidDataTest.java @@ -82,7 +82,7 @@ public class ModelBasedProcessingInvalidDataTest extends AAISetup { @Before public void init() { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion()); processor = new ModelBasedProcessing(loader, dbEngine, serializer); diff --git a/aai-traversal/src/test/java/org/onap/aai/dbgraphgen/ModelBasedProcessingTest.java b/aai-traversal/src/test/java/org/onap/aai/dbgraphgen/ModelBasedProcessingTest.java index ab1ea0a..fad6587 100644 --- a/aai-traversal/src/test/java/org/onap/aai/dbgraphgen/ModelBasedProcessingTest.java +++ b/aai-traversal/src/test/java/org/onap/aai/dbgraphgen/ModelBasedProcessingTest.java @@ -98,7 +98,7 @@ public class ModelBasedProcessingTest extends AAISetup { @Before public void init() throws AAIException { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); version = schemaVersions.getDefaultVersion(); loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version); TransactionalGraphEngine newDbEngine = new JanusGraphDBEngine(queryStyle, loader); diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/BadQueryFormatTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/BadQueryFormatTest.java index eb8902e..1efbce7 100644 --- a/aai-traversal/src/test/java/org/onap/aai/rest/BadQueryFormatTest.java +++ b/aai-traversal/src/test/java/org/onap/aai/rest/BadQueryFormatTest.java @@ -19,9 +19,9 @@ */ package org.onap.aai.rest; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; -import static org.hamcrest.junit.MatcherAssert.assertThat; import java.util.Base64; import java.util.Collections; diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/ConfigurationTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/ConfigurationTest.java index b0f9869..0992519 100644 --- a/aai-traversal/src/test/java/org/onap/aai/rest/ConfigurationTest.java +++ b/aai-traversal/src/test/java/org/onap/aai/rest/ConfigurationTest.java @@ -35,6 +35,7 @@ import org.onap.aai.config.SpringContextAware; import org.onap.aai.restclient.PropertyPasswordConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Import; import org.springframework.http.*; @@ -42,9 +43,12 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.web.client.RestTemplate; +import io.prometheus.client.exporter.common.TextFormat; + /** * Test REST requests against configuration resource */ +@AutoConfigureMetrics @TestPropertySource(locations = "classpath:application-test.properties") @ContextConfiguration( initializers = PropertyPasswordConfiguration.class, @@ -69,7 +73,6 @@ public class ConfigurationTest extends AbstractSpringRestTest { headers = new HttpHeaders(); - headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.set("Accept", "text/plain"); headers.add("Real-Time", "true"); headers.add("X-FromAppId", "JUNIT"); @@ -85,13 +88,13 @@ public class ConfigurationTest extends AbstractSpringRestTest { @Test public void TestManagementEndpointConfiguration() { - ResponseEntity responseEntity = null; + ResponseEntity responseEntity = null; String responseBody = null; // set Accept as text/plain in order to get access of endpoint "/actuator/prometheus" responseEntity = restTemplate.exchange(actuatorurl + "/actuator/prometheus", HttpMethod.GET, httpEntity, String.class); - responseBody = (String) responseEntity.getBody(); + responseBody = responseEntity.getBody(); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); System.out.println("responseBody---------" + responseBody); assertFalse(responseBody.contains("aai_uri")); diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/GlobalExceptionHandlerTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/GlobalExceptionHandlerTest.java new file mode 100644 index 0000000..75cc1a8 --- /dev/null +++ b/aai-traversal/src/test/java/org/onap/aai/rest/GlobalExceptionHandlerTest.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 GlobalExceptionHandlerTest { + + private static final ObjectMapper objectMapper = new ObjectMapper(); + + @InjectMocks + private GlobalExceptionHandler springExceptionHandler; + + @Mock RequestContextHolder requestContextHolder; + + @Mock + private WebRequest webRequest; + + @Before + public void setup() { + MockitoAnnotations.openMocks(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 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 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 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 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/java/org/onap/aai/rest/SpringExceptionHandlerTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/SpringExceptionHandlerTest.java deleted file mode 100644 index b3d1ac9..0000000 --- a/aai-traversal/src/test/java/org/onap/aai/rest/SpringExceptionHandlerTest.java +++ /dev/null @@ -1,127 +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.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 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 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 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 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/java/org/onap/aai/rest/search/OnapQueryTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/search/OnapQueryTest.java index 6d12842..44264c1 100644 --- a/aai-traversal/src/test/java/org/onap/aai/rest/search/OnapQueryTest.java +++ b/aai-traversal/src/test/java/org/onap/aai/rest/search/OnapQueryTest.java @@ -142,7 +142,7 @@ public abstract class OnapQueryTest { System.setProperty("AJSC_HOME", "."); System.setProperty("BUNDLECONFIG_DIR", "src/main/resources"); logger = LoggerFactory.getLogger(getClass()); - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); graph = TinkerGraph.open(); gts = graph.traversal(); createGraph(); diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/search/QueryTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/search/QueryTest.java index bdd52ee..4574ca1 100644 --- a/aai-traversal/src/test/java/org/onap/aai/rest/search/QueryTest.java +++ b/aai-traversal/src/test/java/org/onap/aai/rest/search/QueryTest.java @@ -142,7 +142,7 @@ public abstract class QueryTest { System.setProperty("AJSC_HOME", "."); System.setProperty("BUNDLECONFIG_DIR", "src/main/resources"); logger = LoggerFactory.getLogger(getClass()); - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); graph = TinkerGraph.open(); gts = graph.traversal(); createGraph(); diff --git a/aai-traversal/src/test/java/org/onap/aai/rest/search/SimpleFormatTest.java b/aai-traversal/src/test/java/org/onap/aai/rest/search/SimpleFormatTest.java index 0b53b72..dc33e07 100644 --- a/aai-traversal/src/test/java/org/onap/aai/rest/search/SimpleFormatTest.java +++ b/aai-traversal/src/test/java/org/onap/aai/rest/search/SimpleFormatTest.java @@ -70,7 +70,7 @@ public class SimpleFormatTest extends AAISetup { @Before public void setUp() throws AAIException, NoEdgeRuleFoundException { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); graph = TinkerGraph.open(); loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getRelatedLinkVersion()); -- cgit 1.2.3-korg