bugfix - date converter error for gson
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / utils / GsonUtilTest.java
1 /**
2  * Copyright 2020 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.common.utils;
18
19 import com.google.gson.*;
20 import org.junit.Test;
21
22 import java.util.Arrays;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Map;
26
27 import static org.hamcrest.core.Is.is;
28 import static org.hamcrest.core.IsEqual.equalTo;
29 import static org.junit.Assert.assertThat;
30
31 public class GsonUtilTest {
32
33     private final TestBean bean1;
34     private final TestBean bean2;
35     private final Gson gson = buildGson();
36     private Date date;
37
38     public GsonUtilTest() {
39         date = new Date();
40         bean1 = new TestBean("onap1", 10, 10f, 10d, date);
41         bean2 = new TestBean("onap2", 20, 20f, 20d, date);
42     }
43
44     @Test
45     public void beanToJson() {
46         String expected = gson.toJson(bean1);
47         String actual = GsonUtil.beanToJson(bean1);
48         assertThat(actual, equalTo(expected));
49     }
50
51     @Test
52     public void jsonToBean() {
53         TestBean expected = bean1;
54         TestBean actual = GsonUtil.jsonToBean(gson.toJson(expected), TestBean.class);
55         assertThat(expected.getString(), equalTo(actual.getString()));
56         assertThat(expected.getInteger(), equalTo(actual.getInteger()));
57         assertThat(expected.getaDouble(), equalTo(actual.getaDouble()));
58         assertThat(expected.getaFloat(), equalTo(actual.getaFloat()));
59         assertThat(expected.getaDate(), equalTo(actual.getaDate()));
60     }
61
62     @Test
63     public void jsonToList() {
64         List<TestBean> expected = Arrays.asList( bean1, bean2);
65         List<TestBean> actual = GsonUtil.jsonToList(gson.toJson(expected), TestBean.class);
66
67         assertThat(expected.size(), equalTo(actual.size()));
68         for (TestBean tb : expected) {
69             assertThat(actual.contains(tb), is(true));
70         }
71     }
72
73     @Test
74     public void jsonToListMaps() {
75         long timestamp = date.getTime();
76         List<Map<String, TestBean>> actual = GsonUtil.jsonToListMaps(
77                 String.format("[{\"onap1\":{\"string\":\"onap1\",\"integer\":10,\"aFloat\":10.0,\"aDouble\":10.0,\"aDate\": %d}},", timestamp)
78                  + String.format("{\"onap2\":{\"string\":\"onap2\",\"integer\":20,\"aFloat\":20.0,\"aDouble\":20.0,\"aDate\": \"%s\"}}]", timestamp), TestBean.class);
79
80         assertThat(actual.get(0).get("onap1"), equalTo(new TestBean("onap1", 10, 10f, 10d, date)));
81         assertThat(actual.get(1).get("onap2"), equalTo(new TestBean("onap2", 20, 20f, 20d, date)));
82     }
83
84     @Test
85     public void jsonToMap() {
86         Map<String, TestBean> actual = GsonUtil
87                 .jsonToMap(String.format("{\"onap1\":{\"string\":\"onap1\",\"integer\":10,\"aFloat\":10.0,\"aDouble\":10.0,\"aDate\":%d}}",date.getTime()), TestBean.class);
88         assertThat(actual.get("onap1"), equalTo(new TestBean("onap1", 10, 10f, 10d, date)));
89     }
90
91     @Test
92     public void getAsString() {
93         assertThat("onap1",
94                 equalTo(GsonUtil.getAsString(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"string")));
95     }
96
97     @Test
98     public void getAsLong() {
99         assertThat(10L,
100                 is(GsonUtil.getAsLong(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"integer")));
101     }
102
103     @Test
104     public void getAsInt() {
105         assertThat(10,
106                 is(GsonUtil.getAsInt(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"integer")));
107     }
108
109     private Gson buildGson() {
110         return new GsonBuilder()
111                 .registerTypeAdapter(Integer.class, (JsonDeserializer<Integer>) (json, typeOfT, context) -> {
112                     try {
113                         return json.getAsInt();
114                     } catch (NumberFormatException e) {
115                         return 0;
116                     }
117                 })
118                 .registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (jsonElement, type, jsonDeserializationContext) -> {
119                     try {
120                         return jsonElement == null ? null : new Date(jsonElement.getAsLong());
121                     } catch (NumberFormatException e) {
122                         return null;
123                     }
124                 })
125                 .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (date, type, jsonSerializationContext)
126                         -> date == null ? null : new JsonPrimitive(date.getTime()))
127                 .create();
128     }
129 }
130
131 class TestBean {
132     private String string;
133     private int integer;
134     private float aFloat;
135     private double aDouble;
136     private Date aDate;
137
138     public TestBean(String string, int integer, float aFloat, double aDouble, Date aDate) {
139         this.string = string;
140         this.integer = integer;
141         this.aFloat = aFloat;
142         this.aDouble = aDouble;
143         this.aDate = aDate;
144     }
145
146     public String getString() {
147         return string;
148     }
149
150     public int getInteger() {
151         return integer;
152     }
153
154     public float getaFloat() {
155         return aFloat;
156     }
157
158     public double getaDouble() {
159         return aDouble;
160     }
161
162     public Date getaDate(){ return aDate;}
163
164     @Override
165     public boolean equals(Object o) {
166         if (o == null || ! (o instanceof TestBean)) {
167             return false;
168         }
169
170         return  string.equals(((TestBean) o).string)
171                 && integer == ((TestBean) o).integer
172                 && aDouble == ((TestBean) o).aDouble
173                 && aFloat == ((TestBean) o).aFloat
174                 && ((aDate == null && ((TestBean)o).aDate == null) || aDate.equals(((TestBean)o).aDate));
175     }
176
177     @Override
178     public int hashCode() {
179         return string.hashCode();
180     }
181 }