Custom Query Code
[policy/models.git] / models-interactions / model-impl / aai / src / test / java / org / onap / policy / aai / AaiManagerTest.java
index 111042f..c453d89 100644 (file)
@@ -31,10 +31,12 @@ import static org.mockito.ArgumentMatchers.startsWith;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.onap.policy.aai.util.Serialization;
@@ -48,12 +50,18 @@ public class AaiManagerTest {
     Pair<Integer, String> httpResponseErr0;
     Pair<Integer, String> httpResponseErr1;
     Pair<Integer, String> httpResponseWait;
+    Pair<Integer, String> httpTenantResponseOk;
+    Pair<Integer, String> httpCqResponseOk;
+
+    private static final String TENANT_RESPONSE_SAMPLE =
+            "src/test/resources/org/onap/policy/aai/AaiTenantResponse.json";
 
     /**
      * Set up test cases.
+     * @throws Exception  if error occurs
      */
     @Before
-    public void beforeTestAaiManager() {
+    public void beforeTestAaiManager() throws Exception {
         restManagerMock = mock(RestManager.class);
 
         Map<String, String> expectedHeaders = new HashMap<>();
@@ -61,6 +69,10 @@ public class AaiManagerTest {
         expectedHeaders.put("X-TransactionId", aaiNqRequestUuid.toString());
         expectedHeaders.put("Accept", "application/json");
 
+        String aaiCqResponse = new AaiCqResponseTest().getAaiCqResponse();
+        String tenantResponse = this.getTenantQueryResponse();
+        httpCqResponseOk = restManagerMock.new Pair<>(200, aaiCqResponse);
+        httpTenantResponseOk = restManagerMock.new Pair<>(200, tenantResponse);
         AaiNqResponse aaiNqResponse = new AaiNqResponseTest().getAaiNqResponse();
         httpResponseOk = restManagerMock.new Pair<>(200, Serialization.gsonPretty.toJson(aaiNqResponse));
         httpResponseErr0 = restManagerMock.new Pair<>(200, null);
@@ -68,6 +80,51 @@ public class AaiManagerTest {
         httpResponseWait = restManagerMock.new Pair<>(503, null);
     }
 
+    @Test
+    public void testAaiCqResponse() {
+        AaiManager aaiManager = new AaiManager(restManagerMock);
+        assertNotNull(aaiManager);
+
+        UUID vserverNameRequestId = UUID.randomUUID();
+
+        when(restManagerMock.put(startsWith("http://testing.cq.query"), eq("Foo"), eq("Bar"), anyMap(), anyString(),
+                anyString())).thenReturn(httpCqResponseOk);
+
+        when(restManagerMock.get(startsWith("http://testing.cq.query"), eq("Foo"), eq("Bar"), anyMap()))
+                .thenReturn(httpTenantResponseOk);
+
+        AaiCqResponse aaiCqResponse =
+                aaiManager.getCustomQueryResponse("http://testing.cq.query", "Foo", "Bar", vserverNameRequestId, "Foo");
+        assertNotNull(aaiCqResponse);
+
+        when(restManagerMock.put(eq(""), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
+                .thenReturn(httpResponseErr0);
+
+        when(restManagerMock.get(eq("/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap()))
+                .thenReturn(httpResponseErr0);
+
+        AaiCqResponse aaiCqResponseNull =
+                aaiManager.getCustomQueryResponse("", "Foo", "Bar", vserverNameRequestId, "Foo");
+        assertNull(aaiCqResponseNull);
+
+
+        when(restManagerMock.put(eq("Error"), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
+                .thenReturn(httpResponseErr1);
+
+        when(restManagerMock.get(eq("Error/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap()))
+                .thenReturn(httpResponseErr1);
+
+        AaiCqResponse aaiCqResponseErr =
+                aaiManager.getCustomQueryResponse("Error", "Foo", "Bar", vserverNameRequestId, "Foo");
+        assertNull(aaiCqResponseErr);
+    }
+
+    private String getTenantQueryResponse() throws IOException {
+        String responseString = "";
+        responseString = new String(Files.readAllBytes(new File(TENANT_RESPONSE_SAMPLE).toPath()));
+        return responseString;
+    }
+
     @Test
     public void testAaiManagerAaiNqRequest() {