diff options
author | Shiwei Tian <tian.shiwei@zte.com.cn> | 2018-04-08 17:34:12 +0800 |
---|---|---|
committer | Shiwei Tian <tian.shiwei@zte.com.cn> | 2018-04-09 08:56:27 +0800 |
commit | 96a6ea5cab6575b7e04d6736cee6906298065a7b (patch) | |
tree | 8b9fa14e619551e8edf774004d6b142da17ae093 /holmes-actions/src/test/java | |
parent | 814861d9a0b4d093c482e3338453771423dcd7f0 (diff) |
fix https bug
Issue-ID: HOLMES-104
Change-Id: I9146e6f89d9e72744eadd46f28b2ad10eefcfb40
Signed-off-by: Shiwei Tian <tian.shiwei@zte.com.cn>
Diffstat (limited to 'holmes-actions/src/test/java')
3 files changed, 55 insertions, 55 deletions
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 44e39b1..f6488c2 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 @@ -23,6 +23,8 @@ import static org.powermock.api.mockito.PowerMockito.when; import java.util.HashMap; import java.util.Map; import org.apache.http.HttpResponse; +import org.apache.http.impl.client.CloseableHttpClient; +import org.easymock.EasyMock; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -96,7 +98,9 @@ public class AaiQueryTest { headers.put("Accept", "application/json"); String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11"; HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); - when(HttpsUtils.get(url, headers)).thenReturn(httpResponse); + CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); + when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); + when(HttpsUtils.get(url, headers, httpClient)).thenReturn(httpResponse); when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("{}"); PowerMockito.mockStatic(MicroServiceConfig.class); @@ -104,6 +108,8 @@ public class AaiQueryTest { PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2") .andReturn("/aai/v11/cloud-infrastructure"); + PowerMock.expectPrivate(httpClient, "close"); + EasyMock.expectLastCall(); PowerMock.replayAll(); VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2"); PowerMock.verifyAll(); @@ -128,14 +134,15 @@ 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)).thenThrow(new CorrelationException("")); - + CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); + when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); + when(HttpsUtils.get(url, headers, httpClient)).thenThrow(new CorrelationException("")); PowerMockito.mockStatic(MicroServiceConfig.class); when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80"); - PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2") .andReturn("/aai/v11/cloud-infrastructure"); + PowerMock.expectPrivate(httpClient,"close"); + EasyMock.expectLastCall(); PowerMock.replayAll(); Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2"); PowerMock.verifyAll(); @@ -212,8 +219,12 @@ public class AaiQueryTest { String url = "host_url"; HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); - when(HttpsUtils.get(url, headers)).thenReturn(httpResponse); + CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); + when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); + when(HttpsUtils.get(url, headers, httpClient)).thenReturn(httpResponse); when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(""); + PowerMock.expectPrivate(httpClient, "close"); + EasyMock.expectLastCall(); PowerMock.replayAll(); String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url"); @@ -236,7 +247,11 @@ public class AaiQueryTest { headers.put("Authorization", AaiConfig.getAuthenticationCredentials()); headers.put("Accept", "application/json"); String url = "host_url"; - when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException("")); + CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); + when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); + when(HttpsUtils.get(url, headers, httpClient)).thenThrow(new CorrelationException("")); + PowerMock.expectPrivate(httpClient, "close"); + EasyMock.expectLastCall(); PowerMock.replayAll(); String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url"); PowerMock.verifyAll(); 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 164c176..95bde25 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 @@ -30,6 +30,7 @@ import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.StatusLine; import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; import org.easymock.EasyMock; import org.junit.Rule; import org.junit.Test; @@ -73,8 +74,10 @@ public class PublisherTest { 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); + PowerMockito.when(HttpsUtils + .post(Matchers.eq("http://localhost/dmaapTopic"), Matchers.any(HashMap.class), + Matchers.any(HashMap.class), Matchers.any(StringEntity.class), + Matchers.any(CloseableHttpClient.class))).thenReturn(httpResponse); StatusLine statusLine = PowerMockito.mock(StatusLine.class); PowerMockito.when(httpResponse.getStatusLine()).thenReturn(statusLine); PowerMockito.when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK); 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 21bf0e2..c47542b 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 @@ -43,6 +43,7 @@ import org.junit.rules.ExpectedException; 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; @@ -50,6 +51,7 @@ 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 @@ -64,28 +66,22 @@ public class HttpsUtilsTest { @Test public void testHttpsUtil_get_excepiton() throws Exception { + PowerMock.resetAll(); thrown.expect(CorrelationException.class); 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"); - HttpResponse httpResponse = HttpsUtils.get(url, header); + CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + HttpResponse httpResponse = HttpsUtils.get(url, 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.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); @@ -105,7 +101,7 @@ public class HttpsUtilsTest { header.put("accept", "application/json"); HttpEntity entity = new StringEntity("Test"); - HttpResponse httpResponse = HttpsUtils.get(url, header); + HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient); String res = HttpsUtils.extractResponseEntity(httpResponse); PowerMock.verifyAll(); @@ -115,28 +111,22 @@ public class HttpsUtilsTest { @Test public void testHttpsUtil_delete_excepiton() throws Exception { + PowerMock.resetAll(); 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); + CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient); String response = HttpsUtils.extractResponseEntity(httpResponse); assertThat(response, equalTo("")); } @Test public void testHttpsUtil_delete_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.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); @@ -156,7 +146,7 @@ public class HttpsUtilsTest { header.put("accept", "application/json"); HttpEntity entity = new StringEntity("Test"); - HttpResponse httpResponse = HttpsUtils.delete(url, header); + HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient); String res = HttpsUtils.extractResponseEntity(httpResponse); PowerMock.verifyAll(); @@ -166,6 +156,7 @@ public class HttpsUtilsTest { @Test public void testHttpsUtil_post_excepiton() throws Exception { + PowerMock.resetAll(); thrown.expect(CorrelationException.class); thrown.expectMessage("Failed to query data from server through POST method!"); String url = "host"; @@ -173,24 +164,16 @@ public class HttpsUtilsTest { header.put("accept", "application/json"); Map<String, String> para = new HashMap<>(); para.put("tset", "1111"); - - HttpResponse httpResponse = HttpsUtils.post(url, header, para, null); + CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + HttpResponse httpResponse = HttpsUtils.post(url, 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.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); @@ -212,7 +195,7 @@ public class HttpsUtilsTest { para.put("tset", "1111"); HttpEntity entity = new StringEntity("Test"); - HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity); + HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity, httpClient); String res = HttpsUtils.extractResponseEntity(httpResponse); PowerMock.verifyAll(); @@ -229,24 +212,16 @@ public class HttpsUtilsTest { header.put("accept", "application/json"); Map<String, String> para = new HashMap<>(); para.put("tset", "1111"); - - HttpResponse httpResponse = HttpsUtils.put(url, header, para, null); + CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + HttpResponse httpResponse = HttpsUtils.put(url, header, para, null, httpClient); String response = HttpsUtils.extractResponseEntity(httpResponse); assertThat(response, equalTo("")); } @Test public void testHttpsUtil_put_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.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); @@ -268,7 +243,7 @@ public class HttpsUtilsTest { para.put("tset", "1111"); HttpEntity entity = new StringEntity("Test"); - HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity); + HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity, httpClient); String res = HttpsUtils.extractResponseEntity(httpResponse); PowerMock.verifyAll(); @@ -295,4 +270,11 @@ public class HttpsUtilsTest { PowerMock.verifyAll(); } + @Test + public void testHttpsUtil_getHttpClient_ok() throws Exception { + PowerMock.resetAll(); + HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + PowerMock.verifyAll(); + } + }
\ No newline at end of file |