Add some logs to AAI queries
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQueryTest.java
index f089881..11dba54 100644 (file)
 
 package org.onap.holmes.common.aai;
 import static org.easymock.EasyMock.anyObject;
-import static org.hamcrest.CoreMatchers.equalTo;
-import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
 import static org.powermock.api.mockito.PowerMockito.when;
 
 import java.util.HashMap;
 import java.util.Map;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.easymock.EasyMock;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -39,7 +43,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
 import org.powermock.reflect.Whitebox;
 
 
-@PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class})
+@PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class})
 @RunWith(PowerMockRunner.class)
 public class AaiQueryTest {
 
@@ -93,13 +97,22 @@ public class AaiQueryTest {
         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
         headers.put("Accept", "application/json");
-        String url = "host_url";
-        when(HttpsUtils.get(url, headers)).thenReturn("{}");
+        String url = "http://10.96.33.33:80/api/aai-cloudInfrastructure/v11";
+        HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
+        CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
+        when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
+        HttpGet httpGet = new HttpGet(url);
+        PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
+        when(HttpsUtils.get(httpGet, headers, httpClient)).thenReturn(httpResponse);
+        when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("{}");
 
         PowerMockito.mockStatic(MicroServiceConfig.class);
-        when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
+        when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
 
-        PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2").andReturn("");
+        PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
+                .andReturn("/aai/v11/cloud-infrastructure");
+        PowerMock.expectPrivate(httpClient, "close");
+        EasyMock.expectLastCall();
         PowerMock.replayAll();
         VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
         PowerMock.verifyAll();
@@ -107,38 +120,6 @@ public class AaiQueryTest {
         assertThat(vmEntity == null, equalTo(true));
     }
 
-
-
-    @Test
-    public void testAaiQuery_getAaiVmData_exception() throws Exception {
-        PowerMock.resetAll();
-        thrown.expect(CorrelationException.class);
-        thrown.expectMessage("Failed to convert aai vm response data to vm entity");
-        aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
-
-        aaiResponseUtil = new AaiResponseUtil();
-        Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
-
-        PowerMockito.mockStatic(HttpsUtils.class);
-        Map<String, String> headers = new HashMap<>();
-        headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
-        headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
-        headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
-        headers.put("Accept", "application/json");
-        String url = "host_url";
-
-        when(HttpsUtils.get(url, headers)).thenReturn("");
-
-        PowerMockito.mockStatic(MicroServiceConfig.class);
-        when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
-
-        PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2").andReturn("");
-
-        PowerMock.replayAll();
-        Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
-        PowerMock.verifyAll();
-    }
-
     @Test
     public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
         PowerMock.resetAll();
@@ -155,14 +136,18 @@ public class AaiQueryTest {
         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
         headers.put("Accept", "application/json");
-        String url = "host_url";
-
-        when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
-
+        String url = "http://10.96.33.33:80/api/aai-cloudInfrastructure/v11";
+        CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
+        when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
+        HttpGet httpGet = new HttpGet(url);
+        PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
+        when(HttpsUtils.get(httpGet, headers, httpClient)).thenThrow(new CorrelationException(""));
         PowerMockito.mockStatic(MicroServiceConfig.class);
-        when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
-
-        PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2").andReturn("");
+        when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
+        PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
+                .andReturn("/aai/v11/cloud-infrastructure");
+        PowerMock.expectPrivate(httpClient,"close");
+        EasyMock.expectLastCall();
         PowerMock.replayAll();
         Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
         PowerMock.verifyAll();
@@ -186,23 +171,7 @@ public class AaiQueryTest {
         assertThat(resource, equalTo("le-vserver-id-val-51834"));
     }
 
-    @Test
-    public void testAaiQuery_getVmResourceLinks_exception() throws Exception {
-        PowerMock.resetAll();
-        thrown.expect(CorrelationException.class);
-        thrown.expectMessage("Failed to get aai resource link");
-        aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getResourceLinksResponse");
-
-        aaiResponseUtil = new AaiResponseUtil();
-        Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
-
-        PowerMock.expectPrivate(aaiQuery, "getResourceLinksResponse", "test1", "test2").andReturn("");
-        PowerMock.replayAll();
-        String resource = Whitebox.invokeMethod(aaiQuery, "getVmResourceLinks", "test1", "test2");
-        PowerMock.verifyAll();
 
-        assertThat(resource, equalTo("le-vserver-id-val-51834"));
-    }
 
     @Test
     public void testAaiQuery_getResourceLinksResponse() throws Exception {
@@ -213,7 +182,7 @@ public class AaiQueryTest {
         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
 
         PowerMockito.mockStatic(MicroServiceConfig.class);
-        when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
+        when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
 
         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
         PowerMock.replayAll();
@@ -232,7 +201,7 @@ public class AaiQueryTest {
         Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
 
         PowerMockito.mockStatic(MicroServiceConfig.class);
-        when(MicroServiceConfig.getMsbServerAddr()).thenReturn("host_url");
+        when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("host_url");
 
         PowerMock.expectPrivate(aaiQuery, "getResponse", anyObject(String.class)).andReturn("").anyTimes();
         PowerMock.replayAll();
@@ -254,7 +223,15 @@ public class AaiQueryTest {
         headers.put("Accept", "application/json");
         String url = "host_url";
 
-        when(HttpsUtils.get(url, headers)).thenReturn("");
+        HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
+        CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
+        when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
+        HttpGet httpGet = new HttpGet(url);
+        PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
+        when(HttpsUtils.get(httpGet, headers, httpClient)).thenReturn(httpResponse);
+        when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("");
+        PowerMock.expectPrivate(httpClient, "close");
+        EasyMock.expectLastCall();
 
         PowerMock.replayAll();
         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
@@ -277,15 +254,17 @@ public class AaiQueryTest {
         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
         headers.put("Accept", "application/json");
         String url = "host_url";
-
-        when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
-
+        CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
+        when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
+        HttpGet httpGet = new HttpGet(url);
+        PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
+        when(HttpsUtils.get(httpGet, headers, httpClient)).thenThrow(new CorrelationException(""));
+        PowerMock.expectPrivate(httpClient, "close");
+        EasyMock.expectLastCall();
         PowerMock.replayAll();
         String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
         PowerMock.verifyAll();
-
         assertThat(resource, equalTo(""));
-
     }
 
     @Test
@@ -301,4 +280,64 @@ public class AaiQueryTest {
         assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
         assertThat(actual.get("Accept"), equalTo("application/json"));
     }
+
+    @Test
+    public void testAaiQuery_getBaseUrl_msb() throws Exception {
+        PowerMock.resetAll();
+        aaiQuery = new AaiQuery();
+
+        PowerMockito.mockStatic(MicroServiceConfig.class);
+        when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
+        when(MicroServiceConfig.getServiceConfigInfoFromCBS("nihao")).thenReturn("");
+
+        PowerMock.replayAll();
+        String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url");
+        PowerMock.verifyAll();
+        assertThat(actual, equalTo("http://10.96.33.33:80/url"));
+    }
+
+    @Test
+    public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
+        PowerMock.resetAll();
+        aaiQuery = new AaiQuery();
+
+        PowerMockito.mockStatic(MicroServiceConfig.class);
+        when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
+        when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config")).thenReturn("aai");
+
+        PowerMock.replayAll();
+        String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
+        System.out.println(actual);
+        PowerMock.verifyAll();
+        assertThat(actual, equalTo("https://aaiurl"));
+    }
+
+    @Test
+    public void testAaiQuery_getBaseUrl_exception() throws Exception {
+        PowerMock.resetAll();
+        aaiQuery = new AaiQuery();
+
+        PowerMockito.mockStatic(MicroServiceConfig.class);
+        when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenThrow(new NullPointerException());
+        when(MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config"))
+                .thenThrow(new NullPointerException());
+
+        PowerMock.replayAll();
+        String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
+        System.out.println(actual);
+        PowerMock.verifyAll();
+        assertThat(actual, equalTo(""));
+    }
+
+    @Test
+    public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception {
+        PowerMock.resetAll();
+        String url = "/aai/v11/network/generic-vnfs/generic-vnf?";
+        String expect = "/api/aai-network/v11/generic-vnfs/generic-vnf?";
+        aaiQuery = new AaiQuery();
+        PowerMock.replayAll();
+        String actual = Whitebox.invokeMethod(aaiQuery, "getMsbSuffixAddr", url);
+        PowerMock.verifyAll();
+        assertThat(actual, equalTo(expect));
+    }
 }