Add Unit Tests 95/28895/1
authorGuangrong Fu <fu.guangrong@zte.com.cn>
Tue, 23 Jan 2018 09:22:04 +0000 (17:22 +0800)
committerGuangrong Fu <fu.guangrong@zte.com.cn>
Tue, 23 Jan 2018 09:22:04 +0000 (17:22 +0800)
Change-Id: Icc109a9aad5a09b37353ff8baf6fe842e39f0b34
Issue-ID: HOLMES-97
Signed-off-by: Guangrong Fu <fu.guangrong@zte.com.cn>
holmes-actions/src/test/java/org/onap/holmes/common/utils/HttpsUtilsTest.java

index bbd9a0c..464a6f0 100644 (file)
@@ -21,7 +21,21 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.HashMap;
 import java.util.Map;
+
+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.methods.CloseableHttpResponse;
+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.Test;
@@ -33,7 +47,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
 import org.powermock.modules.junit4.PowerMockRunner;
 import org.powermock.reflect.Whitebox;
 
-@PrepareForTest({HttpsUtils.class, CloseableHttpClient.class})
+@PrepareForTest({CloseableHttpClient.class, HttpClientBuilder.class, HttpClients.class, CloseableHttpResponse.class,
+        StatusLine.class})
 @RunWith(PowerMockRunner.class)
 public class HttpsUtilsTest {
 
@@ -54,12 +69,47 @@ public class HttpsUtilsTest {
         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.get(url, header);
         assertThat(response, equalTo(""));
     }
 
+    @Test
+    public void testHttpsUtil_get_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.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");
+        String res = HttpsUtils.get(url, header);
+
+        PowerMock.verifyAll();
+
+        assertThat(res, equalTo("Test"));
+    }
+
     @Test
     public void testHttpsUtil_post_excepiton() throws Exception {
         thrown.expect(CorrelationException.class);
@@ -73,6 +123,42 @@ public class HttpsUtilsTest {
         assertThat(response, equalTo(""));
     }
 
+    @Test
+    public void testHttpsUtil_post_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.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");
+        String res = HttpsUtils.post(url, header, para, entity);
+
+        PowerMock.verifyAll();
+
+        assertThat(res, equalTo("Test"));
+    }
+
     @Test
     public void testHttpsUtil_getResponseEntity_input_null() throws Exception {
         PowerMock.resetAll();