import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
-import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
-import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
-import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Rule;
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 use get method query data from server");
+ thrown.expectMessage("Failed to connect to server");
String url = "host";
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
- String response = HttpsUtils.get(url, header);
+ CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpGet httpRequestBase = new HttpGet(url);
+ HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, 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.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);
HttpEntity responseEntity = new StringEntity("Test");
EasyMock.expect(response.getEntity()).andReturn(responseEntity);
- httpClient.close();
- EasyMock.expectLastCall();
+ PowerMock.replayAll();
+
+
+ String url = "localhost";
+ Map<String, String> header = new HashMap<>();
+ header.put("accept", "application/json");
+
+ HttpGet httpRequestBase = new HttpGet(url);
+ HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient);
+ String res = HttpsUtils.extractResponseEntity(httpResponse);
+
+ PowerMock.verifyAll();
+
+ assertThat(res, equalTo("Test"));
+ }
+
+ @Test
+ public void testHttpsUtil_delete_excepiton() throws Exception {
+ PowerMock.resetAll();
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage("Failed to connect to server");
+ String url = "host";
+ Map<String, String> header = new HashMap<>();
+ header.put("accept", "application/json");
+ HttpDelete httpRequestBase = new HttpDelete(url);
+ CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient);
+ String response = HttpsUtils.extractResponseEntity(httpResponse);
+ assertThat(response, equalTo(""));
+ }
+
+ @Test
+ public void testHttpsUtil_delete_normal() throws Exception {
+ PowerMock.resetAll();
+ CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
+ CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
+ EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
+ StatusLine sl = PowerMock.createMock(StatusLine.class);
+ EasyMock.expect(response.getStatusLine()).andReturn(sl);
+ EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
+ HttpEntity responseEntity = new StringEntity("Test");
+ EasyMock.expect(response.getEntity()).andReturn(responseEntity);
PowerMock.replayAll();
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
- HttpEntity entity = new StringEntity("Test");
- String res = HttpsUtils.get(url, header);
+ HttpDelete httpRequestBase = new HttpDelete(url);
+ HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, 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 use post method query data from server");
+ thrown.expectMessage("Failed to connect to server");
String url = "host";
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
Map<String, String> para = new HashMap<>();
para.put("tset", "1111");
- String response = HttpsUtils.post(url, header, para, null);
+ CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpPost httpPost = new HttpPost(url);
+ HttpResponse httpResponse = HttpsUtils.post(httpPost, 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.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);
+ EasyMock.expect(response.getStatusLine()).andReturn(sl);
+ EasyMock.expect(sl.getStatusCode()).andReturn(HttpStatus.SC_OK);
+ HttpEntity responseEntity = new StringEntity("Test");
+ EasyMock.expect(response.getEntity()).andReturn(responseEntity);
+
+ PowerMock.replayAll();
+
+
+ String url = "localhost";
+ Map<String, String> header = new HashMap<>();
+ header.put("accept", "application/json");
+ Map<String, String> para = new HashMap<>();
+ para.put("tset", "1111");
+
+ HttpEntity entity = new StringEntity("Test");
+ HttpPost httpPost = new HttpPost(url);
+ HttpResponse httpResponse = HttpsUtils.post(httpPost, header, para, entity, httpClient);
+ String res = HttpsUtils.extractResponseEntity(httpResponse);
+
+ PowerMock.verifyAll();
+ assertThat(res, equalTo("Test"));
+ }
+
+ @Test
+ public void testHttpsUtil_put_excepiton() throws Exception {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage("Failed to connect to server");
+ String url = "host";
+ Map<String, String> header = new HashMap<>();
+ header.put("accept", "application/json");
+ Map<String, String> para = new HashMap<>();
+ para.put("tset", "1111");
+ CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpPut httpPut = new HttpPut(url);
+ HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, null, httpClient);
+ String response = HttpsUtils.extractResponseEntity(httpResponse);
+ assertThat(response, equalTo(""));
+ }
+
+ @Test
+ public void testHttpsUtil_put_normal() throws Exception {
+ PowerMock.resetAll();
+ CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
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");
- String res = HttpsUtils.post(url, header, para, entity);
+ HttpPut httpPut = new HttpPut(url);
+ HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, entity, httpClient);
+ String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
PowerMock.resetAll();
httpsUtils = PowerMock.createMock(HttpsUtils.class);
PowerMock.replayAll();
- String actual = Whitebox.invokeMethod(httpsUtils, "getResponseEntity", null);
+ String actual = Whitebox.invokeMethod(httpsUtils, "extractResponseEntity", null);
PowerMock.verifyAll();
assertThat(actual, equalTo(""));
}
public void testHttpsUtil_getHttpClient_exception() throws Exception {
PowerMock.resetAll();
thrown.expect(Exception.class);
- Whitebox.invokeMethod(HttpsUtils.class, "getHttpClient");
+ Whitebox.invokeMethod(HttpsUtils.class, "getConditionalHttpsClient");
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void testHttpsUtil_getHttpClient_ok() throws Exception {
+ PowerMock.resetAll();
+ HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
PowerMock.verifyAll();
}