Added Some Tools to GsonUtil
[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.Gson;
20 import com.google.gson.JsonParser;
21 import org.junit.Test;
22
23 import java.util.Arrays;
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 = new TestBean("onap1", 10, 10f, 10d);
34     private final TestBean bean2 = new TestBean("onap2", 20, 20f, 20d);
35     private final Gson gson = new Gson();
36
37     @Test
38     public void beanToJson() {
39         String expected = gson.toJson(bean1);
40         String actual = GsonUtil.beanToJson(bean1);
41         assertThat(actual, equalTo(expected));
42     }
43
44     @Test
45     public void jsonToBean() {
46         TestBean expected = bean1;
47         TestBean actual = GsonUtil.jsonToBean(gson.toJson(expected), TestBean.class);
48         assertThat(expected.getString(), equalTo(actual.getString()));
49         assertThat(expected.getInteger(), equalTo(actual.getInteger()));
50         assertThat(expected.getaDouble(), equalTo(actual.getaDouble()));
51         assertThat(expected.getaFloat(), equalTo(actual.getaFloat()));
52     }
53
54     @Test
55     public void jsonToList() {
56         List<TestBean> expected = Arrays.asList( bean1, bean2);
57         List<TestBean> actual = GsonUtil.jsonToList(gson.toJson(expected), TestBean.class);
58
59         assertThat(expected.size(), equalTo(actual.size()));
60         for (TestBean tb : expected) {
61             assertThat(actual.contains(tb), is(true));
62         }
63     }
64
65     @Test
66     public void jsonToListMaps() {
67         List<Map<String, TestBean>> actual = GsonUtil.jsonToListMaps(
68                 "[{\"onap1\":{\"string\":\"onap1\",\"integer\":10,\"aFloat\":10.0,\"aDouble\":10.0}},"
69                  + "{\"onap2\":{\"string\":\"onap2\",\"integer\":20,\"aFloat\":20.0,\"aDouble\":20.0}}]", TestBean.class);
70
71         assertThat(actual.get(0).get("onap1"), equalTo(new TestBean("onap1", 10, 10f, 10d)));
72         assertThat(actual.get(1).get("onap2"), equalTo(new TestBean("onap2", 20, 20f, 20d)));
73     }
74
75     @Test
76     public void jsonToMap() {
77         Map<String, TestBean> actual = GsonUtil
78                 .jsonToMap("{\"onap1\":{\"string\":\"onap1\",\"integer\":10,\"aFloat\":10.0,\"aDouble\":10.0}}", TestBean.class);
79         assertThat(actual.get("onap1"), equalTo(new TestBean("onap1", 10, 10f, 10d)));
80     }
81
82     @Test
83     public void getAsString() {
84         assertThat("onap1",
85                 equalTo(GsonUtil.getAsString(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"string")));
86     }
87
88     @Test
89     public void getAsLong() {
90         assertThat(10L,
91                 is(GsonUtil.getAsLong(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"integer")));
92     }
93
94     @Test
95     public void getAsInt() {
96         assertThat(10,
97                 is(GsonUtil.getAsInt(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"integer")));
98     }
99 }
100
101 class TestBean {
102     private String string;
103     private int integer;
104     private float aFloat;
105     private double aDouble;
106
107     public TestBean(String string, int integer, float aFloat, double aDouble) {
108         this.string = string;
109         this.integer = integer;
110         this.aFloat = aFloat;
111         this.aDouble = aDouble;
112     }
113
114     public String getString() {
115         return string;
116     }
117
118     public int getInteger() {
119         return integer;
120     }
121
122     public float getaFloat() {
123         return aFloat;
124     }
125
126     public double getaDouble() {
127         return aDouble;
128     }
129
130     @Override
131     public boolean equals(Object o) {
132         if (o == null || ! (o instanceof TestBean)) {
133             return false;
134         }
135
136         return  string.equals(((TestBean) o).string)
137                 && integer == ((TestBean) o).integer
138                 && aDouble == ((TestBean) o).aDouble
139                 && aFloat == ((TestBean) o).aFloat;
140     }
141
142     @Override
143     public int hashCode() {
144         return string.hashCode();
145     }
146 }