From a59f5607eaf196e032990b3ca962f2378e45fa52 Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Fri, 16 Feb 2018 13:01:15 +0000 Subject: Impl Verify feature for CLI Implement verify feature for CLI. So that command developer will come up with his mocking file and can use framework to develope the command. Change-Id: I0ac22aaa7284626de60c66e56e83bb75ec9d773d Issue-ID: CLI-74 Signed-off-by: subhash kumar singh --- .../zip/src/main/release/conf/open-cli.properties | 3 +- .../main/java/org/onap/cli/fw/cmd/OnapCommand.java | 18 +- .../org/onap/cli/fw/conf/OnapCommandConstants.java | 20 ++ .../cli/fw/registrar/OnapCommandRegistrar.java | 71 +++---- .../onap/cli/fw/schema/OnapCommandSchemaInfo.java | 9 +- .../cli/fw/schema/OnapCommandSchemaLoader.java | 23 +-- .../cli/fw/utils/OnapCommandDiscoveryUtils.java | 151 +++++++++++--- .../open-cli-schema/default_input_parameters.yaml | 10 + .../onap/cli/fw/utils/OnapCommandUtilsTest.java | 2 +- main/pom.xml | 5 - main/src/main/java/org/onap/cli/main/OnapCli.java | 147 +++++++++----- .../java/org/onap/cli/main/OnapCliMainTest.java | 19 +- .../cloud-region/cloud-create-schema-1.1-moco.json | 24 +++ .../cloud-create-schema-1.1-sample.yaml | 24 +++ .../cloud-region/cloud-list-schema-1.1-moco.json | 34 ++++ .../cloud-region/cloud-list-schema-1.1-sample.yaml | 30 +++ .../cloud-region/cloud-create-schema-1.1-moco.json | 24 --- .../cloud-create-schema-1.1-sample.yaml | 24 --- .../cloud-region/cloud-list-schema-1.1-moco.json | 34 ---- .../cloud-region/cloud-list-schema-1.1-sample.yaml | 30 --- profiles/http/pom.xml | 17 +- .../org/onap/cli/fw/http/cmd/OnapHttpCommand.java | 74 ++++++- .../cli/fw/http/conf/OnapCommandHttpConstants.java | 15 ++ .../java/org/onap/cli/fw/http/mock/MocoServer.java | 105 ++++++++++ .../src/main/resources/open-cli-http.properties | 4 + .../http/default_input_parameters_http.yaml | 8 +- .../cli/fw/http/utils/OnapCommandUtilsTest.java | 2 +- validate/validation/pom.xml | 11 +- .../onap/cli/moco/OnapCommandHttpMocoServer.java | 221 --------------------- .../onap/cli/validation/OnapValidationTest.java | 140 +++++++++++-- .../sample/sample-test-verify-schema-1.1-moco.json | 34 ++++ .../sample-test-verify-schema-1.1-sample.yaml | 43 ++++ .../sample-test-verify-schema-1.1.yaml | 72 +++++++ 33 files changed, 932 insertions(+), 516 deletions(-) create mode 100644 products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-moco.json create mode 100644 products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-sample.yaml create mode 100644 products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-moco.json create mode 100644 products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-sample.yaml delete mode 100644 products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-moco.json delete mode 100644 products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-sample.yaml delete mode 100644 products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-moco.json delete mode 100644 products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-sample.yaml create mode 100644 profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java delete mode 100644 validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java create mode 100644 validate/validation/src/test/resources/open-cli-sample/sample/sample-test-verify-schema-1.1-moco.json create mode 100644 validate/validation/src/test/resources/open-cli-sample/sample/sample-test-verify-schema-1.1-sample.yaml create mode 100644 validate/validation/src/test/resources/open-cli-schema/sample-test-verify-schema-1.1.yaml diff --git a/deployment/zip/src/main/release/conf/open-cli.properties b/deployment/zip/src/main/release/conf/open-cli.properties index c15a84ce..9b5bceeb 100644 --- a/deployment/zip/src/main/release/conf/open-cli.properties +++ b/deployment/zip/src/main/release/conf/open-cli.properties @@ -28,5 +28,4 @@ cli.schema.profile.available=http,snmp #other properties to load (it should be hanled when plugins are made as externally register-able #when command plugin management support is enabled in oclip -cli.schema.profile.confs=open-cli-http.properties,open-cli-snmp.properties - +cli.schema.profile.confs=open-cli-http.properties,open-cli-snmp.properties \ No newline at end of file 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 29a29ce3..2a5956cc 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 @@ -16,11 +16,13 @@ package org.onap.cli.fw.cmd; +import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandException; @@ -32,12 +34,16 @@ import org.onap.cli.fw.output.OnapCommandResult; import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.output.OnapCommandResultAttributeScope; import org.onap.cli.fw.output.OnapCommandResultType; +import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.onap.cli.fw.schema.OnapCommandSchemaLoader; import org.onap.cli.fw.schema.OnapCommandSchemaMerger; +import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; import org.onap.cli.fw.utils.OnapCommandHelperUtils; import org.onap.cli.fw.utils.OnapCommandUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; /** * Oclip Command. @@ -149,7 +155,6 @@ public abstract class OnapCommand { return this.initializeSchema(schema, false); } - public List initializeSchema(String schema, boolean validate) throws OnapCommandException { this.setSchemaName(schema); @@ -178,6 +183,13 @@ public abstract class OnapCommand { } } + protected void preRun() throws OnapCommandException { + LOG.debug("CMD: " + this.getName() + "pre run."); + } + + protected void postRun() throws OnapCommandException { + LOG.debug("CMD: " + this.getName() + "post run."); + } /** * Oclip command execute with given parameters on service. Before calling this method, its mandatory to set all * parameters value. @@ -238,10 +250,13 @@ public abstract class OnapCommand { } } + preRun(); + this.run(); LOG.info("OUTPUT: " + this.cmdResult.getRecords()); + postRun(); return this.cmdResult; } @@ -270,6 +285,5 @@ public abstract class OnapCommand { public String printHelp() throws OnapCommandHelpFailed { return OnapCommandHelperUtils.help(this); } - // (mrkanag) Add toString for all command, parameter, result, etc objects in JSON format } diff --git a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java index 23d42f87..c2bfc1d5 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java @@ -148,6 +148,26 @@ public class OnapCommandConstants { public static final String SAMPLE_GEN_ENABLED = "cli.sample.gen.enable"; public static final String SAMPLE_GEN_TARGET_FOLDER = "cli.sample.gen.target"; + public static final String VERIFY_SAMPLES_DIRECTORY = "open-cli-sample"; + public static final String VERIFY_SAMPLES_FILE_PATTERN = VERIFY_SAMPLES_DIRECTORY + YAML_PATTERN; + public static final String VERIFY_SAMPLES_MOCK_PATTERN = VERIFY_SAMPLES_DIRECTORY + JSON_PATTERN; + public static final String VERIFY_SAMPLES = "samples"; + public static final String VERIFY_CMD_NAME = "name"; + public static final String VERIFY_CMD_VERSION = "version"; + public static final String VERIFY_OUPUT = "output"; + public static final String VERIFY_INPUT = "input"; + public static final String VERIFY_MOCO = "moco"; + public static final String VERIFY_SAMPLE_FILE_ID = "samplefileid"; + public static final String VERIFY_SAMPLE_ID = "sampleid"; + public static final String VERIFY_RESULT_PASS = "pass"; + public static final String VERIFY_RESULT_FAIL = "fail"; + public static final String VERIFY_CONTEXT_PARAM = "context"; + + + + public static final String VERIFY_LONG_OPTION = "--verify"; + public static final String VERIFY_SHORT_OPTION = "-V"; + private OnapCommandConstants() { } diff --git a/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java b/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java index 6564628d..15a086c7 100644 --- a/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java +++ b/framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java @@ -16,13 +16,6 @@ package org.onap.cli.fw.registrar; -import java.io.IOException; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - import org.apache.commons.io.IOUtils; import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.conf.OnapCommandConfig; @@ -48,6 +41,13 @@ import org.onap.cli.fw.utils.OnapCommandUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + /** * Oclip Command registrar provides a common place, where every command would get registered automatically when its @@ -93,11 +93,11 @@ public class OnapCommandRegistrar { public void setProfile(String profileName, List includes, List excludes) { this.paramCache.setProfile(profileName); - for (String profile: includes) { + for (String profile : includes) { this.paramCache.includeProfile(profile); } - for (String profile: excludes) { + for (String profile : excludes) { this.paramCache.excludeProfile(profile); } } @@ -111,12 +111,9 @@ public class OnapCommandRegistrar { /** * Register the command into registrar and throws OnapInvalidCommandRegistration for invalid command. * - * @param name - * Command Name - * @param cmd - * Command Class - * @throws OnapCommandInvalidRegistration - * Invalid registration exception + * @param name Command Name + * @param cmd Command Class + * @throws OnapCommandInvalidRegistration Invalid registration exception * @throws OnapCommandRegistrationProductInfoMissing */ private void register(String name, String version, Class cmd) throws OnapCommandInvalidRegistration, OnapCommandRegistrationProductInfoMissing { @@ -135,16 +132,15 @@ public class OnapCommandRegistrar { private OnapCommandRegistrar() { this.enabledProductVersion = System.getenv(OnapCommandConstants.OPEN_CLI_PRODUCT_IN_USE_ENV_NAME); - if (this.enabledProductVersion == null) { - this.enabledProductVersion = OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_PRODUCT_NAME); + if (this.enabledProductVersion == null) { + this.enabledProductVersion = OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_PRODUCT_NAME); } } /** * Get global registrar. * - * @throws OnapCommandException - * exception + * @throws OnapCommandException exception */ public static OnapCommandRegistrar getRegistrar() throws OnapCommandException { if (registrar == null) { @@ -177,7 +173,7 @@ public class OnapCommandRegistrar { return cmds; } - for (String cmd: this.registry.keySet()) { + for (String cmd : this.registry.keySet()) { if (cmd.split(":")[1].equalsIgnoreCase(version)) { cmds.add(cmd.split(":")[0]); } @@ -213,8 +209,7 @@ public class OnapCommandRegistrar { * Returns command details. * * @return map - * @throws OnapCommandException - * exception + * @throws OnapCommandException exception */ public List listCommandInfo() throws OnapCommandException { return OnapCommandDiscoveryUtils.discoverSchemas(); @@ -223,11 +218,9 @@ public class OnapCommandRegistrar { /** * Get the OnapCommand, which CLI main would use to find the command based on the command name. * - * @param cmdName - * Name of command + * @param cmdName Name of command * @return OnapCommand - * @throws OnapCommandException - * Exception + * @throws OnapCommandException Exception */ public OnapCommand get(String cmdName) throws OnapCommandException { return this.get(cmdName, this.getEnabledProductVersion()); @@ -236,13 +229,10 @@ public class OnapCommandRegistrar { /** * Get the OnapCommand, which CLI main would use to find the command based on the command name. * - * @param cmdName - * Name of command - * @param version - * product version + * @param cmdName Name of command + * @param version product version * @return OnapCommand - * @throws OnapCommandException - * Exception + * @throws OnapCommandException Exception */ public OnapCommand get(String cmdName, String version) throws OnapCommandException { Class cls = registry.get(cmdName + ":" + version); @@ -293,9 +283,9 @@ public class OnapCommandRegistrar { } //First check if there is an specific plugin exist, otherwise check for profile plugin - if (plugins.containsKey(schema.getSchemaName())) { - this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaName())); - } else if (plugins.containsKey(schema.getSchemaProfile())) { + if (plugins.containsKey(schema.getSchemaName())) { + this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaName())); + } else if (plugins.containsKey(schema.getSchemaProfile())) { this.register(schema.getCmdName(), schema.getProduct(), plugins.get(schema.getSchemaProfile())); } else { LOG.info("Ignoring schema " + schema.getSchemaURI()); @@ -315,7 +305,7 @@ public class OnapCommandRegistrar { } String buildTime = OnapCommandHelperUtils.findLastBuildTime(); - if (buildTime!= null && !buildTime.isEmpty()) { + if (buildTime != null && !buildTime.isEmpty()) { buildTime = " [" + buildTime + "]"; } else { buildTime = ""; @@ -341,8 +331,7 @@ public class OnapCommandRegistrar { * Provides the help message in tabular format for all commands registered in this registrar. * * @return string - * @throws OnapCommandHelpFailed - * Help cmd failed + * @throws OnapCommandHelpFailed Help cmd failed */ public String getHelp() throws OnapCommandHelpFailed { return this.getHelp(false); @@ -387,7 +376,7 @@ public class OnapCommandRegistrar { OnapCommand cmd; try { if (!isEnabledProductVersionOnly) { - String []cmdVer = cmdName.split(":"); + String[] cmdVer = cmdName.split(":"); cmd = this.get(cmdVer[0], cmdVer[1]); attr.getValues().add(cmdVer[0]); attrVer.getValues().add(cmdVer[1]); @@ -409,4 +398,8 @@ public class OnapCommandRegistrar { throw new OnapCommandHelpFailed(e); } } + + public List> getTestSuite(String cmd) throws OnapCommandException { + return OnapCommandDiscoveryUtils.createTestSuite(cmd, enabledProductVersion); + } } 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 67675480..af444b8f 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 @@ -19,6 +19,9 @@ package org.onap.cli.fw.schema; import org.onap.cli.fw.cmd.OnapCommandType; import org.onap.cli.fw.conf.OnapCommandConstants; +import java.util.ArrayList; +import java.util.List; + /** * OnapCommandSchemaInfo is used in discovery caching. * @@ -39,6 +42,8 @@ public class OnapCommandSchemaInfo { private String product; + private List sampleFiles = new ArrayList(); + /** * OCS version */ @@ -118,5 +123,7 @@ public class OnapCommandSchemaInfo { this.ignore = ignore; } - + public List getSampleFiles() { + return sampleFiles; + } } 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 439eb970..628ddf3a 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 @@ -67,6 +67,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.commons.io.FileUtils; import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.cmd.OnapCommandType; import org.onap.cli.fw.conf.OnapCommandConfig; @@ -524,26 +525,8 @@ public class OnapCommandSchemaLoader { /** * Get schema map. * - * @param resource - * resource obj - * @return map - * @throws OnapCommandInvalidSchema - * exception - */ - public static Map loadSchema(Resource resource) throws OnapCommandInvalidSchema { - try { - return loadSchema(resource.getInputStream(), resource.getFilename()); - } catch (IOException e) { - throw new OnapCommandInvalidSchema(resource.getFilename(), e); - } - - } - - /** - * Get schema map. - * - * @param resource - * resource obj + * @param stream + * @param schemaName * @return map * @throws OnapCommandInvalidSchema * exception 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 b878503a..2b3cf941 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 @@ -16,33 +16,13 @@ package org.onap.cli.fw.utils; -import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_DIRECTORY; -import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_PATH_JSON_PATTERN; -import static org.onap.cli.fw.conf.OnapCommandConstants.DISCOVERY_FILE; -import static org.onap.cli.fw.conf.OnapCommandConstants.NAME; -import static org.onap.cli.fw.conf.OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION; -import static org.onap.cli.fw.conf.OnapCommandConstants.SCHEMA_DIRECTORY; -import static org.onap.cli.fw.conf.OnapCommandConstants.SCHEMA_PATH_PATERN; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.ServiceLoader; -import java.util.Map.Entry; - +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.io.FileUtils; import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.conf.OnapCommandConfig; import org.onap.cli.fw.conf.OnapCommandConstants; -import org.onap.cli.fw.error.OnapCommandDiscoveryFailed; -import org.onap.cli.fw.error.OnapCommandException; -import org.onap.cli.fw.error.OnapCommandInstantiationFailed; -import org.onap.cli.fw.error.OnapCommandInvalidSchema; +import org.onap.cli.fw.error.*; +import org.onap.cli.fw.registrar.OnapCommandRegistrar; import org.onap.cli.fw.schema.OnapCommandSchemaInfo; import org.onap.cli.fw.schema.OnapCommandSchemaLoader; import org.springframework.core.io.Resource; @@ -50,7 +30,13 @@ 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 java.io.File; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.*; + +import static org.onap.cli.fw.conf.OnapCommandConstants.*; public class OnapCommandDiscoveryUtils { @@ -248,7 +234,7 @@ public class OnapCommandDiscoveryUtils { for (Resource resource : res) { try { - resourceMap = OnapCommandSchemaLoader.loadSchema(resource); + resourceMap = loadYaml(resource); } catch (OnapCommandException e) { OnapCommandUtils.LOG.error("Ignores invalid schema " + resource.getURI().toString(), e); continue; @@ -297,9 +283,34 @@ public class OnapCommandDiscoveryUtils { throw new OnapCommandDiscoveryFailed(SCHEMA_DIRECTORY, e); } + try { + Resource[] samples = findResources(OnapCommandConstants.VERIFY_SAMPLES_FILE_PATTERN); + for (Resource sample : samples) { + updateSchemaInfoWithSample(sample, extSchemas); + } + } catch (IOException e) { + throw new OnapCommandDiscoveryFailed(OnapCommandConstants.VERIFY_SAMPLES_DIRECTORY, e); + } + return extSchemas; } + private static void updateSchemaInfoWithSample(Resource sampleResourse, + List schemaInfos) throws OnapCommandInvalidSchema, IOException { + Map infoMap = loadSchema(sampleResourse); + String cmdName = (String) infoMap.get(OnapCommandConstants.VERIFY_CMD_NAME); + String version = (String) infoMap.get(OnapCommandConstants.VERIFY_CMD_VERSION); + + Optional optSchemaInfo = schemaInfos.stream() + .filter(e -> e.getCmdName().equals(cmdName) && e.getProduct().equals(version)) + .findFirst(); + + if (optSchemaInfo.isPresent()) { + OnapCommandSchemaInfo onapCommandSchemaInfo = optSchemaInfo.get(); + onapCommandSchemaInfo.getSampleFiles().add(sampleResourse.getFilename()); + } + } + /** * Discover the Oclip commands. * @@ -329,4 +340,92 @@ public class OnapCommandDiscoveryUtils { } } + + public static List> createTestSuite(String cmd, String version) throws OnapCommandException { + + ArrayList> testSamples = new ArrayList(); + + List resources = new ArrayList(); + OnapCommandSchemaInfo schemaInfo = getSchemaInfo(cmd, version); + + List sampleFiles = new ArrayList(); + if (schemaInfo != null && !schemaInfo.getSampleFiles().isEmpty()) { + sampleFiles.addAll(schemaInfo.getSampleFiles()); + } + + for (String sampleFile : sampleFiles) { + try { + Resource resource = OnapCommandDiscoveryUtils.findResource(sampleFile, + OnapCommandConstants.VERIFY_SAMPLES_FILE_PATTERN); + resources.add(resource); + } catch (IOException e) { + throw new OnapCommandInvalidSample("Sample file does not exist : " + sampleFile , e); + } + } + + for (Resource resource : resources) { + + Map stringMap = OnapCommandDiscoveryUtils.loadYaml(resource); + Map> samples = (Map>) stringMap + .get(OnapCommandConstants.VERIFY_SAMPLES); + + for (String sampleId : samples.keySet()) { + + Map sample = samples.get(sampleId); + + List inputArgs = new ArrayList(); + inputArgs.add(cmd); + if (sample.get(OnapCommandConstants.VERIFY_INPUT) != null) { + inputArgs.addAll(Arrays.asList(sample.get(OnapCommandConstants.VERIFY_INPUT).trim().split(" "))); + } + inputArgs.add(OnapCommandConstants.VERIFY_LONG_OPTION); + + HashMap map = new HashMap(); + map.put(OnapCommandConstants.VERIFY_INPUT, inputArgs); + map.put(OnapCommandConstants.VERIFY_OUPUT, sample.get(OnapCommandConstants.VERIFY_OUPUT)); + map.put(OnapCommandConstants.VERIFY_MOCO, sample.get(OnapCommandConstants.VERIFY_MOCO)); + map.put(OnapCommandConstants.VERIFY_SAMPLE_FILE_ID, resource.getFilename()); + map.put(OnapCommandConstants.VERIFY_SAMPLE_ID, sampleId); + testSamples.add(map); + } + } + return testSamples; + } + + /** + * Get schema map. + * + * @param resource + * resource obj + * @return map + * @throws OnapCommandInvalidSchema + * exception + */ + public static Map loadYaml(Resource resource) throws OnapCommandInvalidSchema { + Map values = null; + try { + values = (Map) new Yaml().load(resource.getInputStream()); + } catch (Exception e) { + throw new OnapCommandInvalidSchema(resource.getFilename(), e); + } + return values; + } + + /** + * Get schema map. + * + * @param filePath + * @return map + * @throws OnapCommandInvalidSchema + * exception + */ + public static Map loadYaml(String filePath) throws OnapCommandInvalidSchema { + Map values = null; + try { + values = (Map) new Yaml().load(FileUtils.readFileToString(new File(filePath))); + } catch (Exception e) { + throw new OnapCommandInvalidSchema(filePath, e); + } + return values; + } } diff --git a/framework/src/main/resources/open-cli-schema/default_input_parameters.yaml b/framework/src/main/resources/open-cli-schema/default_input_parameters.yaml index 1dbeedb4..652401e3 100644 --- a/framework/src/main/resources/open-cli-schema/default_input_parameters.yaml +++ b/framework/src/main/resources/open-cli-schema/default_input_parameters.yaml @@ -43,10 +43,20 @@ parameters: long_option: no-title default_value: false is_default_param: true + - name: verify + type: bool + description: verify the command using available command sample file and mocking file + short_option: V + long_option: verify + default_value: false + is_default_param: true + is_include: false + is_optional: true - name: context type: map description: command context short_option: D long_option: context is_default_param: true + is_optional: true is_optional: true \ No newline at end of file diff --git a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java index 6b465c09..1d7c2422 100644 --- a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java +++ b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java @@ -183,7 +183,7 @@ public class OnapCommandUtilsTest { } Map map = OnapCommandUtils.getInputMap(cmd.getParameters()); - assertTrue(map.size() == 17); + assertTrue(map.size() == 18); } @Test diff --git a/main/pom.xml b/main/pom.xml index 3bfd447a..dcd42e37 100644 --- a/main/pom.xml +++ b/main/pom.xml @@ -47,11 +47,6 @@ cli-framework ${project.parent.version} - - commons-io - commons-io - 1.3.2 - jline jline diff --git a/main/src/main/java/org/onap/cli/main/OnapCli.java b/main/src/main/java/org/onap/cli/main/OnapCli.java index 98a8f576..e70c7186 100644 --- a/main/src/main/java/org/onap/cli/main/OnapCli.java +++ b/main/src/main/java/org/onap/cli/main/OnapCli.java @@ -16,16 +16,8 @@ package org.onap.cli.main; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import org.apache.commons.io.FileUtils; +import jline.TerminalFactory; +import jline.console.ConsoleReader; import org.apache.commons.io.IOUtils; import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.conf.OnapCommandConfig; @@ -33,7 +25,6 @@ import org.onap.cli.fw.conf.OnapCommandConstants; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandHelpFailed; import org.onap.cli.fw.error.OnapCommandInvalidSample; -import org.onap.cli.fw.error.OnapCommandInvalidSchema; import org.onap.cli.fw.error.OnapCommandWarning; import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.output.OnapCommandPrintDirection; @@ -42,16 +33,22 @@ import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.output.OnapCommandResultAttributeScope; import org.onap.cli.fw.output.OnapCommandResultType; import org.onap.cli.fw.registrar.OnapCommandRegistrar; +import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; import org.onap.cli.main.conf.OnapCliConstants; import org.onap.cli.main.interactive.StringCompleter; import org.onap.cli.main.utils.OnapCliArgsParser; import org.onap.cli.sample.yaml.SampleYamlGenerator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; -import jline.TerminalFactory; -import jline.console.ConsoleReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; /** * Oclip Command Line Interface (CLI). @@ -111,7 +108,7 @@ public class OnapCli { public void handleHelp() { try { if ((args.size() == 1) && (this.getLongOption(OnapCliConstants.PARAM_HELP_LOGN).equals(args.get(0)) - || this.getShortOption(OnapCliConstants.PARAM_HELP_SHORT).equals(args.get(0)))) { + || this.getShortOption(OnapCliConstants.PARAM_HELP_SHORT).equals(args.get(0)))) { this.print(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("oclip-readme.txt"))); String help = OnapCommandRegistrar.getRegistrar().getHelp(); this.print(help); @@ -147,7 +144,7 @@ public class OnapCli { public void handleProfile() { try { if ((this.args.size() >= 2) && (this.getLongOption(OnapCliConstants.PARAM_PROFILE_LONG).equals(this.args.get(0)) - || this.getShortOption(OnapCliConstants.PARAM_PROFILE_SHORT).equals(this.args.get(0)))) { + || this.getShortOption(OnapCliConstants.PARAM_PROFILE_SHORT).equals(this.args.get(0)))) { OnapCommandRegistrar.getRegistrar().setProfile( this.args.get(1), @@ -170,7 +167,7 @@ public class OnapCli { public void handleBatchCommand() { try { if ((this.args.size() >= 3) && (this.getLongOption(OnapCliConstants.PARAM_PARAM_FILE_LONG).equals(this.args.get(0)) - || this.getShortOption(OnapCliConstants.PARAM_PARAM_FILE_SHORT).equals(this.args.get(0)))) { + || this.getShortOption(OnapCliConstants.PARAM_PARAM_FILE_SHORT).equals(this.args.get(0)))) { String paramFilePath = this.args.get(1); @@ -180,17 +177,17 @@ public class OnapCli { //Read YAML and loop thru it // one - // - param-long-option-1: value - // - param-long-option-1: value - // - positional-arg1 - // - positional-arg2 - // two - // - param-long-option-1: value - // - param-long-option-1: value - // - positional-arg1 - // - positional-arg2 + // - param-long-option-1: value + // - param-long-option-1: value + // - positional-arg1 + // - positional-arg2 + // two + // - param-long-option-1: value + // - param-long-option-1: value + // - positional-arg1 + // - positional-arg2 try { - Map values = (Map) new Yaml().load(FileUtils.readFileToString(new File(paramFilePath))); + Map values = (Map) OnapCommandDiscoveryUtils.loadYaml(paramFilePath); for (Entry cmdsParam: values.entrySet()) { List args = new ArrayList<>(); @@ -221,6 +218,58 @@ public class OnapCli { } } + public void verifyCommand(OnapCommand cmd) throws OnapCommandException { + List> testSuite = OnapCommandRegistrar.getRegistrar().getTestSuite(cmd.getName()); + + OnapCommandResult testSuiteResult = new OnapCommandResult(); + testSuiteResult.setType(OnapCommandResultType.TABLE); + testSuiteResult.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE); + testSuiteResult.setIncludeTitle(true); + + OnapCommandResultAttribute sampleFileAtt = new OnapCommandResultAttribute(); + OnapCommandResultAttribute sampleIdAtt = new OnapCommandResultAttribute(); + OnapCommandResultAttribute resultAtt = new OnapCommandResultAttribute(); + + sampleFileAtt.setName("Test"); + sampleIdAtt.setName("SampleId"); + resultAtt.setName("Result"); + + testSuiteResult.setRecords(Arrays.asList(sampleFileAtt, + sampleIdAtt, + resultAtt)); + + for (Map sampleTest : testSuite) { + + sampleFileAtt.getValues().add((String) sampleTest.get(OnapCommandConstants.VERIFY_SAMPLE_FILE_ID)); + sampleIdAtt.getValues().add((String) sampleTest.get(OnapCommandConstants.VERIFY_SAMPLE_ID)); + + cmd = OnapCommandRegistrar.getRegistrar().get(args.get(0)); + OnapCliArgsParser.populateParams(cmd.getParameters(), (List) sampleTest.get(OnapCommandConstants.VERIFY_INPUT)); + + + Optional contextOpt = cmd.getParameters().stream() + .filter(e -> e.getName().equals(OnapCommandConstants.VERIFY_CONTEXT_PARAM)) + .findFirst(); + + if (contextOpt.isPresent()) { + HashMap map = new HashMap(); + map.put(OnapCommandConstants.VERIFY_MOCO, sampleTest.get(OnapCommandConstants.VERIFY_MOCO)); + contextOpt.get().setValue(map); + } + + OnapCommandResult testResult = cmd.execute(); + String actualOutput = testResult.print().trim(); + String expectedOutput = (String) sampleTest.get(OnapCommandConstants.VERIFY_OUPUT); + expectedOutput = expectedOutput == null ? "" : expectedOutput.trim(); + + if (actualOutput.equals(expectedOutput)) { + resultAtt.getValues().add(OnapCommandConstants.VERIFY_RESULT_PASS); + } else { + resultAtt.getValues().add(OnapCommandConstants.VERIFY_RESULT_FAIL); + } + } + this.print(testSuiteResult.print()); + } /** * Handles Interactive Mode. */ @@ -320,7 +369,6 @@ public class OnapCli { } } - /** * Handles command. */ @@ -338,7 +386,16 @@ public class OnapCli { this.exitFailure(); return; } + try { + + // verify + if(args.contains(OnapCommandConstants.VERIFY_LONG_OPTION) + || args.contains(OnapCommandConstants.VERIFY_SHORT_OPTION)) { + verifyCommand(cmd); + this.exitSuccessfully(); + return; + } // check for help or version if (args.size() == 2) { if (this.getLongOption(OnapCliConstants.PARAM_HELP_LOGN).equals(args.get(1)) @@ -476,23 +533,23 @@ public class OnapCli { */ private ConsoleReader createConsoleReader() throws IOException { ConsoleReader console = new ConsoleReader(); // NOSONAR - try { - StringCompleter strCompleter = new StringCompleter(OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion()); - strCompleter.add(OnapCliConstants.PARAM_INTERACTIVE_EXIT, - OnapCliConstants.PARAM_INTERACTIVE_CLEAR, - OnapCliConstants.PARAM_INTERACTIVE_USE, - OnapCliConstants.PARAM_INTERACTIVE_HELP, - OnapCliConstants.PARAM_INTERACTIVE_VERSION, - OnapCliConstants.PARAM_INTERACTIVE_SET, - OnapCliConstants.PARAM_INTERACTIVE_UNSET, - OnapCliConstants.PARAM_INTERACTIVE_PROFILE); - console.addCompleter(strCompleter); - console.setPrompt(OnapCliConstants.PARAM_INTERACTIVE_PROMPT + ":" + OnapCommandRegistrar.getRegistrar().getEnabledProductVersion() + ">"); - } catch (OnapCommandException e) { // NOSONAR - this.print("Failed to load oclip commands," + e.getMessage()); - } + try { + StringCompleter strCompleter = new StringCompleter(OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion()); + strCompleter.add(OnapCliConstants.PARAM_INTERACTIVE_EXIT, + OnapCliConstants.PARAM_INTERACTIVE_CLEAR, + OnapCliConstants.PARAM_INTERACTIVE_USE, + OnapCliConstants.PARAM_INTERACTIVE_HELP, + OnapCliConstants.PARAM_INTERACTIVE_VERSION, + OnapCliConstants.PARAM_INTERACTIVE_SET, + OnapCliConstants.PARAM_INTERACTIVE_UNSET, + OnapCliConstants.PARAM_INTERACTIVE_PROFILE); + console.addCompleter(strCompleter); + console.setPrompt(OnapCliConstants.PARAM_INTERACTIVE_PROMPT + ":" + OnapCommandRegistrar.getRegistrar().getEnabledProductVersion() + ">"); + } catch (OnapCommandException e) { // NOSONAR + this.print("Failed to load oclip commands," + e.getMessage()); + } - return console; + return console; } @@ -521,4 +578,4 @@ public class OnapCli { System.exit(cli.getExitCode()); } -} +} \ No newline at end of file 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 8fd34413..6d7dbdd2 100644 --- a/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java +++ b/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java @@ -16,19 +16,22 @@ package org.onap.cli.main; -import static org.junit.Assert.fail; - -import java.io.IOException; - +import jline.console.ConsoleReader; +import mockit.Invocation; +import mockit.Mock; +import mockit.MockUp; import org.junit.Test; +import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandHelpFailed; import org.onap.cli.fw.registrar.OnapCommandRegistrar; +import org.onap.cli.fw.schema.OnapCommandSchemaLoader; -import jline.console.ConsoleReader; -import mockit.Invocation; -import mockit.Mock; -import mockit.MockUp; +import java.io.IOException; +import java.util.List; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class OnapCliMainTest { diff --git a/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-moco.json b/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-moco.json new file mode 100644 index 00000000..223ebd26 --- /dev/null +++ b/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-moco.json @@ -0,0 +1,24 @@ +[ { + "request" : { + "method" : "put", + "uri" : "/aai/v11/cloud-infrastructure/cloud-regions/cloud-region/huawei-cloud/bangalore", + "headers" : { + "Authorization" : "Basic QUFJOkFBSQ==", + "X-FromAppId" : "onap-cli", + "Accept" : "application/json", + "X-TransactionId" : "req-63eebfa7-ebdb-4707-9208-a159d2f53c6e", + "Content-Type" : "application/json" + }, + "json" : { + "sriov-automation" : false, + "cloud-region-id" : "bangalore", + "cloud-owner" : "huawei-cloud", + "new-attribute-for-demo" : "blah", + "number-attribute-for-demo" : 1 + } + }, + "response" : { + "status" : 201, + "json" : null + } +} ] \ No newline at end of file diff --git a/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-sample.yaml b/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-sample.yaml new file mode 100644 index 00000000..211fecbb --- /dev/null +++ b/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-sample.yaml @@ -0,0 +1,24 @@ +# Copyright 2017-18 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_sample_version: 1.0 +name: cloud-create +version: onap-amsterdam +samples: + sample1: + name: cloud-create + input: --cloud-name huawei-cloud --region-name bangalore + moco: cloud-create-schema-1.1-moco.json + output: + diff --git a/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-moco.json b/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-moco.json new file mode 100644 index 00000000..f5e4e444 --- /dev/null +++ b/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-moco.json @@ -0,0 +1,34 @@ +[{ + "request" : { + "method" : "get", + "uri" : "/aai/v11/cloud-infrastructure/cloud-regions", + "headers" : { + "Authorization" : "Basic QUFJOkFBSQ==", + "X-FromAppId" : "onap-cli", + "Accept" : "application/json", + "X-TransactionId" : "req-77c08aa0-662e-4d32-9206-bd569c3bf61b", + "Content-Type" : "application/json" + }, + "json" : null + }, + "response" : { + "status" : 200, + "json" : { + "cloud-region" : [ { + "cloud-owner" : "huawei-cloud", + "cloud-region-id" : "bangalore", + "sriov-automation" : false, + "resource-version" : "1509027332165" + }, { + "cloud-owner" : "Rackspace", + "cloud-region-id" : "RegionOne", + "cloud-type" : "SharedNode", + "owner-defined-type" : "OwnerType", + "cloud-region-version" : "v1", + "cloud-zone" : "CloudZone", + "sriov-automation" : false, + "resource-version" : "1508827902543" + } ] + } + } +}] \ No newline at end of file diff --git a/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-sample.yaml b/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-sample.yaml new file mode 100644 index 00000000..d64a1833 --- /dev/null +++ b/products/onap-amsterdam/features/aai/src/main/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-sample.yaml @@ -0,0 +1,30 @@ +# Copyright 2017-18 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_sample_version: 1.0 +name: cloud-list +version: onap-amsterdam +samples: + sample1: + name: cloud-list + input: + moco: cloud-list-schema-1.1-moco.json + output: | + +--------------+------------+------------------+ + |cloud |region |resource-version | + +--------------+------------+------------------+ + |huawei-cloud |bangalore |1509027332165 | + +--------------+------------+------------------+ + |Rackspace |RegionOne |1508827902543 | + +--------------+------------+------------------+ diff --git a/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-moco.json b/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-moco.json deleted file mode 100644 index 223ebd26..00000000 --- a/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-moco.json +++ /dev/null @@ -1,24 +0,0 @@ -[ { - "request" : { - "method" : "put", - "uri" : "/aai/v11/cloud-infrastructure/cloud-regions/cloud-region/huawei-cloud/bangalore", - "headers" : { - "Authorization" : "Basic QUFJOkFBSQ==", - "X-FromAppId" : "onap-cli", - "Accept" : "application/json", - "X-TransactionId" : "req-63eebfa7-ebdb-4707-9208-a159d2f53c6e", - "Content-Type" : "application/json" - }, - "json" : { - "sriov-automation" : false, - "cloud-region-id" : "bangalore", - "cloud-owner" : "huawei-cloud", - "new-attribute-for-demo" : "blah", - "number-attribute-for-demo" : 1 - } - }, - "response" : { - "status" : 201, - "json" : null - } -} ] \ No newline at end of file diff --git a/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-sample.yaml b/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-sample.yaml deleted file mode 100644 index 6a665584..00000000 --- a/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-create-schema-1.1-sample.yaml +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright 2017-18 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_sample_version: 1.0 -name: cloud-create -version: onap-amsterdam -samples: - sample1: - name: cloud-create - input: --cloud-name huawei-cloud --region-name bangalore - moco: cloud-create-schema-1.1-moco.json - output: | - diff --git a/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-moco.json b/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-moco.json deleted file mode 100644 index 2e5b1db0..00000000 --- a/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-moco.json +++ /dev/null @@ -1,34 +0,0 @@ -[ { - "request" : { - "method" : "get", - "uri" : "/aai/v11/cloud-infrastructure/cloud-regions", - "headers" : { - "Authorization" : "Basic QUFJOkFBSQ==", - "X-FromAppId" : "onap-cli", - "Accept" : "application/json", - "X-TransactionId" : "req-77c08aa0-662e-4d32-9206-bd569c3bf61b", - "Content-Type" : "application/json" - }, - "json" : null - }, - "response" : { - "status" : 200, - "json" : { - "cloud-region" : [ { - "cloud-owner" : "huawei-cloud", - "cloud-region-id" : "bangalore", - "sriov-automation" : false, - "resource-version" : "1509027332165" - }, { - "cloud-owner" : "Rackspace", - "cloud-region-id" : "RegionOne", - "cloud-type" : "SharedNode", - "owner-defined-type" : "OwnerType", - "cloud-region-version" : "v1", - "cloud-zone" : "CloudZone", - "sriov-automation" : false, - "resource-version" : "1508827902543" - } ] - } - } -} ] \ No newline at end of file diff --git a/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-sample.yaml b/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-sample.yaml deleted file mode 100644 index d64a1833..00000000 --- a/products/onap-amsterdam/features/aai/src/test/resources/open-cli-sample/cloud-region/cloud-list-schema-1.1-sample.yaml +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright 2017-18 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_sample_version: 1.0 -name: cloud-list -version: onap-amsterdam -samples: - sample1: - name: cloud-list - input: - moco: cloud-list-schema-1.1-moco.json - output: | - +--------------+------------+------------------+ - |cloud |region |resource-version | - +--------------+------------+------------------+ - |huawei-cloud |bangalore |1509027332165 | - +--------------+------------+------------------+ - |Rackspace |RegionOne |1508827902543 | - +--------------+------------+------------------+ diff --git a/profiles/http/pom.xml b/profiles/http/pom.xml index 1c9e037f..913c8cec 100644 --- a/profiles/http/pom.xml +++ b/profiles/http/pom.xml @@ -59,7 +59,22 @@ 1.19 test - + + com.github.dreamhead + moco-runner + 0.12.0 + + + log4j + log4j + + + ch.qos.logback + logback-classic + + + + 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 94baa7a7..9477cfa9 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 @@ -16,13 +16,6 @@ package org.onap.cli.fw.http.cmd; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - import org.onap.cli.fw.cmd.OnapCommand; import org.onap.cli.fw.cmd.OnapCommandType; import org.onap.cli.fw.conf.OnapCommandConfig; @@ -35,8 +28,10 @@ import org.onap.cli.fw.http.conf.OnapCommandHttpConstants; import org.onap.cli.fw.http.connect.HttpInput; import org.onap.cli.fw.http.connect.HttpResult; import org.onap.cli.fw.http.error.OnapCommandFailedMocoGenerate; +import org.onap.cli.fw.http.mock.MocoServer; import org.onap.cli.fw.http.schema.OnapCommandSchemaHttpLoader; import org.onap.cli.fw.http.utils.OnapCommandHttpUtils; +import org.onap.cli.fw.input.OnapCommandParameter; import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.schema.OnapCommandSchema; import org.onap.cli.fw.utils.OnapCommandUtils; @@ -44,6 +39,14 @@ import org.onap.cli.http.mock.MockJsonGenerator; import org.onap.cli.http.mock.MockRequest; import org.onap.cli.http.mock.MockResponse; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; + /** * Oclip http Command. * @@ -61,6 +64,10 @@ public class OnapHttpCommand extends OnapCommand { private OnapCommandHttpService oclipService = new OnapCommandHttpService(); + private MocoServer mocoServer; + + boolean shouldVerify = false; + public OnapHttpCommand() { super.addDefaultSchemas(OnapCommandHttpConstants.DEFAULT_PARAMETER_HTTP_FILE_NAME); } @@ -131,6 +138,59 @@ public class OnapHttpCommand extends OnapCommand { this.getInfo().getCommandType().equals(OnapCommandType.CATALOG)); } + private Optional findParameterByName(String parameterName) { + return this.getParameters().stream() + .filter(e -> e.getName().equals(parameterName)) + .findFirst(); + } + + @Override + protected void preRun() throws OnapCommandException { + Optional verifyOpt = this.getParameters().stream() + .filter(e -> e.getName().equals("verify")) + .findFirst(); + if(verifyOpt.isPresent()) { + shouldVerify = (boolean) verifyOpt.get().getValue(); + } + + if (shouldVerify) { + Optional hostUrlParamOpt = findParameterByName(OnapCommandHttpConstants.VERIFY_HOST_PARAMETER_OPT); + Optional noAuthParamOpt = findParameterByName(OnapCommandHttpConstants.VERIFY_NO_AUTH_PARAMETER_OPT); + + if (hostUrlParamOpt.isPresent()) { + OnapCommandParameter onapCommandParameter = hostUrlParamOpt.get(); + onapCommandParameter.setValue( + OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.VERIFY_MOCO_HOST) + + ":" + OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.VERIFY_MOCO_PORT)); + } + + if (noAuthParamOpt.isPresent()) { + OnapCommandParameter onapCommandParameter = noAuthParamOpt.get(); + onapCommandParameter.setValue(true); + } + + + Optional contextOpt = this.getParameters().stream() + .filter(e -> e.getName().equals(OnapCommandConstants.VERIFY_CONTEXT_PARAM)) + .findFirst(); + + if (contextOpt.isPresent()) { + OnapCommandParameter context = contextOpt.get(); + String mockedFile = ((Map)context.getValue()).get(OnapCommandConstants.VERIFY_MOCO); + + mocoServer = new MocoServer(mockedFile); + mocoServer.start(); + } + } + } + + @Override + protected void postRun() throws OnapCommandException { + if (shouldVerify) { + mocoServer.stop(); + } + } + @Override protected void run() throws OnapCommandException { try { diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java b/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java index 9663f87b..185582cb 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/conf/OnapCommandHttpConstants.java @@ -93,6 +93,21 @@ public class OnapCommandHttpConstants { //context param public static final String CONTEXT = "context"; public static final String CONTEXT_REMOVE_EMPTY_JSON_NODES = "remove_empty_node"; + + // moco server const + public static final String VERIFY_MOCO_HOST = "cli.verify.host"; + public static final String VERIFY_MOCO_PORT = "cli.verify.port"; + + public static final String VERIFY_HOST_PARAMETER_OPT = DEAFULT_PARAMETER_HOST_URL; + public static final String VERIFY_NO_AUTH_PARAMETER_OPT = DEFAULT_PARAMETER_NO_AUTH; + + public static final String VERIFY_REQUEST_URI = URI; + public static final String VERIFY_RESPONSE_STATUS = "status"; + public static final String VERIFY_RESPONSE_JSON = "json"; + public static final String VERIFY_REQUEST = REQUEST; + public static final String VERIFY_RESPONSE = "response"; + public static final String VERIFY_CONTENT_TYPE = "Content-Type"; + public static final String VERIFY_CONTENT_TYPE_VALUE = APPLICATION_JSON; } 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 new file mode 100644 index 00000000..f6e58757 --- /dev/null +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java @@ -0,0 +1,105 @@ +/* + * Copyright 2017 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. + */ +package org.onap.cli.fw.http.mock; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +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 org.onap.cli.fw.conf.OnapCommandConfig; +import org.onap.cli.fw.conf.OnapCommandConstants; +import org.onap.cli.fw.error.OnapCommandDiscoveryFailed; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.error.OnapCommandInvalidSchema; +import org.onap.cli.fw.http.conf.OnapCommandHttpConstants; +import org.onap.cli.fw.schema.OnapCommandSchemaLoader; +import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils; +import org.springframework.core.io.Resource; +import org.yaml.snakeyaml.Yaml; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static com.github.dreamhead.moco.Runner.runner; + +public class MocoServer { + + private Runner runner; + private Map mocoServerConfigs = new HashMap(); + + public MocoServer(String mockFile) throws OnapCommandException { + Resource resource = null; + + try { + resource = OnapCommandDiscoveryUtils.findResource(mockFile, + OnapCommandConstants.VERIFY_SAMPLES_MOCK_PATTERN); + } catch (IOException e) { + throw new OnapCommandDiscoveryFailed(mockFile, e); + } + + List> stringMap = null; + try { + stringMap = (List>) new Yaml().load(resource.getInputStream()); + } catch (IOException e) { + throw new OnapCommandException("Invalid mocking file" + mockFile, e); + } + if(!stringMap.isEmpty()) { + Map jsonConfigs = stringMap.get(0); + Map request = (Map) jsonConfigs.get(OnapCommandHttpConstants.VERIFY_REQUEST); + mocoServerConfigs.put(OnapCommandHttpConstants.VERIFY_REQUEST_URI, request.get(OnapCommandHttpConstants.VERIFY_REQUEST_URI)); + + Map response = (Map) jsonConfigs.get(OnapCommandHttpConstants.VERIFY_RESPONSE); + mocoServerConfigs.put(OnapCommandHttpConstants.VERIFY_RESPONSE_STATUS, response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_STATUS)); + + 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) { + throw new OnapCommandException("Invalid mocking file" + mockFile, e); + } + } + } + } + + public void start() { + HttpServer server = Moco.httpServer(Integer.parseInt( + OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.VERIFY_MOCO_PORT))); + + List responseHandlers = new ArrayList<>(); + + 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))); + + 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), + responseHandlers.toArray(new ResponseHandler[responseHandlers.size()])); + + runner = runner(server); + runner.start(); + } + + public void stop() { + runner.stop(); + } +} diff --git a/profiles/http/src/main/resources/open-cli-http.properties b/profiles/http/src/main/resources/open-cli-http.properties index bc7d33c9..8cde793e 100644 --- a/profiles/http/src/main/resources/open-cli-http.properties +++ b/profiles/http/src/main/resources/open-cli-http.properties @@ -16,3 +16,7 @@ cli.schema.http.request.method.values=post,get,delete,put,head cli.schema.http.service.auth.values=none,basic cli.schema.http.service.mode.values=direct,catalog + +#verify properties +cli.verify.host=http://localhost +cli.verify.port=8585 \ No newline at end of file diff --git a/profiles/http/src/main/resources/open-cli-schema/http/default_input_parameters_http.yaml b/profiles/http/src/main/resources/open-cli-schema/http/default_input_parameters_http.yaml index 6721b207..6ac0ded2 100644 --- a/profiles/http/src/main/resources/open-cli-schema/http/default_input_parameters_http.yaml +++ b/profiles/http/src/main/resources/open-cli-schema/http/default_input_parameters_http.yaml @@ -40,4 +40,10 @@ parameters: long_option: no-catalog is_optional: true is_default_param: true - default_value: false \ No newline at end of file + default_value: false + - name: verify + type: bool + description: verify the command using available command sample file and mocking file + default_value: false + is_include: true + is_optional: true \ No newline at end of file diff --git a/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java b/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java index d98f9bf3..13b2db42 100644 --- a/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java +++ b/profiles/http/src/test/java/org/onap/cli/fw/http/utils/OnapCommandUtilsTest.java @@ -94,7 +94,7 @@ public class OnapCommandUtilsTest { assertTrue("sample-test".equals(cmd.getName())); Map map = OnapCommandUtils.getInputMap(cmd.getParameters()); - assertTrue(map.size() == 8); + assertTrue(map.size() == 9); } @Test(expected = OnapCommandHttpHeaderNotFound.class) diff --git a/validate/validation/pom.xml b/validate/validation/pom.xml index 26f507d6..3dbe456f 100644 --- a/validate/validation/pom.xml +++ b/validate/validation/pom.xml @@ -31,12 +31,6 @@ cli/validate/validation jar - - com.github.dreamhead - moco-runner - 0.11.1 - test - junit junit @@ -83,6 +77,11 @@ cli-products-onap-amsterdam-features-aai ${project.version} + + commons-io + commons-io + 2.6 +