From 9d2cccba20f32fe54e4ccf1f433f68b5cee95bd5 Mon Sep 17 00:00:00 2001 From: Guangrong Fu Date: Sat, 16 Sep 2017 14:48:57 +0800 Subject: [PATCH] Get the MSB info from DCAE Consul Change-Id: I67206b028abefd99ff4b249a254882f08a642a4d Issue-ID: HOLMES-45 Signed-off-by: Guangrong Fu --- holmes-actions/pom.xml | 12 +- .../org/onap/holmes/common/api/stat/VesAlarm.java | 2 +- .../holmes/common/config/MicroServiceConfig.java | 103 +++++++- .../onap/holmes/common/constant/AlarmConst.java | 2 + .../onap/holmes/common/dmaap/entity/VesAlarm.java | 29 --- .../onap/holmes/common/utils/MSBRegisterUtil.java | 40 +-- .../onap/holmes/common/api/stat/VesAlarmTest.java | 107 ++++++++ .../common/config/MicroServiceConfigTest.java | 287 +++++++++++++++++++-- .../holmes/common/utils/MSBRegisterUtilTest.java | 23 +- pom.xml | 25 +- 10 files changed, 496 insertions(+), 134 deletions(-) delete mode 100644 holmes-actions/src/main/java/org/onap/holmes/common/dmaap/entity/VesAlarm.java create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/api/stat/VesAlarmTest.java diff --git a/holmes-actions/pom.xml b/holmes-actions/pom.xml index e8a1605..506d60a 100644 --- a/holmes-actions/pom.xml +++ b/holmes-actions/pom.xml @@ -51,10 +51,6 @@ org.projectlombok lombok - - mysql - mysql-connector-java - io.dropwizard dropwizard-jdbi @@ -108,10 +104,6 @@ powermock-classloading-xstream test - - org.apache.httpcomponents - httpclient - com.eclipsesource.jaxrs consumer @@ -122,6 +114,10 @@ jersey-container-servlet-core ${jersey.version} + + org.glassfish.jersey.core + jersey-common + net.sf.json-lib json-lib diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java b/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java index 3cdcb88..127903f 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java @@ -27,7 +27,7 @@ import lombok.Setter; public class VesAlarm implements Cloneable, Serializable{ private String domain; private String eventId; - private String EventName; + private String eventName; private String eventType; private Object internalHeaderFields; private Long lastEpochMicrosec; diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java index 8f9c9e8..bb78288 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java @@ -15,11 +15,27 @@ */ package org.onap.holmes.common.config; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.Response; +import lombok.extern.slf4j.Slf4j; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; +import org.glassfish.jersey.client.ClientConfig; +import org.onap.holmes.common.api.stat.Alarm; import org.onap.holmes.common.constant.AlarmConst; +@Slf4j public class MicroServiceConfig { - private static String getProperty(String name) { + final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/"; + final static public String CONSUL_HOST = "CONSUL_HOST"; + final static public String HOSTNAME = "HOSTNAME"; + final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE"; + final static public String DOCKER_HOST = "DOCKER_HOST"; + final static public String MSB_ADDR = "MSB_ADDR"; + + private static String getEnv(String name) { String value = System.getenv(name); if (value == null) { value = System.getProperty(name); @@ -27,24 +43,89 @@ public class MicroServiceConfig { return value; } - public static String getMsbServerAddr() { - return AlarmConst.HTTP + getProperty("MSB_ADDR"); + public static String getConsulAddrInfo() { + return getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF; + } + + public static String getConfigBindingServiceAddrInfo() { + String ret = null; + String queryString = getConsulAddrInfo() + CONFIG_BINDING_SERVICE; + try { + JSONObject addrJson = (JSONObject) JSONArray.fromObject(execQuery(queryString)).get(0); + if (addrJson.has("ServiceAddress") && addrJson.has("ServicePort")) { + ret = addrJson.getString("ServiceAddress") + ":" + addrJson.getString("ServicePort"); + } + } catch (Exception e) { + log.warn(e.getMessage(), e); + } + return ret; } - public static String getMsbServerIp() { - return getProperty("MSB_ADDR"); + private static String execQuery(String queryString) { + Client client = ClientBuilder.newClient(new ClientConfig()); + Response response = client.target(queryString).request().get(); + return response.readEntity(String.class); } - public static int getMsbServerPort() { + public static String getServiceAddrInfoFromCBS(String serviceName) { + String ret = null; + String url = getConfigBindingServiceAddrInfo() + "/service_component/" +serviceName; try { - return Integer.valueOf(getProperty("MSB_PORT")); - } catch (NumberFormatException e) { - return 80; + JSONObject jsonObject = JSONObject.fromObject(execQuery(url)); + if (jsonObject.has(serviceName)) { + ret = (String) jsonObject.getJSONArray(serviceName).get(0); + } + } catch (Exception e) { + log.warn(e.getMessage(), e); + } + return ret; + } + + public static String getMsbServerAddr() { + String[] addrInfo = getMsbAddrInfo(); + String ret = addrInfo[0] + ":" + addrInfo[1]; + if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){ + ret = AlarmConst.HTTP + ret; } + return ret; } - public static String getServiceIp() { - return getProperty("SERVICE_IP"); + public static String[] getMsbAddrInfo() { + String[] msbServerInfo = null; + + String info = getServiceAddrInfoFromCBS(MSB_ADDR); + if (info != null){ + msbServerInfo = split(info); + } else { + msbServerInfo = split(getEnv(MSB_ADDR)); + } + + return msbServerInfo; + } + + public static String[] getServiceAddrInfo() { + String[] serviceAddrInfo = null; + String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME)); + if (info != null){ + serviceAddrInfo = split(info); + } else { + serviceAddrInfo = split(getEnv(HOSTNAME)); + } + return serviceAddrInfo; + } + + private static String[] split(String addr) { + String ip; + String port = "80"; + if (addr.lastIndexOf(":") == -1){ + ip = addr; + } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) { + ip = addr.substring(addr.indexOf("//") + 2); //remove the http(s):// prefix + } else { + ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":")); + port = addr.substring(addr.lastIndexOf(":") + 1); + } + return new String[] {ip, port}; } } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/constant/AlarmConst.java b/holmes-actions/src/main/java/org/onap/holmes/common/constant/AlarmConst.java index 690cde6..ba3d80e 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/constant/AlarmConst.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/constant/AlarmConst.java @@ -38,4 +38,6 @@ public interface AlarmConst { int MICRO_SERVICE_PORT = 8086; String HTTP = "http://"; + + String HTTPS = "https://"; } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/entity/VesAlarm.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/entity/VesAlarm.java deleted file mode 100644 index b49fac2..0000000 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/entity/VesAlarm.java +++ /dev/null @@ -1,29 +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.dmaap.entity; - -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class VesAlarm { - private String eventId; - private String sourceId; - private String specificProblem; - private long alarmRaisedTime; -} diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java index 8052f78..1cd481e 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java @@ -35,44 +35,12 @@ import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; @Service public class MSBRegisterUtil { - public void register(ServiceRegisterEntity entity) throws IOException { - log.info("Start register Holmes Service to MSB..."); - boolean flag = false; - int retry = 0; - while (!flag && retry < 20) { - log.info("Holmes Service Registration. Retry: " + retry); - retry++; - flag = innerRegister(entity); - if (!flag) { - log.warn("Failed to register the service to MSB. Sleep 30s and try again."); - threadSleep(30000); - } else { - log.info("Registration succeeded!"); - break; - } - } - log.info("Service registration completed."); - } - - private boolean innerRegister(ServiceRegisterEntity entity) { - try { - log.info("msbServerAddr:" + MicroServiceConfig.getMsbServerAddr()); - log.info("entity:" + entity); - MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer( - MicroServiceConfig.getMsbServerAddr(), MicroserviceBusRest.class); - resourceserviceproxy.registerServce("false", entity); - } catch (Exception error) { - log.error("Micro-service registration failed!" + error.getMessage(), error); - return false; - } - return true; - } - public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException { - MSBServiceClient msbClient = new MSBServiceClient(MicroServiceConfig.getMsbServerIp(), - MicroServiceConfig.getMsbServerPort()); + String[] msbAddrInfo = MicroServiceConfig.getMsbAddrInfo(); + MSBServiceClient msbClient = new MSBServiceClient(msbAddrInfo[0], + Integer.parseInt(msbAddrInfo[1])); - log.info("Start register Holmes Service to MSB..."); + log.info("Start to register Holmes Service to MSB..."); MicroServiceFullInfo microServiceFullInfo = null; int retry = 0; while (null == microServiceFullInfo && retry < 20) { 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; } diff --git a/pom.xml b/pom.xml index a2254c5..449c56d 100644 --- a/pom.xml +++ b/pom.xml @@ -38,9 +38,7 @@ **/*$* https://nexus.open-o.org/content - 3.2.1 - 5.1.38 0.8.0 1.5.3 1.16.8 @@ -56,6 +54,18 @@ msb-java-sdk 1.0.0-SNAPSHOT + + org.glassfish.jersey.core + jersey-common + ${jersey.version} + test + + + org.glassfish.jersey.core + jersey-client + ${jersey.version} + test + io.dropwizard dropwizard-core @@ -84,12 +94,6 @@ lombok ${lombok.version} - - mysql - mysql-connector-java - ${mysql.connector.version} - - org.antlr stringtemplate @@ -162,11 +166,6 @@ activemq-pool 5.8.0 - - org.apache.httpcomponents - httpclient - 4.3.6 - -- 2.16.6