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=7ce3fcfd53119317a9708b1eb1ae62e057ea10e1;hb=729f9e2226a8a0f0a0b06f1d53d68a493041eba5;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..7ce3fcf 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 @@ -16,12 +16,18 @@ package org.onap.holmes.common.aai; import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; 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.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -34,12 +40,14 @@ import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.HttpsUtils; import org.powermock.api.easymock.PowerMock; import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; 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}) +@PowerMockIgnore("javax.net.ssl.*") @RunWith(PowerMockRunner.class) public class AaiQueryTest { @@ -49,6 +57,11 @@ public class AaiQueryTest { private AaiQuery aaiQuery; private AaiResponseUtil aaiResponseUtil; + @BeforeClass + static public void before() { + System.setProperty("ENABLE_ENCRYPT", "true"); + } + @Test public void testAaiQuery_getAaiVnfData_ok() throws Exception { PowerMock.resetAll(); @@ -87,20 +100,26 @@ public class AaiQueryTest { aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks"); aaiResponseUtil = new AaiResponseUtil(); Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil); - PowerMockito.mockStatic(HttpsUtils.class); + PowerMock.mockStatic(HttpsUtils.class); Map 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 = "http://10.96.33.33/api/aai-cloudInfrastructure/v11"; - when(HttpsUtils.get(url, headers)).thenReturn("{}"); - - PowerMockito.mockStatic(MicroServiceConfig.class); - when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80"); + String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure"; + HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); + CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); + expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); + HttpGet httpGet = new HttpGet(url); + PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); + expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class), + anyObject(CloseableHttpClient.class))).andReturn(httpResponse); + expect(HttpsUtils.extractResponseEntity(httpResponse)).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(); @@ -108,10 +127,6 @@ public class AaiQueryTest { assertThat(vmEntity == null, equalTo(true)); } - - - - @Test public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception { PowerMock.resetAll(); @@ -122,21 +137,24 @@ public class AaiQueryTest { aaiResponseUtil = new AaiResponseUtil(); Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil); - PowerMockito.mockStatic(HttpsUtils.class); + PowerMock.mockStatic(HttpsUtils.class); Map 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 = "http://10.96.33.33/api/aai-cloudInfrastructure/v11"; - - when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException("")); - - PowerMockito.mockStatic(MicroServiceConfig.class); - when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80"); - + String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure"; + CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); + EasyMock.expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); + HttpGet httpGet = new HttpGet(url); + PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); + EasyMock.expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class), + anyObject(CloseableHttpClient.class))).andThrow(new CorrelationException("")); + PowerMock.mockStatic(MicroServiceConfig.class); 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(); @@ -204,7 +222,7 @@ public class AaiQueryTest { public void testAaiQuery_getResponse_ok() throws Exception { PowerMock.resetAll(); aaiQuery = new AaiQuery(); - PowerMockito.mockStatic(HttpsUtils.class); + PowerMock.mockStatic(HttpsUtils.class); Map headers = new HashMap<>(); headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID); headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID); @@ -212,7 +230,16 @@ 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); + expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); + HttpGet httpGet = new HttpGet(url); + PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); + expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class), + anyObject(CloseableHttpClient.class))).andReturn(httpResponse); + expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn(""); + PowerMock.expectPrivate(httpClient, "close"); + EasyMock.expectLastCall(); PowerMock.replayAll(); String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url"); @@ -228,14 +255,20 @@ public class AaiQueryTest { thrown.expectMessage("Failed to get data from aai"); aaiQuery = new AaiQuery(); - PowerMockito.mockStatic(HttpsUtils.class); + PowerMock.mockStatic(HttpsUtils.class); Map 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)).thenThrow(new CorrelationException("")); + CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); + expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); + HttpGet httpGet = new HttpGet(url); + PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); + expect(HttpsUtils.get(httpGet, headers, httpClient)).andThrow(new CorrelationException("")); + PowerMock.expectPrivate(httpClient, "close"); + EasyMock.expectLastCall(); PowerMock.replayAll(); String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url"); PowerMock.verifyAll(); @@ -256,52 +289,18 @@ public class AaiQueryTest { 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/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); + String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url"); 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("")); + assertThat(actual, equalTo("https://aai.onap:8443/url")); } @Test