/**\r
- * Copyright 2017 ZTE Corporation.\r
- *\r
+ * Copyright 2017-2020 ZTE Corporation.\r
+ * <p>\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
+ * <p>\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * <p>\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
\r
package org.onap.holmes.common.config;\r
\r
-import static org.hamcrest.core.Is.is;\r
-import static org.hamcrest.core.IsEqual.equalTo;\r
-import static org.hamcrest.core.IsNull.nullValue;\r
-import static org.junit.Assert.assertThat;\r
-import static org.junit.Assert.assertTrue;\r
-import static org.onap.holmes.common.config.MicroServiceConfig.*;\r
-\r
+import org.apache.commons.lang3.StringUtils;\r
import org.easymock.EasyMock;\r
import org.junit.Ignore;\r
-import org.junit.Rule;\r
import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
import org.powermock.api.easymock.PowerMock;\r
import org.powermock.core.classloader.annotations.PowerMockIgnore;\r
import org.powermock.core.classloader.annotations.PrepareForTest;\r
-import org.powermock.modules.junit4.rule.PowerMockRule;\r
+import org.powermock.modules.junit4.PowerMockRunner;\r
+import org.powermock.reflect.internal.WhiteboxImpl;\r
+\r
+import static org.hamcrest.core.Is.is;\r
+import static org.hamcrest.core.IsEqual.equalTo;\r
+import static org.hamcrest.core.IsNull.nullValue;\r
+import static org.junit.Assert.assertThat;\r
+import static org.onap.holmes.common.config.MicroServiceConfig.*;\r
\r
@PrepareForTest(MicroServiceConfig.class)\r
@PowerMockIgnore({"javax.ws.*"})\r
+@RunWith(PowerMockRunner.class)\r
public class MicroServiceConfigTest {\r
\r
- @Rule\r
- public PowerMockRule powerMockRule = new PowerMockRule();\r
+ private static String ACTUAL_HOSTNAME = System.getenv(HOSTNAME);\r
+\r
\r
@Test\r
public void getMsbServerAddrTest() {\r
\r
@Test\r
public void getServiceIpTest() {\r
- System.setProperty(HOSTNAME, "127.0.0.1");\r
- assertThat("127.0.0.1", equalTo(getMicroServiceIpAndPort()[0]));\r
+ String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;\r
+ String hostname = String.format("http://%s", ip);\r
+ System.setProperty(HOSTNAME, hostname);\r
+ assertThat(ip, equalTo(getMicroServiceIpAndPort()[0]));\r
assertThat("80", equalTo(getMicroServiceIpAndPort()[1]));\r
System.clearProperty(HOSTNAME);\r
}\r
\r
@Test\r
public void getMicroServiceIpAndPort_service_not_registered_to_consul() throws Exception {\r
- System.setProperty(HOSTNAME, "10.74.5.8:1545");\r
+ String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;\r
+ String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";\r
+ String hostname = String.format("http://%s:%s", ip, port);\r
+ System.setProperty(HOSTNAME, hostname);\r
PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
.andReturn("[]");\r
String[] msbInfo = getMicroServiceIpAndPort();\r
PowerMock.verifyAll();\r
\r
- assertThat(msbInfo[0], equalTo("10.74.5.8"));\r
- assertThat(msbInfo[1], equalTo("1545"));\r
+ assertThat(msbInfo[0], equalTo(ip));\r
+ assertThat(msbInfo[1], equalTo(port));\r
\r
System.clearProperty(HOSTNAME);\r
}\r
\r
@Test\r
public void getMicroServiceIpAndPort_service_not_registered_full_addr() throws Exception {\r
- System.setProperty(HOSTNAME, "http://10.74.5.8:1545");\r
+ String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;\r
+ String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";\r
+ String hostname = String.format("http://%s:%s", ip, port);\r
+ System.setProperty(HOSTNAME, hostname);\r
PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
.andReturn("{}");\r
String[] msbInfo = getMicroServiceIpAndPort();\r
PowerMock.verifyAll();\r
\r
- assertThat(msbInfo[0], equalTo("10.74.5.8"));\r
- assertThat(msbInfo[1], equalTo("1545"));\r
+ assertThat(msbInfo[0], equalTo(ip));\r
+ assertThat(msbInfo[1], equalTo(port));\r
\r
System.clearProperty(MSB_ADDR);\r
}\r
\r
@Test\r
public void getMicroServiceIpAndPort_service_not_registered_no_port() throws Exception {\r
- System.setProperty(HOSTNAME, "http://10.74.5.8");\r
+ String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;\r
+ String hostname = String.format("http://%s", ip);\r
+ System.setProperty(HOSTNAME, hostname);\r
PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
.andReturn("{}");\r
String[] msbInfo = getMicroServiceIpAndPort();\r
PowerMock.verifyAll();\r
\r
- assertThat(msbInfo[0], equalTo("10.74.5.8"));\r
+ assertThat(msbInfo[0], equalTo(ip));\r
assertThat(msbInfo[1], equalTo("80"));\r
\r
System.clearProperty(MSB_ADDR);\r
\r
@Test\r
public void getMicroServiceIpAndPort_service_not_registered_only_ip() throws Exception {\r
- System.setProperty(HOSTNAME, "10.74.5.8");\r
+ String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;\r
+ String hostname = String.format("http://%s", ip);\r
+ System.setProperty(HOSTNAME, hostname);\r
PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
.andReturn("{}");\r
String[] msbInfo = getMicroServiceIpAndPort();\r
PowerMock.verifyAll();\r
\r
- assertThat(msbInfo[0], equalTo("10.74.5.8"));\r
+ assertThat(msbInfo[0], equalTo(ip));\r
assertThat(msbInfo[1], equalTo("80"));\r
\r
System.clearProperty(MSB_ADDR);\r
\r
@Test\r
public void getMicroServiceIpAndPort_service_not_registered_full_addr_https() throws Exception {\r
- System.setProperty(HOSTNAME, "https://10.74.5.8:5432");\r
+ String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME;\r
+ String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80";\r
+ String hostname = String.format("http://%s:%s", ip, port);\r
+ System.setProperty(HOSTNAME, hostname);\r
PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
.andReturn("[]");\r
String[] msbInfo = getMicroServiceIpAndPort();\r
PowerMock.verifyAll();\r
\r
- assertThat(msbInfo[0], equalTo("10.74.5.8"));\r
- assertThat(msbInfo[1], equalTo("5432"));\r
+ assertThat(msbInfo[0], equalTo(ip));\r
+ assertThat(msbInfo[1], equalTo(port));\r
\r
System.clearProperty(MSB_ADDR);\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:80");\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:80");\r
+ assertThat(res, is(false));\r
+ }\r
}
\ No newline at end of file