From 132177fb71c08f157fb42037f9dd26ceb2d0ba43 Mon Sep 17 00:00:00 2001 From: "priyanka.akhade" Date: Fri, 28 Feb 2020 05:47:34 +0000 Subject: Migrate to gson Issue-ID: CLI-247 Signed-off-by: priyanka.akhade Change-Id: If66134db5503382fb45210ece883402fc130ac93 --- profiles/http/pom.xml | 11 ++++++-- .../java/org/onap/cli/fw/http/mock/MocoServer.java | 9 +++--- .../http/schema/OnapCommandSchemaHttpLoader.java | 17 ++++++------ .../cli/fw/http/utils/OnapCommandHttpUtils.java | 32 ++++++++++++++-------- profiles/snmp/pom.xml | 2 +- profiles/snmp/src/test/resources/log4j.properties | 23 ---------------- 6 files changed, 42 insertions(+), 52 deletions(-) delete mode 100644 profiles/snmp/src/test/resources/log4j.properties (limited to 'profiles') diff --git a/profiles/http/pom.xml b/profiles/http/pom.xml index fe2cee86..058749d7 100644 --- a/profiles/http/pom.xml +++ b/profiles/http/pom.xml @@ -77,9 +77,9 @@ Excluded commons-codec vulnerable version and added invulnerable version 2.2.0 - com.fasterxml.jackson.core - jackson-databind - 2.9.4 + com.google.code.gson + gson + 2.8.2 org.onap.cli @@ -110,6 +110,11 @@ Excluded commons-codec vulnerable version and added invulnerable version io.netty netty-codec-http + + + com.fasterxml.jackson.core + jackson-databind + 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 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 loadHttpSchema(OnapHttpCommand cmd, String schemaName, boolean includeDefault, - boolean validateSchema) throws OnapCommandException { + public static List loadHttpSchema(OnapHttpCommand cmd, String schemaName, boolean includeDefault, boolean validateSchema) throws OnapCommandException { try { List errors = new ArrayList<>(); if (includeDefault) { @@ -70,7 +69,7 @@ public class OnapCommandSchemaHttpLoader { } Map>> commandYamlMap = - (Map>>)OnapCommandSchemaLoader.validateSchemaVersion(schemaName, cmd.getSchemaVersion()); + (Map>>) 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 it = node.iterator(); + public static void normalizeJson(JsonElement node) { + Iterator> 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); } diff --git a/profiles/snmp/pom.xml b/profiles/snmp/pom.xml index ba59cf8d..293230ff 100644 --- a/profiles/snmp/pom.xml +++ b/profiles/snmp/pom.xml @@ -43,7 +43,7 @@ org.snmp4j snmp4j 2.5.6 - + junit junit diff --git a/profiles/snmp/src/test/resources/log4j.properties b/profiles/snmp/src/test/resources/log4j.properties deleted file mode 100644 index 78f32285..00000000 --- a/profiles/snmp/src/test/resources/log4j.properties +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2018 Huawei Technologies Co., Ltd. -# -# 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. - -log4j.rootLogger=ERROR, stdout - -log4j.logger.org.onap.cli=DEBUG, stdout - -# Direct log messages to stdout -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.Target=System.out -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n -- cgit 1.2.3-korg