import org.junit.runner.RunWith;
import org.onap.holmes.common.exception.CorrelationException;
import org.powermock.api.easymock.PowerMock;
+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({CloseableHttpClient.class, HttpClientBuilder.class, HttpClients.class, CloseableHttpResponse.class,
StatusLine.class})
@RunWith(PowerMockRunner.class)
+@PowerMockIgnore("javax.net.ssl.*")
public class HttpsUtilsTest {
@Rule
@Test
public void testHttpsUtil_get_excepiton() throws Exception {
+ PowerMock.resetAll();
thrown.expect(CorrelationException.class);
thrown.expectMessage("Failed to query data from server through GET method!");
String url = "host";
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
- HttpResponse httpResponse = HttpsUtils.get(url, header);
+ CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
@Test
public void testHttpsUtil_get_normal() throws Exception {
- HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
+ PowerMock.resetAll();
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- PowerMock.mockStatic(HttpClients.class);
- EasyMock.expect(HttpClients.custom()).andReturn(hcb);
- EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
- EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
- EasyMock.expect(hcb.build()).andReturn(httpClient);
-
CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
StatusLine sl = PowerMock.createMock(StatusLine.class);
header.put("accept", "application/json");
HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.get(url, header);
+ HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
@Test
public void testHttpsUtil_delete_excepiton() throws Exception {
+ PowerMock.resetAll();
thrown.expect(CorrelationException.class);
thrown.expectMessage("Failed to query data from server through DELETE method!");
String url = "host";
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
- HttpResponse httpResponse = HttpsUtils.delete(url, header);
+ CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
@Test
public void testHttpsUtil_delete_normal() throws Exception {
- HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
+ PowerMock.resetAll();
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- PowerMock.mockStatic(HttpClients.class);
- EasyMock.expect(HttpClients.custom()).andReturn(hcb);
- EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
- EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
- EasyMock.expect(hcb.build()).andReturn(httpClient);
-
CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
StatusLine sl = PowerMock.createMock(StatusLine.class);
header.put("accept", "application/json");
HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.delete(url, header);
+ HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
@Test
public void testHttpsUtil_post_excepiton() throws Exception {
+ PowerMock.resetAll();
thrown.expect(CorrelationException.class);
thrown.expectMessage("Failed to query data from server through POST method!");
String url = "host";
header.put("accept", "application/json");
Map<String, String> para = new HashMap<>();
para.put("tset", "1111");
-
- HttpResponse httpResponse = HttpsUtils.post(url, header, para, null);
+ CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.post(url, header, para, null, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
@Test
public void testHttpsUtil_post_normal() throws Exception {
- HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
+ PowerMock.resetAll();
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- PowerMock.mockStatic(HttpClients.class);
- EasyMock.expect(HttpClients.custom()).andReturn(hcb);
- EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
- EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
- EasyMock.expect(hcb.build()).andReturn(httpClient);
-
CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
StatusLine sl = PowerMock.createMock(StatusLine.class);
para.put("tset", "1111");
HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity);
+ HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
header.put("accept", "application/json");
Map<String, String> para = new HashMap<>();
para.put("tset", "1111");
-
- HttpResponse httpResponse = HttpsUtils.put(url, header, para, null);
+ CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.put(url, header, para, null, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
@Test
public void testHttpsUtil_put_normal() throws Exception {
- HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
+ PowerMock.resetAll();
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- PowerMock.mockStatic(HttpClients.class);
- EasyMock.expect(HttpClients.custom()).andReturn(hcb);
- EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
- EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
- EasyMock.expect(hcb.build()).andReturn(httpClient);
-
CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
StatusLine sl = PowerMock.createMock(StatusLine.class);
para.put("tset", "1111");
HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity);
+ HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
PowerMock.verifyAll();
}
+ @Test
+ public void testHttpsUtil_getHttpClient_ok() throws Exception {
+ PowerMock.resetAll();
+ HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ PowerMock.verifyAll();
+ }
+
}
\ No newline at end of file