Get the MSB info from DCAE Consul 49/12849/2
authorGuangrong Fu <fu.guangrong@zte.com.cn>
Sat, 16 Sep 2017 06:48:57 +0000 (14:48 +0800)
committerGuangrong Fu <fu.guangrong@zte.com.cn>
Sat, 16 Sep 2017 07:12:27 +0000 (15:12 +0800)
Change-Id: I67206b028abefd99ff4b249a254882f08a642a4d
Issue-ID: HOLMES-45
Signed-off-by: Guangrong Fu <fu.guangrong@zte.com.cn>
holmes-actions/pom.xml
holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java
holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java
holmes-actions/src/main/java/org/onap/holmes/common/constant/AlarmConst.java
holmes-actions/src/main/java/org/onap/holmes/common/dmaap/entity/VesAlarm.java [deleted file]
holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java
holmes-actions/src/test/java/org/onap/holmes/common/api/stat/VesAlarmTest.java [new file with mode: 0644]
holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java
holmes-actions/src/test/java/org/onap/holmes/common/utils/MSBRegisterUtilTest.java
pom.xml

index e8a1605..506d60a 100644 (file)
             <groupId>org.projectlombok</groupId>\r
             <artifactId>lombok</artifactId>\r
         </dependency>\r
-        <dependency>\r
-            <groupId>mysql</groupId>\r
-            <artifactId>mysql-connector-java</artifactId>\r
-        </dependency>\r
         <dependency>\r
             <groupId>io.dropwizard</groupId>\r
             <artifactId>dropwizard-jdbi</artifactId>\r
             <artifactId>powermock-classloading-xstream</artifactId>\r
             <scope>test</scope>\r
         </dependency>\r
-        <dependency>\r
-            <groupId>org.apache.httpcomponents</groupId>\r
-            <artifactId>httpclient</artifactId>\r
-        </dependency>\r
         <dependency>\r
             <groupId>com.eclipsesource.jaxrs</groupId>\r
             <artifactId>consumer</artifactId>\r
             <artifactId>jersey-container-servlet-core</artifactId>\r
             <version>${jersey.version}</version>\r
         </dependency>\r
+        <dependency>\r
+            <groupId>org.glassfish.jersey.core</groupId>\r
+            <artifactId>jersey-common</artifactId>\r
+        </dependency>\r
         <dependency>\r
             <groupId>net.sf.json-lib</groupId>\r
             <artifactId>json-lib</artifactId>\r
index 3cdcb88..127903f 100644 (file)
@@ -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;
index 8f9c9e8..bb78288 100644 (file)
  */\r
 package org.onap.holmes.common.config;\r
 \r
+import javax.ws.rs.client.Client;\r
+import javax.ws.rs.client.ClientBuilder;\r
+import javax.ws.rs.core.Response;\r
+import lombok.extern.slf4j.Slf4j;\r
+import net.sf.json.JSONArray;\r
+import net.sf.json.JSONObject;\r
+import org.glassfish.jersey.client.ClientConfig;\r
+import org.onap.holmes.common.api.stat.Alarm;\r
 import org.onap.holmes.common.constant.AlarmConst;\r
 \r
