diff options
35 files changed, 624 insertions, 293 deletions
diff --git a/deployment/zip/src/main/release/conf/log4j.properties b/deployment/zip/src/main/release/conf/log4j.properties deleted file mode 100644 index d535098e..00000000 --- a/deployment/zip/src/main/release/conf/log4j.properties +++ /dev/null @@ -1,30 +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, file -log4j.logger.org.onap.cli=ERROR, file -log4j.logger.org.open.infc.grpc.server=INFO, file, 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 - -# Redirect log messages to a log file, support file rolling. -log4j.appender.file=org.apache.log4j.RollingFileAppender -log4j.appender.file.File=${OPEN_CLI_HOME}/logs/open-cli.log -log4j.appender.file.MaxFileSize=5MB -log4j.appender.file.MaxBackupIndex=10 -log4j.appender.file.layout=org.apache.log4j.PatternLayout -log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n diff --git a/framework/pom.xml b/framework/pom.xml index 367ac63f..a743a979 100644 --- a/framework/pom.xml +++ b/framework/pom.xml @@ -88,14 +88,9 @@ <version>2.2.0</version> </dependency> <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-databind</artifactId> - <version>2.9.4</version> - </dependency> - <dependency> - <groupId>com.fasterxml.jackson.dataformat</groupId> - <artifactId>jackson-dataformat-yaml</artifactId> - <version>2.9.4</version> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.8.2</version> </dependency> <dependency> <groupId>junit</groupId> diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java index d73df4fb..29994d09 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java @@ -22,7 +22,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; - import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandHelpFailed; @@ -45,8 +44,9 @@ import org.onap.cli.fw.utils.OnapCommandUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + /** * Oclip Command. @@ -55,6 +55,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; public abstract class OnapCommand { private static Logger log = LoggerFactory.getLogger(OnapCommand.class); + private static Gson gson = new GsonBuilder().serializeNulls().create(); private String cmdDescription; @@ -180,8 +181,8 @@ public abstract class OnapCommand { } try { - return new ObjectMapper().writeValueAsString(args); - } catch (JsonProcessingException e) { + return gson.toJson(args); + } catch (Exception e) { // NOSONAR log.error("exception occured {}", e.getMessage()); return "{}"; } diff --git a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java index 3b13c9f8..d0b3c5f4 100644 --- a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java +++ b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java @@ -16,14 +16,14 @@ package org.onap.cli.fw.input; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; import org.onap.cli.fw.error.OnapCommandParameterMissing; import org.onap.cli.fw.utils.OnapCommandUtils; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -38,6 +38,11 @@ import java.util.UUID; public class OnapCommandParameter { /* + * Used locally for json conversion + */ + private static Gson gson = new GsonBuilder().serializeNulls().create(); + + /* * Name, for positional parameters, the place is decided from schema file definition */ private String cmdName; @@ -211,16 +216,16 @@ public class OnapCommandParameter { switch (parameterType) { case MAP: try { - defaultValue = new ObjectMapper().readValue(processedValue, Map.class); - } catch (IOException e) { + defaultValue = gson.fromJson(processedValue, Map.class); + } catch (Exception e) { // NOSONAR throw new OnapCommandInvalidParameterValue("Invalid default value for " + this.getName(), e); } break; case ARRAY: try { - defaultValue = new ObjectMapper().readValue(processedValue, List.class); - } catch (IOException e) { + defaultValue = gson.fromJson(processedValue, List.class); + } catch (Exception e) { // NOSONAR throw new OnapCommandInvalidParameterValue("Invalid default value for " + this.getName(), e); } break; diff --git a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java index 834dea06..c4be02e3 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java +++ b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java @@ -28,12 +28,11 @@ import java.util.StringTokenizer; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; -import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed; import org.onap.cli.fw.output.OnapCommandPrintDirection; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import net.minidev.json.JSONArray; import net.minidev.json.JSONObject; @@ -44,6 +43,8 @@ import net.minidev.json.JSONValue; */ public class OnapCommandPrint { + private static Gson gson = new GsonBuilder().serializeNulls().create(); + public static final int MAX_COLUMN_LENGTH = 50; private OnapCommandPrintDirection direction; @@ -272,8 +273,8 @@ public class OnapCommandPrint { array.add(rowO); } try { - return new ObjectMapper().readTree(array.toJSONString()).toString(); - } catch (IOException e) { + return gson.toJson(array.toJSONString()).toString(); + } catch (Exception e) { // NOSONAR // TODO Auto-generated catch block return array.toJSONString(); } @@ -281,11 +282,19 @@ public class OnapCommandPrint { } } + /* + required vulnerable fix + jackson-dataformat-yaml:YAMLMapper is a sub component of jackson-databind + jackson-databind is replaced with gson + JIRA: CLI-251 + */ public String printYaml() throws OnapCommandOutputPrintingFailed { - try { + /* try { return new YAMLMapper().writeValueAsString(new ObjectMapper().readTree(this.printJson())); } catch (IOException e) { throw new OnapCommandOutputPrintingFailed(e); // NOSONAR } + */ + return ""; } } diff --git a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java index ff8d85f1..d87f4ef9 100644 --- a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java +++ b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java @@ -26,13 +26,11 @@ import org.onap.cli.fw.cmd.OnapCommandType; import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.info.OnapCommandState; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; /** * OnapCommandSchemaInfo is used in discovery caching. * */ -@JsonIgnoreProperties(ignoreUnknown = true) public class OnapCommandSchemaInfo implements Comparable<OnapCommandSchemaInfo> { /** diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java index 2e63b03e..d43b51d8 100644 --- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java +++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java @@ -43,10 +43,14 @@ import org.onap.cli.fw.error.OnapCommandArtifactNotFound; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.stream.JsonReader; +import java.io.FileReader; public class OnapCommandArtifactStore { private static Logger log = LoggerFactory.getLogger(OnapCommandArtifactStore.class); + private static Gson gson = new GsonBuilder().serializeNulls().create(); private static boolean storeReady = false; @@ -180,8 +184,8 @@ public class OnapCommandArtifactStore { artifact.setCreateAt(dateFormatter.format(new Date())); artifact.setLastUpdatedAt(artifact.getCreateAt()); - FileUtils.writeStringToFile(new File(storePath), new ObjectMapper().writeValueAsString(artifact)); - } catch (NoSuchAlgorithmException | IOException e) { + FileUtils.writeStringToFile(new File(storePath), gson.toJson(artifact)); + } catch (Exception e) { // NOSONAR //It is expected that this never occurs log.error("Failed to store the artifact at " + storePath); } @@ -197,8 +201,8 @@ public class OnapCommandArtifactStore { } try { - return new ObjectMapper().readValue(FileUtils.readFileToString(aFile), Artifact.class); - } catch (IOException e) { + return gson.fromJson(FileUtils.readFileToString(aFile), Artifact.class); + } catch (Exception e) { // NOSONAR //It is expected that this never occurs log.error("Failed to retrieve the artifact at " + storePath); } @@ -234,9 +238,9 @@ public class OnapCommandArtifactStore { return name.matches(SP); } }*/)) { - try { - artifacts.add(new ObjectMapper().readValue(file, Artifact.class)); - } catch (IOException e) { + try (JsonReader jsonReader = new JsonReader(new FileReader(file))){ + artifacts.add(gson.fromJson(jsonReader, Artifact.class)); + } catch (Exception e) { // NOSONAR //It is expected that this never occurs log.error("While seraching Failed to retrieve the artifact at " + file.getAbsolutePath()); } @@ -303,12 +307,12 @@ public class OnapCommandArtifactStore { artifact.setMetadata(existing.getMetadata()); } - FileUtils.writeStringToFile(new File(newStorePath), new ObjectMapper().writeValueAsString(artifact)); + FileUtils.writeStringToFile(new File(newStorePath), gson.toJson(artifact)); if (!newStorePath.equalsIgnoreCase(existingStorePath)) { this.deleteArtifact(name, category); } - } catch (NoSuchAlgorithmException | IOException e) { + } catch (Exception e) { // NOSONAR //It is expected that this never occurs log.error("Failed to update the artifact at " + existingStorePath); } diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java index 09ecf05a..68d57c77 100644 --- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java +++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java @@ -33,15 +33,23 @@ import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandPersistProfileFailed; import org.onap.cli.fw.error.OnapCommandProfileLoadFailed; -import org.onap.cli.fw.error.OnapCommandProfileNotFound; import org.onap.cli.fw.input.cache.OnapCommandParamEntity; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.io.FileWriter; +import java.io.FileReader; +import java.io.Writer; +import java.io.Reader; + + public class OnapCommandProfileStore { private static Logger log = LoggerFactory.getLogger(OnapCommandProfileStore.class); + private Gson gson = new GsonBuilder().setPrettyPrinting().create(); + private Map<String, Map<String, String>> paramCache = new HashMap<>(); private static OnapCommandProfileStore single = null; @@ -165,11 +173,10 @@ public class OnapCommandProfileStore { public void persistProfile(List<OnapCommandParamEntity> params, String profileName) throws OnapCommandPersistProfileFailed { if (params != null) { String dataDir = getDataStorePath(); - try { File file = new File(dataDir + File.separator + profileName + DATA_PATH_PROFILE_JSON); - ObjectMapper mapper = new ObjectMapper(); - mapper.writerWithDefaultPrettyPrinter().writeValue(file, params); - } catch (IOException e1) { + try (Writer writer = new FileWriter(file)){ + gson.toJson(params, writer); + } catch (Exception e1) { // NOSONAR throw new OnapCommandPersistProfileFailed(e1); } } @@ -178,17 +185,18 @@ public class OnapCommandProfileStore { public List<OnapCommandParamEntity> loadParamFromCache(String profileName) throws OnapCommandException { List<OnapCommandParamEntity> params = new ArrayList<>(); String dataDir = getDataStorePath(); - try { - File file = new File(dataDir + File.separator + profileName + DATA_PATH_PROFILE_JSON); - if (file.exists()) { - ObjectMapper mapper = new ObjectMapper(); - OnapCommandParamEntity[] list = mapper.readValue(file, OnapCommandParamEntity[].class); + File file = new File(dataDir + File.separator + profileName + DATA_PATH_PROFILE_JSON); + if (file.exists()) { + try (Reader jsonReader = new FileReader(file)){ + + OnapCommandParamEntity[] list = gson.fromJson(jsonReader, + OnapCommandParamEntity[].class); params.addAll(Arrays.asList(list)); // } else { // throw new OnapCommandProfileNotFound(profileName); + } catch (Exception e) { // NOSONAR + throw new OnapCommandProfileLoadFailed(e); } - } catch (IOException e) { - throw new OnapCommandProfileLoadFailed(e); } return params; diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java index a94087ec..105f68dc 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java @@ -58,10 +58,16 @@ import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.yaml.snakeyaml.Yaml; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.stream.JsonReader; +import java.io.FileReader; +import java.io.Writer; +import java.io.FileWriter; + public class OnapCommandDiscoveryUtils { + private static Gson gson = new GsonBuilder().serializeNulls().create(); /** * Fetch a particular schema details. @@ -90,10 +96,10 @@ public class OnapCommandDiscoveryUtils { throw new OnapCommandNotFound(cmd, version); return schemaInfo; - } + } /** - * Load the previous discovered json file. + * Load the previous discovered json file. * * @return list * @throws OnapCommandInvalidSchema @@ -148,12 +154,11 @@ public class OnapCommandDiscoveryUtils { if (!OnapCommandDiscoveryUtils.isAlreadyDiscovered()) return schemas; String dataDir = OnapCommandDiscoveryUtils.getDataStorePath(); - try { - File file = new File(dataDir + File.separator + DISCOVERY_FILE); - ObjectMapper mapper = new ObjectMapper(); - OnapCommandSchemaInfo[] list = mapper.readValue(file, OnapCommandSchemaInfo[].class); + File file = new File(dataDir + File.separator + DISCOVERY_FILE); + try (JsonReader jsonReader = new JsonReader(new FileReader(file))){ + OnapCommandSchemaInfo[] list = gson.fromJson(jsonReader, OnapCommandSchemaInfo[].class); schemas.addAll(Arrays.asList(list)); - } catch (IOException e) { + } catch (Exception e) { // NOSONAR throw new OnapCommandDiscoveryFailed(dataDir, DISCOVERY_FILE, e); } @@ -189,10 +194,10 @@ public class OnapCommandDiscoveryUtils { FileUtils.forceMkdir(new File(dataDir)); File file = new File(dataDir + File.separator + DISCOVERY_FILE); - ObjectMapper mapper = new ObjectMapper(); - mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); - mapper.writerWithDefaultPrettyPrinter().writeValue(file, schemas); - } catch (IOException e1) { + try(Writer writer = new FileWriter(file)){ + gson.toJson(schemas,writer); + } + } catch (Exception e1) { // NOSONAR throw new OnapCommandDiscoveryFailed(dataDir, DISCOVERY_FILE, e1); } @@ -552,7 +557,7 @@ public class OnapCommandDiscoveryUtils { * @throws OnapCommandInvalidSchema * exception */ - public static Map<String, ?> loadYaml(String filePath) throws OnapCommandInvalidSchema { + public static Map<String, ?> loadYaml(String filePath) throws OnapCommandInvalidSchema { Map<String, ?> values = null; try { values = (Map<String, Object>) new Yaml().load(FileUtils.readFileToString(new File(filePath))); diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java index 96f864e0..043ec8ed 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java @@ -46,10 +46,11 @@ import org.onap.cli.fw.input.OnapCommandParameterType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.jayway.jsonpath.JsonPath; + /** * Provides helper method to parse Yaml files and produce required objects. * @@ -57,6 +58,8 @@ import com.jayway.jsonpath.JsonPath; public class OnapCommandUtils { static Logger log = LoggerFactory.getLogger(OnapCommandUtils.class); + private static Gson gson = new GsonBuilder().serializeNulls().create(); + /** * Private constructor. */ @@ -336,13 +339,13 @@ public class OnapCommandUtils { currentIdx = idxE + 2; } else if (OnapCommandParameterType.MAP.equals(param.getParameterType())) { try { - String value = new ObjectMapper().writeValueAsString(params.get(paramName).getValue()); + String value = gson.toJson(params.get(paramName).getValue()); if ((idxS == 0) && (currentIdx == 0)) { result = value; } else { result += line.substring(currentIdx, idxS - 1) + value; } - } catch (JsonProcessingException e) { // NOSONAR + } catch (Exception e) { // NOSONAR //never occur as map is coverted to json string here } diff --git a/framework/src/main/resources/log4j.properties b/framework/src/main/resources/log4j.properties deleted file mode 100644 index d535098e..00000000 --- a/framework/src/main/resources/log4j.properties +++ /dev/null @@ -1,30 +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, file -log4j.logger.org.onap.cli=ERROR, file -log4j.logger.org.open.infc.grpc.server=INFO, file, 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 - -# Redirect log messages to a log file, support file rolling. -log4j.appender.file=org.apache.log4j.RollingFileAppender -log4j.appender.file.File=${OPEN_CLI_HOME}/logs/open-cli.log -log4j.appender.file.MaxFileSize=5MB -log4j.appender.file.MaxBackupIndex=10 -log4j.appender.file.layout=org.apache.log4j.PatternLayout -log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n diff --git a/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterTest.java b/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterTest.java index 18959e8e..1bf42e00 100644 --- a/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterTest.java +++ b/framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterTest.java @@ -129,4 +129,33 @@ public class OnapCommandParameterTest { param.validate(); } + @Test + public void parameterObjTestForGson() throws OnapCommandInvalidParameterValue { + OnapCommandParameter param = new OnapCommandParameter(); + param.setDefaultValue("defaultValue"); + param.setDescription("description"); + param.setLongOption("longOption"); + param.setName("name"); + param.setOptional(true); + param.setParameterType(OnapCommandParameterType.JSON); + param.setSecured(false); + param.setShortOption("shortOption"); + param.setValue("value"); + + param.setParameterType(OnapCommandParameterType.ARRAY); + List<String> list = Arrays.asList("1", "2", "3"); + param.setValue(list); + assertTrue(((List)param.getValue()).containsAll(list)); + + param.setRawDefaultValue("[\"1\", \"2\", \"3\", \"4\"]"); + assertTrue(((List<String>)param.getDefaultValue()) + .containsAll(Arrays.asList("1", "2", "3", "4"))); + + param.setParameterType(OnapCommandParameterType.MAP); + param.setRawDefaultValue("{\"testKey\":\"testValue\"}"); + assertTrue(((Map<String, String>)param.getDefaultValue()).values().containsAll( + Arrays.asList("testValue") + )); + } + } diff --git a/framework/src/test/java/org/onap/cli/fw/output/OnapCommandResultTest.java b/framework/src/test/java/org/onap/cli/fw/output/OnapCommandResultTest.java index a4458670..97fc9e01 100644 --- a/framework/src/test/java/org/onap/cli/fw/output/OnapCommandResultTest.java +++ b/framework/src/test/java/org/onap/cli/fw/output/OnapCommandResultTest.java @@ -28,7 +28,11 @@ import org.junit.Test; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.input.OnapCommandParameterType; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + public class OnapCommandResultTest { + private static Gson gson = new GsonBuilder().serializeNulls().create(); @Test @Ignore @@ -206,5 +210,30 @@ public class OnapCommandResultTest { assertEquals(expRes, result); } + @Test + public void commandResultPrintLandscapeJsonTestForGson() throws OnapCommandException { + OnapCommandResult res = new OnapCommandResult(); + res.setDebugInfo("debugInfo"); + res.setIncludeSeparator(true); + res.setIncludeTitle(true); + res.setOutput("Output"); + res.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE); + OnapCommandResultAttribute att = new OnapCommandResultAttribute(); + att.setName("param"); + att.setDescription("description"); + att.setType(OnapCommandParameterType.JSON); + att.setValues( + new ArrayList<String>(Arrays.asList(new String[] { "{\"id\": \"0001\",\"value\": \"result\"}" }))); + List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>(); + list.add(att); + res.setRecords(list); + res.setScope(OnapCommandResultAttributeScope.LONG); + res.setType(OnapCommandResultType.JSON); + + String result = res.print(); + String expRes="[{\"param\":{\"id\":\"0001\",\"value\":\"result\"}}]"; + assertEquals(gson.toJson(expRes),result); + + } } diff --git a/framework/src/test/java/org/onap/cli/fw/store/OnapCommandArtifactStoreTest.java b/framework/src/test/java/org/onap/cli/fw/store/OnapCommandArtifactStoreTest.java index 1a4d982a..45186b38 100644 --- a/framework/src/test/java/org/onap/cli/fw/store/OnapCommandArtifactStoreTest.java +++ b/framework/src/test/java/org/onap/cli/fw/store/OnapCommandArtifactStoreTest.java @@ -28,7 +28,13 @@ import java.io.File; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.*; +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; public class OnapCommandArtifactStoreTest { OnapCommandArtifactStore onapCommandArtifactStore; @@ -83,4 +89,35 @@ public class OnapCommandArtifactStoreTest { public void listArtifactTest() throws OnapCommandArtifactNotFound { assertNotNull(onapCommandArtifactStore.listArtifact("category","namePattern")); } + @Test + public void createArtifactTestForGson() throws OnapCommandArtifactContentChecksumNotMatch, OnapCommandArtifactAlreadyExist, OnapCommandArtifactContentNotExist, OnapCommandArtifactNotFound { + assertNotNull(onapCommandArtifactStore.createArtifact(artifact)); + String artifactPath=System.getProperty("user.dir")+File.separator+"data/artifacts"; + File artifactFile= new File(artifactPath+File.separator+"artifact__category.json"); + assertTrue(artifactFile.exists()); + onapCommandArtifactStore.deleteArtifact("artifact","category"); + + } + + @Test + public void listArtifactTestForGson() throws OnapCommandArtifactNotFound { + String basePath= System.getProperty("user.dir")+File.separator+"data/artifacts/testFile.json"; + File testFile = new File(basePath); + if (!testFile.exists()) { + try { + testFile.createNewFile(); + try(BufferedWriter writer = new BufferedWriter(new FileWriter(testFile))) { + String content = "{\"name\": \"name\",\"description\": \"description\",\"categoty\": \"categoty\"," + + "\"checksum\": \"checksum\",\"size\": 100,\"createAt\": \"createAt\"" + + ",\"lastUpdatedAt\":\"lastUpdatedAt\",\"path\": \"path\",\"metadata\": {}}"; + writer.write(content); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + assertNotNull(onapCommandArtifactStore.listArtifact("category","namePattern")); + + testFile.delete(); + } }
\ No newline at end of file diff --git a/grpc/grpc-client/pom.xml b/grpc/grpc-client/pom.xml index 8cfc2191..db3ba6fb 100644 --- a/grpc/grpc-client/pom.xml +++ b/grpc/grpc-client/pom.xml @@ -40,10 +40,10 @@ <version>${project.version}</version> </dependency> <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.7.16</version> - </dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.7.16</version> + </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> diff --git a/grpc/grpc-client/src/main/resources/log4j.properties b/grpc/grpc-client/src/main/resources/log4j.properties deleted file mode 100644 index 778641ed..00000000 --- a/grpc/grpc-client/src/main/resources/log4j.properties +++ /dev/null @@ -1,32 +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, file - -log4j.logger.org.onap.cli=DEBUG, file -log4j.logger.org.open.infc=DEBUG, file - -# 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 - -# Redirect log messages to a log file, support file rolling. -log4j.appender.file=org.apache.log4j.RollingFileAppender -log4j.appender.file.File=${OPEN_CLI_HOME}/logs/open-cli.log -log4j.appender.file.MaxFileSize=5MB -log4j.appender.file.MaxBackupIndex=10 -log4j.appender.file.layout=org.apache.log4j.PatternLayout -log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n diff --git a/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java b/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java index 9f325067..8b9448b7 100644 --- a/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java +++ b/grpc/grpc-server/src/main/java/org/open/infc/grpc/server/OpenInterfaceGrpcServer.java @@ -45,7 +45,8 @@ import org.open.infc.grpc.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import io.grpc.Server; import io.grpc.ServerBuilder; @@ -54,6 +55,7 @@ import io.grpc.stub.StreamObserver; public class OpenInterfaceGrpcServer { private static final Logger logger = LoggerFactory.getLogger(OpenInterfaceGrpcServer.class.getName()); + private static Gson gson = new GsonBuilder().serializeNulls().create(); private static final String CONF_FILE = "oclip-grpc-server.properties"; private static final String CONF_SERVER_PORT = "oclip.grpc_server_port"; @@ -246,8 +248,8 @@ public class OpenInterfaceGrpcServer { reply.setSuccess(cmd.getResult().isPassed()); try { - reply.putAttrs(OnapCommandConstants.RESULTS, new ObjectMapper().readTree(printOut).toString()); - } catch (IOException e) { + reply.putAttrs(OnapCommandConstants.RESULTS, gson.fromJson(printOut,String.class)); + } catch (Exception e) { // NOSONAR reply.putAttrs(OnapCommandConstants.RESULTS, printOut); } diff --git a/grpc/grpc-server/src/main/resources/log4j.properties b/grpc/grpc-server/src/main/resources/log4j.properties deleted file mode 100644 index 29a491cd..00000000 --- a/grpc/grpc-server/src/main/resources/log4j.properties +++ /dev/null @@ -1,32 +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, file - -log4j.logger.org.onap.cli=DEBUG, file, stdout -log4j.logger.org.open.infc=DEBUG, file, 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 - -# Redirect log messages to a log file, support file rolling. -log4j.appender.file=org.apache.log4j.RollingFileAppender -log4j.appender.file.File=${OPEN_CLI_HOME}/logs/open-cli.log -log4j.appender.file.MaxFileSize=5MB -log4j.appender.file.MaxBackupIndex=10 -log4j.appender.file.layout=org.apache.log4j.PatternLayout -log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n diff --git a/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java b/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java index a07c08f2..3dcb74ed 100644 --- a/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java +++ b/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java @@ -35,16 +35,21 @@ import org.onap.cli.main.error.OnapCliArgumentValueMissing; import org.onap.cli.main.error.OnapCliInvalidArgument; import org.yaml.snakeyaml.Yaml; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.reflect.TypeToken; -import net.minidev.json.JSONObject; +import java.io.Reader; +import java.io.InputStreamReader; +import java.io.FileReader; /** * Oclip CLI utilities. * */ public class OnapCliArgsParser { + private static Gson gson = new GsonBuilder().serializeNulls().create(); /** * private Constructor. @@ -193,18 +198,24 @@ public class OnapCliArgsParser { } public static String readJsonStringFromUrl(String input, String argName) throws OnapCliInvalidArgument { - ObjectMapper mapper = new ObjectMapper(); + String jsonValue; try { File file = new File(input); if (file.isFile()) { - return mapper.readValue(file, JSONObject.class).toJSONString(); + try(Reader reader = new FileReader(file)){ + jsonValue = gson.fromJson(reader, JsonElement.class).toString(); + } + return jsonValue; } else if (input.startsWith("file:") || input.startsWith("http:") || input.startsWith("ftp:")) { URL jsonUrl = new URL(input); - return mapper.readValue(jsonUrl, JSONObject.class).toJSONString(); + try(Reader reader = new InputStreamReader(jsonUrl.openStream())){ + jsonValue = gson.fromJson(reader, JsonElement.class).toString(); + } + return jsonValue; } else { - return mapper.readValue(input, JSONObject.class).toJSONString(); + return gson.fromJson(input, JsonElement.class).toString(); } - } catch (IOException e) { + } catch (Exception e) { // NOSONAR throw new OnapCliInvalidArgument(argName, e); } } @@ -255,21 +266,21 @@ public class OnapCliArgsParser { } public static List<String> convertJsonToListString(String arg, String json) throws OnapCliInvalidArgument { - TypeReference<List<String>> mapType = new TypeReference<List<String>>() { + TypeToken<List<String>> mapType = new TypeToken<List<String>>() { }; try { - return new ObjectMapper().readValue(json, mapType); - } catch (IOException e) { + return gson.fromJson(json, mapType.getType()); + } catch (Exception e) { // NOSONAR throw new OnapCliInvalidArgument(arg, e); } } public static Map<String, String> convertJsonToMapString(String arg, String json) throws OnapCliInvalidArgument { - TypeReference<Map<String, String>> mapType = new TypeReference<Map<String, String>>() { + TypeToken<Map<String, String>> mapType = new TypeToken<Map<String, String>>() { }; try { - return new ObjectMapper().readValue(json, mapType); - } catch (IOException e) { + return gson.fromJson(json, mapType.getType()); + } catch (Exception e) { // NOSONAR throw new OnapCliInvalidArgument(arg, e); } } diff --git a/products/sample/src/main/resources/open-cli-schema/reqres-create-user.yaml b/products/sample/src/main/resources/open-cli-schema/reqres-create-user.yaml new file mode 100644 index 00000000..1dc6985b --- /dev/null +++ b/products/sample/src/main/resources/open-cli-schema/reqres-create-user.yaml @@ -0,0 +1,77 @@ +# Copyright 2020 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. + +open_cli_schema_version: 1.0 +name: create-user +description: | + Place this OCS YAML under $OPEN_CLI_HOME/open-cli-schema folder. Then run the commands + oclip schema-refresh + + It is used to create a new user record at https://reqres.in. + + Sample usage: + oclip --product test create-user --host-url https://reqres.in --user-name Priyanka --job-role Developer + +info: + product: test + service: ut + author: Priyanka Akhade priyanka.akhade@huawei.com + +parameters: + - name: name + description: User name + type: string + short_option: n + long_option: user-name + is_optional: false + - name: job + description: job role + type: string + short_option: r + long_option: job-role + is_optional: true + default_value: Software Engineer + +results: + direction: landscape + attributes: + - name: ID + description: user id + scope: short + type: string + - name: NAME + description: user name + scope: short + type: string + - name : DESIGNATION + description: job role + scope: short + type: string +http: + service: + auth: none + mode: direct + request: + uri: /api/users + method: POST + body: '{ + "name":"${name}", + "job":"${job}" + }' + success_codes: + - 201 + result_map: + ID: $b{$.id} + NAME: $b{$.name} + DESIGNATION: $b{$.job} diff --git a/products/sample/src/main/resources/open-cli-schema/reqres-delete-user.yaml b/products/sample/src/main/resources/open-cli-schema/reqres-delete-user.yaml new file mode 100644 index 00000000..706ba0ed --- /dev/null +++ b/products/sample/src/main/resources/open-cli-schema/reqres-delete-user.yaml @@ -0,0 +1,43 @@ +# Copyright 2020 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. + +open_cli_schema_version: 1.0 +name: delete-user +description: | + Place this OCS YAML under $OPEN_CLI_HOME/open-cli-schema folder. Then run the commands + oclip schema-refresh + + It is used to delete a user record at https://reqres.in. + + Sample usage: + oclip --product test delete-user --host-url https://reqres.in + +info: + product: test + service: ut + author: Priyanka Akhade priyanka.akhade@huawei.com + + +results: + direction: landscape + +http: + service: + auth: none + mode: direct + request: + uri: /api/users/2 + method: DELETE + success_codes: + - 204 diff --git a/products/sample/src/main/resources/open-cli-schema/reqres-list-users.yaml b/products/sample/src/main/resources/open-cli-schema/reqres-list-users.yaml new file mode 100644 index 00000000..eae51fd9 --- /dev/null +++ b/products/sample/src/main/resources/open-cli-schema/reqres-list-users.yaml @@ -0,0 +1,58 @@ +# Copyright 2020 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. + +open_cli_schema_version: 1.0 +name: list-users +description: | + Place this OCS YAML under $OPEN_CLI_HOME/open-cli-schema folder. Then run the commands + oclip schema-refresh + + It is used to display the users' details from https://reqres.in. + + Sample usage: + oclip --product test list-users --host-url https://reqres.in + +info: + product: test + service: ut + author: Priyanka Akhade priyanka.akhade@huawei.com + +results: + direction: landscape + attributes: + - name: FIRST_NAME + description: FirstName + scope: short + type: string + - name: LAST_NAME + description: LastName + scope: short + type: string + - name: EMAIL + description: Email + scope: short + type: string +http: + service: + auth: none + mode: direct + request: + uri: /api/users?page=2 + method: GET + success_codes: + - 200 + result_map: + FIRST_NAME: $b{$.data.[*].first_name} + LAST_NAME: $b{$.data.[*].last_name} + EMAIL: $b{$.data.[*].email} diff --git a/products/sample/src/main/resources/open-cli-schema/reqres-single-user.yaml b/products/sample/src/main/resources/open-cli-schema/reqres-single-user.yaml new file mode 100644 index 00000000..98db2ac6 --- /dev/null +++ b/products/sample/src/main/resources/open-cli-schema/reqres-single-user.yaml @@ -0,0 +1,58 @@ +# Copyright 2020 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. + +open_cli_schema_version: 1.0 +name: single-user +description: | + Place this OCS YAML under $OPEN_CLI_HOME/open-cli-schema folder. Then run the commands + oclip schema-refresh + + It is used to display the specific user's details from https://reqres.in. + + Sample usage: + oclip --product test single-user --host-url https://reqres.in + +info: + product: test + service: ut + author: Priyanka Akhade priyanka.akhade@huawei.com + +results: + direction: landscape + attributes: + - name: FIRST_NAME + description: demo response + scope: short + type: string + - name: LAST_NAME + description: LastName + scope: short + type: string + - name: EMAIL + description: Email + scope: short + type: string +http: + service: + auth: none + mode: direct + request: + uri: /api/users/2 + method: GET + success_codes: + - 200 + result_map: + FIRST_NAME: $b{$.data.first_name} + LAST_NAME: $b{$.data.last_name} + EMAIL: $b{$.data.email} diff --git a/products/sample/src/main/resources/open-cli-schema/reqres-update-user.yaml b/products/sample/src/main/resources/open-cli-schema/reqres-update-user.yaml new file mode 100644 index 00000000..ee5e07bb --- /dev/null +++ b/products/sample/src/main/resources/open-cli-schema/reqres-update-user.yaml @@ -0,0 +1,77 @@ +# Copyright 2020 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. + +open_cli_schema_version: 1.0 +name: update-user +description: | + Place this OCS YAML under $OPEN_CLI_HOME/open-cli-schema folder. Then run the commands + oclip schema-refresh + + It is used to update the specific user's details at https://reqres.in. + + Sample usage: + oclip --product test update-user --host-url https://reqres.in --user-name Priyanka --job-role Tester + +info: + product: test + service: ut + author: Priyanka Akhade priyanka.akhade@huawei.com + +parameters: + - name: name + description: User name + type: string + short_option: n + long_option: user-name + is_optional: false + - name: job + description: job role + type: string + short_option: r + long_option: job-role + is_optional: true + default_value: Software Engineer + +results: + direction: landscape + attributes: + - name: NAME + description: user name + scope: short + type: string + - name : DESIGNATION + description: job role + scope: short + type: string + - name : UPDATED_AT + description: updated DateTime + scope: short + type: string +http: + service: + auth: none + mode: direct + request: + uri: /api/users/2 + method: PUT + body: '{ + "name":"${name}", + "job":"${job}" + }' + success_codes: + - 200 + result_map: + NAME: $b{$.name} + DESIGNATION: $b{$.job} + UPDATED_AT: $b{$.updatedAt} 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 <version>2.2.0</version> </dependency> <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-databind</artifactId> - <version>2.9.4</version> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.8.2</version> </dependency> <dependency> <groupId>org.onap.cli</groupId> @@ -110,6 +110,11 @@ Excluded commons-codec vulnerable version and added invulnerable version <groupId>io.netty</groupId> <artifactId>netty-codec-http</artifactId> </exclusion> + <!--JIRA: CLI-253--> + <exclusion> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + </exclusion> </exclusions> </dependency> <dependency> 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); } 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 @@ <groupId>org.snmp4j</groupId> <artifactId>snmp4j</artifactId> <version>2.5.6</version> - </dependency> + </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> 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 diff --git a/validate/sample-mock-generator/pom.xml b/validate/sample-mock-generator/pom.xml index 63bf1e47..a4bee09c 100644 --- a/validate/sample-mock-generator/pom.xml +++ b/validate/sample-mock-generator/pom.xml @@ -60,9 +60,9 @@ <scope>test</scope> </dependency> <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-databind</artifactId> - <version>2.9.4</version> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.8.2</version> </dependency> </dependencies> <build><plugins> diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java index 4b2e116c..c0258ae5 100644 --- a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java @@ -19,21 +19,24 @@ import java.io.File; import java.io.IOException; import java.util.Arrays; -import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.io.FileWriter; + public class MockJsonGenerator { - public static void generateMocking(MockRequest mockRequest, MockResponse mockResponse, - String jsonFilePath) throws IOException { + private static Gson gson = new GsonBuilder().serializeNulls().create(); + + public static void generateMocking(MockRequest mockRequest, MockResponse mockResponse, String jsonFilePath) throws IOException { MockObject mockObject = new MockObject(); mockObject.setRequest(mockRequest); mockObject.setResponse(mockResponse); - ObjectMapper mapper = new ObjectMapper(); - ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); - writer.writeValue(new File(jsonFilePath), - Arrays.asList(mockObject)); + try(FileWriter writer = new FileWriter(jsonFilePath)){ + gson.toJson(Arrays.asList(mockObject), writer); + }catch (Exception e){ // NOSONAR + // + } } } diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java index d1d8b08d..2ad9b85c 100644 --- a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java @@ -20,15 +20,16 @@ import java.io.IOException; import java.net.URL; import java.util.Map; -import com.fasterxml.jackson.core.JsonFactory; -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; public class MockRequest { private String method; private String uri; private Map<String, String> headers; - private JsonNode json; + private JsonElement json; + private static Gson gson = new GsonBuilder().serializeNulls().create(); public String getMethod() { return method; @@ -56,17 +57,16 @@ public class MockRequest { this.headers = headers; } - public JsonNode getJson() { + public JsonElement getJson() { return json; } public void setJson(String json) throws IOException { if (!json.isEmpty()) { try { - ObjectMapper objectMapper = new ObjectMapper(); - this.json = objectMapper.readTree(json); + this.json = gson.fromJson(json,JsonElement.class); } catch (Exception e) { - this.json = new ObjectMapper().readTree("{}"); + this.json = gson.fromJson("{}",JsonElement.class); } } diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java index e49e4d41..9184d3ad 100644 --- a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java @@ -18,12 +18,14 @@ package org.onap.cli.http.mock; import java.io.IOException; -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; public class MockResponse { private int status; - private JsonNode json; + private JsonElement json; + private static Gson gson = new GsonBuilder().serializeNulls().create(); public int getStatus() { return status; @@ -33,17 +35,16 @@ public class MockResponse { this.status = status; } - public JsonNode getJson() { + public JsonElement getJson() { return json; } public void setJson(String json) throws IOException { if (json != null && !json.isEmpty()) { try { - ObjectMapper objectMapper = new ObjectMapper(); - this.json = objectMapper.readTree(json); + this.json = gson.fromJson(json,JsonElement.class); } catch (Exception e) { - this.json = new ObjectMapper().readTree("{}"); + this.json = gson.fromJson("{}", JsonElement.class); } } } diff --git a/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java b/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java index 181ae515..e94de60a 100644 --- a/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java +++ b/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java @@ -17,6 +17,7 @@ package org.onap.cli.validation; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import java.io.File; @@ -48,6 +49,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + public class OnapValidationTest { public static final String SAMPLE_VERSION = "open_cli_sample_version"; @@ -64,6 +68,7 @@ public class OnapValidationTest { OnapCli cli = new OnapCli(); private static Logger LOG = LoggerFactory.getLogger(OnapValidationTest.class); + private static Gson gson = new GsonBuilder().serializeNulls().create(); private void handle(String[] args) { cli.resetExitCode(); @@ -261,4 +266,11 @@ public class OnapValidationTest { onapCli.handle(); assertEquals(OnapCliConstants.EXIT_SUCCESS, onapCli.getExitCode()); } + @Test + public void testOnapCommandSchemaInfoForUnknownFields(){ + OnapCommandSchemaInfo ocsi = new OnapCommandSchemaInfo(); + String testExp = "{\"schemaName\":\"testSchema\",\"schemaURI\":\"testUri\",\"unknownField\":\"unknown\"}"; + ocsi= gson.fromJson(testExp,OnapCommandSchemaInfo.class); + assertNotNull(ocsi); + } } |