diff options
author | priyanka.akhade <priyanka.akhade@huawei.com> | 2020-02-28 05:47:34 +0000 |
---|---|---|
committer | priyanka.akhade <priyanka.akhade@huawei.com> | 2020-02-29 11:26:08 +0000 |
commit | 132177fb71c08f157fb42037f9dd26ceb2d0ba43 (patch) | |
tree | f47d3c4d950e98d5bcf104aa8952f39a84e3ba6d /profiles/http/src | |
parent | 4f1d279e42a3326cdc97e1eacaf0fdb58ee944e7 (diff) |
Migrate to gson
Issue-ID: CLI-247
Signed-off-by: priyanka.akhade <priyanka.akhade@huawei.com>
Change-Id: If66134db5503382fb45210ece883402fc130ac93
Diffstat (limited to 'profiles/http/src')
3 files changed, 33 insertions, 25 deletions
diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java b/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java index f0fe43cd..25cf14a3 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java @@ -32,8 +32,8 @@ import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; import org.springframework.core.io.Resource; import org.yaml.snakeyaml.Yaml; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.github.dreamhead.moco.HttpServer; import com.github.dreamhead.moco.Moco; import com.github.dreamhead.moco.ResponseHandler; @@ -43,6 +43,7 @@ public class MocoServer { private Runner runner; private Map<String, Object> mocoServerConfigs = new HashMap(); + private static Gson gson = new GsonBuilder().serializeNulls().create(); public MocoServer(String mockFile) throws OnapCommandException { Resource resource = null; @@ -71,8 +72,8 @@ public class MocoServer { if(response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON) != null) { try { mocoServerConfigs.put(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON, - new ObjectMapper().writeValueAsString(response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON))); - } catch (JsonProcessingException e) { + gson.toJson(response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON))); + } catch (Exception e) { // NOSONAR throw new OnapCommandException("Invalid mocking file" + mockFile, e); } } diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java b/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java index aaca5c87..c07a6f7f 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java @@ -16,7 +16,6 @@ package org.onap.cli.fw.http.schema; -import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -42,20 +41,20 @@ import org.onap.cli.fw.registrar.OnapCommandRegistrar; import org.onap.cli.fw.schema.OnapCommandSchemaLoader; import org.onap.cli.fw.utils.OnapCommandUtils; -import com.fasterxml.jackson.databind.ObjectMapper; - -import net.minidev.json.JSONObject; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; public class OnapCommandSchemaHttpLoader { private static final String ATTRIBUTE = "Attribute '"; + private static Gson gson = new GsonBuilder().serializeNulls().create(); private OnapCommandSchemaHttpLoader() { // to follow standards ! } - public static List<String> loadHttpSchema(OnapHttpCommand cmd, String schemaName, boolean includeDefault, - boolean validateSchema) throws OnapCommandException { + public static List<String> loadHttpSchema(OnapHttpCommand cmd, String schemaName, boolean includeDefault, boolean validateSchema) throws OnapCommandException { try { List<String> errors = new ArrayList<>(); if (includeDefault) { @@ -70,7 +69,7 @@ public class OnapCommandSchemaHttpLoader { } Map<String, List<Map<String, String>>> commandYamlMap = - (Map<String, List<Map<String, String>>>)OnapCommandSchemaLoader.validateSchemaVersion(schemaName, cmd.getSchemaVersion()); + (Map<String, List<Map<String, String>>>) OnapCommandSchemaLoader.validateSchemaVersion(schemaName, cmd.getSchemaVersion()); errors.addAll(parseHttpSchema(cmd, commandYamlMap, validateSchema)); @@ -417,8 +416,8 @@ public class OnapCommandSchemaHttpLoader { errorList.add(OnapCommandHttpConstants.HTTP_BODY_JSON_EMPTY); } else { try { - new ObjectMapper().readValue(body, JSONObject.class); - } catch (IOException e1) { // NOSONAR + gson.fromJson(body, JsonElement.class); + } catch (Exception e1) { // NOSONAR errorList.add(OnapCommandHttpConstants.HTTP_BODY_FAILED_PARSING); } } diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java index d1f88df4..0eb06c96 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java @@ -40,8 +40,9 @@ import org.onap.cli.fw.utils.OnapCommandUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.PathNotFoundException; @@ -50,6 +51,7 @@ import net.minidev.json.JSONArray; public class OnapCommandHttpUtils { static Logger LOG = LoggerFactory.getLogger(OnapCommandHttpUtils.class); + private static Gson gson = new GsonBuilder().serializeNulls().create(); /** * Set argument to param value. @@ -277,26 +279,32 @@ public class OnapCommandHttpUtils { } } - public static void normalizeJson(JsonNode node) { - Iterator<JsonNode> it = node.iterator(); + public static void normalizeJson(JsonElement node) { + Iterator<Entry<String, JsonElement>> it = node.getAsJsonObject().entrySet().iterator(); + while (it.hasNext()) { - JsonNode child = it.next(); - if (child.isTextual() && child.asText().equals("")) + JsonElement child = it.next().getValue(); + if (child.isJsonPrimitive() && child.getAsJsonPrimitive().isString() && child.getAsJsonPrimitive().getAsString().equals("")) it.remove(); - else if (child.isNull()) + else if (child.isJsonNull()) it.remove(); - else + else if (child.isJsonObject()) normalizeJson(child); + else if (child.isJsonArray()) { + for (JsonElement ele:child.getAsJsonArray()) { + if (ele.isJsonObject()) + normalizeJson(ele); + } + } } } public static String normalizeJson(String json) throws OnapCommandHttpInvalidRequestBody { - ObjectMapper mapper = new ObjectMapper(); - JsonNode node; + JsonElement node; try { - node = mapper.readTree(json); + node = gson.fromJson(json,JsonElement.class); normalizeJson(node); - return mapper.writeValueAsString(node); + return gson.toJson(node); } catch (Exception e) { //NOSONAR throw new OnapCommandHttpInvalidRequestBody(e); } |