diff options
author | Jim Hahn <jrh3@att.com> | 2019-09-13 12:14:25 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-09-13 17:40:29 -0400 |
commit | af2e4018c92b0fb311f60dfe507335b12e2935a4 (patch) | |
tree | 62705712e2a88682e17bdd9fef248e866c7407e4 /utils/src/test/java/org/onap/policy/common/utils/coder/StandardYamlCoderTest.java | |
parent | 63922e3dc9101f019cb490f6c5d02991c10fcdf5 (diff) |
Extract YamlJsonTranslator from StandardYamlCoder
Refactored StandardYamlCoder, extracting a new class, YamlJsonTranslator,
from it. This facilitates performing yaml translation when not using a
standard "gson" coder.
Added YamlJacksonHandler which supports YAML translation layered on
top of a JacksonHandler instead of a GsonMessageBodyHandler.
Also added junit tests to complete coverage of StandardCoder.
Also added public APPLICATION_YAML to YamlMessageBodyHandler.
Change-Id: Ia470fa194661fbf1aebeaf0f18b57f2a984cb64b
Issue-ID: POLICY-2081
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'utils/src/test/java/org/onap/policy/common/utils/coder/StandardYamlCoderTest.java')
-rw-r--r-- | utils/src/test/java/org/onap/policy/common/utils/coder/StandardYamlCoderTest.java | 100 |
1 files changed, 17 insertions, 83 deletions
diff --git a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardYamlCoderTest.java b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardYamlCoderTest.java index 6ec20663..e38c5c9c 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardYamlCoderTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardYamlCoderTest.java @@ -20,26 +20,20 @@ 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.assertTrue; import java.io.File; -import java.io.IOException; -import java.io.Writer; +import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.util.List; -import java.util.Map; -import lombok.EqualsAndHashCode; import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.coder.YamlJsonTranslatorTest.Container; public class StandardYamlCoderTest { private static final File YAML_FILE = - new File("src/test/resources/org/onap/policy/common/utils/coder/StandardYamlCoder.yaml"); + new File("src/test/resources/org/onap/policy/common/utils/coder/YamlJsonTranslator.yaml"); private StandardYamlCoder coder; private Container cont; @@ -59,30 +53,13 @@ public class StandardYamlCoderTest { } @Test - public void testToJsonWriterObject() throws Exception { - IOException ex = new IOException("expected exception"); + public void testToJsonWriterObject() throws CoderException { + StringWriter wtr = new StringWriter(); + coder.encode(wtr, cont); + String yaml = wtr.toString(); - // writer that throws an exception when the write() method is invoked - Writer wtr = new Writer() { - @Override - public void write(char[] cbuf, int off, int len) throws IOException { - throw ex; - } - - @Override - public void flush() throws IOException { - // do nothing - } - - @Override - public void close() throws IOException { - // do nothing - } - }; - - assertThatThrownBy(() -> coder.encode(wtr, cont)).isInstanceOf(CoderException.class); - - wtr.close(); + Container cont2 = coder.decode(yaml, Container.class); + assertEquals(cont, cont2); } @Test @@ -94,58 +71,15 @@ public class StandardYamlCoderTest { @Test public void testFromJsonReaderClassOfT() { - assertNotNull(cont.item); - assertTrue(cont.item.boolVal); - assertEquals(1000L, cont.item.longVal); - assertEquals(1010.1f, cont.item.floatVal, 0.00001); - - assertEquals(4, cont.list.size()); - assertNull(cont.list.get(1)); - - assertEquals(20, cont.list.get(0).intVal); - assertEquals("string 30", cont.list.get(0).stringVal); - assertNull(cont.list.get(0).nullVal); - - assertEquals(40.0, cont.list.get(2).doubleVal, 0.000001); - assertNull(cont.list.get(2).nullVal); - assertNotNull(cont.list.get(2).another); - assertEquals(50, cont.list.get(2).another.intVal); - - assertTrue(cont.list.get(3).boolVal); - - assertNotNull(cont.map); - assertEquals(3, cont.map.size()); - - assertNotNull(cont.map.get("itemA")); - assertEquals("stringA", cont.map.get("itemA").stringVal); - - assertNotNull(cont.map.get("itemB")); - assertEquals("stringB", cont.map.get("itemB").stringVal); - - double dbl = 123456789012345678901234567890.0; - assertEquals(dbl, cont.map.get("itemB").doubleVal, 1000.0); - - assertNotNull(cont.map.get("itemC")); - assertTrue(cont.map.get("itemC").boolVal); + YamlJsonTranslatorTest.verify(cont); } - - @EqualsAndHashCode - public static class Container { - private Item item; - private List<Item> list; - private Map<String, Item> map; - } - - @EqualsAndHashCode - public static class Item { - private boolean boolVal; - private int intVal; - private long longVal; - private double doubleVal; - private float floatVal; - private String stringVal; - private Object nullVal; - private Item another; + @Test + public void testStandardTypeAdapter() throws Exception { + String yaml = "abc: def\n"; + StandardCoderObject sco = coder.fromJson(yaml, StandardCoderObject.class); + assertNotNull(sco.getData()); + assertEquals("{'abc':'def'}".replace('\'', '"'), sco.getData().toString()); + assertEquals(yaml, coder.toJson(sco)); } } |