+@Slf4j\r
 public class MicroServiceConfig {\r
 \r
-    private static String getProperty(String name) {\r
+    final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";\r
+    final static public String CONSUL_HOST = "CONSUL_HOST";\r
+    final static public String HOSTNAME = "HOSTNAME";\r
+    final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";\r
+    final static public String DOCKER_HOST = "DOCKER_HOST";\r
+    final static public String MSB_ADDR = "MSB_ADDR";\r
+\r
+    private static String getEnv(String name) {\r
         String value = System.getenv(name);\r
         if (value == null) {\r
             value = System.getProperty(name);\r
@@ -27,24 +43,89 @@ public class MicroServiceConfig {
         return value;\r
     }\r
 \r
-    public static String getMsbServerAddr() {\r
-        return AlarmConst.HTTP + getProperty("MSB_ADDR");\r
+    public static String getConsulAddrInfo() {\r
+        return getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
+    }\r
+\r
+    public static String getConfigBindingServiceAddrInfo() {\r
+        String ret = null;\r
+        String queryString = getConsulAddrInfo() + CONFIG_BINDING_SERVICE;\r
+        try {\r
+            JSONObject addrJson = (JSONObject) JSONArray.fromObject(execQuery(queryString)).get(0);\r
+            if (addrJson.has("ServiceAddress") && addrJson.has("ServicePort")) {\r
+                ret = addrJson.getString("ServiceAddress") + ":" + addrJson.getString("ServicePort");\r
+            }\r
+        } catch (Exception e) {\r
+            log.warn(e.getMessage(), e);\r
+        }\r
+        return ret;\r
     }\r
 \r
-    public static String getMsbServerIp() {\r
-        return getProperty("MSB_ADDR");\r
+    private static String execQuery(String queryString) {\r
+        Client client = ClientBuilder.newClient(new ClientConfig());\r
+        Response response = client.target(queryString).request().get();\r
+        return response.readEntity(String.class);\r
     }\r
 \r
-    public static int getMsbServerPort() {\r
+    public static String getServiceAddrInfoFromCBS(String serviceName) {\r
+        String ret = null;\r
+        String url = getConfigBindingServiceAddrInfo() + "/service_component/" +serviceName;\r
         try {\r
-            return Integer.valueOf(getProperty("MSB_PORT"));\r
-        } catch (NumberFormatException e) {\r
-            return 80;\r
+            JSONObject jsonObject = JSONObject.fromObject(execQuery(url));\r
+            if (jsonObject.has(serviceName)) {\r
+                ret = (String) jsonObject.getJSONArray(serviceName).get(0);\r
+            }\r
+        } catch (Exception e) {\r
+            log.warn(e.getMessage(), e);\r
+        }\r
+        return ret;\r
+    }\r
+\r
+    public static String getMsbServerAddr() {\r
+        String[] addrInfo = getMsbAddrInfo();\r
+        String ret = addrInfo[0] + ":" + addrInfo[1];\r
+        if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){\r
+            ret = AlarmConst.HTTP + ret;\r
         }\r
+        return ret;\r
     }\r
 \r
-    public static String getServiceIp() {\r
-        return getProperty("SERVICE_IP");\r
+    public static String[] getMsbAddrInfo() {\r
+        String[] msbServerInfo = null;\r
+\r
+        String info = getServiceAddrInfoFromCBS(MSB_ADDR);\r
+        if (info != null){\r
+            msbServerInfo = split(info);\r
+        } else {\r
+            msbServerInfo = split(getEnv(MSB_ADDR));\r
+        }\r
+\r
+        return msbServerInfo;\r
+    }\r
+\r
+    public static String[] getServiceAddrInfo() {\r
+        String[] serviceAddrInfo = null;\r
+        String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME));\r
+        if (info != null){\r
+            serviceAddrInfo = split(info);\r
+        } else {\r
+            serviceAddrInfo = split(getEnv(HOSTNAME));\r
+        }\r
+        return serviceAddrInfo;\r
+    }\r
+\r
+    private static String[] split(String addr) {\r
+        String ip;\r
+        String port = "80";\r
+        if (addr.lastIndexOf(":") == -1){\r
+            ip = addr;\r
+        } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
+            ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
+        } else {\r
+            ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
+            port = addr.substring(addr.lastIndexOf(":") + 1);\r
+        }\r
+        return new String[] {ip, port};\r
     }\r
 \r
 }\r
index 690cde6..ba3d80e 100644 (file)
@@ -38,4 +38,6 @@ public interface AlarmConst {
     int MICRO_SERVICE_PORT = 8086;\r
 \r
     String HTTP = "http://";\r
+\r
+    String HTTPS = "https://";\r
 }\r
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 (file)
index b49fac2..0000000
+++ /dev/null
@@ -1,29 +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.dmaap.entity;\r
-\r
-import lombok.Getter;\r
-import lombok.Setter;\r
-\r
-@Getter\r
-@Setter\r
-public class VesAlarm {\r
-    private String eventId;\r
-    private String sourceId;\r
-    private String specificProblem;\r
-    private long alarmRaisedTime;\r
-}\r
index 8052f78..1cd481e 100644 (file)
@@ -35,44 +35,12 @@ import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
 @Service\r
 public class MSBRegisterUtil {\r
 \r
-    public void register(ServiceRegisterEntity entity) throws IOException {\r
-        log.info("Start register Holmes Service to MSB...");\r
-        boolean flag = false;\r
-        int retry = 0;\r
-        while (!flag && retry < 20) {\r
-            log.info("Holmes Service Registration. Retry: " + retry);\r
-            retry++;\r
-            flag = innerRegister(entity);\r
-            if (!flag) {\r
-                log.warn("Failed to register the service to MSB. Sleep 30s and try again.");\r
-                threadSleep(30000);\r
-            } else {\r
-                log.info("Registration succeeded!");\r
-                break;\r
-            }\r
-        }\r
-        log.info("Service registration completed.");\r
-    }\r
-\r
-    private boolean innerRegister(ServiceRegisterEntity entity) {\r
-        try {\r
-            log.info("msbServerAddr:" + MicroServiceConfig.getMsbServerAddr());\r
-            log.info("entity:" + entity);\r
-            MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer(\r
-                    MicroServiceConfig.getMsbServerAddr(), MicroserviceBusRest.class);\r
-            resourceserviceproxy.registerServce("false", entity);\r
-        } catch (Exception error) {\r
-            log.error("Micro-service registration failed!" + error.getMessage(), error);\r
-            return false;\r
-        }\r
-        return true;\r
-    }\r
-\r
     public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {\r
-        MSBServiceClient msbClient = new MSBServiceClient(MicroServiceConfig.getMsbServerIp(),\r
-                MicroServiceConfig.getMsbServerPort());\r
+        String[] msbAddrInfo = MicroServiceConfig.getMsbAddrInfo();\r
+        MSBServiceClient msbClient = new MSBServiceClient(msbAddrInfo[0],\r
+                Integer.parseInt(msbAddrInfo[1]));\r
 \r
-        log.info("Start register Holmes Service to MSB...");\r
+        log.info("Start to register Holmes Service to MSB...");\r
         MicroServiceFullInfo microServiceFullInfo = null;\r
         int retry = 0;\r
         while (null == microServiceFullInfo && retry < 20) {\r
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 (file)
index 0000000..c2772e8
--- /dev/null
@@ -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
index b29e490..4b697b0 100644 (file)
 \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.easymock.EasyMock;\r
+import org.junit.Rule;\r
 import org.junit.Test;\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
 \r
+@PrepareForTest(MicroServiceConfig.class)\r
+@PowerMockIgnore({"javax.ws.*"})\r
 public class MicroServiceConfigTest {\r
 \r
+    @Rule\r
+    public PowerMockRule powerMockRule = new PowerMockRule();\r
+\r
     @Test\r
     public void getMsbServerAddrTest() {\r
-        System.setProperty("MSB_ADDR", "test");\r
-        assertThat("http://test", equalTo(MicroServiceConfig.getMsbServerAddr()));\r
-        System.clearProperty("MSB_ADDR");\r
+        System.setProperty(MSB_ADDR, "test:80");\r
+        assertThat("http://test:80", equalTo(getMsbServerAddr()));\r
+        System.clearProperty(MicroServiceConfig.MSB_ADDR);\r
     }\r
 \r
     @Test\r
     public void getMsbServerIpTest() {\r
-        System.setProperty("MSB_ADDR", "10.54.23.79");\r
-        assertThat("10.54.23.79", equalTo(MicroServiceConfig.getMsbServerIp()));\r
-        System.clearProperty("MSB_ADDR");\r
+        System.setProperty(MSB_ADDR, "10.54.23.79:80");\r
+        assertThat("10.54.23.79", equalTo(getMsbAddrInfo()[0]));\r
+        assertThat("80", equalTo(getMsbAddrInfo()[1]));\r
+        System.clearProperty(MSB_ADDR);\r
     }\r
 \r
     @Test\r
-    public void getMsbPortTest() {\r
-        System.setProperty("MSB_PORT", "110");\r
-        assertTrue(110 == MicroServiceConfig.getMsbServerPort());\r
-        System.clearProperty("MSB_PORT");\r
+    public void getServiceIpTest() {\r
+        System.setProperty(HOSTNAME, "127.0.0.1");\r
+        assertThat("127.0.0.1", equalTo(getServiceAddrInfo()[0]));\r
+        assertThat("80", equalTo(getServiceAddrInfo()[1]));\r
+        System.clearProperty(HOSTNAME);\r
     }\r
 \r
     @Test\r
-    public void getMsbPortTestNonnumeric() {\r
-        System.setProperty("MSB_PORT", "test");\r
-        assertTrue(80 == MicroServiceConfig.getMsbServerPort());\r
-        System.clearProperty("MSB_PORT");\r
+    public void getConsulAddrInfoTest() {\r
+        System.setProperty(CONSUL_HOST, "127.0.0.1");\r
+        assertThat("127.0.0.1:8500/v1/catalog/service/", equalTo(getConsulAddrInfo()));\r
+        System.clearProperty(CONSUL_HOST);\r
     }\r
 \r
     @Test\r
-    public void getMsbPortTestNullValue() {\r
-        assertTrue(80 == MicroServiceConfig.getMsbServerPort());\r
+    public void getConfigBindingServiceAddrInfoTest_consul_not_exist() throws Exception {\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andThrow(new RuntimeException("Invalid URL."));\r
+\r
+        PowerMock.replayAll();\r
+\r
+        assertThat(getConfigBindingServiceAddrInfo(), is(nullValue()));\r
+\r
+        PowerMock.verifyAll();\r
     }\r
 \r
     @Test\r
-    public void getServiceIpTest() {\r
-        System.setProperty("SERVICE_IP", "test");\r
-        assertThat("test", equalTo(MicroServiceConfig.getServiceIp()));\r
-        System.clearProperty("SERVICE_IP");\r
+    public void getConfigBindingServiceAddrInfoTest_consul_exists() throws Exception {\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");\r
+        System.setProperty(CONSUL_HOST, "127.0.0.1");\r
+\r
+        PowerMock.replayAll();\r
+\r
+        assertThat(getConfigBindingServiceAddrInfo(), equalTo("127.0.0.2:8080"));\r
+\r
+        PowerMock.verifyAll();\r
+\r
+        System.clearProperty(CONSUL_HOST);\r
+    }\r
+\r
+    @Test\r
+    public void getConfigBindingServiceAddrInfoTest_consul_exists_propertie_not_exist() throws Exception {\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\"}]");\r
+        System.setProperty(CONSUL_HOST, "127.0.0.1");\r
+\r
+        PowerMock.replayAll();\r
+\r
+        assertThat(getConfigBindingServiceAddrInfo(), is(nullValue()));\r
+\r
+        PowerMock.verifyAll();\r
+\r
+        System.clearProperty(CONSUL_HOST);\r
+    }\r
+\r
+    @Test\r
+    public void getServiceAddrInfoFromCBS_consul_not_exist() throws Exception {\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andThrow(new RuntimeException("Invalid URL.")).times(2);\r
+\r
+        PowerMock.replayAll();\r
+\r
+        assertThat(getServiceAddrInfoFromCBS(HOSTNAME), is(nullValue()));\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void getServiceAddrInfoFromCBS_consul_exists_service_not_exist() throws Exception {\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("{}");\r
+\r
+        PowerMock.replayAll();\r
+\r
+        assertThat(getServiceAddrInfoFromCBS(HOSTNAME), is(nullValue()));\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void getServiceAddrInfoFromCBS_normal() throws Exception {\r
+        System.setProperty(HOSTNAME, "rule-mgmt");\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("{\"rule-mgmt\": \"[\\\"127.0.0.3:5432\\\"]\"}");\r
+\r
+        PowerMock.replayAll();\r
+\r
+        assertThat(getServiceAddrInfoFromCBS("rule-mgmt"), equalTo("127.0.0.3:5432"));\r
+\r
+        PowerMock.verifyAll();\r
+\r
+        System.clearProperty(HOSTNAME);\r
+    }\r
+\r
+    @Test\r
+    public void getMsbAddrInfo_msb_registered() throws Exception {\r
+        System.setProperty(MSB_ADDR, "10.74.5.8:1545");\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("{\"" + MSB_ADDR + "\": \"[\\\"127.0.0.3:5432\\\"]\"}");\r
+\r
+        PowerMock.replayAll();\r
+        String[] msbInfo = getMsbAddrInfo();\r
+        assertThat(msbInfo[0], equalTo("127.0.0.3"));\r
+        assertThat(msbInfo[1], equalTo("5432"));\r
+\r
+        PowerMock.verifyAll();\r
+\r
+        System.clearProperty(MSB_ADDR);\r
+    }\r
+\r
+    @Test\r
+    public void getMsbAddrInfo_msb_not_registered() throws Exception {\r
+        System.setProperty(MSB_ADDR, "10.74.5.8:1545");\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("{}");\r
+\r
+        PowerMock.replayAll();\r
+        String[] msbInfo = getMsbAddrInfo();\r
+        assertThat(msbInfo[0], equalTo("10.74.5.8"));\r
+        assertThat(msbInfo[1], equalTo("1545"));\r
+\r
+        PowerMock.verifyAll();\r
+\r
+        System.clearProperty(MSB_ADDR);\r
+    }\r
+\r
+    @Test\r
+    public void getServiceAddrInfo_msb_registered() throws Exception {\r
+        System.setProperty(HOSTNAME, "rule-mgmt");\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("{\"rule-mgmt\": \"[\\\"127.0.0.3:5432\\\"]\"}");\r
+\r
+        PowerMock.replayAll();\r
+        String[] msbInfo = getServiceAddrInfo();\r
+        assertThat(msbInfo[0], equalTo("127.0.0.3"));\r
+        assertThat(msbInfo[1], equalTo("5432"));\r
+\r
+        PowerMock.verifyAll();\r
+\r
+        System.clearProperty(HOSTNAME);\r
+    }\r
+\r
+    @Test\r
+    public void getServiceAddrInfo_msb_not_registered() throws Exception {\r
+        System.setProperty(HOSTNAME, "10.74.5.8:1545");\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("{}");\r
+\r
+        PowerMock.replayAll();\r
+        String[] msbInfo = getServiceAddrInfo();\r
+        assertThat(msbInfo[0], equalTo("10.74.5.8"));\r
+        assertThat(msbInfo[1], equalTo("1545"));\r
+\r
+        PowerMock.verifyAll();\r
+\r
+        System.clearProperty(HOSTNAME);\r
+    }\r
+\r
+    @Test\r
+    public void getServiceAddrInfo_msb_not_registered_full_addr() throws Exception {\r
+        System.setProperty(HOSTNAME, "http://10.74.5.8:1545");\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("{}");\r
+\r
+        PowerMock.replayAll();\r
+        String[] msbInfo = getServiceAddrInfo();\r
+        assertThat(msbInfo[0], equalTo("10.74.5.8"));\r
+        assertThat(msbInfo[1], equalTo("1545"));\r
+\r
+        PowerMock.verifyAll();\r
+\r
+        System.clearProperty(MSB_ADDR);\r
+    }\r
+\r
+    @Test\r
+    public void getServiceAddrInfo_msb_not_registered_no_port() throws Exception {\r
+        System.setProperty(HOSTNAME, "http://10.74.5.8");\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("{}");\r
+\r
+        PowerMock.replayAll();\r
+        String[] msbInfo = getServiceAddrInfo();\r
+        assertThat(msbInfo[0], equalTo("10.74.5.8"));\r
+        assertThat(msbInfo[1], equalTo("80"));\r
+\r
+        PowerMock.verifyAll();\r
+\r
+        System.clearProperty(MSB_ADDR);\r
+    }\r
+\r
+    @Test\r
+    public void getServiceAddrInfo_msb_not_registered_only_ip() throws Exception {\r
+        System.setProperty(HOSTNAME, "10.74.5.8");\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("{}");\r
+\r
+        PowerMock.replayAll();\r
+        String[] msbInfo = getServiceAddrInfo();\r
+        assertThat(msbInfo[0], equalTo("10.74.5.8"));\r
+        assertThat(msbInfo[1], equalTo("80"));\r
+\r
+        PowerMock.verifyAll();\r
+\r
+        System.clearProperty(MSB_ADDR);\r
+    }\r
+\r
+    @Test\r
+    public void getServiceAddrInfo_msb_not_registered_full_addr_https() throws Exception {\r
+        System.setProperty(HOSTNAME, "https://10.74.5.8:5432");\r
+        PowerMock.mockStaticPartial(MicroServiceConfig.class, "execQuery", String.class);\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("[{\"ServiceAddress\": \"127.0.0.2\", \"ServicePort\": \"8080\"}]");\r
+        PowerMock.expectPrivate(MicroServiceConfig.class, "execQuery", EasyMock.anyObject())\r
+                .andReturn("{}");\r
+\r
+        PowerMock.replayAll();\r
+        String[] msbInfo = getServiceAddrInfo();\r
+        assertThat(msbInfo[0], equalTo("10.74.5.8"));\r
+        assertThat(msbInfo[1], equalTo("5432"));\r
+\r
+        PowerMock.verifyAll();\r
+\r
+        System.clearProperty(MSB_ADDR);\r
     }\r
 }
\ No newline at end of file
index 72d9a61..ab2554f 100644 (file)
@@ -16,6 +16,8 @@
 \r
 package org.onap.holmes.common.utils;\r
 \r
+import static org.onap.holmes.common.config.MicroServiceConfig.HOSTNAME;\r
+\r
 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;\r
 import javax.ws.rs.QueryParam;\r
 import org.easymock.EasyMock;\r
@@ -25,10 +27,12 @@ import org.onap.holmes.common.msb.MicroserviceBusRest;
 import org.onap.holmes.common.api.entity.ServiceRegisterEntity;\r
 import org.onap.holmes.common.config.MicroServiceConfig;\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
 \r
-@PrepareForTest(ConsumerFactory.class)\r
+@PrepareForTest({ConsumerFactory.class, MicroServiceConfig.class})\r
+@PowerMockIgnore({"javax.ws.*"})\r
 public class MSBRegisterUtilTest {\r
 \r
     @Rule\r
@@ -36,27 +40,14 @@ public class MSBRegisterUtilTest {
     private MSBRegisterUtil msbRegisterUtil = new MSBRegisterUtil();\r
     private MicroserviceBusRest microserviceBusRest = new MicroserviceBusRestProxy();\r
 \r
-    @Test\r
-    public void registerTest() throws Exception {\r
-        ServiceRegisterEntity entity = initServiceEntity();\r
-        PowerMock.mockStatic(ConsumerFactory.class);\r
-        EasyMock.expect(ConsumerFactory\r
-                .createConsumer(EasyMock.anyObject(String.class), EasyMock.anyObject(Class.class)))\r
-                .andReturn(microserviceBusRest);\r
-        PowerMock.replayAll();\r
-\r
-        msbRegisterUtil.register(initServiceEntity());\r
-\r
-        PowerMock.verifyAll();\r
-    }\r
-\r
     private ServiceRegisterEntity initServiceEntity() {\r
+        String[] serviceAddrInfo = MicroServiceConfig.getServiceAddrInfo();\r
         ServiceRegisterEntity serviceRegisterEntity = new ServiceRegisterEntity();\r
         serviceRegisterEntity.setServiceName("holmes-rule-mgmt");\r
         serviceRegisterEntity.setProtocol("REST");\r
         serviceRegisterEntity.setVersion("v1");\r
         serviceRegisterEntity.setUrl("/api/holmes-rule-mgmt/v1");\r
-        serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9101", 0);\r
+        serviceRegisterEntity.setSingleNode(serviceAddrInfo[0], serviceAddrInfo[1], 0);\r
         serviceRegisterEntity.setVisualRange("1|0");\r
         return serviceRegisterEntity;\r
     }\r
diff --git a/pom.xml b/pom.xml
index a2254c5..449c56d 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -38,9 +38,7 @@
         <excludesFile>**/*$*</excludesFile>\r
         <nexusproxy>https://nexus.open-o.org/content</nexusproxy>\r
 \r
-\r
         <stringtemplate.version>3.2.1</stringtemplate.version>\r
-        <mysql.connector.version>5.1.38</mysql.connector.version>\r
         <dropwizard.version>0.8.0</dropwizard.version>\r
         <swagger.version>1.5.3</swagger.version>\r
         <lombok.version>1.16.8</lombok.version>\r
                 <artifactId>msb-java-sdk</artifactId>\r
                 <version>1.0.0-SNAPSHOT</version>\r
             </dependency>\r
+            <dependency>\r
+                <groupId>org.glassfish.jersey.core</groupId>\r
+                <artifactId>jersey-common</artifactId>\r
+                <version>${jersey.version}</version>\r
+                <scope>test</scope>\r
+            </dependency>\r
+            <dependency>\r
+                <groupId>org.glassfish.jersey.core</groupId>\r
+                <artifactId>jersey-client</artifactId>\r
+                <version>${jersey.version}</version>\r
+                <scope>test</scope>\r
+            </dependency>\r
             <dependency>\r
                 <groupId>io.dropwizard</groupId>\r
                 <artifactId>dropwizard-core</artifactId>\r
                 <artifactId>lombok</artifactId>\r
                 <version>${lombok.version}</version>\r
             </dependency>\r
-            <dependency>\r
-                <groupId>mysql</groupId>\r
-                <artifactId>mysql-connector-java</artifactId>\r
-                <version>${mysql.connector.version}</version>\r
-            </dependency>\r
-\r
             <dependency>\r
                 <groupId>org.antlr</groupId>\r
                 <artifactId>stringtemplate</artifactId>\r
                 <artifactId>activemq-pool</artifactId>\r
                 <version>5.8.0</version>\r
             </dependency>\r
-            <dependency>\r
-                <groupId>org.apache.httpcomponents</groupId>\r
-                <artifactId>httpclient</artifactId>\r
-                <version>4.3.6</version>\r
-            </dependency>\r
         </dependencies>\r
     </dependencyManagement>\r
 </project>\r