X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=holmes-actions%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fholmes%2Fcommon%2Faai%2FAaiQueryTest.java;h=c3407e6c3476f2ebe705be7e7bc9d16ae0fe2823;hb=75b8475a3c9a55a154db8d5d2a05dd5f993907a0;hp=e0d5bf7d4bad03d24cae824c3db1bbcb928a3c79;hpb=83307e37a264972f4f9c60a114f40a375e32634b;p=holmes%2Fcommon.git diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java index e0d5bf7..c3407e6 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java @@ -22,6 +22,10 @@ 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,14 +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 = "http://10.96.33.33/api/aai-cloudInfrastructure/v11"; - when(HttpsUtils.get(url, headers)).thenReturn("{}"); + String url = "http://10.96.33.33:80/aai/v11/cloud-infrastructure"; + 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.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(); VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2"); PowerMock.verifyAll(); @@ -108,10 +120,6 @@ public class AaiQueryTest { assertThat(vmEntity == null, equalTo(true)); } - - - - @Test public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception { PowerMock.resetAll(); @@ -128,15 +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 = "http://10.96.33.33/api/aai-cloudInfrastructure/v11"; - - when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException("")); - + String url = "http://10.96.33.33:80/aai/v11/cloud-infrastructure"; + 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.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(); @@ -212,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"); @@ -235,7 +254,13 @@ 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(); @@ -268,7 +293,7 @@ public class AaiQueryTest { PowerMock.replayAll(); String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url"); PowerMock.verifyAll(); - assertThat(actual, equalTo("http://10.96.33.33/url")); + assertThat(actual, equalTo("http://10.96.33.33:80/url")); } @Test