Release Version 1.3.3
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / utils / HttpsUtilsTest.java
index c47542b..db9423a 100644 (file)
@@ -26,15 +26,16 @@ 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.config.RequestConfig;
 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;
@@ -68,12 +69,13 @@ public class HttpsUtilsTest {
     public void testHttpsUtil_get_excepiton() throws Exception {
         PowerMock.resetAll();
         thrown.expect(CorrelationException.class);
-        thrown.expectMessage("Failed to query data from server through GET method!");
+        thrown.expectMessage("Failed to connect to server");
         String url = "host";
         Map<String, String> header = new HashMap<>();
         header.put("accept", "application/json");
-        CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
-        HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
+        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(""));
     }
@@ -90,9 +92,6 @@ public class HttpsUtilsTest {
         HttpEntity responseEntity = new StringEntity("Test");
         EasyMock.expect(response.getEntity()).andReturn(responseEntity);
 
-        httpClient.close();
-        EasyMock.expectLastCall();
-
         PowerMock.replayAll();
 
 
@@ -100,8 +99,8 @@ public class HttpsUtilsTest {
         Map<String, String> header = new HashMap<>();
         header.put("accept", "application/json");
 
-        HttpEntity entity = new StringEntity("Test");
-        HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
+        HttpGet httpRequestBase = new HttpGet(url);
+        HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient);
         String res = HttpsUtils.extractResponseEntity(httpResponse);
 
         PowerMock.verifyAll();
@@ -113,12 +112,13 @@ public class HttpsUtilsTest {
     public void testHttpsUtil_delete_excepiton() throws Exception {
         PowerMock.resetAll();
         thrown.expect(CorrelationException.class);
-        thrown.expectMessage("Failed to query data from server through DELETE method!");
+        thrown.expectMessage("Failed to connect to server");
         String url = "host";
         Map<String, String> header = new HashMap<>();
         header.put("accept", "application/json");
-        CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
-        HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
+        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(""));
     }
@@ -135,9 +135,6 @@ public class HttpsUtilsTest {
         HttpEntity responseEntity = new StringEntity("Test");
         EasyMock.expect(response.getEntity()).andReturn(responseEntity);
 
-        httpClient.close();
-        EasyMock.expectLastCall();
-
         PowerMock.replayAll();
 
 
@@ -145,8 +142,8 @@ public class HttpsUtilsTest {
         Map<String, String> header = new HashMap<>();
         header.put("accept", "application/json");
 
-        HttpEntity entity = new StringEntity("Test");
-        HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
+        HttpDelete httpRequestBase = new HttpDelete(url);
+        HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient);
         String res = HttpsUtils.extractResponseEntity(httpResponse);
 
         PowerMock.verifyAll();
@@ -158,14 +155,15 @@ public class HttpsUtilsTest {
     public void testHttpsUtil_post_excepiton() throws Exception {
         PowerMock.resetAll();
         thrown.expect(CorrelationException.class);
-        thrown.expectMessage("Failed to query data from server through POST method!");
+        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.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
-        HttpResponse httpResponse = HttpsUtils.post(url, header, para, null, httpClient);
+        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(""));
     }
@@ -182,9 +180,6 @@ public class HttpsUtilsTest {
         HttpEntity responseEntity = new StringEntity("Test");
         EasyMock.expect(response.getEntity()).andReturn(responseEntity);
 
-        httpClient.close();
-        EasyMock.expectLastCall();
-
         PowerMock.replayAll();
 
 
@@ -195,7 +190,8 @@ public class HttpsUtilsTest {
         para.put("tset", "1111");
 
         HttpEntity entity = new StringEntity("Test");
-        HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity, httpClient);
+        HttpPost httpPost = new HttpPost(url);
+        HttpResponse httpResponse = HttpsUtils.post(httpPost, header, para, entity, httpClient);
         String res = HttpsUtils.extractResponseEntity(httpResponse);
 
         PowerMock.verifyAll();
@@ -206,14 +202,15 @@ public class HttpsUtilsTest {
     @Test
     public void testHttpsUtil_put_excepiton() throws Exception {
         thrown.expect(CorrelationException.class);
-        thrown.expectMessage("Failed to query data from server through PUT method!");
+        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.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
-        HttpResponse httpResponse = HttpsUtils.put(url, header, para, null, httpClient);
+        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(""));
     }
@@ -230,9 +227,6 @@ public class HttpsUtilsTest {
         HttpEntity responseEntity = new StringEntity("Test");
         EasyMock.expect(response.getEntity()).andReturn(responseEntity);
 
-        httpClient.close();
-        EasyMock.expectLastCall();
-
         PowerMock.replayAll();
 
 
@@ -243,7 +237,8 @@ public class HttpsUtilsTest {
         para.put("tset", "1111");
 
         HttpEntity entity = new StringEntity("Test");
-        HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity, httpClient);
+        HttpPut httpPut = new HttpPut(url);
+        HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, entity, httpClient);
         String res = HttpsUtils.extractResponseEntity(httpResponse);
 
         PowerMock.verifyAll();
@@ -266,14 +261,14 @@ public class HttpsUtilsTest {
     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.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+        HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
         PowerMock.verifyAll();
     }