Remove Class: HttpsUtils & MicroserviceBusRest 75/121675/3
authortang peng <tang.peng5@zte.com.cn>
Thu, 3 Jun 2021 09:10:23 +0000 (17:10 +0800)
committertang peng <tang.peng5@zte.com.cn>
Thu, 3 Jun 2021 09:26:36 +0000 (17:26 +0800)
Issue-ID: HOLMES-414
Signed-off-by: tang peng <tang.peng5@zte.com.cn>
Change-Id: I8722cb9230e7f132cb38ca143d2a4de9d41025f1

holmes-actions/pom.xml
holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java
holmes-actions/src/main/java/org/onap/holmes/common/msb/MicroserviceBusRest.java [deleted file]
holmes-actions/src/main/java/org/onap/holmes/common/utils/CommonUtils.java [new file with mode: 0644]
holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java [deleted file]
holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java
holmes-actions/src/test/java/org/onap/holmes/common/utils/CommonUtilsTest.java [new file with mode: 0644]
holmes-actions/src/test/java/org/onap/holmes/common/utils/HttpsUtilsTest.java [deleted file]
pom.xml

index fabb1a8..3a5b042 100644 (file)
@@ -12,7 +12,7 @@
     <parent>\r
         <groupId>org.onap.holmes.common</groupId>\r
         <artifactId>holmes-common-parent</artifactId>\r
-        <version>1.3.3-SNAPSHOT</version>\r
+        <version>1.3.4-SNAPSHOT</version>\r
     </parent>\r
 \r
     <name>holmes-common-service</name>\r
index 7566fa1..43fef11 100644 (file)
@@ -27,6 +27,9 @@ import javax.ws.rs.client.ClientBuilder;
 import javax.ws.rs.core.Response;\r
 import java.util.regex.Pattern;\r
 \r
