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.java47
1 files changed, 39 insertions, 8 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 d5cde55a..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-2020 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;
@@ -49,6 +52,7 @@ 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;
@@ -66,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));
@@ -288,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
@@ -312,7 +347,7 @@ public class StandardCoderTest {
// test when decoding into a map
@SuppressWarnings("unchecked")
- Map<String,Object> map2 = coder.decode("{'intValue':10, 'dblVal':20.1}", TreeMap.class);
+ Map<String, Object> map2 = coder.decode("{'intValue':10, 'dblVal':20.1}", TreeMap.class);
assertEquals("{dblVal=20.1, intValue=10}", map2.toString());
}
@@ -324,13 +359,9 @@ public class StandardCoderTest {
}
+ @ToString
private static class MyObject {
private String abc;
-
- @Override
- public String toString() {
- return "MyObject [abc=" + abc + "]";
- }
}
public static class MyMap {