diff options
author | tang peng <tang.peng5@zte.com.cn> | 2019-03-30 07:47:12 +0000 |
---|---|---|
committer | tangpeng <tang.peng5@zte.com.cn> | 2019-03-30 07:50:39 +0000 |
commit | 58401075a66f7c5963848a7349067f995fe4f947 (patch) | |
tree | 56955166fa769a996d0f2723ee52d94cf1a35083 /holmes-actions/src/test | |
parent | 555a3d9b10af05f2dc64b2a230f80fc436432a7a (diff) |
Make HTTP/s Optional
Change-Id: Ia09126b06ad43f05582ae0f3dd8692df223953fe
Issue-ID: HOLMES-207
Signed-off-by: tang peng <tang.peng5@zte.com.cn>
Diffstat (limited to 'holmes-actions/src/test')
-rw-r--r-- | holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java | 41 | ||||
-rw-r--r-- | holmes-actions/src/test/java/org/onap/holmes/common/utils/HttpsUtilsTest.java | 16 |
2 files changed, 31 insertions, 26 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 9f211d9..7ce3fcf 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 @@ -16,6 +16,7 @@ package org.onap.holmes.common.aai; import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; import static org.powermock.api.mockito.PowerMockito.when; @@ -26,6 +27,7 @@ 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.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -55,6 +57,11 @@ public class AaiQueryTest { private AaiQuery aaiQuery; private AaiResponseUtil aaiResponseUtil; + @BeforeClass + static public void before() { + System.setProperty("ENABLE_ENCRYPT", "true"); + } + @Test public void testAaiQuery_getAaiVnfData_ok() throws Exception { PowerMock.resetAll(); @@ -93,7 +100,7 @@ public class AaiQueryTest { aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks"); aaiResponseUtil = new AaiResponseUtil(); Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil); - PowerMockito.mockStatic(HttpsUtils.class); + PowerMock.mockStatic(HttpsUtils.class); Map<String, String> headers = new HashMap<>(); headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID); headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID); @@ -102,11 +109,12 @@ public class AaiQueryTest { String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure"; HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); + expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); 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("{}"); + expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class), + anyObject(CloseableHttpClient.class))).andReturn(httpResponse); + expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn("{}"); PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2") .andReturn("/aai/v11/cloud-infrastructure"); @@ -120,7 +128,6 @@ public class AaiQueryTest { } @Test - public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception { PowerMock.resetAll(); thrown.expect(CorrelationException.class); @@ -130,7 +137,7 @@ public class AaiQueryTest { aaiResponseUtil = new AaiResponseUtil(); Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil); - PowerMockito.mockStatic(HttpsUtils.class); + PowerMock.mockStatic(HttpsUtils.class); Map<String, String> headers = new HashMap<>(); headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID); headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID); @@ -138,11 +145,12 @@ public class AaiQueryTest { headers.put("Accept", "application/json"); String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure"; CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); + EasyMock.expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); 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); + EasyMock.expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class), + anyObject(CloseableHttpClient.class))).andThrow(new CorrelationException("")); + PowerMock.mockStatic(MicroServiceConfig.class); PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2") .andReturn("/aai/v11/cloud-infrastructure"); PowerMock.expectPrivate(httpClient,"close"); @@ -214,7 +222,7 @@ public class AaiQueryTest { public void testAaiQuery_getResponse_ok() throws Exception { PowerMock.resetAll(); aaiQuery = new AaiQuery(); - PowerMockito.mockStatic(HttpsUtils.class); + PowerMock.mockStatic(HttpsUtils.class); Map<String, String> headers = new HashMap<>(); headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID); headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID); @@ -224,11 +232,12 @@ public class AaiQueryTest { HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); + expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); 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(""); + expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class), + anyObject(CloseableHttpClient.class))).andReturn(httpResponse); + expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn(""); PowerMock.expectPrivate(httpClient, "close"); EasyMock.expectLastCall(); @@ -246,7 +255,7 @@ public class AaiQueryTest { thrown.expectMessage("Failed to get data from aai"); aaiQuery = new AaiQuery(); - PowerMockito.mockStatic(HttpsUtils.class); + PowerMock.mockStatic(HttpsUtils.class); Map<String, String> headers = new HashMap<>(); headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID); headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID); @@ -254,10 +263,10 @@ public class AaiQueryTest { headers.put("Accept", "application/json"); String url = "host_url"; CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); + expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); HttpGet httpGet = new HttpGet(url); PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); - when(HttpsUtils.get(httpGet, headers, httpClient)).thenThrow(new CorrelationException("")); + expect(HttpsUtils.get(httpGet, headers, httpClient)).andThrow(new CorrelationException("")); PowerMock.expectPrivate(httpClient, "close"); EasyMock.expectLastCall(); PowerMock.replayAll(); 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 3ff5dda..db9423a 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,20 +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.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; 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; @@ -77,7 +73,7 @@ public class HttpsUtilsTest { String url = "host"; Map<String, String> header = new HashMap<>(); header.put("accept", "application/json"); - CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); HttpGet httpRequestBase = new HttpGet(url); HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient); String response = HttpsUtils.extractResponseEntity(httpResponse); @@ -121,7 +117,7 @@ public class HttpsUtilsTest { Map<String, String> header = new HashMap<>(); header.put("accept", "application/json"); HttpDelete httpRequestBase = new HttpDelete(url); - CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient); String response = HttpsUtils.extractResponseEntity(httpResponse); assertThat(response, equalTo("")); @@ -165,7 +161,7 @@ public class HttpsUtilsTest { header.put("accept", "application/json"); Map<String, String> para = new HashMap<>(); para.put("tset", "1111"); - CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + 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); @@ -212,7 +208,7 @@ public class HttpsUtilsTest { header.put("accept", "application/json"); Map<String, String> para = new HashMap<>(); para.put("tset", "1111"); - CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT); + 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); @@ -265,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(); } |