X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=holmes-actions%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fholmes%2Fcommon%2Fconfig%2FMicroServiceConfigTest.java;h=1fdc3355498ab689ac03c192128fc42cd255951e;hb=6cc976de165087178e784c40423242bc7f1b07ee;hp=23dd075755a75077fdc2bd5d84c4c716db1c73d9;hpb=3dfe506a8ea1bec7096da63541e426958d04a1a0;p=holmes%2Fcommon.git 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 23dd075..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 @@ -1,12 +1,12 @@ /** - * Copyright 2017 ZTE Corporation. - * + * Copyright 2017-2020 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 - * + *

+ * 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. @@ -16,86 +16,91 @@ package org.onap.holmes.common.config; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.hamcrest.core.IsNull.nullValue; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.onap.holmes.common.config.MicroServiceConfig.*; - +import org.apache.commons.lang3.StringUtils; import org.easymock.EasyMock; -import org.junit.Rule; +import org.junit.Ignore; import org.junit.Test; +import org.junit.runner.RunWith; 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.rule.PowerMockRule; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.internal.WhiteboxImpl; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; +import static org.onap.holmes.common.config.MicroServiceConfig.*; @PrepareForTest(MicroServiceConfig.class) @PowerMockIgnore({"javax.ws.*"}) +@RunWith(PowerMockRunner.class) public class MicroServiceConfigTest { - @Rule - public PowerMockRule powerMockRule = new PowerMockRule(); + private static String ACTUAL_HOSTNAME = System.getenv(HOSTNAME); @Test public void getMsbServerAddrTest() { - System.setProperty(MSB_ADDR, "test:80"); - System.setProperty(HOSTNAME, "rule-mgmt"); - PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceAddrInfoFromCBS", String.class); - EasyMock.expect(MicroServiceConfig.getServiceAddrInfoFromCBS(System.getProperty(HOSTNAME))) - .andReturn("{\"msb.hostname\": \"test:80\"}"); - PowerMock.replayAll(); - assertThat("http://test:80", equalTo(getMsbServerAddr())); - PowerMock.verifyAll(); - System.clearProperty(MicroServiceConfig.HOSTNAME); - System.clearProperty(MicroServiceConfig.MSB_ADDR); + System.setProperty(MSB_IAG_SERVICE_HOST, "test"); + System.setProperty(MSB_IAG_SERVICE_PORT, "443"); + assertThat("http://test:443", equalTo(getMsbServerAddrWithHttpPrefix())); + System.clearProperty(MicroServiceConfig.MSB_IAG_SERVICE_PORT); + System.clearProperty(MicroServiceConfig.MSB_IAG_SERVICE_HOST); } @Test public void getMsbServerIpTest() { - System.setProperty(MSB_ADDR, "10.54.23.79:80"); + System.setProperty(MSB_IAG_SERVICE_HOST, "10.54.23.79"); + System.setProperty(MSB_IAG_SERVICE_PORT, "443"); System.setProperty(HOSTNAME, "rule-mgmt"); - PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceAddrInfoFromCBS", String.class); - EasyMock.expect(MicroServiceConfig.getServiceAddrInfoFromCBS(System.getProperty(HOSTNAME))) - .andReturn("{\"msb.hostname\": \"10.54.23.79:80\"}").times(2); + PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class); + EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME))) + .andReturn("{\"msb.hostname\": \"10.54.23.79:443\"}").times(2); PowerMock.replayAll(); - assertThat("10.54.23.79", equalTo(getMsbAddrInfo()[0])); - assertThat("80", equalTo(getMsbAddrInfo()[1])); + assertThat("10.54.23.79", equalTo(getMsbIpAndPort()[0])); + assertThat("443", equalTo(getMsbIpAndPort()[1])); System.clearProperty(MicroServiceConfig.HOSTNAME); - System.clearProperty(MSB_ADDR); + System.clearProperty(MicroServiceConfig.MSB_IAG_SERVICE_PORT); + System.clearProperty(MicroServiceConfig.MSB_IAG_SERVICE_HOST); } @Test public void getServiceIpTest() { - System.setProperty(HOSTNAME, "127.0.0.1"); - assertThat("127.0.0.1", equalTo(getServiceAddrInfo()[0])); - assertThat("80", equalTo(getServiceAddrInfo()[1])); + String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME; + String hostname = String.format("http://%s", ip); + System.setProperty(HOSTNAME, hostname); + assertThat(ip, equalTo(getMicroServiceIpAndPort()[0])); + assertThat("80", equalTo(getMicroServiceIpAndPort()[1])); System.clearProperty(HOSTNAME); } @Test public void getConsulAddrInfoTest() { System.setProperty(CONSUL_HOST, "127.0.0.1"); - assertThat("127.0.0.1:8500/v1/catalog/service/", equalTo(getConsulAddrInfo())); + assertThat("http://127.0.0.1:8500/v1/catalog/service/", equalTo(getConsulAddrInfo())); System.clearProperty(CONSUL_HOST); } @Test public void getConfigBindingServiceAddrInfoTest_consul_not_exist() throws Exception { + System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service"); PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) .andThrow(new RuntimeException("Invalid URL.")); PowerMock.replayAll(); - assertThat(getConfigBindingServiceAddrInfo(), is(nullValue())); + assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)) + , is(nullValue())); PowerMock.verifyAll(); + System.clearProperty(CONFIG_BINDING_SERVICE); } @Test - public void getConfigBindingServiceAddrInfoTest_consul_exists() throws Exception { + public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists() throws Exception { + System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service"); PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]"); @@ -103,122 +108,128 @@ public class MicroServiceConfigTest { PowerMock.replayAll(); - assertThat(getConfigBindingServiceAddrInfo(), equalTo("127.0.0.2:8080")); + assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)), + equalTo("http://127.0.0.2:8080")); PowerMock.verifyAll(); System.clearProperty(CONSUL_HOST); + System.clearProperty(CONFIG_BINDING_SERVICE); } @Test - public void getConfigBindingServiceAddrInfoTest_consul_exists_propertie_not_exist() throws Exception { + public void getConfigBindingServiceAddrInfoTest_consul_return_empty_array() throws Exception { + System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service"); PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("[{\"ServiceAddress\": \"127.0.0.2\"}]"); + .andReturn("[]"); System.setProperty(CONSUL_HOST, "127.0.0.1"); PowerMock.replayAll(); - assertThat(getConfigBindingServiceAddrInfo(), is(nullValue())); + assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)), + is(nullValue())); PowerMock.verifyAll(); System.clearProperty(CONSUL_HOST); + System.clearProperty(CONFIG_BINDING_SERVICE); } @Test - public void getServiceAddrInfoFromCBS_consul_not_exist() throws Exception { + public void getConfigBindingServiceAddrInfoTest_consul_exists_property_not_exist() throws Exception { + System.setProperty(CONFIG_BINDING_SERVICE, "config_binding_service"); PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andThrow(new RuntimeException("Invalid URL.")).times(2); + .andReturn("[{\"ServiceAddress\": \"127.0.0.2\"}]"); + System.setProperty(CONSUL_HOST, "127.0.0.1"); PowerMock.replayAll(); - assertThat(getServiceAddrInfoFromCBS(HOSTNAME), is(nullValue())); + assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)), + is(nullValue())); PowerMock.verifyAll(); + + System.clearProperty(CONSUL_HOST); + System.clearProperty(CONFIG_BINDING_SERVICE); } @Test - public void getServiceAddrInfoFromCBS_consul_exists_service_not_exist() throws Exception { + public void getServiceAddrInfoFromCBS_consul_not_exist() throws Exception { PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]"); - PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("{}"); + .andThrow(new RuntimeException("Invalid URL.")).times(2); PowerMock.replayAll(); - assertThat(getServiceAddrInfoFromCBS(HOSTNAME), is(nullValue())); + + assertThat(getServiceConfigInfoFromCBS(HOSTNAME), is(nullValue())); + PowerMock.verifyAll(); } @Test - public void getServiceAddrInfoFromCBS_normal() throws Exception { - System.setProperty(HOSTNAME, "rule-mgmt"); + public void getServiceAddrInfoFromDcaeConsulByHostName_consul_exists_service_not_exist() throws Exception { PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]"); - PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("{\"rule-mgmt\": \"[\\\"127.0.0.3:5432\\\"]\"}"); + .andReturn("[]"); PowerMock.replayAll(); - - assertThat(getServiceAddrInfoFromCBS("rule-mgmt"), equalTo("127.0.0.3:5432")); - + assertThat(getServiceAddrInfoFromDcaeConsulByHostName(HOSTNAME), is(nullValue())); PowerMock.verifyAll(); - - System.clearProperty(HOSTNAME); } - @Test + @Ignore public void getMsbAddrInfo_msb_registered() throws Exception { - System.setProperty(MSB_ADDR, "10.74.5.8:1545"); + System.setProperty(MSB_IAG_SERVICE_HOST, "10.74.5.8"); + System.setProperty(MSB_IAG_SERVICE_PORT, "1545"); System.setProperty(HOSTNAME, "rule-mgmt"); - PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceAddrInfoFromCBS", String.class); - EasyMock.expect(MicroServiceConfig.getServiceAddrInfoFromCBS(System.getProperty(HOSTNAME))) + PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class); + EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME))) .andReturn("{\"msb.hostname\": \"127.0.0.3:5432\"}"); PowerMock.replayAll(); - String[] msbInfo = getMsbAddrInfo(); + String[] msbInfo = getMsbIpAndPort(); PowerMock.verifyAll(); assertThat(msbInfo[0], equalTo("127.0.0.3")); assertThat(msbInfo[1], equalTo("5432")); System.clearProperty(HOSTNAME); - System.clearProperty(MSB_ADDR); + System.clearProperty(MSB_IAG_SERVICE_PORT); + System.clearProperty(MSB_IAG_SERVICE_HOST); } - @Test + @Ignore public void getMsbAddrInfo_msb_not_registered() throws Exception { - System.setProperty(MSB_ADDR, "10.74.5.8:1545"); + System.setProperty(MSB_IAG_SERVICE_HOST, "10.74.5.8"); + System.setProperty(MSB_IAG_SERVICE_PORT, "1545"); System.setProperty(HOSTNAME, "rule-mgmt"); - PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceAddrInfoFromCBS", String.class); - EasyMock.expect(MicroServiceConfig.getServiceAddrInfoFromCBS(System.getProperty(HOSTNAME))) + PowerMock.mockStaticPartial(MicroServiceConfig.class, "getServiceConfigInfoFromCBS", String.class); + EasyMock.expect(MicroServiceConfig.getServiceConfigInfoFromCBS(System.getProperty(HOSTNAME))) .andReturn("{}"); PowerMock.replayAll(); - String[] msbInfo = getMsbAddrInfo(); + String[] msbInfo = getMsbIpAndPort(); PowerMock.verifyAll(); assertThat(msbInfo[0], equalTo("10.74.5.8")); assertThat(msbInfo[1], equalTo("1545")); System.clearProperty(HOSTNAME); - System.clearProperty(MSB_ADDR); + System.clearProperty(MSB_IAG_SERVICE_PORT); + System.clearProperty(MSB_IAG_SERVICE_HOST); } @Test - public void getServiceAddrInfo_msb_registered() throws Exception { + public void getMicroServiceIpAndPort_service_registered_to_consul() throws Exception { System.setProperty(HOSTNAME, "rule-mgmt"); PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]"); - PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("{\"rule-mgmt\": \"[\\\"127.0.0.3:5432\\\"]\"}"); + .andReturn("[{\"ServiceAddress\": \"127.0.0.3\", \"ServicePort\": \"5432\"}]"); PowerMock.replayAll(); - String[] msbInfo = getServiceAddrInfo(); + String[] msbInfo = getMicroServiceIpAndPort(); PowerMock.verifyAll(); assertThat(msbInfo[0], equalTo("127.0.0.3")); @@ -228,97 +239,100 @@ public class MicroServiceConfigTest { } @Test - public void getServiceAddrInfo_msb_not_registered() throws Exception { - System.setProperty(HOSTNAME, "10.74.5.8:1545"); + public void getMicroServiceIpAndPort_service_not_registered_to_consul() throws Exception { + String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME; + String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80"; + String hostname = String.format("http://%s:%s", ip, port); + System.setProperty(HOSTNAME, hostname); PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]"); - PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("{}"); + .andReturn("[]"); PowerMock.replayAll(); - String[] msbInfo = getServiceAddrInfo(); + String[] msbInfo = getMicroServiceIpAndPort(); PowerMock.verifyAll(); - assertThat(msbInfo[0], equalTo("10.74.5.8")); - assertThat(msbInfo[1], equalTo("1545")); + assertThat(msbInfo[0], equalTo(ip)); + assertThat(msbInfo[1], equalTo(port)); System.clearProperty(HOSTNAME); } @Test - public void getServiceAddrInfo_msb_not_registered_full_addr() throws Exception { - System.setProperty(HOSTNAME, "http://10.74.5.8:1545"); + public void getMicroServiceIpAndPort_service_not_registered_full_addr() throws Exception { + String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME; + String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80"; + String hostname = String.format("http://%s:%s", ip, port); + System.setProperty(HOSTNAME, hostname); PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); - PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]"); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) .andReturn("{}"); PowerMock.replayAll(); - String[] msbInfo = getServiceAddrInfo(); + String[] msbInfo = getMicroServiceIpAndPort(); PowerMock.verifyAll(); - assertThat(msbInfo[0], equalTo("10.74.5.8")); - assertThat(msbInfo[1], equalTo("1545")); + assertThat(msbInfo[0], equalTo(ip)); + assertThat(msbInfo[1], equalTo(port)); - System.clearProperty(MSB_ADDR); + System.clearProperty(HOSTNAME); } @Test - public void getServiceAddrInfo_msb_not_registered_no_port() throws Exception { - System.setProperty(HOSTNAME, "http://10.74.5.8"); + public void getMicroServiceIpAndPort_service_not_registered_no_port() throws Exception { + String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME; + String hostname = String.format("http://%s", ip); + System.setProperty(HOSTNAME, hostname); PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); - PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]"); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) .andReturn("{}"); PowerMock.replayAll(); - String[] msbInfo = getServiceAddrInfo(); + String[] msbInfo = getMicroServiceIpAndPort(); PowerMock.verifyAll(); - assertThat(msbInfo[0], equalTo("10.74.5.8")); + assertThat(msbInfo[0], equalTo(ip)); assertThat(msbInfo[1], equalTo("80")); - System.clearProperty(MSB_ADDR); + System.clearProperty(HOSTNAME); } @Test - public void getServiceAddrInfo_msb_not_registered_only_ip() throws Exception { - System.setProperty(HOSTNAME, "10.74.5.8"); + public void getMicroServiceIpAndPort_service_not_registered_only_ip() throws Exception { + String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME; + String hostname = String.format("http://%s", ip); + System.setProperty(HOSTNAME, hostname); PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); - PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]"); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) .andReturn("{}"); PowerMock.replayAll(); - String[] msbInfo = getServiceAddrInfo(); + String[] msbInfo = getMicroServiceIpAndPort(); PowerMock.verifyAll(); - assertThat(msbInfo[0], equalTo("10.74.5.8")); + assertThat(msbInfo[0], equalTo(ip)); assertThat(msbInfo[1], equalTo("80")); - System.clearProperty(MSB_ADDR); + System.clearProperty(HOSTNAME); } @Test - public void getServiceAddrInfo_msb_not_registered_full_addr_https() throws Exception { - System.setProperty(HOSTNAME, "https://10.74.5.8:5432"); + public void getMicroServiceIpAndPort_service_not_registered_full_addr_https() throws Exception { + String ip = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "10.74.5.8" : ACTUAL_HOSTNAME; + String port = StringUtils.isEmpty(ACTUAL_HOSTNAME) ? "1545" : "80"; + String hostname = String.format("http://%s:%s", ip, port); + System.setProperty(HOSTNAME, hostname); PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]"); - PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) - .andReturn("{}"); + .andReturn("[]"); PowerMock.replayAll(); - String[] msbInfo = getServiceAddrInfo(); + String[] msbInfo = getMicroServiceIpAndPort(); PowerMock.verifyAll(); - assertThat(msbInfo[0], equalTo("10.74.5.8")); - assertThat(msbInfo[1], equalTo("5432")); + assertThat(msbInfo[0], equalTo(ip)); + assertThat(msbInfo[1], equalTo(port)); - System.clearProperty(MSB_ADDR); + System.clearProperty(HOSTNAME); } } \ No newline at end of file