aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints
diff options
context:
space:
mode:
Diffstat (limited to 'policy-endpoints')
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlExceptionMapper.java2
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlJacksonHandler.java51
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlMessageBodyHandler.java54
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpServerTest.java3
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java3
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java10
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlJacksonHandlerTest.java172
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java2
8 files changed, 263 insertions, 34 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlExceptionMapper.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlExceptionMapper.java
index ac96cab0..7eac932a 100644
--- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlExceptionMapper.java
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlExceptionMapper.java
@@ -34,7 +34,7 @@ import org.yaml.snakeyaml.error.YAMLException;
* error code to an HTTP 400 error code.
*/
@Provider
-@Produces("application/yaml")
+@Produces(YamlMessageBodyHandler.APPLICATION_YAML)
public class YamlExceptionMapper implements ExceptionMapper<YAMLException> {
private static Logger logger = LoggerFactory.getLogger(YamlExceptionMapper.class);
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlJacksonHandler.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlJacksonHandler.java
new file mode 100644
index 00000000..0cab374f
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlJacksonHandler.java
@@ -0,0 +1,51 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.endpoints.http.server;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.ext.Provider;
+import org.onap.policy.common.gson.JacksonHandler;
+import org.onap.policy.common.utils.coder.YamlJsonTranslator;
+
+/**
+ * Provider that serializes and de-serializes YAML via snakeyaml and gson using jackson
+ * default behaviors and annotations.
+ */
+@Provider
+@Consumes(MediaType.WILDCARD)
+@Produces(MediaType.WILDCARD)
+public class YamlJacksonHandler extends YamlMessageBodyHandler {
+
+ /**
+ * Translator to be used. We want a GSON object that's configured the same way as it
+ * is in {@link JacksonHandler}, so just get it from there.
+ */
+ private static final YamlJsonTranslator TRANS = new YamlJsonTranslator(new JacksonHandler().getGson());
+
+ /**
+ * Constructs the object.
+ */
+ public YamlJacksonHandler() {
+ super(TRANS);
+ }
+}
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlMessageBodyHandler.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlMessageBodyHandler.java
index 36418e4a..89aa8ff6 100644
--- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlMessageBodyHandler.java
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlMessageBodyHandler.java
@@ -26,7 +26,6 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
-import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
@@ -37,14 +36,14 @@ import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
-import org.onap.policy.common.utils.coder.CoderException;
-import org.onap.policy.common.utils.coder.StandardYamlCoder;
+import org.onap.policy.common.gson.GsonMessageBodyHandler;
+import org.onap.policy.common.utils.coder.YamlJsonTranslator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.error.YAMLException;
/**
- * Provider that serializes and de-serializes JSON via gson.
+ * Provider that serializes and de-serializes YAML via snakeyaml and gson.
*/
@Provider
@Consumes(MediaType.WILDCARD)
@@ -53,11 +52,33 @@ public class YamlMessageBodyHandler implements MessageBodyReader<Object>, Messag
public static final Logger logger = LoggerFactory.getLogger(YamlMessageBodyHandler.class);
+ public static final String APPLICATION_YAML = "application/yaml";
+
+ /**
+ * Translator that's used when none is specified. We want a GSON object that's
+ * configured the same way as it is in {@link GsonMessageBodyHandler}, so just get it
+ * from there.
+ */
+ private static final YamlJsonTranslator DEFAULT_TRANSLATOR =
+ new YamlJsonTranslator(new GsonMessageBodyHandler().getGson());
+
+ private final YamlJsonTranslator translator;
+
/**
* Constructs the object.
*/
public YamlMessageBodyHandler() {
+ this(DEFAULT_TRANSLATOR);
+ }
+
+ /**
+ * Constructs the object.
+ *
+ * @param translator translator to use to translate to/from YAML
+ */
+ public YamlMessageBodyHandler(YamlJsonTranslator translator) {
logger.info("Accepting YAML for REST calls");
+ this.translator = translator;
}
@Override
@@ -75,10 +96,7 @@ public class YamlMessageBodyHandler implements MessageBodyReader<Object>, Messag
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
- new MyYamlCoder().encode(writer, object);
-
- } catch (CoderException e) {
- throw new IOException(e);
+ translator.toYaml(writer, object);
}
}
@@ -104,24 +122,10 @@ public class YamlMessageBodyHandler implements MessageBodyReader<Object>, Messag
try (InputStreamReader streamReader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) {
Class<?> clazz = (Class<?>) genericType;
- return new MyYamlCoder().decode(streamReader, clazz);
- }
- }
+ return translator.fromYaml(streamReader, clazz);
- /**
- * Yaml coder that yields YAMLException on input so that the http servlet can identify
- * it and generate a bad-request status code. Only the {@link #decode(Reader, Class)}
- * method must be overridden.
- */
- private static class MyYamlCoder extends StandardYamlCoder {
- @Override
- public <T> T decode(Reader source, Class<T> clazz) {
- try {
- return fromJson(source, clazz);
-
- } catch (JsonSyntaxException e) {
- throw new YAMLException(e);
- }
+ } catch (JsonSyntaxException e) {
+ throw new YAMLException(e);
}
}
}
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("<bogus yaml", APPLICATION_YAML));
+ assertEquals(200, roundTrip(new StandardCoder().encode(new MyRequest()),
+ YamlMessageBodyHandler.APPLICATION_YAML));
+ assertEquals(400, roundTrip("<bogus yaml", YamlMessageBodyHandler.APPLICATION_YAML));
assertThat(errorMsg).contains("Invalid request");
}
@@ -340,8 +340,8 @@ public class RestServerTest {
}
@Path("/")
- @Produces({MediaType.APPLICATION_JSON, APPLICATION_YAML})
- @Consumes({MediaType.APPLICATION_JSON, APPLICATION_YAML})
+ @Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
+ @Consumes({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
public static class RealProvider {
@POST
@Path("/request")
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlJacksonHandlerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlJacksonHandlerTest.java
new file mode 100644
index 00000000..7ca131ec
--- /dev/null
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlJacksonHandlerTest.java
@@ -0,0 +1,172 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.endpoints.http.server.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import com.google.gson.JsonObject;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.TreeMap;
+import javax.ws.rs.core.MediaType;
+import org.junit.Test;
+import org.onap.policy.common.endpoints.http.server.YamlJacksonHandler;
+import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
+import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter;
+import org.onap.policy.common.gson.annotation.GsonJsonAnySetter;
+
+public class YamlJacksonHandlerTest {
+
+ @Test
+ public void test() throws Exception {
+ YamlJacksonHandler hdlr = new YamlJacksonHandler();
+
+ assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf(YamlMessageBodyHandler.APPLICATION_YAML)));
+ assertFalse(hdlr.isReadable(null, null, null, MediaType.TEXT_PLAIN_TYPE));
+
+ JsonObject expected = new JsonObject();
+ expected.addProperty("myId", 100);
+ expected.addProperty("value", "a value");
+ expected.addProperty("abc", "def");
+ expected.addProperty("hello", "world");
+
+ Data data = new Data();
+ data.id = 10;
+ data.value = "a value";
+ data.props = new HashMap<>();
+ 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<String, String> props;
+
+ public int getMyId() {
+ return 100;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+ @GsonJsonAnyGetter
+ public Map<String, String> 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<String, Object> props;
+
+ @Override
+ public String toString() {
+ return props.toString();
+ }
+
+ @SuppressWarnings("unused")
+ public Map<String, Object> getProps() {
+ return props;
+ }
+
+ @SuppressWarnings("unused")
+ public void setProps(Map<String, Object> 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();
}