diff options
author | Guangrong Fu <fu.guangrong@zte.com.cn> | 2021-12-03 17:32:42 +0800 |
---|---|---|
committer | Guangrong Fu <fu.guangrong@zte.com.cn> | 2021-12-03 19:09:42 +0800 |
commit | 64e336dadd4eb89dd0a8caf6bf62f0e86f4732a6 (patch) | |
tree | 83fbde5f57fbe848e30e8ec1f34570700da24be4 | |
parent | c03d170278c22f9c7ba271f1783811f3b0f5bda8 (diff) |
bugfix - date converter error for gson
Issue-ID: HOLMES-492
Signed-off-by: Guangrong Fu <fu.guangrong@zte.com.cn>
Change-Id: I995ac283a00edb35ff5a08adc87acfd2b9043fb1
5 files changed, 81 insertions, 39 deletions
diff --git a/holmes-actions/pom.xml b/holmes-actions/pom.xml index bab5cef..c553b53 100644 --- a/holmes-actions/pom.xml +++ b/holmes-actions/pom.xml @@ -12,7 +12,7 @@ <parent>
<groupId>org.onap.holmes.common</groupId>
<artifactId>holmes-common-parent</artifactId>
- <version>1.3.8-SNAPSHOT</version>
+ <version>1.3.9-SNAPSHOT</version>
</parent>
<name>holmes-common-service</name>
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 a5e6461..f5ad56c 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 @@ -1,12 +1,12 @@ /** - * Copyright 2018-2020 ZTE Corporation. - * + * Copyright 2018-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. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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. @@ -15,13 +15,11 @@ */ package org.onap.holmes.common.utils; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonObject; +import com.google.gson.*; import com.google.gson.reflect.TypeToken; import org.apache.commons.lang3.StringUtils; +import java.util.Date; import java.util.List; import java.util.Map; @@ -38,6 +36,15 @@ public class GsonUtil { return 0; } }) + .registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (jsonElement, type, jsonDeserializationContext) -> { + try { + return jsonElement == null ? null : new Date(jsonElement.getAsLong()); + } catch (NumberFormatException e) { + return null; + } + }) + .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (date, type, jsonSerializationContext) + -> date == null ? null : new JsonPrimitive(date.getTime())) .create(); } } @@ -74,9 +81,9 @@ public class GsonUtil { List<Map<String, T>> list = null; if (gson != null) { list = gson.fromJson(gsonString, - TypeToken.getParameterized(List.class, - TypeToken.getParameterized(Map.class, String.class, cls).getType() - ).getType() + TypeToken.getParameterized(List.class, + TypeToken.getParameterized(Map.class, String.class, cls).getType() + ).getType() ); } return list; diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/CommonUtilsTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/CommonUtilsTest.java index 15cc44d..6e02055 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/CommonUtilsTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/CommonUtilsTest.java @@ -22,73 +22,73 @@ import static org.junit.Assert.assertThat; public class CommonUtilsTest { @Test - public void isHttpsEnabled_normal_true() throws Exception { + public void isHttpsEnabled_normal_true() { System.setProperty("ENABLE_ENCRYPT", "true"); assertThat(CommonUtils.isHttpsEnabled(), is(true)); } @Test - public void isHttpsEnabled_normal_false() throws Exception { + public void isHttpsEnabled_normal_false() { System.setProperty("ENABLE_ENCRYPT", "false"); assertThat(CommonUtils.isHttpsEnabled(), is(false)); } @Test - public void isHttpsEnabled_invalid_input() throws Exception { + public void isHttpsEnabled_invalid_input() { System.setProperty("ENABLE_ENCRYPT", "whatever"); assertThat(CommonUtils.isHttpsEnabled(), is(false)); } @Test - public void getEnv() throws Exception { + public void getEnv() { System.setProperty("TEST", "COMMON_UTILS"); assertThat(CommonUtils.getEnv("TEST"), equalTo("COMMON_UTILS")); } @Test - public void isValidIpAddress_with_port() throws Exception { + public void isValidIpAddress_with_port() { boolean res = CommonUtils.isIpAddress("10.75.13.21:90"); assertThat(res, is(true)); } @Test - public void isValidIpAddress_without_port() throws Exception { + public void isValidIpAddress_without_port() { boolean res = CommonUtils.isIpAddress("10.75.13.21"); assertThat(res, is(true)); } @Test - public void isValidIpAddress_with_port_with_http_prefix() throws Exception { + public void isValidIpAddress_with_port_with_http_prefix() { boolean res = CommonUtils.isIpAddress("http://10.75.13.21:90"); assertThat(res, is(true)); } @Test - public void isValidIpAddress_without_port_with_https_prefix() throws Exception { + public void isValidIpAddress_without_port_with_https_prefix() { boolean res = CommonUtils.isIpAddress("https://10.75.13.21"); assertThat(res, is(true)); } @Test - public void isValidIpAddress_invalid_ip_without_port() throws Exception { + public void isValidIpAddress_invalid_ip_without_port() { boolean res = CommonUtils.isIpAddress("holmes-rule-mgmt"); assertThat(res, is(false)); } @Test - public void isValidIpAddress_invalid_ip_with_port() throws Exception { + public void isValidIpAddress_invalid_ip_with_port() { boolean res = CommonUtils.isIpAddress("holmes-rule-mgmt:443"); assertThat(res, is(false)); } @Test - public void isValidIpAddress_invalid_ip_without_port_with_http_prefix() throws Exception { + public void isValidIpAddress_invalid_ip_without_port_with_http_prefix() { boolean res = CommonUtils.isIpAddress("http://holmes-rule-mgmt"); assertThat(res, is(false)); } @Test - public void isValidIpAddress_invalid_ip_with_port_with_https_prefix() throws Exception { + public void isValidIpAddress_invalid_ip_with_port_with_https_prefix() { boolean res = CommonUtils.isIpAddress("https://holmes-rule-mgmt:443"); assertThat(res, is(false)); } diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/GsonUtilTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/GsonUtilTest.java index ebba1d2..7a0e4d7 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/GsonUtilTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/GsonUtilTest.java @@ -16,11 +16,11 @@ package org.onap.holmes.common.utils; -import com.google.gson.Gson; -import com.google.gson.JsonParser; +import com.google.gson.*; import org.junit.Test; import java.util.Arrays; +import java.util.Date; import java.util.List; import java.util.Map; @@ -30,9 +30,16 @@ 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(); + private final TestBean bean1; + private final TestBean bean2; + private final Gson gson = buildGson(); + private Date date; + + public GsonUtilTest() { + date = new Date(); + bean1 = new TestBean("onap1", 10, 10f, 10d, date); + bean2 = new TestBean("onap2", 20, 20f, 20d, date); + } @Test public void beanToJson() { @@ -49,6 +56,7 @@ public class GsonUtilTest { assertThat(expected.getInteger(), equalTo(actual.getInteger())); assertThat(expected.getaDouble(), equalTo(actual.getaDouble())); assertThat(expected.getaFloat(), equalTo(actual.getaFloat())); + assertThat(expected.getaDate(), equalTo(actual.getaDate())); } @Test @@ -64,19 +72,20 @@ public class GsonUtilTest { @Test public void jsonToListMaps() { + long timestamp = date.getTime(); 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); + String.format("[{\"onap1\":{\"string\":\"onap1\",\"integer\":10,\"aFloat\":10.0,\"aDouble\":10.0,\"aDate\": %d}},", timestamp) + + String.format("{\"onap2\":{\"string\":\"onap2\",\"integer\":20,\"aFloat\":20.0,\"aDouble\":20.0,\"aDate\": \"%s\"}}]", timestamp), 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))); + assertThat(actual.get(0).get("onap1"), equalTo(new TestBean("onap1", 10, 10f, 10d, date))); + assertThat(actual.get(1).get("onap2"), equalTo(new TestBean("onap2", 20, 20f, 20d, date))); } @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))); + .jsonToMap(String.format("{\"onap1\":{\"string\":\"onap1\",\"integer\":10,\"aFloat\":10.0,\"aDouble\":10.0,\"aDate\":%d}}",date.getTime()), TestBean.class); + assertThat(actual.get("onap1"), equalTo(new TestBean("onap1", 10, 10f, 10d, date))); } @Test @@ -96,6 +105,27 @@ public class GsonUtilTest { assertThat(10, is(GsonUtil.getAsInt(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"integer"))); } + + private Gson buildGson() { + return new GsonBuilder() + .registerTypeAdapter(Integer.class, (JsonDeserializer<Integer>) (json, typeOfT, context) -> { + try { + return json.getAsInt(); + } catch (NumberFormatException e) { + return 0; + } + }) + .registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (jsonElement, type, jsonDeserializationContext) -> { + try { + return jsonElement == null ? null : new Date(jsonElement.getAsLong()); + } catch (NumberFormatException e) { + return null; + } + }) + .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (date, type, jsonSerializationContext) + -> date == null ? null : new JsonPrimitive(date.getTime())) + .create(); + } } class TestBean { @@ -103,12 +133,14 @@ class TestBean { private int integer; private float aFloat; private double aDouble; + private Date aDate; - public TestBean(String string, int integer, float aFloat, double aDouble) { + public TestBean(String string, int integer, float aFloat, double aDouble, Date aDate) { this.string = string; this.integer = integer; this.aFloat = aFloat; this.aDouble = aDouble; + this.aDate = aDate; } public String getString() { @@ -127,6 +159,8 @@ class TestBean { return aDouble; } + public Date getaDate(){ return aDate;} + @Override public boolean equals(Object o) { if (o == null || ! (o instanceof TestBean)) { @@ -136,7 +170,8 @@ class TestBean { return string.equals(((TestBean) o).string) && integer == ((TestBean) o).integer && aDouble == ((TestBean) o).aDouble - && aFloat == ((TestBean) o).aFloat; + && aFloat == ((TestBean) o).aFloat + && ((aDate == null && ((TestBean)o).aDate == null) || aDate.equals(((TestBean)o).aDate)); } @Override @@ -20,7 +20,7 @@ <artifactId>holmes-common-parent</artifactId>
<packaging>pom</packaging>
- <version>1.3.8-SNAPSHOT</version>
+ <version>1.3.9-SNAPSHOT</version>
<name>holmes-common</name>
<modules>
<module>holmes-actions</module>
|