From 4f909d754b638ab4563eb9466836cc2d7a184d63 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 31 Jan 2020 11:15:44 -0500 Subject: Add pretty() method to Coder "Pretty" JSON is often needed, so added Coder.pretty() to facilitate that. Incorporated review comment(s): - changed pretty() to encode(object, pretty) Issue-ID: POLICY-1625 Signed-off-by: Jim Hahn Change-Id: I180fa6de416836008bf1c410132ae30f4dde9271 --- .../org/onap/policy/common/utils/coder/Coder.java | 14 ++++++-- .../policy/common/utils/coder/StandardCoder.java | 40 +++++++++++++++++++--- .../common/utils/coder/StandardValCoder.java | 13 ++++++- .../common/utils/coder/StandardYamlCoder.java | 8 ++++- .../common/utils/coder/StandardCoderTest.java | 26 ++++++++++++++ .../common/utils/coder/StandardValCoderTest.java | 22 ++++++++++++ .../common/utils/coder/StandardYamlCoderTest.java | 24 ++++++++++++- 7 files changed, 138 insertions(+), 9 deletions(-) diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java index bb51f2b9..ec0e5e42 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java @@ -1,8 +1,8 @@ /* * ============LICENSE_START======================================================= - * ONAP PAP + * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 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. @@ -40,6 +40,16 @@ public interface Coder { */ String encode(Object object) throws CoderException; + /** + * Encodes an object into json, optionally making it "pretty". + * + * @param object object to be encoded + * @param pretty {@code true} if it should be encoded as "pretty" json, {@code false} otherwise + * @return a json string representing the object + * @throws CoderException if an error occurs + */ + String encode(Object object, boolean pretty) throws CoderException; + /** * Encodes an object into json, writing to the given target. * diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java index 6d0cbc9d..13973f1c 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java @@ -54,9 +54,21 @@ public class StandardCoder implements Coder { * Gson object used to encode and decode messages. */ @Getter(AccessLevel.PROTECTED) - private static final Gson GSON = GsonMessageBodyHandler.configBuilder( - new GsonBuilder().registerTypeAdapter(StandardCoderObject.class, new StandardTypeAdapter())) - .create(); + private static final Gson GSON; + + /** + * Gson object used to encode messages in "pretty" format. + */ + @Getter(AccessLevel.PROTECTED) + private static final Gson GSON_PRETTY; + + static { + GsonBuilder builder = GsonMessageBodyHandler.configBuilder( + new GsonBuilder().registerTypeAdapter(StandardCoderObject.class, new StandardTypeAdapter())); + + GSON = builder.create(); + GSON_PRETTY = builder.setPrettyPrinting().create(); + } /** * Constructs the object. @@ -67,8 +79,18 @@ public class StandardCoder implements Coder { @Override public String encode(Object object) throws CoderException { + return encode(object, false); + } + + @Override + public String encode(Object object, boolean pretty) throws CoderException { try { - return toJson(object); + if (pretty) { + return toPrettyJson(object); + + } else { + return toJson(object); + } } catch (RuntimeException e) { throw new CoderException(e); @@ -151,6 +173,16 @@ public class StandardCoder implements Coder { } } + /** + * Encodes the object as "pretty" json. + * + * @param object object to be encoded + * @return the encoded object + */ + protected String toPrettyJson(Object object) { + return GSON_PRETTY.toJson(object); + } + @Override public StandardCoderObject toStandard(Object object) throws CoderException { try { diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java index 378254b8..6e08e722 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java @@ -61,11 +61,22 @@ public class StandardValCoder extends StandardCoder { this.validator = validatorApi.loadSchema(schemaSource); } + @Override + protected String toPrettyJson(Object object) { + /* + * The validator strips off the "pretty" stuff (i.e., spaces), thus we have to validate + * and generate the pretty JSON in separate steps. + */ + getGSON().toJson(object, object.getClass(), validatorApi.createJsonWriter(validator, new StringWriter())); + + return super.toPrettyJson(object); + } + @Override protected String toJson(@NonNull Object object) { StringWriter output = new StringWriter(); toJson(output, object); - return String.valueOf(output); + return output.toString(); } @Override diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java index 36f15b96..1bcf6ac0 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 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. @@ -37,6 +37,12 @@ public class StandardYamlCoder extends StandardCoder { translator = new YamlJsonTranslator(getGSON()); } + @Override + protected String toPrettyJson(Object object) { + // YAML is already "pretty" + return toJson(object); + } + @Override protected String toJson(Object object) { return translator.toYaml(object); 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 43a17dec..d5cde55a 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 @@ -76,6 +76,32 @@ public class StandardCoderTest { assertThatThrownBy(() -> coder.encode(arr)).isInstanceOf(CoderException.class).hasCause(jpe); } + @Test + public void testEncodeObjectBoolean() throws Exception { + final List 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 arr = Arrays.asList(1200, 1210); diff --git a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardValCoderTest.java b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardValCoderTest.java index 38106f57..2fcdb0dd 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardValCoderTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardValCoderTest.java @@ -20,8 +20,10 @@ 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.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -119,6 +121,26 @@ public class StandardValCoderTest { StringWriter writer = new StringWriter(); valCoder.encode(writer, valOuter); assertEquals(valOuterJson, writer.toString()); + + // test exception case with an empty object + assertThatThrownBy(() -> valCoder.encode(new ValOuter())).isInstanceOf(CoderException.class); + } + + @Test + public void testPretty() throws CoderException { + StandardValCoder valCoder = new StandardValCoder(jsonSchema, "test-schema"); + ValOuter valOuter = valCoder.decode(validJson, ValOuter.class); + + String valOuterJson = valCoder.encode(valOuter); + assertEquals(valOuterJson, valCoder.encode(valOuter, false)); + String prettyValOuterJson = valCoder.encode(valOuter, true); + assertNotEquals(valOuterJson, prettyValOuterJson); + + assertEquals(valOuter, valCoder.decode(prettyValOuterJson, ValOuter.class)); + + // test exception cases with an empty object + assertThatThrownBy(() -> valCoder.encode(new ValOuter(), false)).isInstanceOf(CoderException.class); + assertThatThrownBy(() -> valCoder.encode(new ValOuter(), true)).isInstanceOf(CoderException.class); } @Test 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 e38c5c9c..c770cd3b 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 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. @@ -20,8 +20,11 @@ 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.mockito.Mockito.spy; +import static org.mockito.Mockito.when; import java.io.File; import java.io.StringWriter; @@ -44,6 +47,25 @@ public class StandardYamlCoderTest { cont = coder.decode(YAML_FILE, Container.class); } + @Test + public void testToPrettyJson() throws CoderException { + String expected = coder.encode(cont); + assertEquals(expected, coder.encode(cont, false)); + + String yaml = coder.encode(cont, true); + assertEquals(expected, yaml); + + Container cont2 = coder.decode(yaml, Container.class); + assertEquals(cont, cont2); + + // test exception cases + IllegalArgumentException expex = new IllegalArgumentException("expected exception"); + coder = spy(new StandardYamlCoder()); + when(coder.toJson(cont)).thenThrow(expex); + assertThatThrownBy(() -> coder.encode(cont, false)).isInstanceOf(CoderException.class).hasCause(expex); + assertThatThrownBy(() -> coder.encode(cont, true)).isInstanceOf(CoderException.class).hasCause(expex); + } + @Test public void testToJsonObject() throws CoderException { String yaml = coder.encode(cont); -- cgit 1.2.3-korg