From af2e4018c92b0fb311f60dfe507335b12e2935a4 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 13 Sep 2019 12:14:25 -0400 Subject: 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 --- .../endpoints/http/server/test/HttpServerTest.java | 3 +- .../http/server/test/RestEchoService.java | 3 +- .../endpoints/http/server/test/RestServerTest.java | 10 +- .../http/server/test/YamlJacksonHandlerTest.java | 172 +++++++++++++++++++++ .../server/test/YamlMessageBodyHandlerTest.java | 2 +- 5 files changed, 182 insertions(+), 8 deletions(-) create mode 100644 policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlJacksonHandlerTest.java (limited to 'policy-endpoints/src/test/java/org/onap/policy/common/endpoints') diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java index 84f82f1a..6dee6f1f 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java @@ -39,6 +39,7 @@ import org.junit.Before; import org.junit.Test; import org.onap.policy.common.endpoints.http.server.HttpServletServer; import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; +import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler; import org.onap.policy.common.utils.coder.StandardYamlCoder; import org.onap.policy.common.utils.gson.GsonTestUtils; import org.onap.policy.common.utils.network.NetworkUtil; @@ -51,7 +52,7 @@ import org.slf4j.LoggerFactory; public class HttpServerTest { private static final String LOCALHOST = "localhost"; private static final String JSON_MEDIA = "application/json"; - private static final String YAML_MEDIA = "application/yaml"; + private static final String YAML_MEDIA = YamlMessageBodyHandler.APPLICATION_YAML; private static final String SWAGGER_JSON = "/swagger.json"; private static final String JUNIT_ECHO_HELLO = "/junit/echo/hello"; private static final String JUNIT_ECHO_FULL_REQUEST = "/junit/echo/full/request"; diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java index 27ce300c..373950b2 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java @@ -32,6 +32,7 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; +import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler; @Api(value = "echo") @@ -56,7 +57,7 @@ public class RestEchoService { @POST @Path("/full/request") - @Produces({MediaType.APPLICATION_JSON, "application/yaml"}) + @Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML}) @ApiOperation(value = "echoes back the request structure", response = RestEchoReqResp.class) public Response echoFullyPost(RestEchoReqResp reqResp) { return Response.status(Status.OK).entity(reqResp).build(); diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java index cd40f012..519bbd1d 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java @@ -69,7 +69,6 @@ import org.onap.policy.common.utils.network.NetworkUtil; import org.powermock.reflect.Whitebox; public class RestServerTest { - private static final String APPLICATION_YAML = "application/yaml"; private static final String SERVER1 = "my-server-A"; private static final String SERVER2 = "my-server-B"; private static final String FACTORY_FIELD = "factory"; @@ -247,8 +246,9 @@ public class RestServerTest { public void testInvalidYaml() throws Exception { initRealParams(); - assertEquals(200, roundTrip(new StandardCoder().encode(new MyRequest()), APPLICATION_YAML)); - assertEquals(400, roundTrip("(); + data.props.put("abc", "def"); + data.props.put("hello", "world"); + + /* + * Ensure everything serializes as expected. + */ + ByteArrayOutputStream outstr = new ByteArrayOutputStream(); + hdlr.writeTo(data, Data.class, Data.class, null, null, null, outstr); + + assertEquals("abc: def\nhello: world\nmyId: 100\nvalue: a value\n", outstr.toString("UTF-8")); + + /* + * Ensure everything deserializes as expected. + */ + Data data2 = (Data) hdlr.readFrom(Object.class, Data.class, null, null, null, + new ByteArrayInputStream(outstr.toByteArray())); + + // id is not serialized, so we must copy it manually before comparing + data2.id = data.id; + + assertEquals(data.toString(), data2.toString()); + } + + @Test + public void testMapDouble() throws Exception { + MyMap map = new MyMap(); + map.props = new HashMap<>(); + map.props.put("plainString", "def"); + map.props.put("negInt", -10); + map.props.put("doubleVal", 12.5); + map.props.put("posLong", 100000000000L); + + YamlJacksonHandler hdlr = new YamlJacksonHandler(); + ByteArrayOutputStream outstr = new ByteArrayOutputStream(); + hdlr.writeTo(map, map.getClass(), map.getClass(), null, null, null, outstr); + + Object obj2 = hdlr.readFrom(Object.class, map.getClass(), null, null, null, + new ByteArrayInputStream(outstr.toByteArray())); + assertEquals(map.toString(), obj2.toString()); + + map = (MyMap) obj2; + + assertEquals(-10, map.props.get("negInt")); + assertEquals(100000000000L, map.props.get("posLong")); + assertEquals(12.5, map.props.get("doubleVal")); + } + + /** + * This class includes all policy-specific gson annotations. + */ + public static class Data { + protected int id; + + protected String value; + + protected Map props; + + public int getMyId() { + return 100; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @GsonJsonAnyGetter + public Map getProps() { + return props; + } + + /** + * Sets a property. + * + * @param name property name + * @param value new value + */ + @GsonJsonAnySetter + public void setProperty(String name, String value) { + if (props == null) { + props = new TreeMap<>(); + } + + props.put(name, value); + } + + @Override + public String toString() { + return "Data [id=" + id + ", value=" + value + ", props=" + props + "]"; + } + } + + private static class MyMap { + private Map props; + + @Override + public String toString() { + return props.toString(); + } + + @SuppressWarnings("unused") + public Map getProps() { + return props; + } + + @SuppressWarnings("unused") + public void setProps(Map props) { + this.props = props; + } + } +} diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java index 0ddad0bf..70b620c0 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java @@ -117,7 +117,7 @@ public class YamlMessageBodyHandlerTest { MyObject obj1 = new MyObject(10); assertThatThrownBy(() -> hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr)) - .isInstanceOf(IOException.class); + .isInstanceOf(YAMLException.class); outstr.close(); } -- cgit 1.2.3-korg