aboutsummaryrefslogtreecommitdiffstats
path: root/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java')
-rw-r--r--utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java85
1 files changed, 78 insertions, 7 deletions
diff --git a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java
index 2a70f85a..33c7331e 100644
--- a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java
+++ b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * 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.
@@ -23,12 +23,15 @@ package org.onap.policy.common.utils.coder;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
+import com.google.gson.JsonSyntaxException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -45,8 +48,11 @@ import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.TreeMap;
+import lombok.ToString;
import org.junit.Before;
import org.junit.Test;
@@ -64,6 +70,35 @@ public class StandardCoderTest {
}
@Test
+ public void testConvert() throws CoderException {
+ // null source
+ assertNull(coder.convert(null, StandardCoderObject.class));
+
+ // same class of object
+ StandardCoderObject sco = new StandardCoderObject();
+ assertSame(sco, coder.convert(sco, StandardCoderObject.class));
+
+ // source is a string
+ assertEquals(Integer.valueOf(10), coder.convert("10", Integer.class));
+
+ // target is a string
+ assertEquals("10", coder.convert(10, String.class));
+
+ // source and target are different types, neither is a string
+ sco = coder.convert(Map.of("hello", "world"), StandardCoderObject.class);
+ assertEquals("world", sco.getString("hello"));
+
+ // throw an exeception
+ coder = new StandardCoder() {
+ @Override
+ protected <T> T fromJson(JsonElement json, Class<T> clazz) {
+ throw jpe;
+ }
+ };
+ assertThatThrownBy(() -> coder.convert(10, Long.class)).isInstanceOf(CoderException.class).hasCause(jpe);
+ }
+
+ @Test
public void testEncodeObject() throws Exception {
List<Integer> arr = Arrays.asList(1100, 1110);
assertEquals("[1100,1110]", coder.encode(arr));
@@ -75,6 +110,32 @@ public class StandardCoderTest {
}
@Test
+ public void testEncodeObjectBoolean() throws Exception {
+ final List<Integer> arr = Arrays.asList(1100, 1110);
+
+ /*
+ * As plain json.
+ */
+ assertEquals("[1100,1110]", coder.encode(arr, false));
+
+ // test exception case
+ coder = spy(new StandardCoder());
+ when(coder.toJson(arr)).thenThrow(jpe);
+ assertThatThrownBy(() -> coder.encode(arr, false)).isInstanceOf(CoderException.class).hasCause(jpe);
+
+
+ /*
+ * As pretty json.
+ */
+ assertEquals("[\n 1100,\n 1110\n]", coder.encode(arr, true));
+
+ // test exception case
+ coder = spy(new StandardCoder());
+ when(coder.toPrettyJson(arr)).thenThrow(jpe);
+ assertThatThrownBy(() -> coder.encode(arr, true)).isInstanceOf(CoderException.class).hasCause(jpe);
+ }
+
+ @Test
public void testEncodeWriterObject() throws Exception {
List<Integer> arr = Arrays.asList(1200, 1210);
StringWriter wtr = new StringWriter();
@@ -260,7 +321,9 @@ public class StandardCoderTest {
assertEquals(json, coder.toJson(sco));
// invalid json -> exception
- assertThatThrownBy(() -> coder.fromJson(new StringReader("["), StandardCoderObject.class));
+ StringReader rdr = new StringReader("[");
+ assertThatThrownBy(() -> coder.fromJson(rdr, StandardCoderObject.class))
+ .isInstanceOf(JsonSyntaxException.class);
}
@Test
@@ -281,16 +344,24 @@ public class StandardCoderTest {
assertEquals(-10, map.props.get("negInt"));
assertEquals(100000000000L, map.props.get("posLong"));
assertEquals(12.5, map.props.get("doubleVal"));
+
+ // test when decoding into a map
+ @SuppressWarnings("unchecked")
+ Map<String, Object> map2 = coder.decode("{'intValue':10, 'dblVal':20.1}", TreeMap.class);
+ assertEquals("{dblVal=20.1, intValue=10}", map2.toString());
+ }
+
+ @Test
+ public void testListDouble() throws Exception {
+ @SuppressWarnings("unchecked")
+ List<Object> list = coder.decode("[10, 20.1, 30]", LinkedList.class);
+ assertEquals("[10, 20.1, 30]", list.toString());
}
+ @ToString
private static class MyObject {
private String abc;
-
- @Override
- public String toString() {
- return "MyObject [abc=" + abc + "]";
- }
}
public static class MyMap {