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;
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 {
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";
+ String url = "https://aai.onap/aai/v11/cloud-infrastructure";
HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
- when(HttpsUtils.get(url, headers)).thenReturn(httpResponse);
+ 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();
}
@Test
+
public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
PowerMock.resetAll();
thrown.expect(CorrelationException.class);
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 = "https://aai.onap/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();
String url = "host_url";
HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
- when(HttpsUtils.get(url, headers)).thenReturn(httpResponse);
+ 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");
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(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/url"));
}
@Test