summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuangrongFu <fu.guangrong@zte.com.cn>2021-05-10 17:33:20 +0800
committerGuangrongFu <fu.guangrong@zte.com.cn>2021-05-10 17:34:11 +0800
commit6852ae1f9ad08c26a57911ef6c633d5f9757d693 (patch)
tree7cffc5a37c7062639b16d6d4f4a606990cb2f83b
parent6af8af01fb0cdceb384cf47cf241736620510465 (diff)
bugfix - IndexOutOfBoundsException in MicroServiceConfig
Change-Id: Id37eaee21a164f26c89a3ef890054e7b5f2718f8 Issue-ID: HOLMES-442 Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java23
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java21
2 files changed, 33 insertions, 11 deletions
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 7cbbb3e..7566fa1 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
@@ -1,5 +1,5 @@
/**
- * Copyright 2017-2020 ZTE Corporation.
+ * Copyright 2017-2021 ZTE Corporation.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package org.onap.holmes.common.config;
+import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.onap.holmes.common.constant.AlarmConst;
@@ -59,14 +60,16 @@ public class MicroServiceConfig {
String queryString = getConsulAddrInfo() + hostname;
log.info("Query the " + hostname + " address using the URL: " + queryString);
try {
- JsonObject addrJson = JsonParser.parseString(execQuery(queryString))
- .getAsJsonArray()
- .get(0)
- .getAsJsonObject();
- if (addrJson != null && addrJson.get("ServiceAddress") != null
- && addrJson.get("ServicePort") != null) {
- ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson
- .get("ServicePort").getAsString();
+ JsonArray addrArray = JsonParser.parseString(execQuery(queryString)).getAsJsonArray();
+ if (addrArray.size() > 0) {
+ JsonObject addrJson = addrArray.get(0).getAsJsonObject();
+ if (addrJson != null && addrJson.get("ServiceAddress") != null
+ && addrJson.get("ServicePort") != null) {
+ ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson
+ .get("ServicePort").getAsString();
+ }
+ } else {
+ log.info("No service info is returned from DCAE Consul. Hostname: {}", hostname);
}
} catch (Exception e) {
log.warn(e.getMessage(), e);
@@ -107,7 +110,7 @@ public class MicroServiceConfig {
}
public static String[] getMsbIpAndPort() {
- return new String[] {getEnv(MSB_IAG_SERVICE_HOST), getEnv(MSB_IAG_SERVICE_PORT)};
+ return new String[]{getEnv(MSB_IAG_SERVICE_HOST), getEnv(MSB_IAG_SERVICE_PORT)};
}
public static String[] getMicroServiceIpAndPort() {
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 fbc22e5..316f09f 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
@@ -119,7 +119,26 @@ public class MicroServiceConfigTest {
}
@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("[]");
+ System.setProperty(CONSUL_HOST, "127.0.0.1");
+
+ PowerMock.replayAll();
+
+ assertThat(getServiceAddrInfoFromDcaeConsulByHostName(System.getProperty(CONFIG_BINDING_SERVICE)),
+ is(nullValue()));
+
+ PowerMock.verifyAll();
+
+ System.clearProperty(CONSUL_HOST);
+ System.clearProperty(CONFIG_BINDING_SERVICE);
+ }
+
+ @Test
+ 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())