summaryrefslogtreecommitdiffstats
path: root/holmes-actions
diff options
context:
space:
mode:
authorShiwei Tian <tian.shiwei@zte.com.cn>2018-03-05 19:38:25 +0800
committerShiwei Tian <tian.shiwei@zte.com.cn>2018-03-05 19:38:25 +0800
commitbf20ddf00200c5468da7a0090caf28beebb93e9c (patch)
tree11826395b90f04a917ff1d5731009e999f70bd53 /holmes-actions
parente37f1a4ce3da909a945e2faf7d3166a3b1a96a93 (diff)
Change HTTP Requests into HTTPS Ones
Issue-ID: HOLMES-104 Change-Id: Iab79fb0d9cda1f99d99110b2006f1231aaa1a305 Signed-off-by: Shiwei Tian <tian.shiwei@zte.com.cn>
Diffstat (limited to 'holmes-actions')
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java6
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java37
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java143
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java13
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java27
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/utils/HttpsUtilsTest.java133
6 files changed, 270 insertions, 89 deletions
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
index b3005d5..ef96476 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
@@ -18,6 +18,7 @@ import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j;
+import org.apache.http.HttpResponse;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.aai.entity.VmEntity;
@@ -137,9 +138,10 @@ public class AaiQuery {
}
private String getResponse(String url) throws CorrelationException {
- String response = "";
+ String response;
try {
- response = HttpsUtils.get(url, getHeaders());
+ HttpResponse httpResponse = HttpsUtils.get(url, getHeaders());
+ response = HttpsUtils.extractResponseEntity(httpResponse);
} catch (Exception e) {
throw new CorrelationException("Failed to get data from aai", e);
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java
index 7201f36..ad5109b 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java
@@ -15,19 +15,18 @@
*/
package org.onap.holmes.common.dmaap;
+import org.onap.holmes.common.dmaap.entity.PolicyMsg;
+import org.onap.holmes.common.exception.CorrelationException;
import com.alibaba.fastjson.JSON;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.WebTarget;
+import java.util.HashMap;
import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
import lombok.Getter;
import lombok.Setter;
+import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
+import org.apache.http.entity.StringEntity;
import org.jvnet.hk2.annotations.Service;
-import org.onap.holmes.common.dmaap.entity.PolicyMsg;
-import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.HttpsUtils;
@Getter
@Setter
@@ -40,20 +39,26 @@ public class Publisher {
private String authExpDate;
public boolean publish(PolicyMsg msg) throws CorrelationException {
- Client client = ClientBuilder.newClient();
- String content = JSON.toJSONString(msg);
- WebTarget webTarget = client.target(url);
- Response response = null;
+ String content;
+ try {
+ content = JSON.toJSONString(msg);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to convert the message object to a json string.",
+ e);
+ }
+ HttpResponse httpResponse;
+ HashMap<String, String> headers = new HashMap<>();
+ headers.put("Accept", MediaType.APPLICATION_JSON);
+ headers.put("Content-Type", MediaType.APPLICATION_JSON);
try {
- response = webTarget.request(MediaType.APPLICATION_JSON)
- .post(Entity.entity(content, MediaType.APPLICATION_JSON));
+ httpResponse = HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content, "utf-8"));
} catch (Exception e) {
throw new CorrelationException("Failed to connect to DCAE.", e);
}
- return checkStatus(response);
+ return checkStatus(httpResponse);
}
- private boolean checkStatus(Response response) {
- return response.getStatus() == HttpStatus.SC_OK;
+ private boolean checkStatus(HttpResponse httpResponse) {
+ return (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) ? true : false;
}
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
index 3d3c827..41db955 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
@@ -26,9 +26,13 @@ import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
+import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
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.config.Registry;
import org.apache.http.config.RegistryBuilder;
@@ -51,6 +55,7 @@ import org.onap.holmes.common.exception.CorrelationException;
public class HttpsUtils {
private static final String HTTP = "http";
private static final String HTTPS = "https";
+ private static final int DEFUALT_TIMEOUT = 30000;
private static SSLConnectionSocketFactory sslConnectionSocketFactory = null;
private static PoolingHttpClientConnectionManager connectionManager = null;
private static SSLContextBuilder sslContextBuilder = null;
@@ -78,50 +83,87 @@ public class HttpsUtils {
}
}
- public static String post(String url, Map<String, String> header, Map<String, String> param,
- HttpEntity entity) throws Exception {
- HttpResponse httpResponse = null;
+ public static HttpResponse post(String url, Map<String, String> header, Map<String, String> param,
+ HttpEntity entity) throws CorrelationException {
+ return post(url, header, param, entity, DEFUALT_TIMEOUT);
+ }
+
+ public static HttpResponse post(String url, Map<String, String> header, Map<String, String> param,
+ HttpEntity entity, int timeout) throws CorrelationException {
+ HttpResponse response;
+ HttpPost httpPost = new HttpPost(url);
try {
- CloseableHttpClient httpClient = getHttpClient();
- HttpPost httpPost = getHttpPost(url, header, param, entity);
- httpResponse = getHttpResponse(httpClient, httpPost);
+ CloseableHttpClient httpClient = getHttpClient(timeout);
+ addHeaders(header, httpPost);
+ addParams(param, httpPost);
+ if (entity != null) {
+ httpPost.setEntity(entity);
+ }
+ response = executeRequest(httpClient, httpPost);
} catch (Exception e) {
- throw new CorrelationException("Failed to use post method query data from server");
+ throw new CorrelationException("Failed to query data from server through POST method!");
}
- return getResponseEntity(httpResponse);
+ return response;
}
- public static String get(String url, Map<String, String> header) throws Exception {
- HttpResponse httpResponse = null;
- CloseableHttpClient httpClient = null;
- HttpGet httpGet = null;
- String response = "";
+ public static HttpResponse put(String url, Map<String, String> header, Map<String, String> param,
+ HttpEntity entity) throws CorrelationException {
+ return put(url, header, param, entity, DEFUALT_TIMEOUT);
+ }
+
+ public static HttpResponse put(String url, Map<String, String> header, Map<String, String> param,
+ HttpEntity entity, int timeout) throws CorrelationException {
+ HttpResponse response;
+ HttpPut httpPut = new HttpPut(url);
try {
- httpClient = getHttpClient();
- httpGet = getHttpGet(url, header);
- httpResponse = getHttpResponse(httpClient, httpGet);
- response = getResponseEntity(httpResponse);
- } catch (Exception e) {
- throw new CorrelationException("Failed to use get method query data from server");
- } finally {
- if (httpGet != null) {
- httpGet.releaseConnection();
- }
- if (httpResponse != null) {
- httpClient.close();
+ CloseableHttpClient httpClient = getHttpClient(timeout);
+ addHeaders(header, httpPut);
+ addParams(param, httpPut);
+ if (entity != null) {
+ httpPut.setEntity(entity);
}
+ response = executeRequest(httpClient, httpPut);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to query data from server through PUT method!");
}
return response;
}
- private static HttpPost getHttpPost(String url, Map<String, String> header,
- Map<String, String> param, HttpEntity entity) {
- HttpPost httpPost = new HttpPost(url);
- if (!header.isEmpty()) {
- for (Map.Entry<String, String> entry : header.entrySet()) {
- httpPost.addHeader(entry.getKey(), entry.getValue());
- }
+ public static HttpResponse get(String url, Map<String, String> header) throws CorrelationException {
+ return get(url, header, DEFUALT_TIMEOUT);
+ }
+
+ public static HttpResponse get(String url, Map<String, String> header, int timeout) throws CorrelationException {
+ HttpResponse response;
+ HttpGet httpGet = new HttpGet(url);
+ try {
+ CloseableHttpClient httpClient = getHttpClient(timeout);
+ addHeaders(header, httpGet);
+ response = executeRequest(httpClient, httpGet);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to query data from server through GET method!");
+ }
+ return response;
+ }
+
+ public static HttpResponse delete(String url, Map<String, String> header) throws CorrelationException {
+ return delete(url, header, DEFUALT_TIMEOUT);
+ }
+
+ public static HttpResponse delete(String url, Map<String, String> header, int timeout) throws CorrelationException {
+ HttpResponse response;
+ HttpDelete httpDelete = new HttpDelete(url);
+ try {
+ CloseableHttpClient httpClient = getHttpClient(timeout);
+ addHeaders(header, httpDelete);
+ response = executeRequest(httpClient, httpDelete);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to query data from server through DELETE method!");
}
+ return response;
+ }
+
+ private static void addParams(Map<String, String> param, HttpEntityEnclosingRequestBase requestBase) {
if (!param.isEmpty()) {
List<NameValuePair> formparams = new ArrayList<>();
for (Map.Entry<String, String> entry : param.entrySet()) {
@@ -129,49 +171,60 @@ public class HttpsUtils {
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams,
Consts.UTF_8);
- httpPost.setEntity(urlEncodedFormEntity);
- }
- if (entity != null) {
- httpPost.setEntity(entity);
+ requestBase.setEntity(urlEncodedFormEntity);
}
- return httpPost;
}
- private static HttpGet getHttpGet(String url, Map<String, String> header) {
- HttpGet httpGet = new HttpGet(url);
+ private static HttpRequestBase addHeaders(Map<String, String> header, HttpRequestBase httpRequestBase) {
if (!header.isEmpty()) {
for (Map.Entry<String, String> entry : header.entrySet()) {
- httpGet.addHeader(entry.getKey(), entry.getValue());
+ httpRequestBase.addHeader(entry.getKey(), entry.getValue());
}
}
- return httpGet;
+ return httpRequestBase;
}
- private static String getResponseEntity(HttpResponse httpResponse) throws IOException {
+ public static String extractResponseEntity(HttpResponse httpResponse)
+ throws CorrelationException, IOException {
String result = "";
if (httpResponse != null) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = httpResponse.getEntity();
result = EntityUtils.toString(resEntity);
+ } else {
+ throw new CorrelationException("Get a error status from server : " + statusCode);
}
}
return result;
}
- private static HttpResponse getHttpResponse(CloseableHttpClient httpClient, HttpRequestBase httpRequest)
+ private static HttpResponse executeRequest(CloseableHttpClient httpClient, HttpRequestBase httpRequest)
throws Exception {
- HttpResponse httpResponse = null;
+ HttpResponse httpResponse;
try {
httpResponse = httpClient.execute(httpRequest);
} catch (Exception e) {
throw new CorrelationException("Failed to get data from server");
+ } finally {
+ if (httpRequest != null) {
+ httpRequest.releaseConnection();
+ }
+ if (httpClient != null) {
+ httpClient.close();
+ }
}
return httpResponse;
}
- private static CloseableHttpClient getHttpClient() throws Exception {
+ private static CloseableHttpClient getHttpClient(int timeout) throws Exception {
+ RequestConfig defaultRequestConfig = RequestConfig.custom()
+ .setSocketTimeout(timeout)
+ .setConnectTimeout(timeout)
+ .setConnectionRequestTimeout(timeout)
+ .build();
CloseableHttpClient httpClient = HttpClients.custom()
+ .setDefaultRequestConfig(defaultRequestConfig)
.setSSLSocketFactory(sslConnectionSocketFactory)
.setConnectionManager(connectionManager)
.setConnectionManagerShared(true)
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..44e39b1 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,7 @@ import static org.powermock.api.mockito.PowerMockito.when;
import java.util.HashMap;
import java.util.Map;
+import org.apache.http.HttpResponse;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -94,7 +95,9 @@ public class AaiQueryTest {
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("{}");
+ HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
+ when(HttpsUtils.get(url, headers)).thenReturn(httpResponse);
+ when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("{}");
PowerMockito.mockStatic(MicroServiceConfig.class);
when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
@@ -108,10 +111,6 @@ public class AaiQueryTest {
assertThat(vmEntity == null, equalTo(true));
}
-
-
-
-
@Test
public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
PowerMock.resetAll();
@@ -212,7 +211,9 @@ public class AaiQueryTest {
headers.put("Accept", "application/json");
String url = "host_url";
- when(HttpsUtils.get(url, headers)).thenReturn("");
+ HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
+ when(HttpsUtils.get(url, headers)).thenReturn(httpResponse);
+ when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("");
PowerMock.replayAll();
String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java
index a36ecc7..164c176 100644
--- a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java
+++ b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java
@@ -18,6 +18,7 @@ package org.onap.holmes.common.dmaap;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import java.util.HashMap;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
@@ -25,19 +26,25 @@ import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
+import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
+import org.apache.http.StatusLine;
+import org.apache.http.entity.StringEntity;
import org.easymock.EasyMock;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
+import org.mockito.Matchers;
import org.onap.holmes.common.dmaap.entity.PolicyMsg;
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.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
-@PrepareForTest({Client.class, WebTarget.class, ClientBuilder.class, Response.class, Builder.class})
+@PrepareForTest({HttpsUtils.class, HttpResponse.class})
@RunWith(PowerMockRunner.class)
public class PublisherTest {
@@ -64,17 +71,13 @@ public class PublisherTest {
Publisher publisher = new Publisher();
publisher.setUrl(URL);
- WebTarget target = PowerMock.createMock(WebTarget.class);
- Client client = PowerMock.createMock(Client.class);
- Builder builder = PowerMock.createMock(Builder.class);
- Response response = PowerMock.createMock(Response.class);
- PowerMock.mockStatic(ClientBuilder.class);
-
- EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
- EasyMock.expect(client.target(publisher.getUrl())).andReturn(target);
- EasyMock.expect(target.request(MediaType.APPLICATION_JSON)).andReturn(builder);
- EasyMock.expect(builder.post(EasyMock.anyObject(Entity.class))).andReturn(response);
- EasyMock.expect(response.getStatus()).andReturn(HttpStatus.SC_OK);
+ PowerMockito.mockStatic(HttpsUtils.class);
+ HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
+ PowerMockito.when(HttpsUtils.post(Matchers.eq("http://localhost/dmaapTopic"),
+ Matchers.any(HashMap.class), Matchers.any(HashMap.class), Matchers.any(StringEntity.class))).thenReturn(httpResponse);
+ StatusLine statusLine = PowerMockito.mock(StatusLine.class);
+ PowerMockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
+ PowerMockito.when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
PowerMock.replayAll();
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/HttpsUtilsTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/HttpsUtilsTest.java
index 464a6f0..21bf0e2 100644
--- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/HttpsUtilsTest.java
+++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/HttpsUtilsTest.java
@@ -26,7 +26,7 @@ import org.apache.http.HttpEntity;
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.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
@@ -65,11 +65,12 @@ public class HttpsUtilsTest {
@Test
public void testHttpsUtil_get_excepiton() throws Exception {
thrown.expect(CorrelationException.class);
- thrown.expectMessage("Failed to use get method query data from server");
+ 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");
- String response = HttpsUtils.get(url, header);
+ HttpResponse httpResponse = HttpsUtils.get(url, header);
+ String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
@@ -79,6 +80,7 @@ public class HttpsUtilsTest {
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);
@@ -103,7 +105,59 @@ public class HttpsUtilsTest {
header.put("accept", "application/json");
HttpEntity entity = new StringEntity("Test");
- String res = HttpsUtils.get(url, header);
+ HttpResponse httpResponse = HttpsUtils.get(url, header);
+ String res = HttpsUtils.extractResponseEntity(httpResponse);
+
+ PowerMock.verifyAll();
+
+ assertThat(res, equalTo("Test"));
+ }
+
+ @Test
+ public void testHttpsUtil_delete_excepiton() throws Exception {
+ 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);
+ String response = HttpsUtils.extractResponseEntity(httpResponse);
+ assertThat(response, equalTo(""));
+ }
+
+ @Test
+ public void testHttpsUtil_delete_normal() throws Exception {
+ HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
+ 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);
+ 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);
+
+ httpClient.close();
+ EasyMock.expectLastCall();
+
+ PowerMock.replayAll();
+
+
+ String url = "localhost";
+ Map<String, String> header = new HashMap<>();
+ header.put("accept", "application/json");
+
+ HttpEntity entity = new StringEntity("Test");
+ HttpResponse httpResponse = HttpsUtils.delete(url, header);
+ String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
@@ -113,13 +167,15 @@ public class HttpsUtilsTest {
@Test
public void testHttpsUtil_post_excepiton() throws Exception {
thrown.expect(CorrelationException.class);
- thrown.expectMessage("Failed to use post method query data from server");
+ thrown.expectMessage("Failed to query data from server through POST method!");
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);
+
+ HttpResponse httpResponse = HttpsUtils.post(url, header, para, null);
+ String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
@@ -129,6 +185,7 @@ public class HttpsUtilsTest {
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);
@@ -142,6 +199,65 @@ public class HttpsUtilsTest {
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");
+ Map<String, String> para = new HashMap<>();
+ para.put("tset", "1111");
+
+ HttpEntity entity = new StringEntity("Test");
+ HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity);
+ 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 query data from server through PUT method!");
+ String url = "host";
+ Map<String, String> header = new HashMap<>();
+ header.put("accept", "application/json");
+ Map<String, String> para = new HashMap<>();
+ para.put("tset", "1111");
+
+ HttpResponse httpResponse = HttpsUtils.put(url, header, para, null);
+ String response = HttpsUtils.extractResponseEntity(httpResponse);
+ assertThat(response, equalTo(""));
+ }
+
+ @Test
+ public void testHttpsUtil_put_normal() throws Exception {
+ HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
+ 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);
+ 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);
+
+ httpClient.close();
+ EasyMock.expectLastCall();
+
PowerMock.replayAll();
@@ -152,7 +268,8 @@ public class HttpsUtilsTest {
para.put("tset", "1111");
HttpEntity entity = new StringEntity("Test");
- String res = HttpsUtils.post(url, header, para, entity);
+ HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity);
+ String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
@@ -164,7 +281,7 @@ public class HttpsUtilsTest {
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(""));
}