From 6cc976de165087178e784c40423242bc7f1b07ee Mon Sep 17 00:00:00 2001 From: tang peng Date: Thu, 3 Jun 2021 17:10:23 +0800 Subject: Remove Class: HttpsUtils & MicroserviceBusRest Issue-ID: HOLMES-414 Signed-off-by: tang peng Change-Id: I8722cb9230e7f132cb38ca143d2a4de9d41025f1 --- .../common/config/MicroServiceConfigTest.java | 49 ---- .../onap/holmes/common/utils/CommonUtilsTest.java | 95 +++++++ .../onap/holmes/common/utils/HttpsUtilsTest.java | 275 --------------------- 3 files changed, 95 insertions(+), 324 deletions(-) create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/utils/CommonUtilsTest.java delete mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/utils/HttpsUtilsTest.java (limited to 'holmes-actions/src/test/java') diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java index 316f09f..1fdc335 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java @@ -40,7 +40,6 @@ public class MicroServiceConfigTest { private static String ACTUAL_HOSTNAME = System.getenv(HOSTNAME); - @Test public void getMsbServerAddrTest() { System.setProperty(MSB_IAG_SERVICE_HOST, "test"); @@ -336,52 +335,4 @@ public class MicroServiceConfigTest { System.clearProperty(HOSTNAME); } - - @Test - public void isValidIpAddress_with_port() throws Exception { - boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21:90"); - assertThat(res, is(true)); - } - - @Test - public void isValidIpAddress_without_port() throws Exception { - boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21"); - assertThat(res, is(true)); - } - - @Test - public void isValidIpAddress_with_port_with_http_prefix() throws Exception { - boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://10.75.13.21:90"); - assertThat(res, is(true)); - } - - @Test - public void isValidIpAddress_without_port_with_https_prefix() throws Exception { - boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://10.75.13.21"); - assertThat(res, is(true)); - } - - @Test - public void isValidIpAddress_invalid_ip_without_port() throws Exception { - boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt"); - assertThat(res, is(false)); - } - - @Test - public void isValidIpAddress_invalid_ip_with_port() throws Exception { - boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt:443"); - assertThat(res, is(false)); - } - - @Test - public void isValidIpAddress_invalid_ip_without_port_with_http_prefix() throws Exception { - boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://holmes-rule-mgmt"); - assertThat(res, is(false)); - } - - @Test - public void isValidIpAddress_invalid_ip_with_port_with_https_prefix() throws Exception { - boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://holmes-rule-mgmt:443"); - assertThat(res, is(false)); - } } \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/CommonUtilsTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/CommonUtilsTest.java new file mode 100644 index 0000000..15cc44d --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/CommonUtilsTest.java @@ -0,0 +1,95 @@ +/** + * Copyright 2021 ZTE Corporation. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.holmes.common.utils; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class CommonUtilsTest { + @Test + public void isHttpsEnabled_normal_true() throws Exception { + System.setProperty("ENABLE_ENCRYPT", "true"); + assertThat(CommonUtils.isHttpsEnabled(), is(true)); + } + + @Test + public void isHttpsEnabled_normal_false() throws Exception { + System.setProperty("ENABLE_ENCRYPT", "false"); + assertThat(CommonUtils.isHttpsEnabled(), is(false)); + } + + @Test + public void isHttpsEnabled_invalid_input() throws Exception { + System.setProperty("ENABLE_ENCRYPT", "whatever"); + assertThat(CommonUtils.isHttpsEnabled(), is(false)); + } + + @Test + public void getEnv() throws Exception { + System.setProperty("TEST", "COMMON_UTILS"); + assertThat(CommonUtils.getEnv("TEST"), equalTo("COMMON_UTILS")); + } + + @Test + public void isValidIpAddress_with_port() throws Exception { + boolean res = CommonUtils.isIpAddress("10.75.13.21:90"); + assertThat(res, is(true)); + } + + @Test + public void isValidIpAddress_without_port() throws Exception { + boolean res = CommonUtils.isIpAddress("10.75.13.21"); + assertThat(res, is(true)); + } + + @Test + public void isValidIpAddress_with_port_with_http_prefix() throws Exception { + boolean res = CommonUtils.isIpAddress("http://10.75.13.21:90"); + assertThat(res, is(true)); + } + + @Test + public void isValidIpAddress_without_port_with_https_prefix() throws Exception { + boolean res = CommonUtils.isIpAddress("https://10.75.13.21"); + assertThat(res, is(true)); + } + + @Test + public void isValidIpAddress_invalid_ip_without_port() throws Exception { + boolean res = CommonUtils.isIpAddress("holmes-rule-mgmt"); + assertThat(res, is(false)); + } + + @Test + public void isValidIpAddress_invalid_ip_with_port() throws Exception { + boolean res = CommonUtils.isIpAddress("holmes-rule-mgmt:443"); + assertThat(res, is(false)); + } + + @Test + public void isValidIpAddress_invalid_ip_without_port_with_http_prefix() throws Exception { + boolean res = CommonUtils.isIpAddress("http://holmes-rule-mgmt"); + assertThat(res, is(false)); + } + + @Test + public void isValidIpAddress_invalid_ip_with_port_with_https_prefix() throws Exception { + boolean res = CommonUtils.isIpAddress("https://holmes-rule-mgmt:443"); + assertThat(res, is(false)); + } +} \ No newline at end of file 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 deleted file mode 100644 index db9423a..0000000 --- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/HttpsUtilsTest.java +++ /dev/null @@ -1,275 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.holmes.common.utils; - -import static org.hamcrest.CoreMatchers.equalTo; -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.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpDelete; -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.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.easymock.EasyMock; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -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; - -@PrepareForTest({CloseableHttpClient.class, HttpClientBuilder.class, HttpClients.class, CloseableHttpResponse.class, - StatusLine.class}) -@RunWith(PowerMockRunner.class) -@PowerMockIgnore("javax.net.ssl.*") -public class HttpsUtilsTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - private HttpsUtils httpsUtils; - - @Before - public void setUp() { - httpsUtils = new HttpsUtils(); - } - - - @Test - public void testHttpsUtil_get_excepiton() throws Exception { - PowerMock.resetAll(); - thrown.expect(CorrelationException.class); - thrown.expectMessage("Failed to connect to server"); - String url = "host"; - Map header = new HashMap<>(); - header.put("accept", "application/json"); - CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - HttpGet httpRequestBase = new HttpGet(url); - HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient); - String response = HttpsUtils.extractResponseEntity(httpResponse); - assertThat(response, equalTo("")); - } - - @Test - public void testHttpsUtil_get_normal() throws Exception { - PowerMock.resetAll(); - CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - 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 header = new HashMap<>(); - header.put("accept", "application/json"); - - HttpGet httpRequestBase = new HttpGet(url); - HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient); - String res = HttpsUtils.extractResponseEntity(httpResponse); - - PowerMock.verifyAll(); - - assertThat(res, equalTo("Test")); - } - - @Test - public void testHttpsUtil_delete_excepiton() throws Exception { - PowerMock.resetAll(); - thrown.expect(CorrelationException.class); - thrown.expectMessage("Failed to connect to server"); - String url = "host"; - Map header = new HashMap<>(); - header.put("accept", "application/json"); - HttpDelete httpRequestBase = new HttpDelete(url); - CloseableHttpClient httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient); - String response = HttpsUtils.extractResponseEntity(httpResponse); - assertThat(response, equalTo("")); - } - - @Test - public void testHttpsUtil_delete_normal() throws Exception { - PowerMock.resetAll(); - CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - 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 header = new HashMap<>(); - header.put("accept", "application/json"); - - HttpDelete httpRequestBase = new HttpDelete(url); - HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient); - String res = HttpsUtils.extractResponseEntity(httpResponse); - - PowerMock.verifyAll(); - - assertThat(res, equalTo("Test")); - } - - @Test - public void testHttpsUtil_post_excepiton() throws Exception { - PowerMock.resetAll(); - thrown.expect(CorrelationException.class); - thrown.expectMessage("Failed to connect to server"); - String url = "host"; - Map header = new HashMap<>(); - header.put("accept", "application/json"); - Map para = new HashMap<>(); - para.put("tset", "1111"); - 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); - assertThat(response, equalTo("")); - } - - @Test - public void testHttpsUtil_post_normal() throws Exception { - PowerMock.resetAll(); - CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - 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 header = new HashMap<>(); - header.put("accept", "application/json"); - Map para = new HashMap<>(); - para.put("tset", "1111"); - - HttpEntity entity = new StringEntity("Test"); - HttpPost httpPost = new HttpPost(url); - HttpResponse httpResponse = HttpsUtils.post(httpPost, header, para, entity, httpClient); - 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 connect to server"); - String url = "host"; - Map header = new HashMap<>(); - header.put("accept", "application/json"); - Map para = new HashMap<>(); - para.put("tset", "1111"); - 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); - assertThat(response, equalTo("")); - } - - @Test - public void testHttpsUtil_put_normal() throws Exception { - PowerMock.resetAll(); - CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - 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 header = new HashMap<>(); - header.put("accept", "application/json"); - Map para = new HashMap<>(); - para.put("tset", "1111"); - - HttpEntity entity = new StringEntity("Test"); - HttpPut httpPut = new HttpPut(url); - HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, entity, httpClient); - String res = HttpsUtils.extractResponseEntity(httpResponse); - - PowerMock.verifyAll(); - - assertThat(res, equalTo("Test")); - } - - @Test - public void testHttpsUtil_getResponseEntity_input_null() throws Exception { - PowerMock.resetAll(); - httpsUtils = PowerMock.createMock(HttpsUtils.class); - PowerMock.replayAll(); - String actual = Whitebox.invokeMethod(httpsUtils, "extractResponseEntity", null); - PowerMock.verifyAll(); - assertThat(actual, equalTo("")); - } - - - @Test - public void testHttpsUtil_getHttpClient_exception() throws Exception { - PowerMock.resetAll(); - thrown.expect(Exception.class); - Whitebox.invokeMethod(HttpsUtils.class, "getConditionalHttpsClient"); - PowerMock.verifyAll(); - } - - @Test - public void testHttpsUtil_getHttpClient_ok() throws Exception { - PowerMock.resetAll(); - HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - PowerMock.verifyAll(); - } - -} \ No newline at end of file -- cgit