X-Git-Url: https://gerrit.onap.org/r/gitweb?p=holmes%2Fcommon.git;a=blobdiff_plain;f=holmes-actions%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fholmes%2Fcommon%2Futils%2FHttpsUtilsTest.java;h=21bf0e2fa00bd6c0aca54a62695e2763e67a9279;hp=464a6f0ebe35d2a7e6231383209bf18211460858;hb=bf20ddf00200c5468da7a0090caf28beebb93e9c;hpb=e37f1a4ce3da909a945e2faf7d3166a3b1a96a93 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 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 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 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 header = new HashMap<>(); header.put("accept", "application/json"); Map 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 header = new HashMap<>(); + header.put("accept", "application/json"); + Map 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 header = new HashMap<>(); + header.put("accept", "application/json"); + Map 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("")); }