diff options
author | YiLi <li.yi101@zte.com.cn> | 2018-02-09 09:53:54 +0800 |
---|---|---|
committer | YiLi <li.yi101@zte.com.cn> | 2018-02-09 10:37:19 +0800 |
commit | 37630177bc70661ce86cb12e9af34e8c09893f4d (patch) | |
tree | 83ccafe8389fd8fc504c628a67b3618ac8c222bf /holmes-actions/src | |
parent | a55c700bc3aaab61731d509e4f4c85f40d6330e6 (diff) |
Test Replace Jackson with GSON
Change-Id: Id4c2ed81e7dbd5546940ff50363555336d55b75b
Issue-ID: HOLMES-115
Signed-off-by: YiLi <li.yi101@zte.com.cn>
Diffstat (limited to 'holmes-actions/src')
10 files changed, 73 insertions, 67 deletions
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/AlarmsCorrelation.java b/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/AlarmsCorrelation.java index ee32be2..4cbd6bf 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/AlarmsCorrelation.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/AlarmsCorrelation.java @@ -15,7 +15,7 @@ */
package org.onap.holmes.common.api.entity;
-import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.gson.annotations.SerializedName;
import java.util.Date;
import lombok.Data;
import lombok.NoArgsConstructor;
@@ -24,30 +24,27 @@ import lombok.NoArgsConstructor; @NoArgsConstructor
public class AlarmsCorrelation {
- @JsonProperty(value = "ruleid")
+ @SerializedName(value = "ruleid")
private String ruleId;
- @JsonProperty(value = "ruleinfo")
+ @SerializedName(value = "ruleinfo")
private String ruleInfo;
- @JsonProperty(value = "resulttype")
+ @SerializedName(value = "resulttype")
private byte resultType;
- @JsonProperty(value = "createtime")
+ @SerializedName(value = "createtime")
private Date createTime;
- @JsonProperty(value = "parentalarmid")
+ @SerializedName(value = "parentalarmid")
private long parentAlarmId;
- @JsonProperty(value = "childalarmid")
+ @SerializedName(value = "childalarmid")
private long childAlarmId;
- @JsonProperty(defaultValue = "-1")
private long reserve1 = -1;
- @JsonProperty(defaultValue = "-1")
private long reserve2 = -1;
- @JsonProperty(defaultValue = "-1")
private long reserve3 = -1;
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/CorrelationResult.java b/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/CorrelationResult.java index 6802696..f3e2562 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/CorrelationResult.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/CorrelationResult.java @@ -15,14 +15,12 @@ */
package org.onap.holmes.common.api.entity;
-import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
-import org.onap.holmes.common.api.stat.Alarm;
-
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
+import org.onap.holmes.common.api.stat.Alarm;
@Setter
@Getter
@@ -30,15 +28,11 @@ import lombok.ToString; @ToString
public class CorrelationResult implements Serializable{
- @JsonProperty
private String ruleId;
- @JsonProperty
private long createTimeL;
- @JsonProperty
private byte resultType;
- @JsonProperty
private Alarm[] affectedAlarms;
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/CorrelationRule.java b/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/CorrelationRule.java index eb9273a..0343dfa 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/CorrelationRule.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/CorrelationRule.java @@ -15,7 +15,7 @@ */
package org.onap.holmes.common.api.entity;
-import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.gson.annotations.SerializedName;
import java.util.Date;
import java.util.Properties;
import lombok.Getter;
@@ -25,35 +25,36 @@ import lombok.Setter; @Setter
public class CorrelationRule {
- @JsonProperty(value = "ruleid")
+ @SerializedName(value = "ruleid")
private String rid;
- @JsonProperty(value = "rulename")
+ @SerializedName(value = "rulename")
private String name;
- @JsonProperty
+
private String description;
- @JsonProperty
+
private int enabled;
- @JsonProperty
+
private long templateID;
+
private String engineID;
- @JsonProperty
+
private String engineType;
- @JsonProperty
+
private String creator;
- @JsonProperty
+
private String modifier;
- @JsonProperty
+
private Properties params;
- @JsonProperty
+
private String content;
- @JsonProperty
+
private String vendor;
- @JsonProperty(value = "createtime")
+ @SerializedName(value = "createtime")
private Date createTime;
- @JsonProperty(value = "updatetime")
+ @SerializedName(value = "updatetime")
private Date updateTime;
- @JsonProperty(value = "package")
+ @SerializedName(value = "package")
private String packageName;
- @JsonProperty(value = "controlloopname")
+ @SerializedName(value = "controlloopname")
private String closedControlLoopName;
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/ServiceNode.java b/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/ServiceNode.java index d456910..2c2b793 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/ServiceNode.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/ServiceNode.java @@ -17,7 +17,6 @@ package org.onap.holmes.common.api.entity;
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
@@ -27,7 +26,6 @@ import lombok.Setter; @Data
@NoArgsConstructor
@AllArgsConstructor
-@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
public class ServiceNode {
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/ServiceRegisterEntity.java b/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/ServiceRegisterEntity.java index 7804082..f5dca01 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/ServiceRegisterEntity.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/api/entity/ServiceRegisterEntity.java @@ -16,7 +16,6 @@ package org.onap.holmes.common.api.entity;
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
@@ -28,7 +27,6 @@ import lombok.Setter; @Data
@NoArgsConstructor
@AllArgsConstructor
-@JsonIgnoreProperties(ignoreUnknown = true)
@Setter
@Getter
public class ServiceRegisterEntity {
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java index 65058c5..240f09b 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java @@ -15,7 +15,6 @@ */ package org.onap.holmes.common.dmaap; -import com.fasterxml.jackson.core.JsonProcessingException; import java.util.Optional; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -31,7 +30,7 @@ import org.onap.holmes.common.dcae.DcaeConfigurationsCache; import org.onap.holmes.common.dmaap.entity.PolicyMsg; import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS; import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.JacksonUtil; +import org.onap.holmes.common.utils.GsonUtil; @Slf4j @Service @@ -48,11 +47,9 @@ public class DmaapService { publisher.setUrl(DcaeConfigurationsCache.getPubSecInfo(dmaapConfigKey).getDmaapInfo() .getTopicUrl()); publisher.publish(policyMsg); - log.info("send policyMsg: " + JacksonUtil.beanToJson(policyMsg)); + log.info("send policyMsg: " + GsonUtil.beanToJson(policyMsg)); } catch (CorrelationException e) { log.error("Failed to publish the control loop event to DMaaP", e); - } catch (JsonProcessingException e) { - log.info("Failed to convert the control loop event to json"); } catch (NullPointerException e) { log.error("DMaaP configurations do not exist!"); } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java index 6f6e8f3..452aaf8 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java @@ -16,20 +16,39 @@ package org.onap.holmes.common.utils; 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.reflect.TypeToken; +import java.lang.reflect.Type; import java.util.List; import java.util.Map; public class GsonUtil { private static Gson gson = null; static { - if (gson == null) {; - gson = new Gson(); + 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; + } + } + }) + .create(); } } private GsonUtil() { } + + public static String beanToJson(Object object) { String gsonString = null; if (gson != null) { diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/Md5Util.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/Md5Util.java index 1c06b52..c630239 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/Md5Util.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/Md5Util.java @@ -16,12 +16,10 @@ package org.onap.holmes.common.utils; -import com.fasterxml.jackson.core.JsonProcessingException; import com.google.common.hash.HashCode; import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing; import java.nio.charset.Charset; -import net.sf.json.JSONObject; public class Md5Util { @@ -38,8 +36,8 @@ public class Md5Util { return hash.toString(); } - public static String md5(Object data) throws JsonProcessingException { - String actualData = data == null ? "{}" : JacksonUtil.beanToJson(data); + public static String md5(Object data) { + String actualData = data == null ? "{}" : GsonUtil.beanToJson(data); return md5(actualData); } } diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/JacksonUtilTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/JacksonUtilTest.java index 5f90c2d..6fa9c11 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/JacksonUtilTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/JacksonUtilTest.java @@ -20,6 +20,15 @@ package org.onap.holmes.common.utils; import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
+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.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+import java.lang.reflect.Type;
import org.junit.Test;
import org.onap.holmes.common.utils.bean.TestBean;
@@ -29,21 +38,22 @@ public class JacksonUtilTest { public void testBeanToJson() throws Exception {
TestBean o = new TestBean();
o.setId("id");
- String result = JacksonUtil.beanToJson(o);
+ String result = GsonUtil.beanToJson(o);
assertThat("{\"id\":\"id\"}", equalTo(result));
}
@Test
public void jsonToBean_json_null() throws Exception {
String jsonNull = null;
- TestBean testBean = JacksonUtil.jsonToBean(jsonNull, TestBean.class);
- assertThat(testBean.getId(), equalTo(null));
+ TestBean testBean = GsonUtil.jsonToBean(jsonNull, TestBean.class);
+ assertThat(testBean, equalTo(null));
}
@Test
public void jsonToBean_json_normal() throws Exception {
String json = "{\"id\":\"id\"}";
- TestBean testBean = JacksonUtil.jsonToBean(json, TestBean.class);
+ TestBean testBean = GsonUtil.jsonToBean(json, TestBean.class);
assertThat(testBean.getId(), equalTo("id"));
}
+
}
\ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/Md5UtilTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/Md5UtilTest.java index 041b09d..af81a11 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/Md5UtilTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/Md5UtilTest.java @@ -26,8 +26,9 @@ import org.onap.holmes.common.dcae.entity.DcaeConfigurations; import org.onap.holmes.common.dcae.entity.SecurityInfo; public class Md5UtilTest { + @Test - public void testMd5NormalDiff(){ + public void testMd5NormalDiff() { String contents1 = "contents1"; String contents2 = "contents2"; @@ -35,7 +36,7 @@ public class Md5UtilTest { } @Test - public void testMd5NormalSame(){ + public void testMd5NormalSame() { String contents1 = "contents"; String contents2 = "contents"; @@ -43,7 +44,7 @@ public class Md5UtilTest { } @Test - public void testMd5Null(){ + public void testMd5Null() { String contents1 = null; String contents2 = null; @@ -51,32 +52,25 @@ public class Md5UtilTest { } @Test - public void testMd5ObjDiff(){ + public void testMd5ObjDiff() { DcaeConfigurations config1 = new DcaeConfigurations(); DcaeConfigurations config2 = new DcaeConfigurations(); config1.addPubSecInfo("config1", new SecurityInfo()); config2.addPubSecInfo("config2", new SecurityInfo()); + assertThat(Md5Util.md5(config1), not(equalTo(Md5Util.md5(config2)))); - try { - assertThat(Md5Util.md5(config1), not(equalTo(Md5Util.md5(config2)))); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } } @Test - public void testMd5ObjSame(){ + public void testMd5ObjSame() { DcaeConfigurations config1 = new DcaeConfigurations(); DcaeConfigurations config2 = new DcaeConfigurations(); config1.addPubSecInfo("config", new SecurityInfo()); config2.addPubSecInfo("config", new SecurityInfo()); - try { - assertThat(Md5Util.md5(config1), equalTo(Md5Util.md5(config2))); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } + assertThat(Md5Util.md5(config1), equalTo(Md5Util.md5(config2))); + } }
\ No newline at end of file |