diff options
author | subhash kumar singh <subhash.kumar.singh@huawei.com> | 2018-02-16 13:01:15 +0000 |
---|---|---|
committer | subhash kumar singh <subhash.kumar.singh@huawei.com> | 2018-03-13 09:17:32 +0000 |
commit | a59f5607eaf196e032990b3ca962f2378e45fa52 (patch) | |
tree | a768bd25375a8667d75cd6e157f1906586e1fe3f /main/src | |
parent | 1239610d64ddfce2e3d1ff3378adcf96f24ee0a0 (diff) |
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 <subhash.kumar.singh@huawei.com>
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/main/java/org/onap/cli/main/OnapCli.java | 147 | ||||
-rw-r--r-- | main/src/test/java/org/onap/cli/main/OnapCliMainTest.java | 19 |
2 files changed, 113 insertions, 53 deletions
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<String, Object> values = (Map<String, Object>) new Yaml().load(FileUtils.readFileToString(new File(paramFilePath))); + Map<String, Object> values = (Map<String, Object>) OnapCommandDiscoveryUtils.loadYaml(paramFilePath); for (Entry<String, Object> cmdsParam: values.entrySet()) { List<String> args = new ArrayList<>(); @@ -221,6 +218,58 @@ public class OnapCli { } } + public void verifyCommand(OnapCommand cmd) throws OnapCommandException { + List<Map<String, ?>> 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<String, ?> 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<String>) sampleTest.get(OnapCommandConstants.VERIFY_INPUT)); + + + Optional<OnapCommandParameter> 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 { |