2 * Copyright 2020 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.holmes.common.utils;
19 import com.google.gson.*;
20 import org.junit.Test;
22 import java.util.Arrays;
23 import java.util.Date;
24 import java.util.List;
27 import static org.hamcrest.core.Is.is;
28 import static org.hamcrest.core.IsEqual.equalTo;
29 import static org.junit.Assert.assertThat;
31 public class GsonUtilTest {
33 private final TestBean bean1;
34 private final TestBean bean2;
35 private final Gson gson = buildGson();
38 public GsonUtilTest() {
40 bean1 = new TestBean("onap1", 10, 10f, 10d, date);
41 bean2 = new TestBean("onap2", 20, 20f, 20d, date);
45 public void beanToJson() {
46 String expected = gson.toJson(bean1);
47 String actual = GsonUtil.beanToJson(bean1);
48 assertThat(actual, equalTo(expected));
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()));
63 public void jsonToList() {
64 List<TestBean> expected = Arrays.asList( bean1, bean2);
65 List<TestBean> actual = GsonUtil.jsonToList(gson.toJson(expected), TestBean.class);
67 assertThat(expected.size(), equalTo(actual.size()));
68 for (TestBean tb : expected) {
69 assertThat(actual.contains(tb), is(true));
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);
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)));
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)));
92 public void getAsString() {
94 equalTo(GsonUtil.getAsString(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"string")));
98 public void getAsLong() {
100 is(GsonUtil.getAsLong(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"integer")));
104 public void getAsInt() {
106 is(GsonUtil.getAsInt(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"integer")));
109 private Gson buildGson() {
110 return new GsonBuilder()
111 .registerTypeAdapter(Integer.class, (JsonDeserializer<Integer>) (json, typeOfT, context) -> {
113 return json.getAsInt();
114 } catch (NumberFormatException e) {
118 .registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (jsonElement, type, jsonDeserializationContext) -> {
120 return jsonElement == null ? null : new Date(jsonElement.getAsLong());
121 } catch (NumberFormatException e) {
125 .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (date, type, jsonSerializationContext)
126 -> date == null ? null : new JsonPrimitive(date.getTime()))
132 private String string;
134 private float aFloat;
135 private double aDouble;
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;
146 public String getString() {
150 public int getInteger() {
154 public float getaFloat() {
158 public double getaDouble() {
162 public Date getaDate(){ return aDate;}
165 public boolean equals(Object o) {
166 if (o == null || ! (o instanceof TestBean)) {
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));
178 public int hashCode() {
179 return string.hashCode();