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.Gson;
20 import com.google.gson.JsonParser;
21 import org.junit.Test;
23 import java.util.Arrays;
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 = new TestBean("onap1", 10, 10f, 10d);
34 private final TestBean bean2 = new TestBean("onap2", 20, 20f, 20d);
35 private final Gson gson = new Gson();
38 public void beanToJson() {
39 String expected = gson.toJson(bean1);
40 String actual = GsonUtil.beanToJson(bean1);
41 assertThat(actual, equalTo(expected));
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()));
55 public void jsonToList() {
56 List<TestBean> expected = Arrays.asList( bean1, bean2);
57 List<TestBean> actual = GsonUtil.jsonToList(gson.toJson(expected), TestBean.class);
59 assertThat(expected.size(), equalTo(actual.size()));
60 for (TestBean tb : expected) {
61 assertThat(actual.contains(tb), is(true));
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);
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)));
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)));
83 public void getAsString() {
85 equalTo(GsonUtil.getAsString(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"string")));
89 public void getAsLong() {
91 is(GsonUtil.getAsLong(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"integer")));
95 public void getAsInt() {
97 is(GsonUtil.getAsInt(JsonParser.parseString(GsonUtil.beanToJson(bean1)).getAsJsonObject(),"integer")));
102 private String string;
104 private float aFloat;
105 private double aDouble;
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;
114 public String getString() {
118 public int getInteger() {
122 public float getaFloat() {
126 public double getaDouble() {
131 public boolean equals(Object o) {
132 if (o == null || ! (o instanceof TestBean)) {
136 return string.equals(((TestBean) o).string)
137 && integer == ((TestBean) o).integer
138 && aDouble == ((TestBean) o).aDouble
139 && aFloat == ((TestBean) o).aFloat;
143 public int hashCode() {
144 return string.hashCode();