summaryrefslogtreecommitdiffstats
path: root/aai-resources/src/test
diff options
context:
space:
mode:
authorRaghav Kataria <raghav.kataria@bell.ca>2022-04-01 10:22:58 +0000
committerRommel Pawar <rommel.pawar@bell.ca>2022-09-21 13:03:25 -0700
commit5b383326540f84c17806831ed299c4c9234db8e1 (patch)
treeeec0ba370cd3c63b094dfbd3b14404c1e792370f /aai-resources/src/test
parent11c5d21682b7c27cd72949a8cdf73ecabc24d018 (diff)
Add aai-resources healthcheck based on Cassandra DB Healthcheck
Issue-ID: AAI-3528 Signed-off-by: Rommel Pawar <rommel.pawar@bell.ca> Change-Id: I21022bd81c5cd9eed2dd841f96812855adebe528
Diffstat (limited to 'aai-resources/src/test')
-rw-r--r--aai-resources/src/test/java/org/onap/aai/AAISetup.java3
-rw-r--r--aai-resources/src/test/java/org/onap/aai/rest/util/EchoResponseTest.java152
-rw-r--r--aai-resources/src/test/java/org/onap/aai/task/AaiGraphCheckerTest.java70
-rw-r--r--aai-resources/src/test/resources/etc/appprops/aaiconfig.properties13
4 files changed, 187 insertions, 51 deletions
diff --git a/aai-resources/src/test/java/org/onap/aai/AAISetup.java b/aai-resources/src/test/java/org/onap/aai/AAISetup.java
index 9a12007..dac7b60 100644
--- a/aai-resources/src/test/java/org/onap/aai/AAISetup.java
+++ b/aai-resources/src/test/java/org/onap/aai/AAISetup.java
@@ -1,4 +1,4 @@
-/**
+/*
* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
@@ -17,6 +17,7 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
+
package org.onap.aai;
import org.apache.commons.io.IOUtils;
diff --git a/aai-resources/src/test/java/org/onap/aai/rest/util/EchoResponseTest.java b/aai-resources/src/test/java/org/onap/aai/rest/util/EchoResponseTest.java
index 9f55961..a8e5583 100644
--- a/aai-resources/src/test/java/org/onap/aai/rest/util/EchoResponseTest.java
+++ b/aai-resources/src/test/java/org/onap/aai/rest/util/EchoResponseTest.java
@@ -1,4 +1,4 @@
-/**
+/*
* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
@@ -7,9 +7,9 @@
* 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
- *
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
* 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.
@@ -17,61 +17,56 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
-package org.onap.aai.rest.util;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mockito;
-import org.onap.aai.AAISetup;
-import javax.ws.rs.core.*;
-import java.util.*;
+package org.onap.aai.rest.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.UUID;
+import javax.ws.rs.core.*;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.onap.aai.AAISetup;
+import org.onap.aai.tasks.AaiGraphChecker;
+import org.onap.aai.tasks.AaiGraphChecker.CheckerType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.test.context.ContextConfiguration;
+
+@ContextConfiguration(classes = {AaiGraphChecker.class})
public class EchoResponseTest extends AAISetup {
+ private static final Logger logger = LoggerFactory.getLogger(EchoResponseTest.class);
protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
+ private static final String CHECK_DB_ACTION = "checkDB";
+ private static final String CHECK_DB_STATUS_NOW_ACTION = "checkDBNow";
- private static final Set<Integer> 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 EchoResponse echoResponse;
+ private final AaiGraphChecker aaiGraphCheckerMock = mock(AaiGraphChecker.class);
private HttpHeaders httpHeaders;
-
- private UriInfo uriInfo;
-
- private MultivaluedMap<String, String> headersMultiMap;
- private MultivaluedMap<String, String> queryParameters;
-
- private List<String> aaiRequestContextList;
-
private List<MediaType> outputMediaTypes;
- private static final Logger logger = LoggerFactory.getLogger(EchoResponseTest.class.getName());
+ public EchoResponseTest() {
+ this.echoResponse = new EchoResponse(aaiGraphCheckerMock);
+ }
@Before
- public void setup(){
+ public void setup() {
logger.info("Starting the setup for the integration tests of Rest Endpoints");
- echoResponse = new EchoResponse();
- httpHeaders = mock(HttpHeaders.class);
- uriInfo = mock(UriInfo.class);
-
- headersMultiMap = new MultivaluedHashMap<>();
- queryParameters = Mockito.spy(new MultivaluedHashMap<>());
+ MultivaluedMap<String, String> headersMultiMap = new MultivaluedHashMap<>();
+ MultivaluedMap<String, String> queryParameters = Mockito.spy(new MultivaluedHashMap<>());
headersMultiMap.add("X-FromAppId", "JUNIT");
headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
@@ -82,28 +77,28 @@ public class EchoResponseTest extends AAISetup {
outputMediaTypes = new ArrayList<>();
outputMediaTypes.add(APPLICATION_JSON);
- aaiRequestContextList = new ArrayList<>();
+ List<String> aaiRequestContextList = new ArrayList<>();
aaiRequestContextList.add("");
+ httpHeaders = mock(HttpHeaders.class);
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("X-FromAppId")).thenReturn(Collections.singletonList("JUNIT"));
+ when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Collections.singletonList("JUNIT"));
when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
+ when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
-
+ UriInfo uriInfo = mock(UriInfo.class);
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 {
+ public void testEchoResultWhenValidHeaders() {
Response response = echoResponse.echoResult(httpHeaders, null, "");
@@ -112,7 +107,7 @@ public class EchoResponseTest extends AAISetup {
}
@Test
- public void testEchoResultWhenInValidHeadersThrowsBadRequest() throws Exception {
+ public void testEchoResultWhenInValidHeadersThrowsBadRequest() {
httpHeaders = mock(HttpHeaders.class);
Response response = echoResponse.echoResult(httpHeaders, null, "");
@@ -122,14 +117,71 @@ public class EchoResponseTest extends AAISetup {
}
@Test
- public void testEchoResultWhenValidHeadersButMediaTypeWrong() throws Exception {
+ public void testEchoResultWhenValidHeadersButMediaTypeWrong() {
- when(httpHeaders.getAcceptableMediaTypes()).thenThrow(new IllegalStateException())
- .thenReturn(outputMediaTypes);
+ when(httpHeaders.getAcceptableMediaTypes()).thenThrow(new IllegalStateException()).thenReturn(outputMediaTypes);
Response response = echoResponse.echoResult(httpHeaders, null, "");
assertNotNull(response);
assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
}
+
+ @Test
+ public void testCheckDbAction_CachedSuccess() {
+ // Prepare
+ when(aaiGraphCheckerMock.isAaiGraphDbAvailable(CheckerType.CACHED)).thenReturn(Boolean.TRUE);
+ // Run
+ Response response = echoResponse.echoResult(httpHeaders, null, CHECK_DB_ACTION);
+ // Verify
+ verify(aaiGraphCheckerMock, never()).isAaiGraphDbAvailable(CheckerType.ACTUAL);
+ assertNotNull(response);
+ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
+ }
+
+ @Test
+ public void testCheckDbAction_CachedFailure() {
+ // Prepare
+ when(aaiGraphCheckerMock.isAaiGraphDbAvailable(CheckerType.CACHED)).thenReturn(Boolean.FALSE);
+ // Run
+ Response response = echoResponse.echoResult(httpHeaders, null, CHECK_DB_ACTION);
+ // Verify
+ verify(aaiGraphCheckerMock, never()).isAaiGraphDbAvailable(CheckerType.ACTUAL);
+ assertNotNull(response);
+ assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
+ }
+
+ @Test
+ public void testCheckDbNowAction_Success() {
+ // Prepare
+ when(aaiGraphCheckerMock.isAaiGraphDbAvailable(CheckerType.ACTUAL)).thenReturn(Boolean.TRUE);
+ // Run
+ Response response = echoResponse.echoResult(httpHeaders, null, CHECK_DB_STATUS_NOW_ACTION);
+ // Verify
+ assertNotNull(response);
+ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
+ }
+
+ @Test
+ public void testCheckDbNowAction_Failure() {
+ // Prepare
+ when(aaiGraphCheckerMock.isAaiGraphDbAvailable(CheckerType.ACTUAL)).thenReturn(Boolean.FALSE);
+ // Run
+ Response response = echoResponse.echoResult(httpHeaders, null, CHECK_DB_STATUS_NOW_ACTION);
+ // Verify
+ assertNotNull(response);
+ assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
+ }
+
+ @Test
+ public void testCheckDbNowAction_Unknown() {
+ // Prepare
+ when(aaiGraphCheckerMock.isAaiGraphDbAvailable(CheckerType.ACTUAL)).thenReturn(null);
+ // Run
+ Response response = echoResponse.echoResult(httpHeaders, null, CHECK_DB_STATUS_NOW_ACTION);
+ // Verify
+ assertNotNull(response);
+ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
+ }
+
}
diff --git a/aai-resources/src/test/java/org/onap/aai/task/AaiGraphCheckerTest.java b/aai-resources/src/test/java/org/onap/aai/task/AaiGraphCheckerTest.java
new file mode 100644
index 0000000..11a5134
--- /dev/null
+++ b/aai-resources/src/test/java/org/onap/aai/task/AaiGraphCheckerTest.java
@@ -0,0 +1,70 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Bell Canada
+ * ================================================================================
+ * 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.task;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.onap.aai.AAISetup;
+import org.onap.aai.tasks.AaiGraphChecker;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
+
+@ContextConfiguration(classes = {AaiGraphChecker.class})
+@TestPropertySource(properties = "aai.graph.checker.task.enabled=true")
+public class AaiGraphCheckerTest extends AAISetup {
+
+ @Autowired
+ private AaiGraphChecker subject;
+
+ @Test
+ public void testIsAaiGraphDbAvailable_Actual() {
+ // Run
+ Boolean result = subject.isAaiGraphDbAvailable(AaiGraphChecker.CheckerType.ACTUAL);
+ // Verify
+ assertNotNull(result);
+ assertTrue(result);
+ }
+
+ @Test
+ public void testIsAaiGraphDbAvailable_CachedAfterClear() {
+ // Prepare
+ subject.clearDbAvailabilityCachedIndicator();
+ // Run
+ Boolean result = subject.isAaiGraphDbAvailable(AaiGraphChecker.CheckerType.CACHED);
+ // Verify
+ assertNull(result);
+ }
+
+ @Test
+ public void testIsAaiGraphDbAvailable_CachedAfterActual() {
+ // Prepare
+ subject.clearDbAvailabilityCachedIndicator();
+ subject.isAaiGraphDbAvailable(AaiGraphChecker.CheckerType.ACTUAL);
+ // Run
+ Boolean result = subject.isAaiGraphDbAvailable(AaiGraphChecker.CheckerType.CACHED);
+ // Verify
+ assertNotNull(result);
+ assertTrue(result);
+ }
+
+}
diff --git a/aai-resources/src/test/resources/etc/appprops/aaiconfig.properties b/aai-resources/src/test/resources/etc/appprops/aaiconfig.properties
index a279738..76167b2 100644
--- a/aai-resources/src/test/resources/etc/appprops/aaiconfig.properties
+++ b/aai-resources/src/test/resources/etc/appprops/aaiconfig.properties
@@ -56,3 +56,16 @@ aai.run.migrations=false
aai.jms.enable=false
property.null.validation.enabled=true
+
+#
+# AAI Graph DB checker task
+#
+
+# Indicator to enable or disable scheduled task (true/false)
+aai.graph.checker.task.enabled=true
+
+# Delay, in seconds, before the scheduled task is started, if enabled
+aai.graph.checker.task.delay=30
+
+# Period, in seconds, between two consecutive executions of the scheduled task, if enabled
+aai.graph.checker.task.period=60