From 0a9a06196c1a7b6988196c2fa6f604355429b851 Mon Sep 17 00:00:00 2001 From: Shantaram Sawant Date: Tue, 1 Oct 2024 17:07:03 +0530 Subject: echo liveness probe available under /actuator/health - Include database connectivity check in the /echo API of AAI modules. Issue-ID: AAI-3999 Change-Id: Ia6eb3ded705e6b5b3f1db0cd9605376a2f442e68 Signed-off-by: Shantaram Sawant --- .../onap/aai/config/WebClientConfiguration.java | 51 +++++++ .../aai/rest/util/EchoHealthIndicatorTest.java | 72 +++++++++ .../org/onap/aai/rest/util/EchoResponseTest.java | 166 +++++++++++++++++++++ 3 files changed, 289 insertions(+) create mode 100644 src/test/java/org/onap/aai/config/WebClientConfiguration.java create mode 100644 src/test/java/org/onap/aai/rest/util/EchoHealthIndicatorTest.java create mode 100644 src/test/java/org/onap/aai/rest/util/EchoResponseTest.java (limited to 'src/test/java/org') diff --git a/src/test/java/org/onap/aai/config/WebClientConfiguration.java b/src/test/java/org/onap/aai/config/WebClientConfiguration.java new file mode 100644 index 0000000..c76b9b1 --- /dev/null +++ b/src/test/java/org/onap/aai/config/WebClientConfiguration.java @@ -0,0 +1,51 @@ +/** + * ============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.config; + +import java.time.Duration; +import java.util.Collections; + +import org.onap.aai.setup.SchemaVersions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Lazy; +import org.springframework.context.annotation.Primary; +import org.springframework.http.MediaType; +import org.springframework.test.web.reactive.server.WebTestClient; + +@TestConfiguration +public class WebClientConfiguration { + + @Lazy + @Bean + WebTestClient mgmtClient(@Value("${local.management.port}") int port) { + return WebTestClient.bindToServer() + .baseUrl("http://localhost:" + port) + .responseTimeout(Duration.ofSeconds(300)) + .defaultHeaders(headers -> { + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + }) + .build(); + } +} diff --git a/src/test/java/org/onap/aai/rest/util/EchoHealthIndicatorTest.java b/src/test/java/org/onap/aai/rest/util/EchoHealthIndicatorTest.java new file mode 100644 index 0000000..7efc0db --- /dev/null +++ b/src/test/java/org/onap/aai/rest/util/EchoHealthIndicatorTest.java @@ -0,0 +1,72 @@ +/** + * ============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.rest.util; + +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.Test; +import org.onap.aai.config.WebClientConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.web.reactive.server.WebTestClient; + + +@Import(WebClientConfiguration.class) +@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, properties = "server.port=8080") +@TestPropertySource(properties = {"aai.actuator.echo.enabled=true", + "server.ssl.enabled=false"}) +public class EchoHealthIndicatorTest { + + @Autowired + @Qualifier("mgmtClient") + WebTestClient webClient; + + @MockBean private AaiGraphChecker aaiGraphChecker; + + @Test + public void thatActuatorCheckIsHealthy() { + when(aaiGraphChecker.isAaiGraphDbAvailable()).thenReturn(true); + + webClient.get() + .uri("/actuator/health") + .exchange() + .expectStatus() + .isOk(); + } + + @Test + public void thatActuatorCheckIsUnhealthy() { + when(aaiGraphChecker.isAaiGraphDbAvailable()).thenReturn(false); + + webClient.get() + .uri("/actuator/health") + .exchange() + .expectStatus() + .is5xxServerError(); + } +} diff --git a/src/test/java/org/onap/aai/rest/util/EchoResponseTest.java b/src/test/java/org/onap/aai/rest/util/EchoResponseTest.java new file mode 100644 index 0000000..95e1230 --- /dev/null +++ b/src/test/java/org/onap/aai/rest/util/EchoResponseTest.java @@ -0,0 +1,166 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. 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.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.anyObject;import static org.mockito.ArgumentMatchers.anyObject; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.onap.aai.AAISetup; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = {AaiGraphChecker.class}) +public class EchoResponseTest extends AAISetup { + + protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json"); + + private static final Set VALID_HTTP_STATUS_CODES = new HashSet<>(); + + static { + VALID_HTTP_STATUS_CODES.add(200); + VALID_HTTP_STATUS_CODES.add(201); + VALID_HTTP_STATUS_CODES.add(204); + } + + private EchoResponse echoResponse; + + private final AaiGraphChecker aaiGraphCheckerMock = mock(AaiGraphChecker.class); + + private HttpHeaders httpHeaders; + + private UriInfo uriInfo; + + private MultivaluedMap headersMultiMap; + private MultivaluedMap queryParameters; + + private List aaiRequestContextList; + + private List outputMediaTypes; + + private static final Logger logger = LoggerFactory.getLogger(EchoResponseTest.class.getName()); + + @BeforeEach + public void setup(){ + logger.info("Starting the setup for the integration tests of Rest Endpoints"); + + echoResponse = new EchoResponse(aaiGraphCheckerMock); + httpHeaders = mock(HttpHeaders.class); + uriInfo = mock(UriInfo.class); + + headersMultiMap = new MultivaluedHashMap<>(); + queryParameters = Mockito.spy(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", ""); + + outputMediaTypes = new ArrayList<>(); + outputMediaTypes.add(APPLICATION_JSON); + + aaiRequestContextList = new ArrayList<>(); + aaiRequestContextList.add(""); + + when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes); + when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap); + when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(Arrays.asList("JUNIT")); + when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT")); + + when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList); + + + when(uriInfo.getQueryParameters()).thenReturn(queryParameters); + when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters); + + // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable + Mockito.doReturn(null).when(queryParameters).remove(anyObject()); + + when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON); + } + + @Test + public void testEchoResultWhenValidHeaders() throws Exception { + + Response response = echoResponse.echoResult(httpHeaders, null, null); + + assertNotNull(response); + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + } + + @Test + public void testEchoResultWhenActionIsProvidedDbAvailable() throws Exception { + when(aaiGraphCheckerMock.isAaiGraphDbAvailable()).thenReturn(true); + Response response = echoResponse.echoResult(httpHeaders, null, "myAction"); + + assertNotNull(response); + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + } + + @Test + public void testEchoResultWhenActionIsProvidedDbNotAvailable() throws Exception { + when(aaiGraphCheckerMock.isAaiGraphDbAvailable()).thenReturn(false); + Response response = echoResponse.echoResult(httpHeaders, null, "myAction"); + + assertNotNull(response); + assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); + } + + @Test + public void testEchoResultWhenInValidHeadersThrowsBadRequest() throws Exception { + + httpHeaders = mock(HttpHeaders.class); + Response response = echoResponse.echoResult(httpHeaders, null, null); + + assertNotNull(response); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + } + + + @Test + public void testCheckDbNowAction_Unknown() { + when(aaiGraphCheckerMock.isAaiGraphDbAvailable()).thenReturn(null); + Response response = echoResponse.echoResult(httpHeaders, null, "myAction"); + // Verify + assertNotNull(response); + assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); + } +} \ No newline at end of file -- cgit 1.2.3-korg