import java.util.Set;\r
import java.util.stream.Stream;\r
\r
+import static org.onap.holmes.common.utils.GsonUtil.getAsString;\r
+\r
public class DcaeConfigurationParser {\r
\r
private static final String RULE_CONTENT_SPLIT = "\\$\\$\\$";\r
\r
private static SecurityInfo createSecurityInfo(String key, JsonObject entity) {\r
SecurityInfo securityInfo = new SecurityInfo();\r
- if (entity.has("type") && !entity.get("type").isJsonNull()) {\r
- securityInfo.setType(entity.get("type").getAsString());\r
- }\r
- if (entity.has("aaf_password") && !entity.get("aaf_password").isJsonNull()) {\r
- securityInfo.setAafPassword(entity.get("aaf_password").getAsString());\r
- }\r
- if (entity.has("aaf_username") && !entity.get("aaf_username").isJsonNull()) {\r
- securityInfo.setAafUsername(entity.get("aaf_username").getAsString());\r
- }\r
+ securityInfo.setType(getAsString(entity, "type"));\r
+ securityInfo.setAafPassword(getAsString(entity, "aaf_password"));\r
+ securityInfo.setAafUsername(getAsString(entity, "aaf_username"));\r
securityInfo.setSecureTopic(!key.endsWith("unsecure"));\r
fillInDmaapInfo(securityInfo, entity.get("dmaap_info").getAsJsonObject());\r
return securityInfo;\r
\r
private static void fillInDmaapInfo(SecurityInfo securityInfo, JsonObject jsonDmaapInfo) {\r
SecurityInfo.DmaapInfo dmaapInfo = securityInfo.getDmaapInfo();\r
- if (jsonDmaapInfo.has("location") && !jsonDmaapInfo.get("location").isJsonNull()){\r
- dmaapInfo.setLocation(jsonDmaapInfo.get("location").getAsString());\r
- }\r
- if (jsonDmaapInfo.has("topic_url") && !jsonDmaapInfo.get("topic_url").isJsonNull()) {\r
- dmaapInfo.setTopicUrl(jsonDmaapInfo.get("topic_url").getAsString());\r
- }\r
- if (jsonDmaapInfo.has("client_id") && !jsonDmaapInfo.get("client_id").isJsonNull()) {\r
- dmaapInfo.setClientId(jsonDmaapInfo.get("client_id").getAsString());\r
- }\r
- if (jsonDmaapInfo.has("client_role") && !jsonDmaapInfo.get("client_role").isJsonNull()) {\r
- dmaapInfo.setClientRole(jsonDmaapInfo.get("client_role").getAsString());\r
- }\r
- if (jsonDmaapInfo.has("type") && !jsonDmaapInfo.get("type").isJsonNull()) {\r
- dmaapInfo.setType(jsonDmaapInfo.get("type").getAsString());\r
- }\r
+ dmaapInfo.setLocation(getAsString(jsonDmaapInfo, "location"));\r
+ dmaapInfo.setTopicUrl(getAsString(jsonDmaapInfo, "topic_url"));\r
+ dmaapInfo.setClientId(getAsString(jsonDmaapInfo, "client_id"));\r
+ dmaapInfo.setClientRole(getAsString(jsonDmaapInfo, "client_role"));\r
+ dmaapInfo.setType(getAsString(jsonDmaapInfo, "type"));\r
}\r
\r
private static void fillInRules(DcaeConfigurations ret, JsonObject jsonObject) {\r
/**
- * Copyright 2018 ZTE Corporation.
+ * Copyright 2018-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.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
-import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonParseException;
+import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
-import java.lang.reflect.Type;
+import org.apache.commons.lang3.StringUtils;
+
import java.util.List;
import java.util.Map;
public class GsonUtil {
private static Gson gson = null;
+
static {
if (gson == null) {
gson = new GsonBuilder()
- .registerTypeAdapter(Integer.class, new JsonDeserializer<Integer>() {
- @Override
- public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
- try {
- return json.getAsInt();
- } catch (NumberFormatException e) {
- return 0;
- }
+ .registerTypeAdapter(Integer.class, (JsonDeserializer<Integer>) (json, typeOfT, context) -> {
+ try {
+ return json.getAsInt();
+ } catch (NumberFormatException e) {
+ return 0;
}
})
.create();
return t;
}
- public static <T> List<T> GsonToList(String gsonString, Class<T> cls) {
+ public static <T> List<T> jsonToList(String gsonString, Class<T> cls) {
List<T> list = null;
if (gson != null) {
- list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
- }.getType());
+ list = gson.fromJson(gsonString, TypeToken.getParameterized(List.class, cls).getType());
}
return list;
}
- public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
+ public static <T> List<Map<String, T>> jsonToListMaps(String gsonString, Class<T> cls) {
List<Map<String, T>> list = null;
if (gson != null) {
list = gson.fromJson(gsonString,
- new TypeToken<List<Map<String, T>>>() {
- }.getType());
+ TypeToken.getParameterized(List.class,
+ TypeToken.getParameterized(Map.class, String.class, cls).getType()
+ ).getType()
+ );
}
return list;
}
- public static <T> Map<String, T> GsonToMaps(String gsonString) {
+ public static <T> Map<String, T> jsonToMap(String gsonString, Class<T> cls) {
Map<String, T> map = null;
if (gson != null) {
- map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
- }.getType());
+ map = gson.fromJson(gsonString, TypeToken.getParameterized(Map.class, String.class, cls).getType());
}
return map;
}
+
+ public static String getAsString(JsonObject o, String field) {
+ String ret = null;
+
+ if (field == null) {
+ field = StringUtils.EMPTY;
+ }
+
+ if (o.has(field) && !o.get(field).isJsonNull()) {
+ ret = o.get(field).getAsString();
+ }
+ return ret;
+ }
+
+ public static long getAsLong(JsonObject o, String field) {
+ long ret = 0;
+
+ if (field == null) {
+ field = StringUtils.EMPTY;
+ }
+
+ if (o.has(field) && !o.get(field).isJsonNull()) {
+ ret = o.get(field).getAsLong();
+ }
+ return ret;
+ }
+
+ public static int getAsInt(JsonObject o, String field) {
+ int ret = 0;
+
+ if (field == null) {
+ field = StringUtils.EMPTY;
+ }
+
+ if (o.has(field) && !o.get(field).isJsonNull()) {
+ ret = o.get(field).getAsInt();
+ }
+ return ret;
+ }
}
--- /dev/null
+/*
+ * Copyright 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
+ *
+ * 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.dcae.entity;
+
+import org.junit.Test;
+
+import java.util.Set;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.*;
+
+public class DcaeConfigurationsTest {
+
+ @Test
+ public void testAddDefaultRule_null_param() throws Exception {
+ DcaeConfigurations dcaeConfigurations = new DcaeConfigurations();
+ dcaeConfigurations.addDefaultRule(null);
+ assertThat(dcaeConfigurations.getDefaultRules().size(), is(0));
+ }
+
+ @Test
+ public void testGettersAndSetters() throws Exception {
+ DcaeConfigurations dcaeConfigurations = new DcaeConfigurations();
+ dcaeConfigurations.addSubSecInfo("test", new SecurityInfo());
+ assertThat(dcaeConfigurations.getSubSecInfo("test"), notNullValue());
+
+ Set<String> keys = dcaeConfigurations.getSubKeys();
+ assertThat(keys.contains("test"), is(true));
+ }
+}
\ No newline at end of file
@Test\r
public void parse() throws Exception {\r
DcaeConfigurations obj = DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json"));\r
-\r
assertThat(obj.getDefaultRules().size(), equalTo(1));\r
assertThat(obj.get("collector.keystore.alias"), equalTo("dynamically generated"));\r
assertThat(((SecurityInfo) obj.getPubSecInfo("sec_measurement")).getAafPassword(), equalTo("aaf_password"));\r
--- /dev/null
+/**
+ * Copyright 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
+ *
+ * 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.utils;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonParser;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+public class GsonUtilTest {
+
+ private final TestBean bean1 = new TestBean("onap1", 10, 10f, 10d);
+ private final TestBean bean2 = new TestBean("onap2", 20, 20f, 20d);
+ private final Gson gson = new Gson();
+
+ @Test
+ public void beanToJson() {
+ String expected = gson.toJson(bean1);
+ String actual = GsonUtil.beanToJson(bean1);
+ assertThat(actual, equalTo(expected));
+ }
+
+ @Test
+ public void jsonToBean() {
+ TestBean expected = bean1;
+ TestBean actual = GsonUtil.jsonToBean(gson.toJson(expected), TestBean.class);
+ assertThat(expected.getString(), equalTo(actual.getString()));
+ assertThat(expected.getInteger(), equalTo(actual.getInteger()));
+ assertThat(expected.getaDouble(), equalTo(actual.getaDouble()));
+ assertThat(expected.getaFloat(), equalTo(actual.getaFloat()));
+ }
+
+ @Test
+ public void jsonToList() {
+ List<TestBean> expected = Arrays.asList( bean1, bean2);
+ List<TestBean> actual = GsonUtil.jsonToList(gson.toJson(expected), TestBean.class);
+
+ assertThat(expected.size(), equalTo(actual.size()));
+ for (TestBean tb : expected) {
+ assertThat(actual.contains(tb), is(true));
+ }
+ }
+
+ @Test
+ public void jsonToListMaps() {
+ List<Map<String, TestBean>> actual = GsonUtil.jsonToListMaps(
+ "[{\"onap1\":{\"string\":\"onap1\",\"integer\":10,\"aFloat\":10.0,\"aDouble\":10.0}},"
+ + "{\"onap2\":{\"string\":\"onap2\",\"integer\":20,\"aFloat\":20.0,\"aDouble\":20.0}}]", TestBean.class);
+
+ assertThat(actual.get(0).get("onap1"), equalTo(new TestBean("onap1", 10, 10f, 10d)));
+ assertThat(actual.get(1).get("onap2"), equalTo(new TestBean("onap2", 20, 20f, 20d)));
+ }
+
+ @Test
+ public void jsonToMap() {
+ Map<String, TestBean> actual = GsonUtil
+ .jsonToMap("{\"onap1\":{\"string\":\"onap1\",\"integer\":10,\"aFloat\":10.0,\"aDouble\":10.0}}", TestBean.class);
+ assertThat(actual.get("onap1"), equalTo(new TestBean("onap1", 10, 10f, 10d)));
+ }
+
+ @Test
+ public void getAsString() {
+ assertThat("onap1",
+ equalTo(GsonUtil.getAsString(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"string")));
+ }
+
+ @Test
+ public void getAsLong() {
+ assertThat(10L,
+ is(GsonUtil.getAsLong(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"integer")));
+ }
+
+ @Test
+ public void getAsInt() {
+ assertThat(10,
+ is(GsonUtil.getAsInt(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"integer")));
+ }
+}
+
+class TestBean {
+ private String string;
+ private int integer;
+ private float aFloat;
+ private double aDouble;
+
+ public TestBean(String string, int integer, float aFloat, double aDouble) {
+ this.string = string;
+ this.integer = integer;
+ this.aFloat = aFloat;
+ this.aDouble = aDouble;
+ }
+
+ public String getString() {
+ return string;
+ }
+
+ public int getInteger() {
+ return integer;
+ }
+
+ public float getaFloat() {
+ return aFloat;
+ }
+
+ public double getaDouble() {
+ return aDouble;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == null || ! (o instanceof TestBean)) {
+ return false;
+ }
+
+ return string.equals(((TestBean) o).string)
+ && integer == ((TestBean) o).integer
+ && aDouble == ((TestBean) o).aDouble
+ && aFloat == ((TestBean) o).aFloat;
+ }
+
+ @Override
+ public int hashCode() {
+ return string.hashCode();
+ }
+}
\ No newline at end of file