From 00ab9f38e08643acda3b560e01236ced0d77d5c2 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 16 Sep 2019 11:38:20 -0400 Subject: Eliminate constructors with side-effects Several of the Gson and Jackson handlers take a GsonBuilder, which they then configure with additional adapters prior to create a Gson object that they subsequently use. The code has been modified so that the constructors no longer take a GsonBuilder, thus eliminating the side-effects that they had on the builders. Instead, a configBuilder() method has been added to the handler classes that makes it clear tht they modify the GsonBuilder. This also has the advantage that classes that want a Gson object configured per a given handler need only call that handler's configBuilder() method; previously, they had to constructor the handler and then retrieve its gson object. Also updated a few tests to specify the HTTP "Accept" header to ensure that return results are in yaml format. Change-Id: I2ef98198041ff9f73913d01ee6ee14ecf20ba617 Issue-ID: POLICY-2081 Signed-off-by: Jim Hahn --- .../onap/policy/common/endpoints/http/server/YamlJacksonHandler.java | 4 +++- .../policy/common/endpoints/http/server/YamlMessageBodyHandler.java | 3 ++- .../onap/policy/common/endpoints/http/server/test/HttpServerTest.java | 1 + .../onap/policy/common/endpoints/http/server/test/RestServerTest.java | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) (limited to 'policy-endpoints/src') 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 index 0cab374f..f71aa90f 100644 --- 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 @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.http.server; +import com.google.gson.GsonBuilder; import javax.ws.rs.Consumes; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @@ -40,7 +41,8 @@ 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()); + private static final YamlJsonTranslator TRANS = + new YamlJsonTranslator(JacksonHandler.configBuilder(new GsonBuilder()).create()); /** * Constructs the object. 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 89aa8ff6..8506a283 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 @@ -20,6 +20,7 @@ package org.onap.policy.common.endpoints.http.server; +import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; import java.io.IOException; import java.io.InputStream; @@ -60,7 +61,7 @@ public class YamlMessageBodyHandler implements MessageBodyReader, Messag * from there. */ private static final YamlJsonTranslator DEFAULT_TRANSLATOR = - new YamlJsonTranslator(new GsonMessageBodyHandler().getGson()); + new YamlJsonTranslator(GsonMessageBodyHandler.configBuilder(new GsonBuilder()).create()); private final YamlJsonTranslator translator; 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 6dee6f1f..82faa052 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 @@ -509,6 +509,7 @@ public class HttpServerTest { conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", mediaType); + conn.setRequestProperty("Accept", mediaType); IOUtils.write(post, conn.getOutputStream()); return response(conn); } 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 519bbd1d..959945b9 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 @@ -265,6 +265,7 @@ public class RestServerTest { String auth = params.getUserName() + ":" + params.getPassword(); conn.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString(auth.getBytes())); conn.setRequestProperty("Content-type", mediaType); + conn.setRequestProperty("Accept", mediaType); conn.connect(); try (PrintWriter wtr = new PrintWriter(conn.getOutputStream())) { -- cgit 1.2.3-korg