+import static org.onap.holmes.common.utils.CommonUtils.getEnv;\r
+import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;\r
+\r
 public class MicroServiceConfig {\r
 \r
     final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";\r
@@ -38,19 +41,11 @@ public class MicroServiceConfig {
     final static public String MSB_ADDR = "MSB_ADDR";\r
     final static public String MSB_IAG_SERVICE_HOST = "MSB_IAG_SERVICE_HOST";\r
     final static public String MSB_IAG_SERVICE_PORT = "MSB_IAG_SERVICE_PORT";\r
-    final static public Pattern IP_REG = Pattern.compile("(http(s)?://)?(\\d+\\.\\d+\\.\\d+\\.\\d+)(:(\\d+))?");\r
+\r
     final static public String AAI_HOSTNAME = "aai.onap";\r
 \r
     final static public Logger log = LoggerFactory.getLogger(MicroServiceConfig.class);\r
 \r
-    public static String getEnv(String name) {\r
-        String value = System.getenv(name);\r
-        if (value == null) {\r
-            value = System.getProperty(name);\r
-        }\r
-        return value;\r
-    }\r
-\r
     public static String getConsulAddrInfo() {\r
         return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
     }\r
@@ -129,9 +124,7 @@ public class MicroServiceConfig {
         return serviceAddrInfo;\r
     }\r
 \r
-    public static boolean isIpAddress(String info) {\r
-        return IP_REG.matcher(info).matches();\r
-    }\r
+\r
 \r
     private static String[] split(String addr) {\r
         String ip;\r
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/msb/MicroserviceBusRest.java b/holmes-actions/src/main/java/org/onap/holmes/common/msb/MicroserviceBusRest.java
deleted file mode 100644 (file)
index 06b91e5..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/**\r
- * Copyright 2017 ZTE Corporation.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *     http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-package org.onap.holmes.common.msb;\r
-\r
-import javax.ws.rs.Consumes;\r
-import javax.ws.rs.POST;\r
-import javax.ws.rs.Path;\r
-import javax.ws.rs.Produces;\r
-import javax.ws.rs.QueryParam;\r
-import javax.ws.rs.core.MediaType;\r
-import org.onap.holmes.common.api.entity.ServiceRegisterEntity;\r
-\r
-@Path("/api/microservices/v1/services")\r
-public interface MicroserviceBusRest {\r
-\r
-    @Path("")\r
-    @POST\r
-    @Consumes(MediaType.APPLICATION_JSON)\r
-    @Produces(MediaType.APPLICATION_JSON)\r
-    ServiceRegisterEntity registerServce(@QueryParam("createOrUpdate") String createOrUpdate,\r
-            ServiceRegisterEntity entity);\r
-}\r
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/CommonUtils.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/CommonUtils.java
new file mode 100644 (file)
index 0000000..9aa2e0b
--- /dev/null
@@ -0,0 +1,42 @@
+/**
+ * Copyright 2021 ZTE Corporation.
+ * <p>
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.jvnet.hk2.annotations.Service;
+
+import java.util.regex.Pattern;
+
+@Service
+public class CommonUtils {
+
+    final static public String IP_REG = "(http(s)?://)?(\\d+\\.\\d+\\.\\d+\\.\\d+)(:(\\d+))?";
+
+    public static boolean isHttpsEnabled() {
+        return Boolean.valueOf(getEnv("ENABLE_ENCRYPT"));
+    }
+
+    public static String getEnv(String name) {
+        String value = System.getenv(name);
+        if (value == null) {
+            value = System.getProperty(name);
+        }
+        return value;
+    }
+
+    public static boolean isIpAddress(String info) {
+        return Pattern.compile(IP_REG).matcher(info).matches();
+    }
+
+}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
deleted file mode 100644 (file)
index 8b4be57..0000000
+++ /dev/null
@@ -1,200 +0,0 @@
-/**
- * Copyright 2017 ZTE Corporation.
- * <p>
- * 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
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
- * 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 lombok.extern.slf4j.Slf4j;
-import org.apache.http.*;
-import org.apache.http.client.config.RequestConfig;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.*;
-import org.apache.http.config.Registry;
-import org.apache.http.config.RegistryBuilder;
-import org.apache.http.conn.socket.ConnectionSocketFactory;
-import org.apache.http.conn.socket.PlainConnectionSocketFactory;
-import org.apache.http.conn.ssl.NoopHostnameVerifier;
-import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
-import org.apache.http.conn.ssl.TrustStrategy;
-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.apache.http.message.BasicNameValuePair;
-import org.apache.http.ssl.SSLContextBuilder;
-import org.apache.http.util.EntityUtils;
-import org.jvnet.hk2.annotations.Service;
-import org.onap.holmes.common.config.MicroServiceConfig;
-import org.onap.holmes.common.exception.CorrelationException;
-
-import java.io.IOException;
-import java.security.cert.CertificateException;
-import java.security.cert.X509Certificate;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
-@Slf4j
-@Service
-public class HttpsUtils {
-    private static final String HTTP = "http";
-    private static final String HTTPS = "https";
-    private static SSLConnectionSocketFactory sslConnectionSocketFactory = null;
-    private static PoolingHttpClientConnectionManager connectionManager = null;
-    private static SSLContextBuilder sslContextBuilder = null;
-    public static final int DEFUALT_TIMEOUT = 30000;
-
-    static {
-        try {
-            sslContextBuilder = new SSLContextBuilder();
-            sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() {
-                public boolean isTrusted(X509Certificate[] x509Certificates, String s)
-                        throws CertificateException {
-                    return true;
-                }
-            });
-            sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(),
-                    new String[]{"SSLv3", "TLSv1", "TLSv1.2"}, null,
-                    NoopHostnameVerifier.INSTANCE);
-            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
-                    .register(HTTP, new PlainConnectionSocketFactory())
-                    .register(HTTPS, sslConnectionSocketFactory)
-                    .build();
-            connectionManager = new PoolingHttpClientConnectionManager(registry);
-            connectionManager.setMaxTotal(200);
-        } catch (Exception e) {
-            log.error("Failed to initialize the ssl builder: " + e.getMessage(), e);
-        }
-    }
-
-    public static HttpResponse get(HttpGet httpGet, Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
-        return getGetAndDeleteResponse(httpGet, header, httpClient);
-    }
-
-    public static HttpResponse post(HttpPost httpPost, Map<String, String> header, Map<String, String> param,
-                                    HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
-        return getPostAndPutResponse(httpPost, header, param, entity, httpClient);
-    }
-
-    public static HttpResponse put(HttpPut httpPut, Map<String, String> header, Map<String, String> param,
-                                   HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
-        return getPostAndPutResponse(httpPut, header, param, entity, httpClient);
-    }
-
-    public static HttpResponse delete(HttpDelete httpDelete, Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
-        return getGetAndDeleteResponse(httpDelete, header, httpClient);
-    }
-
-    private static void addParams(Map<String, String> param, HttpEntityEnclosingRequestBase requestBase) {
-        if (!param.isEmpty()) {
-            List<NameValuePair> formparams = new ArrayList<>();
-            for (Map.Entry<String, String> entry : param.entrySet()) {
-                formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
-            }
-            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams,
-                    Consts.UTF_8);
-            requestBase.setEntity(urlEncodedFormEntity);
-        }
-    }
-
-    private static HttpRequestBase addHeaders(Map<String, String> header, HttpRequestBase httpRequestBase) {
-        if (!header.isEmpty()) {
-            for (Map.Entry<String, String> entry : header.entrySet()) {
-                httpRequestBase.addHeader(entry.getKey(), entry.getValue());
-            }
-        }
-        return httpRequestBase;
-    }
-
-    private static HttpResponse getPostAndPutResponse(HttpEntityEnclosingRequestBase requestBase,
-                                                      Map<String, String> header, Map<String, String> param, HttpEntity entity,
-                                                      CloseableHttpClient httpClient) throws CorrelationException {
-        try {
-            addHeaders(header, requestBase);
-            addParams(param, requestBase);
-            if (entity != null) {
-                requestBase.setEntity(entity);
-            }
-            return executeRequest(httpClient, requestBase);
-        } catch (Exception e) {
-            throw new CorrelationException("Failed to connect to server", e);
-        }
-    }
-
-    private static HttpResponse getGetAndDeleteResponse(HttpRequestBase requestBase,
-                                                        Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
-        try {
-            addHeaders(header, requestBase);
-            return executeRequest(httpClient, requestBase);
-        } catch (Exception e) {
-            throw new CorrelationException("Failed to connect to server", e);
-        }
-    }
-
-    public static String extractResponseEntity(HttpResponse httpResponse)
-            throws CorrelationException, IOException {
-        String result = "";
-        if (httpResponse != null) {
-            int statusCode = httpResponse.getStatusLine().getStatusCode();
-            if (statusCode == HttpStatus.SC_OK) {
-                HttpEntity resEntity = httpResponse.getEntity();
-                result = EntityUtils.toString(resEntity);
-            } else {
-                throw new CorrelationException("Get an error status from server : " + statusCode);
-            }
-        }
-        return result;
-    }
-
-    private static HttpResponse executeRequest(CloseableHttpClient httpClient, HttpRequestBase httpRequest)
-            throws CorrelationException, IOException {
-        HttpResponse httpResponse;
-        try {
-            httpResponse = httpClient.execute(httpRequest);
-        } catch (Exception e) {
-            throw new CorrelationException("Failed to get data from server", e);
-        }
-        return httpResponse;
-    }
-
-    public static CloseableHttpClient getConditionalHttpsClient(int timeout) {
-        HttpClientBuilder builder = getHttpClientBuilder(timeout);
-        if (isHttpsEnabled()) {
-            builder.setSSLSocketFactory(sslConnectionSocketFactory);
-        }
-
-        return builder.build();
-    }
-
-    public static CloseableHttpClient getHttpsClient(int timeout) {
-        HttpClientBuilder builder = getHttpClientBuilder(timeout);
-        return builder.setSSLSocketFactory(sslConnectionSocketFactory).build();
-    }
-
-    private static HttpClientBuilder getHttpClientBuilder(int timeout) {
-        RequestConfig defaultRequestConfig = RequestConfig.custom()
-                .setSocketTimeout(timeout)
-                .setConnectTimeout(timeout)
-                .setConnectionRequestTimeout(timeout)
-                .build();
-
-        return HttpClients.custom()
-                .setDefaultRequestConfig(defaultRequestConfig)
-                .setConnectionManager(connectionManager)
-                .setConnectionManagerShared(true);
-    }
-
-    public static boolean isHttpsEnabled() {
-        return Boolean.valueOf(MicroServiceConfig.getEnv("ENABLE_ENCRYPT"));
-    }
-}
index 316f09f..1fdc335 100644 (file)
@@ -40,7 +40,6 @@ public class MicroServiceConfigTest {
 \r
     private static String ACTUAL_HOSTNAME = System.getenv(HOSTNAME);\r
 \r
-\r
     @Test\r
     public void getMsbServerAddrTest() {\r
         System.setProperty(MSB_IAG_SERVICE_HOST, "test");\r
@@ -336,52 +335,4 @@ public class MicroServiceConfigTest {
 \r
         System.clearProperty(HOSTNAME);\r
     }\r
-\r
-    @Test\r
-    public void isValidIpAddress_with_port() throws Exception {\r
-        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21:90");\r
-        assertThat(res, is(true));\r
-    }\r
-\r
-    @Test\r
-    public void isValidIpAddress_without_port() throws Exception {\r
-        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21");\r
-        assertThat(res, is(true));\r
-    }\r
-\r
-    @Test\r
-    public void isValidIpAddress_with_port_with_http_prefix() throws Exception {\r
-        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://10.75.13.21:90");\r
-        assertThat(res, is(true));\r
-    }\r
-\r
-    @Test\r
-    public void isValidIpAddress_without_port_with_https_prefix() throws Exception {\r
-        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://10.75.13.21");\r
-        assertThat(res, is(true));\r
-    }\r
-\r
-    @Test\r
-    public void isValidIpAddress_invalid_ip_without_port() throws Exception {\r
-        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt");\r
-        assertThat(res, is(false));\r
-    }\r
-\r
-    @Test\r
-    public void isValidIpAddress_invalid_ip_with_port() throws Exception {\r
-        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt:443");\r
-        assertThat(res, is(false));\r
-    }\r
-\r
-    @Test\r
-    public void isValidIpAddress_invalid_ip_without_port_with_http_prefix() throws Exception {\r
-        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://holmes-rule-mgmt");\r
-        assertThat(res, is(false));\r
-    }\r
-\r
-    @Test\r
-    public void isValidIpAddress_invalid_ip_with_port_with_https_prefix() throws Exception {\r
-        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://holmes-rule-mgmt:443");\r
-        assertThat(res, is(false));\r
-    }\r
 }
\ 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 (file)
index 0000000..15cc44d
--- /dev/null
@@ -0,0 +1,95 @@
+/**
+ * Copyright 2021 ZTE Corporation.
+ * <p>
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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 (file)
index db9423a..0000000
+++ /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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> header = new HashMap<>();
-        header.put("accept", "application/json");
-        Map<String, String> 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<String, String> header = new HashMap<>();
-        header.put("accept", "application/json");
-        Map<String, String> 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<String, String> header = new HashMap<>();
-        header.put("accept", "application/json");
-        Map<String, String> 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<String, String> header = new HashMap<>();
-        header.put("accept", "application/json");
-        Map<String, String> 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
diff --git a/pom.xml b/pom.xml
index 2629233..fb9a505 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -20,7 +20,7 @@
     <artifactId>holmes-common-parent</artifactId>\r
     <packaging>pom</packaging>\r
 \r
-    <version>1.3.3-SNAPSHOT</version>\r
+    <version>1.3.4-SNAPSHOT</version>\r
     <name>holmes-common</name>\r
     <modules>\r
         <module>holmes-actions</module>\r