From 9d2cccba20f32fe54e4ccf1f433f68b5cee95bd5 Mon Sep 17 00:00:00 2001 From: Guangrong Fu Date: Sat, 16 Sep 2017 14:48:57 +0800 Subject: Get the MSB info from DCAE Consul Change-Id: I67206b028abefd99ff4b249a254882f08a642a4d Issue-ID: HOLMES-45 Signed-off-by: Guangrong Fu --- .../onap/holmes/common/api/stat/VesAlarmTest.java | 107 ++++++++ .../common/config/MicroServiceConfigTest.java | 287 +++++++++++++++++++-- .../holmes/common/utils/MSBRegisterUtilTest.java | 23 +- 3 files changed, 381 insertions(+), 36 deletions(-) create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/api/stat/VesAlarmTest.java (limited to 'holmes-actions/src/test/java') diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/api/stat/VesAlarmTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/api/stat/VesAlarmTest.java new file mode 100644 index 0000000..c2772e8 --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/api/stat/VesAlarmTest.java @@ -0,0 +1,107 @@ +/* + * 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.api.stat; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import org.junit.Test; + +public class VesAlarmTest { + + @Test + public void hashCodeTest() throws Exception { + VesAlarm alarm = new VesAlarm(); + alarm.setVersion(0L); + VesAlarm alarmClone = (VesAlarm)alarm.clone(); + assertTrue(alarm.hashCode() == alarmClone.hashCode()); + } + + @Test + public void equalsTest() throws Exception { + VesAlarm alarm = new VesAlarm(); + alarm.setVersion(0L); + VesAlarm alarmClone = (VesAlarm)alarm.clone(); + assertTrue(alarm.equals(alarmClone)); + } + + @Test + public void cloneTest() throws Exception { + VesAlarm alarm = new VesAlarm(); + alarm.setDomain("Test"); + alarm.setVersion(0L); + assertThat(alarm, equalTo(alarm.clone())); + } + + @Test + public void getterAndSetterTest() { + VesAlarm alarm = new VesAlarm(); + alarm.setDomain(""); + alarm.setEventId(""); + alarm.setEventName(""); + alarm.setEventType(""); + alarm.setInternalHeaderFields(new Object()); + alarm.setLastEpochMicrosec(0L); + alarm.setNfcNamingCode(""); + alarm.setNfNamingCode(""); + alarm.setPriority(""); + alarm.setReportingEntityId(""); + alarm.setReportingEntityName(""); + alarm.setSequence(1); + alarm.setSourceId(""); + alarm.setSourceName(""); + alarm.setStartEpochMicrosec(0L); + alarm.setVersion(0L); + alarm.setAlarmAdditionalInformation(new ArrayList<>()); + alarm.setAlarmCondition(""); + alarm.setAlarmInterfaceA(""); + alarm.setEventCategory(""); + alarm.setEventSeverity(""); + alarm.setEventSourceType(""); + alarm.setFaultFieldsVersion(0L); + alarm.setSpecificProblem(""); + alarm.setVfStatus(""); + + alarm.getDomain(); + alarm.getEventId(); + alarm.getEventName(); + alarm.getEventType(); + alarm.getInternalHeaderFields(); + alarm.getLastEpochMicrosec(); + alarm.getNfcNamingCode(); + alarm.getNfNamingCode(); + alarm.getPriority(); + alarm.getReportingEntityId(); + alarm.getReportingEntityName(); + alarm.getSequence(); + alarm.getSourceId(); + alarm.getSourceName(); + alarm.getStartEpochMicrosec(); + alarm.getVersion(); + alarm.getAlarmAdditionalInformation(); + alarm.getAlarmCondition(); + alarm.getAlarmInterfaceA(); + alarm.getEventCategory(); + alarm.getEventSeverity(); + alarm.getEventSourceType(); + alarm.getFaultFieldsVersion(); + alarm.getSpecificProblem(); + alarm.getVfStatus(); + } + +} \ No newline at end of file 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 b29e490..4b697b0 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 @@ -16,51 +16,298 @@ 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.easymock.EasyMock; +import org.junit.Rule; import org.junit.Test; +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; +@PrepareForTest(MicroServiceConfig.class) +@PowerMockIgnore({"javax.ws.*"}) public class MicroServiceConfigTest { + @Rule + public PowerMockRule powerMockRule = new PowerMockRule(); + @Test public void getMsbServerAddrTest() { - System.setProperty("MSB_ADDR", "test"); - assertThat("http://test", equalTo(MicroServiceConfig.getMsbServerAddr())); - System.clearProperty("MSB_ADDR"); + System.setProperty(MSB_ADDR, "test:80"); + assertThat("http://test:80", equalTo(getMsbServerAddr())); + System.clearProperty(MicroServiceConfig.MSB_ADDR); } @Test public void getMsbServerIpTest() { - System.setProperty("MSB_ADDR", "10.54.23.79"); - assertThat("10.54.23.79", equalTo(MicroServiceConfig.getMsbServerIp())); - System.clearProperty("MSB_ADDR"); + System.setProperty(MSB_ADDR, "10.54.23.79:80"); + assertThat("10.54.23.79", equalTo(getMsbAddrInfo()[0])); + assertThat("80", equalTo(getMsbAddrInfo()[1])); + System.clearProperty(MSB_ADDR); } @Test - public void getMsbPortTest() { - System.setProperty("MSB_PORT", "110"); - assertTrue(110 == MicroServiceConfig.getMsbServerPort()); - System.clearProperty("MSB_PORT"); + public void getServiceIpTest() { + System.setProperty(HOSTNAME, "127.0.0.1"); + assertThat("127.0.0.1", equalTo(getServiceAddrInfo()[0])); + assertThat("80", equalTo(getServiceAddrInfo()[1])); + System.clearProperty(HOSTNAME); } @Test - public void getMsbPortTestNonnumeric() { - System.setProperty("MSB_PORT", "test"); - assertTrue(80 == MicroServiceConfig.getMsbServerPort()); - System.clearProperty("MSB_PORT"); + public void getConsulAddrInfoTest() { + System.setProperty(CONSUL_HOST, "127.0.0.1"); + assertThat("127.0.0.1:8500/v1/catalog/service/", equalTo(getConsulAddrInfo())); + System.clearProperty(CONSUL_HOST); } @Test - public void getMsbPortTestNullValue() { - assertTrue(80 == MicroServiceConfig.getMsbServerPort()); + public void getConfigBindingServiceAddrInfoTest_consul_not_exist() throws Exception { + 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())); + + PowerMock.verifyAll(); } @Test - public void getServiceIpTest() { - System.setProperty("SERVICE_IP", "test"); - assertThat("test", equalTo(MicroServiceConfig.getServiceIp())); - System.clearProperty("SERVICE_IP"); + public void getConfigBindingServiceAddrInfoTest_consul_exists() throws Exception { + PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); + PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) + .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]"); + System.setProperty(CONSUL_HOST, "127.0.0.1"); + + PowerMock.replayAll(); + + assertThat(getConfigBindingServiceAddrInfo(), equalTo("127.0.0.2:8080")); + + PowerMock.verifyAll(); + + System.clearProperty(CONSUL_HOST); + } + + @Test + public void getConfigBindingServiceAddrInfoTest_consul_exists_propertie_not_exist() throws Exception { + PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); + PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) + .andReturn("[{\"ServiceAddress\": \"127.0.0.2\"}]"); + System.setProperty(CONSUL_HOST, "127.0.0.1"); + + PowerMock.replayAll(); + + assertThat(getConfigBindingServiceAddrInfo(), is(nullValue())); + + PowerMock.verifyAll(); + + System.clearProperty(CONSUL_HOST); + } + + @Test + public void getServiceAddrInfoFromCBS_consul_not_exist() throws Exception { + PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class); + PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject()) + .andThrow(new RuntimeException("Invalid URL.")).times(2); + + PowerMock.replayAll(); + + assertThat(getServiceAddrInfoFromCBS(HOSTNAME), is(nullValue())); + + PowerMock.verifyAll(); + } + + @Test + public void getServiceAddrInfoFromCBS_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("{}"); + + PowerMock.replayAll(); + + assertThat(getServiceAddrInfoFromCBS(HOSTNAME), is(nullValue())); + + PowerMock.verifyAll(); + } + + @Test + public void getServiceAddrInfoFromCBS_normal() 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\\\"]\"}"); + + PowerMock.replayAll(); + + assertThat(getServiceAddrInfoFromCBS("rule-mgmt"), equalTo("127.0.0.3:5432")); + + PowerMock.verifyAll(); + + System.clearProperty(HOSTNAME); + } + + @Test + public void getMsbAddrInfo_msb_registered() throws Exception { + System.setProperty(MSB_ADDR, "10.74.5.8:1545"); + 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("{\"" + MSB_ADDR + "\": \"[\\\"127.0.0.3:5432\\\"]\"}"); + + PowerMock.replayAll(); + String[] msbInfo = getMsbAddrInfo(); + assertThat(msbInfo[0], equalTo("127.0.0.3")); + assertThat(msbInfo[1], equalTo("5432")); + + PowerMock.verifyAll(); + + System.clearProperty(MSB_ADDR); + } + + @Test + public void getMsbAddrInfo_msb_not_registered() throws Exception { + System.setProperty(MSB_ADDR, "10.74.5.8:1545"); + 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 = getMsbAddrInfo(); + assertThat(msbInfo[0], equalTo("10.74.5.8")); + assertThat(msbInfo[1], equalTo("1545")); + + PowerMock.verifyAll(); + + System.clearProperty(MSB_ADDR); + } + + @Test + public void getServiceAddrInfo_msb_registered() 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\\\"]\"}"); + + PowerMock.replayAll(); + String[] msbInfo = getServiceAddrInfo(); + assertThat(msbInfo[0], equalTo("127.0.0.3")); + assertThat(msbInfo[1], equalTo("5432")); + + PowerMock.verifyAll(); + + System.clearProperty(HOSTNAME); + } + + @Test + public void getServiceAddrInfo_msb_not_registered() throws Exception { + System.setProperty(HOSTNAME, "10.74.5.8:1545"); + 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(); + assertThat(msbInfo[0], equalTo("10.74.5.8")); + assertThat(msbInfo[1], equalTo("1545")); + + PowerMock.verifyAll(); + + System.clearProperty(HOSTNAME); + } + + @Test + public void getServiceAddrInfo_msb_not_registered_full_addr() throws Exception { + System.setProperty(HOSTNAME, "http://10.74.5.8:1545"); + 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(); + assertThat(msbInfo[0], equalTo("10.74.5.8")); + assertThat(msbInfo[1], equalTo("1545")); + + PowerMock.verifyAll(); + + System.clearProperty(MSB_ADDR); + } + + @Test + public void getServiceAddrInfo_msb_not_registered_no_port() throws Exception { + System.setProperty(HOSTNAME, "http://10.74.5.8"); + 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(); + assertThat(msbInfo[0], equalTo("10.74.5.8")); + assertThat(msbInfo[1], equalTo("80")); + + PowerMock.verifyAll(); + + System.clearProperty(MSB_ADDR); + } + + @Test + public void getServiceAddrInfo_msb_not_registered_only_ip() throws Exception { + System.setProperty(HOSTNAME, "10.74.5.8"); + 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(); + assertThat(msbInfo[0], equalTo("10.74.5.8")); + assertThat(msbInfo[1], equalTo("80")); + + PowerMock.verifyAll(); + + System.clearProperty(MSB_ADDR); + } + + @Test + public void getServiceAddrInfo_msb_not_registered_full_addr_https() throws Exception { + System.setProperty(HOSTNAME, "https://10.74.5.8:5432"); + 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(); + assertThat(msbInfo[0], equalTo("10.74.5.8")); + assertThat(msbInfo[1], equalTo("5432")); + + PowerMock.verifyAll(); + + System.clearProperty(MSB_ADDR); } } \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/MSBRegisterUtilTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/MSBRegisterUtilTest.java index 72d9a61..ab2554f 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/MSBRegisterUtilTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/MSBRegisterUtilTest.java @@ -16,6 +16,8 @@ package org.onap.holmes.common.utils; +import static org.onap.holmes.common.config.MicroServiceConfig.HOSTNAME; + import com.eclipsesource.jaxrs.consumer.ConsumerFactory; import javax.ws.rs.QueryParam; import org.easymock.EasyMock; @@ -25,10 +27,12 @@ import org.onap.holmes.common.msb.MicroserviceBusRest; import org.onap.holmes.common.api.entity.ServiceRegisterEntity; import org.onap.holmes.common.config.MicroServiceConfig; 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; -@PrepareForTest(ConsumerFactory.class) +@PrepareForTest({ConsumerFactory.class, MicroServiceConfig.class}) +@PowerMockIgnore({"javax.ws.*"}) public class MSBRegisterUtilTest { @Rule @@ -36,27 +40,14 @@ public class MSBRegisterUtilTest { private MSBRegisterUtil msbRegisterUtil = new MSBRegisterUtil(); private MicroserviceBusRest microserviceBusRest = new MicroserviceBusRestProxy(); - @Test - public void registerTest() throws Exception { - ServiceRegisterEntity entity = initServiceEntity(); - PowerMock.mockStatic(ConsumerFactory.class); - EasyMock.expect(ConsumerFactory - .createConsumer(EasyMock.anyObject(String.class), EasyMock.anyObject(Class.class))) - .andReturn(microserviceBusRest); - PowerMock.replayAll(); - - msbRegisterUtil.register(initServiceEntity()); - - PowerMock.verifyAll(); - } - private ServiceRegisterEntity initServiceEntity() { + String[] serviceAddrInfo = MicroServiceConfig.getServiceAddrInfo(); ServiceRegisterEntity serviceRegisterEntity = new ServiceRegisterEntity(); serviceRegisterEntity.setServiceName("holmes-rule-mgmt"); serviceRegisterEntity.setProtocol("REST"); serviceRegisterEntity.setVersion("v1"); serviceRegisterEntity.setUrl("/api/holmes-rule-mgmt/v1"); - serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9101", 0); + serviceRegisterEntity.setSingleNode(serviceAddrInfo[0], serviceAddrInfo[1], 0); serviceRegisterEntity.setVisualRange("1|0"); return serviceRegisterEntity; } -- cgit 1.2.3-korg