diff options
author | priyanka.akhade <priyanka.akhade@huawei.com> | 2020-04-13 13:16:29 +0000 |
---|---|---|
committer | priyanka.akhade <priyanka.akhade@huawei.com> | 2020-05-04 06:43:16 +0000 |
commit | 1db7e72d979afa03bca38d3008f55246f8d6a6c1 (patch) | |
tree | 547661ba2a459201a5aa810e92dfa2941bff4f69 | |
parent | 2bb3344c6b3ec9bd77d1491d5beb579bdabc62f2 (diff) |
Migrate to Yamlbeans
Issue-ID: CLI-248
Signed-off-by: priyanka.akhade <priyanka.akhade@huawei.com>
Change-Id: I182c7a2e2f41bf09990e5b1ac0344f49af33e0c5
12 files changed, 86 insertions, 34 deletions
diff --git a/framework/pom.xml b/framework/pom.xml index 6d60d8b2..9878cd84 100644 --- a/framework/pom.xml +++ b/framework/pom.xml @@ -49,9 +49,9 @@ <version>5.0.3-SNAPSHOT</version> </dependency> <dependency> - <groupId>org.yaml</groupId> - <artifactId>snakeyaml</artifactId> - <version>1.18</version> + <groupId>com.esotericsoftware.yamlbeans</groupId> + <artifactId>yamlbeans</artifactId> + <version>1.13</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapProductsListCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapProductsListCommand.java index d0788c16..10d9c448 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapProductsListCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapProductsListCommand.java @@ -28,7 +28,6 @@ import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.schema.OnapCommandSchema; import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; -import org.yaml.snakeyaml.Yaml; /** * Product list. @@ -58,7 +57,7 @@ public class OnapProductsListCommand extends OnapCommand { "/" + product + OnapCommandConstants.PRODUCT_REGISTRY_YAML); if (stream != null) { - Map<String, ?> map = (Map<String, ?>) new Yaml().load(stream); + Map<String, ?> map = OnapCommandDiscoveryUtils.loadYaml(stream); Map<String, String> productMap = (Map<String, String>) map.get("product"); String description = (String) productMap.get(OnapCommandConstants.DESCRIPTION); this.getResult().getRecordsMap().get("description").getValues().add(description.trim()); diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java index fa4f60b9..880f83ea 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/product/OnapServiceListCommand.java @@ -29,7 +29,6 @@ import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.schema.OnapCommandSchema; import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; -import org.yaml.snakeyaml.Yaml; /** * Service list. @@ -62,7 +61,7 @@ public class OnapServiceListCommand extends OnapCommand { Map<String, String> serviceDescs = new HashMap<>(); if (stream != null) { - Map<String, ?> map = (Map<String, ?>) new Yaml().load(stream); + Map<String, ?> map = OnapCommandDiscoveryUtils.loadYaml(stream); if (map.containsKey("services")) { List<Map<String, String>> services = (List) map.get("services"); diff --git a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java index a1a14745..41be2332 100644 --- a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java +++ b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaLoader.java @@ -88,7 +88,6 @@ import org.onap.cli.fw.output.OnapCommandResultAttributeScope; import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; import org.onap.cli.fw.utils.OnapCommandUtils; import org.springframework.core.io.Resource; -import org.yaml.snakeyaml.Yaml; public class OnapCommandSchemaLoader { @@ -541,13 +540,7 @@ public class OnapCommandSchemaLoader { * exception */ public static Map<String, ?> loadSchema(InputStream stream, String schemaName) throws OnapCommandInvalidSchema { - Map<String, ?> values = null; - try { - values = (Map<String, ?>) new Yaml().load(stream); - } catch (Exception e) { - throw new OnapCommandInvalidSchema(schemaName, e); - } + return OnapCommandDiscoveryUtils.loadYaml(stream); - return values; } } 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 105f68dc..23c825e2 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 @@ -56,7 +56,8 @@ import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; -import org.yaml.snakeyaml.Yaml; +import com.esotericsoftware.yamlbeans.YamlReader; +import com.esotericsoftware.yamlbeans.YamlException; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -64,7 +65,9 @@ import com.google.gson.stream.JsonReader; import java.io.FileReader; import java.io.Writer; import java.io.FileWriter; - +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.FileInputStream; public class OnapCommandDiscoveryUtils { private static Gson gson = new GsonBuilder().serializeNulls().create(); @@ -216,7 +219,7 @@ public class OnapCommandDiscoveryUtils { public static Map<String, ?> loadSchema(Resource resource) throws OnapCommandInvalidSchema { Map<String, ?> values = null; try { - values = (Map<String, ?>) new Yaml().load(resource.getInputStream()); + values = loadYaml(resource.getInputStream()); } catch (Exception e) { throw new OnapCommandInvalidSchema(resource.getFilename(), e); } @@ -306,7 +309,7 @@ public class OnapCommandDiscoveryUtils { if (deafultResourceMap.containsKey(PARAMETERS)) { List<Object> params = new ArrayList<>(); for (Map<String, ?> p: (List<Map<String, ?>>) deafultResourceMap.get(PARAMETERS)) { - if (p.keySet().contains(IS_DEFAULT_PARAM) && !((Boolean) p.get(IS_DEFAULT_PARAM))) { + if (p.keySet().contains(IS_DEFAULT_PARAM) && ! (Boolean.getBoolean(String.valueOf(p.get(IS_DEFAULT_PARAM))))) { params.add(p); } } @@ -542,7 +545,7 @@ public class OnapCommandDiscoveryUtils { public static Map<String, ?> loadYaml(Resource resource) throws OnapCommandInvalidSchema { Map<String, ?> values = null; try { - values = (Map<String, ?>) new Yaml().load(resource.getInputStream()); + values = loadYaml(resource.getInputStream()); } catch (Exception e) { throw new OnapCommandInvalidSchema(resource.getFilename(), e); } @@ -560,11 +563,33 @@ public class OnapCommandDiscoveryUtils { 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))); + values = loadYaml(new FileInputStream(new File(filePath))); } catch (Exception e) { throw new OnapCommandInvalidSchema(filePath, e); } return values; } + + + /** + * Get schema map. + * + * @param inputStream + * @return map + * @throws OnapCommandInvalidSchema + * exception + */ + public static Map<String, ?> loadYaml(InputStream inputStream) throws OnapCommandInvalidSchema { + Map<String, ?> values = null; + try(InputStreamReader inputStreamReader = new InputStreamReader(inputStream);){ + YamlReader reader = new YamlReader(inputStreamReader); + values = (Map<String, ?>) reader.read(); + } catch (YamlException e) { + throw new OnapCommandInvalidSchema(inputStream.getClass().getName(),e.getMessage()); + } catch (IOException e) { + throw new OnapCommandInvalidSchema(inputStream.getClass().getName(),e.getMessage()); + } + return values; + } } 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 3dcb74ed..d0885def 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 @@ -33,7 +33,7 @@ import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.input.OnapCommandParameterType; import org.onap.cli.main.error.OnapCliArgumentValueMissing; import org.onap.cli.main.error.OnapCliInvalidArgument; -import org.yaml.snakeyaml.Yaml; +import com.esotericsoftware.yamlbeans.YamlReader; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -239,7 +239,8 @@ public class OnapCliArgsParser { File file = new File(input); if (file.isFile()) { String value = FileUtils.readFileToString(file); - new Yaml().load(value); + YamlReader reader = new YamlReader(value); + value = (String) reader.read(); return value; } else { return input; diff --git a/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java b/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java index 634a8d7a..0fb83287 100644 --- a/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java +++ b/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java @@ -30,6 +30,10 @@ import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandHelpFailed; import org.onap.cli.fw.registrar.OnapCommandRegistrar; +import java.util.Map; +import org.onap.cli.fw.error.OnapCommandInvalidSchema; +import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; +import static org.junit.Assert.assertFalse; public class OnapCliMainTest { @@ -213,4 +217,9 @@ public class OnapCliMainTest { fail("Directive help failed to run"); } } + @Test + public void testLoadYamlForYamlReader() throws OnapCommandInvalidSchema { + Map<String,?> map = OnapCommandDiscoveryUtils.loadYaml("src/test/resources/open-cli-schema/sample-test-schema.yaml"); + assertFalse(map.isEmpty()); + } } diff --git a/main/src/test/java/org/onap/cli/main/utils/OnapCliUtilsTest.java b/main/src/test/java/org/onap/cli/main/utils/OnapCliUtilsTest.java index 0c92a1a3..e74981fb 100644 --- a/main/src/test/java/org/onap/cli/main/utils/OnapCliUtilsTest.java +++ b/main/src/test/java/org/onap/cli/main/utils/OnapCliUtilsTest.java @@ -395,4 +395,28 @@ public class OnapCliUtilsTest { Assert.assertTrue(((List<String>) arrParam.getValue()) .containsAll(Arrays.asList("test1", "test2"))); } + @Test + public void testReadYamlStringFromUrl() throws OnapCommandException { + OnapCommandParameter param1 = new OnapCommandParameter(); + param1.setLongOption("yaml"); + param1.setName("name"); + param1.setParameterType(OnapCommandParameterType.YAML); + Set<OnapCommandParameter> paramslist = new HashSet<>(); + paramslist.add(param1); + OnapCliArgsParser.populateParams(paramslist, + Arrays.asList("--yaml", "name", "--yaml", "test-schema")); + Assert.assertTrue(paramslist.iterator().next().getValue().equals("test-schema")); + } + @Test + public void testReadYamlStringFromUrlForFile() throws OnapCommandException { + OnapCommandParameter param1 = new OnapCommandParameter(); + param1.setLongOption("yaml"); + param1.setName("name"); + param1.setParameterType(OnapCommandParameterType.YAML); + Set<OnapCommandParameter> paramslist = new HashSet<>(); + paramslist.add(param1); + OnapCliArgsParser.populateParams(paramslist, + Arrays.asList("--yaml", "main/src/test/resources/open-cli-schema/sample-test-schema.yaml", "--yaml", "test-schema")); + Assert.assertTrue(paramslist.iterator().next().getValue().equals("test-schema")); + } }
\ No newline at end of file diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java index 53a2d042..2bf1eb16 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/cmd/OnapHttpCommand.java @@ -242,7 +242,7 @@ public class OnapHttpCommand extends OnapCommand { this.output = this.authClient.run(this.getInput()); this.getResult().setOutput(output); - if (!this.getSuccessStatusCodes().contains(output.getStatus())) { + if (!this.getSuccessStatusCodes().contains(String.valueOf(output.getStatus()))) { throw new OnapCommandExecutionFailed(this.getName(), output.getBody(), output.getStatus()); } 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 25cf14a3..a2e025a8 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 @@ -30,7 +30,7 @@ import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.http.conf.OnapCommandHttpConstants; import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; import org.springframework.core.io.Resource; -import org.yaml.snakeyaml.Yaml; +import com.esotericsoftware.yamlbeans.YamlReader; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -38,6 +38,7 @@ import com.github.dreamhead.moco.HttpServer; import com.github.dreamhead.moco.Moco; import com.github.dreamhead.moco.ResponseHandler; import com.github.dreamhead.moco.Runner; +import java.io.InputStreamReader; public class MocoServer { @@ -56,8 +57,8 @@ public class MocoServer { } List<Map<String, ?>> stringMap = null; - try { - stringMap = (List<Map<String, ?>>) new Yaml().load(resource.getInputStream()); + try(InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream());) { + stringMap = (List<Map<String, ?>>) new YamlReader(inputStreamReader).read(); } catch (IOException e) { throw new OnapCommandException("Invalid mocking file" + mockFile, e); } @@ -89,7 +90,7 @@ public class MocoServer { if (mocoServerConfigs.containsKey(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON)) { responseHandlers.add(Moco.with(mocoServerConfigs.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON).toString())); } - responseHandlers.add(Moco.status((Integer) mocoServerConfigs.get(OnapCommandHttpConstants.VERIFY_RESPONSE_STATUS))); + responseHandlers.add(Moco.status(Integer.parseInt(String.valueOf(mocoServerConfigs.get(OnapCommandHttpConstants.VERIFY_RESPONSE_STATUS))))); server.request(Moco.by(Moco.uri((String) mocoServerConfigs.get(OnapCommandHttpConstants.VERIFY_REQUEST_URI)))) .response(Moco.header(OnapCommandHttpConstants.VERIFY_CONTENT_TYPE, OnapCommandHttpConstants.VERIFY_CONTENT_TYPE_VALUE), 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 c07a6f7f..a01516fb 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 @@ -164,7 +164,7 @@ public class OnapCommandSchemaHttpLoader { for (String key: context.keySet()) { switch (key) { case OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES: - Boolean flag = (Boolean) context.get(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES); + Boolean flag = Boolean.valueOf(context.get(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES).toString()); cmd.getInput().getContext().put(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES, flag.toString()); break; } @@ -351,7 +351,7 @@ public class OnapCommandSchemaHttpLoader { } for (Object successCode : requestSuccessCodes) { - Integer code = (Integer) successCode; + Integer code = Integer.valueOf(String.valueOf(successCode)); if (code < 200 || code >= 300) { if ( code != 404) { errorList.add(OnapCommandHttpConstants.HTTP_SUCCESS_CODE_INVALID); 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 e94de60a..9c46de86 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 @@ -47,7 +47,8 @@ import org.onap.cli.main.conf.OnapCliConstants; import org.onap.cli.moco.OnapCommandSample; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; +import org.onap.cli.fw.error.OnapCommandInvalidSchema; +import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -221,9 +222,9 @@ public class OnapValidationTest { List<OnapCommandSample> samples = new ArrayList<>(); Map<String, ?> values = null; try { - values = (Map<String, ?>) new Yaml().load(inputStream); - } catch (Exception e) { - throw new OnapCommandInvalidSample(fileName, e); + values = OnapCommandDiscoveryUtils.loadYaml(inputStream); + } catch (OnapCommandInvalidSchema e) { //NOSONAR + LOG.error("Failed to load schema file", e.getMessage()); } OnapCommandSample sample = new OnapCommandSample(); |