summaryrefslogtreecommitdiffstats
path: root/gson/src/test/java/org
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-02-18 17:15:07 -0500
committerJim Hahn <jrh3@att.com>2021-02-18 17:24:26 -0500
commit758cd2e691950843415515ba6df2295679bd938b (patch)
tree1c3acdcf18a04f655b6e642fc80a7d8630934f24 /gson/src/test/java/org
parent00ee2d5b4f1e847d8e272c79f66230aa30cb11d6 (diff)
Remove duplicate code from type adapters
Extracted a common superclass, StringTypeAdapter, out of the gson type adapter classes. Issue-ID: POLICY-2914 Change-Id: I4bbd2a6e8372e7f42c4952ba9ff03eed97473fb2 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'gson/src/test/java/org')
-rw-r--r--gson/src/test/java/org/onap/policy/common/gson/InstantTypeAdapterTest.java12
-rw-r--r--gson/src/test/java/org/onap/policy/common/gson/StringTypeAdapterTest.java94
-rw-r--r--gson/src/test/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapterTest.java12
3 files changed, 96 insertions, 22 deletions
diff --git a/gson/src/test/java/org/onap/policy/common/gson/InstantTypeAdapterTest.java b/gson/src/test/java/org/onap/policy/common/gson/InstantTypeAdapterTest.java
index 97219d0d..68f54ed8 100644
--- a/gson/src/test/java/org/onap/policy/common/gson/InstantTypeAdapterTest.java
+++ b/gson/src/test/java/org/onap/policy/common/gson/InstantTypeAdapterTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -53,16 +53,6 @@ public class InstantTypeAdapterTest {
String json2 = json.replace("2020", "invalid-date");
assertThatThrownBy(() -> gson.fromJson(json2, InterestingFields.class)).isInstanceOf(JsonParseException.class)
.hasMessageContaining("invalid date");
-
- // null output
- data.instant = null;
- json = gson.toJson(data);
- data2 = gson.fromJson(json, InterestingFields.class);
- assertEquals(data.toString(), data2.toString());
-
- // null input
- data2 = gson.fromJson("{\"instant\":null}", InterestingFields.class);
- assertEquals(data.toString(), data2.toString());
}
diff --git a/gson/src/test/java/org/onap/policy/common/gson/StringTypeAdapterTest.java b/gson/src/test/java/org/onap/policy/common/gson/StringTypeAdapterTest.java
new file mode 100644
index 00000000..f35677cd
--- /dev/null
+++ b/gson/src/test/java/org/onap/policy/common/gson/StringTypeAdapterTest.java
@@ -0,0 +1,94 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.gson;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonParseException;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.ToString;
+import org.junit.Test;
+
+public class StringTypeAdapterTest {
+ private static Gson gson = new GsonBuilder().registerTypeAdapter(MyData.class, new MyAdapter()).create();
+ private static final int TEST_NUM1 = 10;
+ private static final int TEST_NUM3 = 30;
+
+ @Test
+ public void test() {
+ InterestingFields data = new InterestingFields();
+ data.data1 = new MyData(TEST_NUM1);
+ data.data2 = null;
+ data.data3 = new MyData(TEST_NUM3);
+
+ String json = gson.toJson(data);
+
+ // instant should be encoded as a number, without quotes
+ assertThat(json).contains("10", "30");
+
+ InterestingFields data2 = gson.fromJson(json, InterestingFields.class);
+ assertEquals(data.toString(), data2.toString());
+
+ // try when the string is invalid
+ String json2 = json.replace("30", "invalid-value");
+ assertThatThrownBy(() -> gson.fromJson(json2, InterestingFields.class)).isInstanceOf(JsonParseException.class)
+ .hasMessageContaining("invalid data");
+
+ // null output
+ data = new InterestingFields();
+ json = gson.toJson(data);
+ data2 = gson.fromJson(json, InterestingFields.class);
+ assertEquals(data.toString(), data2.toString());
+
+ // null input
+ data2 = gson.fromJson("{\"data1\":null, \"data1\":null, \"data1\":null}", InterestingFields.class);
+ assertEquals(data.toString(), data2.toString());
+
+ // empty input
+ data2 = gson.fromJson("{}", InterestingFields.class);
+ assertEquals(data.toString(), data2.toString());
+ }
+
+ @Getter
+ @ToString
+ @AllArgsConstructor
+ private static class MyData {
+ private int num;
+ }
+
+ @ToString
+ private static class InterestingFields {
+ private MyData data1;
+ private MyData data2;
+ private MyData data3;
+ }
+
+ private static class MyAdapter extends StringTypeAdapter<MyData> {
+ public MyAdapter() {
+ super("data", string -> new MyData(Integer.parseInt(string)), data -> String.valueOf(data.num));
+ }
+ }
+}
diff --git a/gson/src/test/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapterTest.java b/gson/src/test/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapterTest.java
index 766a979d..032533eb 100644
--- a/gson/src/test/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapterTest.java
+++ b/gson/src/test/java/org/onap/policy/common/gson/ZonedDateTimeTypeAdapterTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -53,16 +53,6 @@ public class ZonedDateTimeTypeAdapterTest {
String json2 = json.replace("2020", "invalid-date");
assertThatThrownBy(() -> gson.fromJson(json2, InterestingFields.class)).isInstanceOf(JsonParseException.class)
.hasMessageContaining("invalid date");
-
- // null output
- data.date = null;
- json = gson.toJson(data);
- data2 = gson.fromJson(json, InterestingFields.class);
- assertEquals(data.toString(), data2.toString());
-
- // null input
- data2 = gson.fromJson("{\"date\":null}", InterestingFields.class);
- assertEquals(data.toString(), data2.toString());
}