diff options
Diffstat (limited to 'gson/src/test/java/org/onap/policy')
15 files changed, 443 insertions, 132 deletions
diff --git a/gson/src/test/java/org/onap/policy/common/gson/GsonMessageBodyHandlerTest.java b/gson/src/test/java/org/onap/policy/common/gson/GsonMessageBodyHandlerTest.java index c05a1e51..fc101430 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/GsonMessageBodyHandlerTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/GsonMessageBodyHandlerTest.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +26,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import jakarta.ws.rs.core.MediaType; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -36,7 +38,6 @@ import java.time.ZonedDateTime; import java.util.HashMap; import java.util.Map; import java.util.UUID; -import javax.ws.rs.core.MediaType; import lombok.ToString; import org.junit.Before; import org.junit.Test; @@ -196,6 +197,7 @@ public class GsonMessageBodyHandlerTest { } + @ToString public static class MyObject { private int id; @@ -206,11 +208,6 @@ public class GsonMessageBodyHandlerTest { public MyObject(int id) { this.id = id; } - - @Override - public String toString() { - return "MyObject [id=" + id + "]"; - } } private static class MyMap { 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/JacksonExclusionStrategyTest.java b/gson/src/test/java/org/onap/policy/common/gson/JacksonExclusionStrategyTest.java index 3ce16964..9d8d3495 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/JacksonExclusionStrategyTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/JacksonExclusionStrategyTest.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Modificaitons Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +32,7 @@ import com.google.gson.JsonElement; import java.lang.reflect.GenericArrayType; import java.util.LinkedList; import java.util.TreeMap; +import lombok.ToString; import org.junit.BeforeClass; import org.junit.Test; @@ -80,39 +82,29 @@ public class JacksonExclusionStrategyTest { @Test public void testIsManaged() { - assertTrue(JacksonExclusionStrategy.isManaged(Data.class)); - assertTrue(JacksonExclusionStrategy.isManaged(Intfc.class)); - assertTrue(JacksonExclusionStrategy.isManaged(com.google.gson.TypeAdapter.class)); - - // generic classes - assertFalse(JacksonExclusionStrategy.isManaged(new Data[0].getClass())); - assertFalse(JacksonExclusionStrategy.isManaged(Enum.class)); - assertFalse(JacksonExclusionStrategy.isManaged(boolean.class)); - assertFalse(JacksonExclusionStrategy.isManaged(byte.class)); - assertFalse(JacksonExclusionStrategy.isManaged(short.class)); - assertFalse(JacksonExclusionStrategy.isManaged(int.class)); - assertFalse(JacksonExclusionStrategy.isManaged(long.class)); - assertFalse(JacksonExclusionStrategy.isManaged(float.class)); - assertFalse(JacksonExclusionStrategy.isManaged(double.class)); - assertFalse(JacksonExclusionStrategy.isManaged(char.class)); - assertFalse(JacksonExclusionStrategy.isManaged(Boolean.class)); - assertFalse(JacksonExclusionStrategy.isManaged(Byte.class)); - assertFalse(JacksonExclusionStrategy.isManaged(Short.class)); - assertFalse(JacksonExclusionStrategy.isManaged(Integer.class)); - assertFalse(JacksonExclusionStrategy.isManaged(Long.class)); - assertFalse(JacksonExclusionStrategy.isManaged(Float.class)); - assertFalse(JacksonExclusionStrategy.isManaged(Double.class)); - assertFalse(JacksonExclusionStrategy.isManaged(Character.class)); - assertFalse(JacksonExclusionStrategy.isManaged(String.class)); - assertFalse(JacksonExclusionStrategy.isManaged(MyMap.class)); - assertFalse(JacksonExclusionStrategy.isManaged(MyList.class)); - assertFalse(JacksonExclusionStrategy.isManaged(MyJson.class)); - assertFalse(JacksonExclusionStrategy.isManaged(GenericArrayType.class)); + // these classes SHOULD be managed + Class<?>[] managed = {Data.class, Intfc.class, com.google.gson.TypeAdapter.class}; + + for (Class<?> clazz : managed) { + assertTrue(clazz.getName(), JacksonExclusionStrategy.isManaged(clazz)); + } + + // generic classes should NOT be managed + Class<?>[] unmanaged = { + new Data[0].getClass(), Enum.class, boolean.class, byte.class, short.class, int.class, + long.class, float.class, double.class, char.class, Boolean.class, Byte.class, Short.class, + Integer.class, Long.class, Float.class, Double.class, Character.class, String.class, + MyMap.class, MyList.class, MyJson.class, GenericArrayType.class}; + + for (Class<?> clazz : unmanaged) { + assertFalse(clazz.getName(), JacksonExclusionStrategy.isManaged(clazz)); + } } /** * Used to verify that no fields are exposed. */ + @ToString public static class Data { private int id; public String text; @@ -132,13 +124,9 @@ public class JacksonExclusionStrategyTest { public void setText(String text) { this.text = text; } - - @Override - public String toString() { - return "Data [id=" + id + ", text=" + text + "]"; - } } + @ToString(callSuper = true) public static class Derived extends Data { protected String value; @@ -149,11 +137,6 @@ public class JacksonExclusionStrategyTest { public void setValue(String value) { this.value = value; } - - @Override - public String toString() { - return "Derived [value=" + value + ", " + super.toString() + "]"; - } } /** @@ -193,6 +176,7 @@ public class JacksonExclusionStrategyTest { /** * Used to verify that JsonElements are not managed. */ + @SuppressWarnings("deprecation") public static class MyJson extends JsonElement { @Override public JsonElement deepCopy() { diff --git a/gson/src/test/java/org/onap/policy/common/gson/JacksonFieldAdapterFactoryTest.java b/gson/src/test/java/org/onap/policy/common/gson/JacksonFieldAdapterFactoryTest.java index bbeb1e26..dc62186b 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/JacksonFieldAdapterFactoryTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/JacksonFieldAdapterFactoryTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 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. @@ -31,6 +31,7 @@ import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; import java.util.ArrayList; import java.util.List; +import lombok.ToString; import org.junit.Test; import org.onap.policy.common.gson.annotation.GsonJsonIgnore; import org.onap.policy.common.gson.annotation.GsonJsonProperty; @@ -137,6 +138,7 @@ public class JacksonFieldAdapterFactoryTest { return text.replaceFirst("@\\w+", "@"); } + @ToString private static class Data { @GsonJsonProperty("my-id") private int id; @@ -155,21 +157,12 @@ public class JacksonFieldAdapterFactoryTest { public void setId(int id) { this.id = id; } - - @Override - public String toString() { - return "Data [id=" + id + ", text=" + text + "]"; - } } + @ToString(callSuper = true) private static class Derived extends Data { // not serialized private String unserialized; - - @Override - public String toString() { - return "Derived [unserialized=" + unserialized + ", toString()=" + super.toString() + "]"; - } } private static class DataList { diff --git a/gson/src/test/java/org/onap/policy/common/gson/JacksonHandlerTest.java b/gson/src/test/java/org/onap/policy/common/gson/JacksonHandlerTest.java index 18a6fc73..7131817d 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/JacksonHandlerTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/JacksonHandlerTest.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,13 +27,14 @@ import static org.junit.Assert.assertTrue; import com.google.gson.Gson; import com.google.gson.JsonObject; +import jakarta.ws.rs.core.MediaType; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.StringReader; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; -import javax.ws.rs.core.MediaType; +import lombok.ToString; import org.junit.Test; import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter; import org.onap.policy.common.gson.annotation.GsonJsonAnySetter; @@ -109,6 +111,7 @@ public class JacksonHandlerTest { /** * This class includes all policy-specific gson annotations. */ + @ToString public static class Data { protected int id; @@ -147,11 +150,6 @@ public class JacksonHandlerTest { props.put(name, value); } - - @Override - public String toString() { - return "Data [id=" + id + ", value=" + value + ", props=" + props + "]"; - } } private static class MyMap { diff --git a/gson/src/test/java/org/onap/policy/common/gson/JacksonMethodAdapterFactoryTest.java b/gson/src/test/java/org/onap/policy/common/gson/JacksonMethodAdapterFactoryTest.java index 6377420d..7afb0e5a 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/JacksonMethodAdapterFactoryTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/JacksonMethodAdapterFactoryTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 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. @@ -32,6 +32,7 @@ import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; import java.util.Map; import java.util.TreeMap; +import lombok.ToString; import org.junit.Test; import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter; import org.onap.policy.common.gson.annotation.GsonJsonAnySetter; @@ -117,6 +118,7 @@ public class JacksonMethodAdapterFactoryTest { assertEquals("{'id':500,'nested':{'value':'bye bye'}}".replace('\'', '"'), result); } + @ToString protected static class Data { private int id; private String text; @@ -142,13 +144,9 @@ public class JacksonMethodAdapterFactoryTest { public void unused(String text) { // do nothing } - - @Override - public String toString() { - return "Data [id=" + id + ", text=" + text + "]"; - } } + @ToString(callSuper = true) protected static class Derived extends Data { // overrides private field from Data @@ -174,11 +172,6 @@ public class JacksonMethodAdapterFactoryTest { map.put(key, value); } - - @Override - public String toString() { - return "Derived [text=" + text + ", map=" + map + ", toString()=" + super.toString() + "]"; - } } /** @@ -258,6 +251,7 @@ public class JacksonMethodAdapterFactoryTest { /** * Used to test serialization of non-static nested classes. */ + @ToString protected static class Container { private int id; private Nested nested; @@ -283,12 +277,8 @@ public class JacksonMethodAdapterFactoryTest { return nested; } - @Override - public String toString() { - return "Container [id=" + id + ", nested=" + nested + "]"; - } - + @ToString protected class Nested { private String value; @@ -299,11 +289,6 @@ public class JacksonMethodAdapterFactoryTest { public String getValue() { return value; } - - @Override - public String toString() { - return "Nested [value=" + value + "]"; - } } } } diff --git a/gson/src/test/java/org/onap/policy/common/gson/LocalDateTypeAdapterTest.java b/gson/src/test/java/org/onap/policy/common/gson/LocalDateTypeAdapterTest.java new file mode 100644 index 00000000..17acf5e6 --- /dev/null +++ b/gson/src/test/java/org/onap/policy/common/gson/LocalDateTypeAdapterTest.java @@ -0,0 +1,73 @@ +/*- + * ============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 java.time.LocalDate; +import lombok.ToString; +import org.junit.Test; + +public class LocalDateTypeAdapterTest { + private static Gson gson = + new GsonBuilder().registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter()).create(); + private static final String TEST_DATE = "2020-01-01"; + + @Test + public void test() { + InterestingFields data = new InterestingFields(); + data.date = LocalDate.parse(TEST_DATE); + + String json = gson.toJson(data); + + // instant should be encoded as a number, without quotes + assertThat(json).doesNotContain("year").contains(TEST_DATE); + + InterestingFields data2 = gson.fromJson(json, InterestingFields.class); + assertEquals(data.toString(), data2.toString()); + + // try when the date-time string is invalid + 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()); + } + + @ToString + private static class InterestingFields { + private LocalDate date; + } + +} diff --git a/gson/src/test/java/org/onap/policy/common/gson/OffsetDateTimeAdapterTest.java b/gson/src/test/java/org/onap/policy/common/gson/OffsetDateTimeAdapterTest.java new file mode 100644 index 00000000..a0bcb1b2 --- /dev/null +++ b/gson/src/test/java/org/onap/policy/common/gson/OffsetDateTimeAdapterTest.java @@ -0,0 +1,72 @@ +/*- + * ============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 java.time.OffsetDateTime; +import lombok.ToString; +import org.junit.Test; + +public class OffsetDateTimeAdapterTest { + private static Gson gson = + new GsonBuilder().registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeTypeAdapter()).create(); + private static final String TEST_DATE = "2020-01-01T12:00:00.999+05:00"; + + @Test + public void test() { + InterestingFields data = new InterestingFields(); + data.date = OffsetDateTime.parse(TEST_DATE); + + String json = gson.toJson(data); + + // instant should be encoded as a number, without quotes + assertThat(json).doesNotContain("year").contains(TEST_DATE); + + InterestingFields data2 = gson.fromJson(json, InterestingFields.class); + assertEquals(data.toString(), data2.toString()); + + // try when the date-time string is invalid + 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()); + } + + @ToString + private static class InterestingFields { + private OffsetDateTime date; + } +} diff --git a/gson/src/test/java/org/onap/policy/common/gson/OffsetTimeTypeAdapterTest.java b/gson/src/test/java/org/onap/policy/common/gson/OffsetTimeTypeAdapterTest.java new file mode 100644 index 00000000..8098af98 --- /dev/null +++ b/gson/src/test/java/org/onap/policy/common/gson/OffsetTimeTypeAdapterTest.java @@ -0,0 +1,72 @@ +/*- + * ============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 java.time.OffsetTime; +import lombok.ToString; +import org.junit.Test; + +public class OffsetTimeTypeAdapterTest { + private static Gson gson = + new GsonBuilder().registerTypeAdapter(OffsetTime.class, new OffsetTimeTypeAdapter()).create(); + private static final String TEST_TIME = "12:00:00.999+05:00"; + + @Test + public void test() { + InterestingFields data = new InterestingFields(); + data.time = OffsetTime.parse(TEST_TIME); + + String json = gson.toJson(data); + + // instant should be encoded as a number, without quotes + assertThat(json).doesNotContain("foo").contains(TEST_TIME); + + InterestingFields data2 = gson.fromJson(json, InterestingFields.class); + assertEquals(data.toString(), data2.toString()); + + // try when the date-time string is invalid + String json2 = json.replace("12", "invalid-time"); + assertThatThrownBy(() -> gson.fromJson(json2, InterestingFields.class)).isInstanceOf(JsonParseException.class) + .hasMessageContaining("invalid time"); + + // null output + data.time = null; + json = gson.toJson(data); + data2 = gson.fromJson(json, InterestingFields.class); + assertEquals(data.toString(), data2.toString()); + + // null input + data2 = gson.fromJson("{\"time\":null}", InterestingFields.class); + assertEquals(data.toString(), data2.toString()); + } + + @ToString + private static class InterestingFields { + private OffsetTime time; + } +} 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/ZoneOffsetTypeAdapterTest.java b/gson/src/test/java/org/onap/policy/common/gson/ZoneOffsetTypeAdapterTest.java new file mode 100644 index 00000000..d9a33169 --- /dev/null +++ b/gson/src/test/java/org/onap/policy/common/gson/ZoneOffsetTypeAdapterTest.java @@ -0,0 +1,72 @@ +/*- + * ============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 java.time.ZoneOffset; +import lombok.ToString; +import org.junit.Test; + +public class ZoneOffsetTypeAdapterTest { + private static Gson gson = + new GsonBuilder().registerTypeAdapter(ZoneOffset.class, new ZoneOffsetTypeAdapter()).create(); + private static final String TEST_ZONE = "+05:00"; + + @Test + public void test() { + InterestingFields data = new InterestingFields(); + data.zone = ZoneOffset.of(TEST_ZONE); + + String json = gson.toJson(data); + + // instant should be encoded as a number, without quotes + assertThat(json).doesNotContain("foo").contains(TEST_ZONE); + + InterestingFields data2 = gson.fromJson(json, InterestingFields.class); + assertEquals(data.toString(), data2.toString()); + + // try when the date-time string is invalid + String json2 = json.replace("05", "invalid-zone"); + assertThatThrownBy(() -> gson.fromJson(json2, InterestingFields.class)).isInstanceOf(JsonParseException.class) + .hasMessageContaining("invalid zone"); + + // null output + data.zone = null; + json = gson.toJson(data); + data2 = gson.fromJson(json, InterestingFields.class); + assertEquals(data.toString(), data2.toString()); + + // null input + data2 = gson.fromJson("{\"zone\":null}", InterestingFields.class); + assertEquals(data.toString(), data2.toString()); + } + + @ToString + private static class InterestingFields { + private ZoneOffset zone; + } +} 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()); } diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/AdapterTest.java b/gson/src/test/java/org/onap/policy/common/gson/internal/AdapterTest.java index 9d80c860..fd999951 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/AdapterTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/AdapterTest.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,7 +43,7 @@ import org.onap.policy.common.gson.annotation.GsonJsonProperty; import org.onap.policy.common.gson.internal.Adapter.Factory; import org.onap.policy.common.gson.internal.DataAdapterFactory.Data; import org.onap.policy.common.gson.internal.DataAdapterFactory.DerivedData; -import org.powermock.reflect.Whitebox; +import org.springframework.test.util.ReflectionTestUtils; public class AdapterTest { private static final String GET_INVALID_NAME = "get$InvalidName"; @@ -83,12 +84,12 @@ public class AdapterTest { @BeforeClass public static void setUpBeforeClass() { - saveFactory = Whitebox.getInternalState(Adapter.class, FACTORY_FIELD); + saveFactory = (Factory) ReflectionTestUtils.getField(Adapter.class, FACTORY_FIELD); } @After public void tearDown() { - Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, saveFactory); + ReflectionTestUtils.setField(Adapter.class, FACTORY_FIELD, saveFactory); } @Test @@ -98,7 +99,7 @@ public class AdapterTest { // return an invalid field name Factory factory = mock(Factory.class); when(factory.getName(any(Field.class))).thenReturn("$invalidFieldName"); - Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory); + ReflectionTestUtils.setField(Adapter.class, FACTORY_FIELD, factory); assertFalse(Adapter.isManaged(field(VALUE_NAME))); } @@ -108,7 +109,7 @@ public class AdapterTest { // return an invalid method name Factory factory = mock(Factory.class); - Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory); + ReflectionTestUtils.setField(Adapter.class, FACTORY_FIELD, factory); when(factory.getName(any(Method.class))).thenReturn(GET_INVALID_NAME); assertFalse(Adapter.isManaged(mget(GET_VALUE_NAME))); @@ -240,7 +241,7 @@ public class AdapterTest { // return an invalid field name Factory factory = mock(Factory.class); when(factory.getName(any(Field.class))).thenReturn("$invalidFieldName"); - Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory); + ReflectionTestUtils.setField(Adapter.class, FACTORY_FIELD, factory); assertEquals(null, Adapter.detmPropName(field(VALUE_NAME))); } @@ -257,7 +258,7 @@ public class AdapterTest { // return an invalid method name Factory factory = mock(Factory.class); - Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory); + ReflectionTestUtils.setField(Adapter.class, FACTORY_FIELD, factory); when(factory.getName(any(Method.class))).thenReturn(GET_INVALID_NAME); assertEquals(null, Adapter.detmGetterPropName(mget(GET_VALUE_NAME))); @@ -273,7 +274,7 @@ public class AdapterTest { // return an invalid method name Factory factory = mock(Factory.class); - Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory); + ReflectionTestUtils.setField(Adapter.class, FACTORY_FIELD, factory); when(factory.getName(any(Method.class))).thenReturn(SET_INVALID_NAME); assertEquals(null, Adapter.detmSetterPropName(mset(SET_VALUE_NAME))); diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/DataAdapterFactory.java b/gson/src/test/java/org/onap/policy/common/gson/internal/DataAdapterFactory.java index 2799d8ba..d2cdf7f8 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/DataAdapterFactory.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/DataAdapterFactory.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 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. @@ -35,6 +35,7 @@ import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.TreeMap; +import lombok.ToString; /** * Factory used with test Data. @@ -54,6 +55,7 @@ public class DataAdapterFactory implements TypeAdapterFactory { /** * Object handled by this factory. */ + @ToString public static class Data { private int id; @@ -72,16 +74,12 @@ public class DataAdapterFactory implements TypeAdapterFactory { public void setId(int id) { this.id = id; } - - @Override - public String toString() { - return "Data [id=" + id + "]"; - } } /** * Object derived from Data. */ + @ToString(callSuper = true) public static class DerivedData extends Data { private String text; @@ -101,11 +99,6 @@ public class DataAdapterFactory implements TypeAdapterFactory { public void setText(String text) { this.text = text; } - - @Override - public String toString() { - return "DerivedData [text=" + text + ", toString()=" + super.toString() + "]"; - } } /** diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/JacksonTypeAdapterTest.java b/gson/src/test/java/org/onap/policy/common/gson/internal/JacksonTypeAdapterTest.java index 6be4e590..5e73d06e 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/JacksonTypeAdapterTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/JacksonTypeAdapterTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 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. @@ -33,6 +33,7 @@ import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; +import lombok.ToString; import org.junit.Before; import org.junit.Test; @@ -138,6 +139,7 @@ public class JacksonTypeAdapterTest { assertEquals("read text", data); } + @ToString private static class Data { private String id; private String value; @@ -155,11 +157,6 @@ public class JacksonTypeAdapterTest { this.id = id; this.value = value; } - - @Override - public String toString() { - return "Data [id=" + id + ", value=" + value + "]"; - } } private abstract static class NamedSer implements Serializer { |