diff options
author | 2018-04-16 16:28:18 +0800 | |
---|---|---|
committer | 2018-04-16 16:38:49 +0800 | |
commit | 3356fb82b20ebe7209522e4e009dd6aea5ed1802 (patch) | |
tree | 03c0af590af3f6d189e20d057162daa95c7809a5 /holmes-actions/src/test/java | |
parent | 77a032f176183287746858d8eb57a6062286d327 (diff) |
fix https timeout get connection
Issue-ID: HOLMES-104
Change-Id: Ib79bb3dea470fd922f2e8fc906f33d8d238cd62e
Signed-off-by: Shiwei Tian <tian.shiwei@zte.com.cn>
Diffstat (limited to 'holmes-actions/src/test/java')
3 files changed, 42 insertions, 20 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 f6488c2..42becaa 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,7 @@ 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.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.easymock.EasyMock; import org.junit.Rule; @@ -42,7 +43,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; -@PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class}) +@PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class}) @RunWith(PowerMockRunner.class) public class AaiQueryTest { @@ -100,7 +101,9 @@ public class AaiQueryTest { HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); - when(HttpsUtils.get(url, headers, httpClient)).thenReturn(httpResponse); + HttpGet httpGet = new HttpGet(url); + PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); + when(HttpsUtils.get(httpGet, headers, httpClient)).thenReturn(httpResponse); when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("{}"); PowerMockito.mockStatic(MicroServiceConfig.class); @@ -136,7 +139,9 @@ public class AaiQueryTest { String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11"; CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); - when(HttpsUtils.get(url, headers, httpClient)).thenThrow(new CorrelationException("")); + HttpGet httpGet = new HttpGet(url); + PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); + when(HttpsUtils.get(httpGet, 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") @@ -221,7 +226,9 @@ public class AaiQueryTest { HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); - when(HttpsUtils.get(url, headers, httpClient)).thenReturn(httpResponse); + HttpGet httpGet = new HttpGet(url); + PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); + when(HttpsUtils.get(httpGet, headers, httpClient)).thenReturn(httpResponse); when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(""); PowerMock.expectPrivate(httpClient, "close"); EasyMock.expectLastCall(); @@ -249,7 +256,9 @@ public class AaiQueryTest { String url = "host_url"; CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); - when(HttpsUtils.get(url, headers, httpClient)).thenThrow(new CorrelationException("")); + HttpGet httpGet = new HttpGet(url); + PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); + when(HttpsUtils.get(httpGet, headers, httpClient)).thenThrow(new CorrelationException("")); PowerMock.expectPrivate(httpClient, "close"); EasyMock.expectLastCall(); PowerMock.replayAll(); 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 95bde25..be9f74f 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 @@ -29,6 +29,8 @@ 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.client.methods.HttpEntityEnclosingRequestBase; +import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.easymock.EasyMock; @@ -75,7 +77,7 @@ 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), + .post(Matchers.any(HttpPost.class), Matchers.any(HashMap.class), Matchers.any(HashMap.class), Matchers.any(StringEntity.class), Matchers.any(CloseableHttpClient.class))).thenReturn(httpResponse); StatusLine statusLine = PowerMockito.mock(StatusLine.class); 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 087c1d3..3ff5dda 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 @@ -28,6 +28,11 @@ 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.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.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.StringEntity; @@ -68,12 +73,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); + HttpGet httpRequestBase = new HttpGet(url); + HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient); String response = HttpsUtils.extractResponseEntity(httpResponse); assertThat(response, equalTo("")); } @@ -97,8 +103,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(); @@ -110,12 +116,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"); + HttpDelete httpRequestBase = new HttpDelete(url); CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); - HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient); + HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient); String response = HttpsUtils.extractResponseEntity(httpResponse); assertThat(response, equalTo("")); } @@ -139,8 +146,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(); @@ -152,14 +159,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); + HttpPost httpPost = new HttpPost(url); + HttpResponse httpResponse = HttpsUtils.post(httpPost, header, para, null, httpClient); String response = HttpsUtils.extractResponseEntity(httpResponse); assertThat(response, equalTo("")); } @@ -186,7 +194,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(); @@ -197,14 +206,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); + HttpPut httpPut = new HttpPut(url); + HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, null, httpClient); String response = HttpsUtils.extractResponseEntity(httpResponse); assertThat(response, equalTo("")); } @@ -231,7 +241,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(); |