diff options
Diffstat (limited to 'main')
7 files changed, 311 insertions, 144 deletions
diff --git a/main/pom.xml b/main/pom.xml index 9e14e63c..dcd42e37 100644 --- a/main/pom.xml +++ b/main/pom.xml @@ -48,14 +48,9 @@ <version>${project.parent.version}</version> </dependency> <dependency> - <groupId>commons-io</groupId> - <artifactId>commons-io</artifactId> - <version>1.3.2</version> - </dependency> - <dependency> <groupId>jline</groupId> <artifactId>jline</artifactId> - <version>2.14.3</version> + <version>2.6</version> </dependency> <dependency> <groupId>org.jmockit</groupId> 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 443b86f2..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,11 +16,8 @@ package org.onap.cli.main; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - +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; @@ -36,6 +33,7 @@ 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; @@ -43,8 +41,14 @@ import org.onap.cli.sample.yaml.SampleYamlGenerator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -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). @@ -61,7 +65,7 @@ public class OnapCli { private int exitCode = -1; public OnapCli(String[] args) { - this.args = Arrays.asList(args); + this.args.addAll(Arrays.asList(args)); } public OnapCli(String product, String[] args) { @@ -104,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); @@ -139,12 +143,74 @@ public class OnapCli { */ public void handleProfile() { try { - if ((args.size() == 2) && (this.getLongOption(OnapCliConstants.PARAM_PROFILE_LONG).equals(args.get(0)) - || this.getShortOption(OnapCliConstants.PARAM_PROFILE_SHORT).equals(args.get(0)))) { + 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)))) { + + OnapCommandRegistrar.getRegistrar().setProfile( + this.args.get(1), + new ArrayList<String>(), + new ArrayList<String>()); + //Make space of interactive mode/command mode + this.args.remove(0); //--profile or -c + this.args.remove(0); //profile name + } + } catch (Exception e) { + this.print(e); + this.exitFailure(); + } + } + + /** + * Handles batch command. --param-file or -p + * CAUTION: This option should be passed after --profile always. + */ + 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)))) { + + String paramFilePath = this.args.get(1); + + //Make space of interactive mode/command mode + this.args.remove(0); //--param-file or -p + this.args.remove(0); //file name + + //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 + try { + Map<String, Object> values = (Map<String, Object>) OnapCommandDiscoveryUtils.loadYaml(paramFilePath); + + for (Entry<String, Object> cmdsParam: values.entrySet()) { + List<String> args = new ArrayList<>(); + args.add(this.args.get(0)); + for (Object param: (List)cmdsParam.getValue()) { + if (param instanceof Map) { //optional args + Map <String, String> paramMap = (Map<String, String>) param; + String paramName = paramMap.keySet().iterator().next(); + Object paramValue = paramMap.get(paramName); + args.add("--" + paramName); + args.add(paramValue.toString()); + } else { //positional args + args.add(param.toString()); + } + } - OnapCommandRegistrar.getRegistrar().setProfile(args.get(1)); - //Make space of interactive mode - this.args = new ArrayList<>(); + this.handleCommand(args); + } + + } catch (Exception e) { // NOSONAR + this.print("Failed to read param file " + paramFilePath); + this.print(e); + } } } catch (Exception e) { this.print(e); @@ -152,51 +218,57 @@ public class OnapCli { } } - public static String getDirectiveHelp() throws OnapCommandHelpFailed { - OnapCommandResult help = new OnapCommandResult(); - help.setType(OnapCommandResultType.TABLE); - help.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE); + public void verifyCommand(OnapCommand cmd) throws OnapCommandException { + List<Map<String, ?>> testSuite = OnapCommandRegistrar.getRegistrar().getTestSuite(cmd.getName()); - OnapCommandResultAttribute attr = new OnapCommandResultAttribute(); - attr.setName(OnapCommandConstants.NAME.toUpperCase()); - attr.setDescription(OnapCommandConstants.DESCRIPTION); - attr.setScope(OnapCommandResultAttributeScope.SHORT); - help.getRecords().add(attr); + OnapCommandResult testSuiteResult = new OnapCommandResult(); + testSuiteResult.setType(OnapCommandResultType.TABLE); + testSuiteResult.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE); + testSuiteResult.setIncludeTitle(true); - OnapCommandResultAttribute attrDesc = new OnapCommandResultAttribute(); - attrDesc.setName(OnapCommandConstants.DESCRIPTION.toUpperCase()); - attrDesc.setDescription(OnapCommandConstants.DESCRIPTION); - attrDesc.setScope(OnapCommandResultAttributeScope.SHORT); - help.getRecords().add(attrDesc); + OnapCommandResultAttribute sampleFileAtt = new OnapCommandResultAttribute(); + OnapCommandResultAttribute sampleIdAtt = new OnapCommandResultAttribute(); + OnapCommandResultAttribute resultAtt = new OnapCommandResultAttribute(); - attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR); - attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR_MSG); + sampleFileAtt.setName("Test"); + sampleIdAtt.setName("SampleId"); + resultAtt.setName("Result"); - attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT); - attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT_MSG); + testSuiteResult.setRecords(Arrays.asList(sampleFileAtt, + sampleIdAtt, + resultAtt)); - attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION); - attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION_MSG); + for (Map<String, ?> sampleTest : testSuite) { - attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE); - attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE_MSG); + sampleFileAtt.getValues().add((String) sampleTest.get(OnapCommandConstants.VERIFY_SAMPLE_FILE_ID)); + sampleIdAtt.getValues().add((String) sampleTest.get(OnapCommandConstants.VERIFY_SAMPLE_ID)); - attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET); - attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET_MSG); + cmd = OnapCommandRegistrar.getRegistrar().get(args.get(0)); + OnapCliArgsParser.populateParams(cmd.getParameters(), (List<String>) sampleTest.get(OnapCommandConstants.VERIFY_INPUT)); - attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET); - attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET_MSG); - attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP); - attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP_MSG); + Optional<OnapCommandParameter> contextOpt = cmd.getParameters().stream() + .filter(e -> e.getName().equals(OnapCommandConstants.VERIFY_CONTEXT_PARAM)) + .findFirst(); - attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE); - attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE_MSG); - try { - return "\n\nDirectives:\n" + help.print(); - } catch (OnapCommandException e) { - throw new OnapCommandHelpFailed(e); + 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. @@ -217,51 +289,56 @@ public class OnapCli { console.clearScreen(); continue; } - this.args = Arrays.asList(line.split(OnapCliConstants.PARAM_INTERACTIVE_ARG_SPLIT_PATTERN)); + this.args.clear(); + this.args.addAll(Arrays.asList(line.split(OnapCliConstants.PARAM_INTERACTIVE_ARG_SPLIT_PATTERN))); if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_USE)) { if (args.size() == 1) { - this.print("Please input the product version to use, supported versions: " + + this.print("Please use it in the form of use <product-version>.\nSupported versions: " + OnapCommandRegistrar.getRegistrar().getAvailableProductVersions()); } else { try { OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(args.get(1)); - console.close(); console = createConsoleReader(); } catch (OnapCommandException e) { this.print(e); } } + } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_HELP)) { try { this.print(OnapCommandRegistrar.getRegistrar().getHelpForEnabledProductVersion()); - this.print(this.getDirectiveHelp()); + this.print(OnapCli.getDirectiveHelp()); } catch (OnapCommandException e) { this.print(e); } + } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_VERSION)) { this.args = Arrays.asList(new String [] {this.getLongOption(OnapCliConstants.PARAM_VERSION_LONG)}); handleVersion(); + } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_PROFILE)) { if (args.size() == 1) { - this.print("Please use it in the form of 'profile <profile-name>'"); + this.print("Please use it in the form of 'profile <profile-name>'\n"); + this.print("Available profiles: "); + this.print(OnapCommandRegistrar.getRegistrar().getUserProfiles().toString()); } else { - this.args = Arrays.asList(new String [] { - this.getLongOption(OnapCliConstants.PARAM_PROFILE_LONG), - this.args.get(1)}); + this.args.set(0, this.getLongOption(OnapCliConstants.PARAM_PROFILE_LONG)); handleProfile(); } + } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_SET)) { if (args.size() > 1) { - String [] paramEntry = args.get(1).trim().split("="); - if (paramEntry.length >= 2) { + String [] paramEntry = args.get(1).trim().split("=", 2); + if (paramEntry.length == 2) { OnapCommandRegistrar.getRegistrar().addParamCache(paramEntry[0].trim(), paramEntry[1].trim()); } else { - this.print("Please use it in the form of 'set param-name=param-value'"); + this.print("Please use it in the form of 'set <param-name>=<param-value>'"); } } else { this.print(OnapCommandRegistrar.getRegistrar().getParamCache().toString()); } + } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_UNSET)) { if (args.size() > 1) { for (int i = 1; i <args.size(); i++) { @@ -274,7 +351,7 @@ public class OnapCli { continue; } - handleCommand(); + handleCommand(new ArrayList<>()); } } } catch (IOException e) { // NOSONAR @@ -287,46 +364,15 @@ public class OnapCli { TerminalFactory.get().restore(); } catch (Exception e) { // NOSONAR } - if (console != null) { - console.close(); - } this.exitSuccessfully(); } } } /** - * Creates console reader object. - * - * @return ConsoleReader - * @throws IOException - * exception - */ - private ConsoleReader createConsoleReader() throws IOException { - try(ConsoleReader console = new ConsoleReader()){ - 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; - } - } - - /** * Handles command. */ - public void handleCommand() { + public void handleCommand(List<String> params) { OnapCommand cmd; if (!args.isEmpty()) { try { @@ -340,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)) @@ -358,6 +413,7 @@ public class OnapCli { } } + //refer params from profile for (OnapCommandParameter param: cmd.getParameters()) { if (OnapCommandRegistrar.getRegistrar().getParamCache().containsKey( cmd.getInfo().getService() + ":" + param.getLongOption())) { @@ -368,7 +424,13 @@ public class OnapCli { } } + //load the parameters value from the map read from param-file + if (params != null && !params.isEmpty()) { + OnapCliArgsParser.populateParams(cmd.getParameters(), params); + } + OnapCliArgsParser.populateParams(cmd.getParameters(), args); + OnapCommandResult result = cmd.execute(); this.print(result.getDebugInfo()); @@ -388,19 +450,6 @@ public class OnapCli { } } - private void generateSmapleYaml(OnapCommand cmd) throws OnapCommandException { - if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_ENABLED)) && this.getExitCode() == OnapCliConstants.EXIT_SUCCESS) { - try { - SampleYamlGenerator.generateSampleYaml(args, cmd.getResult().print(), - OnapCommandRegistrar.getRegistrar().getEnabledProductVersion(), - OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_TARGET_FOLDER) + "/" + cmd.getSchemaName().replaceAll(".yaml", "") + "-sample.yaml", - cmd.getResult().isDebug()); - } catch (IOException error) { - throw new OnapCommandInvalidSample(args.get(0), error); - } - } - } - /** * Handles all client input. */ @@ -416,11 +465,104 @@ public class OnapCli { } if (this.exitCode == -1) { + this.handleBatchCommand(); + } + + if (this.exitCode == -1) { this.handleInteractive(); } if (this.exitCode == -1) { - this.handleCommand(); + this.handleCommand(new ArrayList<>()); + } + } + + public static String getDirectiveHelp() throws OnapCommandHelpFailed { + OnapCommandResult help = new OnapCommandResult(); + help.setType(OnapCommandResultType.TABLE); + help.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE); + + OnapCommandResultAttribute attr = new OnapCommandResultAttribute(); + attr.setName(OnapCommandConstants.NAME.toUpperCase()); + attr.setDescription(OnapCommandConstants.DESCRIPTION); + attr.setScope(OnapCommandResultAttributeScope.SHORT); + help.getRecords().add(attr); + + OnapCommandResultAttribute attrDesc = new OnapCommandResultAttribute(); + attrDesc.setName(OnapCommandConstants.DESCRIPTION.toUpperCase()); + attrDesc.setDescription(OnapCommandConstants.DESCRIPTION); + attrDesc.setScope(OnapCommandResultAttributeScope.SHORT); + help.getRecords().add(attrDesc); + + attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR); + attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_CLEAR_MSG); + + attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT); + attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_EXIT_MSG); + + attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION); + attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_VERSION_MSG); + + attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE); + attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_USE_MSG); + + attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET); + attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_SET_MSG); + + attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET); + attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_UNSET_MSG); + + attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP); + attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_HELP_MSG); + + attr.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE); + attrDesc.getValues().add(OnapCliConstants.PARAM_INTERACTIVE_PROFILE_MSG); + try { + return "\n\nDirectives:\n" + help.print(); + } catch (OnapCommandException e) { + throw new OnapCommandHelpFailed(e); + } + } + + /** + * Creates console reader object. + * + * @return ConsoleReader + * @throws IOException + * exception + */ + 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()); + } + + return console; + } + + + private void generateSmapleYaml(OnapCommand cmd) throws OnapCommandException { + if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_ENABLED)) && this.getExitCode() == OnapCliConstants.EXIT_SUCCESS) { + try { + SampleYamlGenerator.generateSampleYaml(args, cmd.getResult().print(), + OnapCommandRegistrar.getRegistrar().getEnabledProductVersion(), + OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_TARGET_FOLDER) + "/" + cmd.getSchemaName().replaceAll(".yaml", "") + "-sample.yaml", + cmd.getResult().isDebug()); + } catch (IOException error) { + throw new OnapCommandInvalidSample(args.get(0), error); + } } } @@ -436,4 +578,4 @@ public class OnapCli { System.exit(cli.getExitCode()); } -} +}
\ No newline at end of file diff --git a/main/src/main/java/org/onap/cli/main/conf/OnapCliConstants.java b/main/src/main/java/org/onap/cli/main/conf/OnapCliConstants.java index c4a21614..4389a8cc 100644 --- a/main/src/main/java/org/onap/cli/main/conf/OnapCliConstants.java +++ b/main/src/main/java/org/onap/cli/main/conf/OnapCliConstants.java @@ -27,6 +27,9 @@ public final class OnapCliConstants { public static final String PARAM_PROFILE_SHORT = "c"; public static final String PARAM_PROFILE_LONG = "profile"; + public static final String PARAM_PARAM_FILE_SHORT = "p"; + public static final String PARAM_PARAM_FILE_LONG = "param-file"; + public static final int EXIT_SUCCESS = 0; public static final int EXIT_FAILURE = 1; 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 fdf6d5b8..d5807d79 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 @@ -105,7 +105,7 @@ public class OnapCliArgsParser { // end of the list or if its option rather than a value if ((i + 1) == args.size() || args.get(i + 1).startsWith("-")) { if (paramMap.get(paramName).getParameterType().equals(OnapCommandParameterType.BOOL)) { - paramMap.get(paramName).setValue("true"); + paramMap.get(paramName).setValue(true); continue; } throw new OnapCliArgumentValueMissing(args.get(i)); @@ -116,45 +116,40 @@ public class OnapCliArgsParser { paramMap.get(paramName).getName())); i++; continue; + } if (paramMap.get(paramName).getParameterType().equals(OnapCommandParameterType.TEXT)) { paramMap.get(paramName).setValue(readTextStringFromUrl(args.get(i + 1), paramMap.get(paramName).getName())); i++; continue; + } else if (paramMap.get(paramName).getParameterType() .equals(OnapCommandParameterType.ARRAY)) { Object value = paramMap.get(paramName).getValue(); - List<String> list; - if (value == "") { - list = new ArrayList<>(); - } else { - list = convertJsonToListString(paramMap.get(paramName).getName(), - value.toString()); - } - list.add(args.get(i + 1)); + List<String> list = (List<String>) value; + + list.add(readTextStringFromUrl(args.get(i + 1), paramMap.get(paramName).getName())); paramMap.get(paramName).setValue(list); i++; continue; - } else if (paramMap.get(paramName).getParameterType().equals(OnapCommandParameterType.MAP)) { - Object value = paramMap.get(paramName).getValue(); - Map<String, String> map; + } else if (paramMap.get(paramName).getParameterType() + .equals(OnapCommandParameterType.MAP)) { + Object value = paramMap.get(paramName).getValue(); - if (value == "") { - map = new HashMap<>(); - } else { - map = convertJsonToMapString(paramMap.get(paramName).getName(), - value.toString()); - } + Map<String, String> map = (Map<String, String>) value; String arg = args.get(i + 1); - String[] argArr = arg.split("="); + String[] argArr = arg.split("=", 2); if (argArr.length != 2) { - throw new OnapCliInvalidArgument(paramMap.get(paramName).getName()); + throw new OnapCliInvalidArgument( + paramMap.get(paramName).getName(), + "it should be in the form of <key>=<value>"); } - map.put(argArr[0], argArr[1]); + //Make sure to read values from file, in case file path is given. + map.put(argArr[0], readTextStringFromUrl(argArr[1], paramMap.get(paramName).getName())); paramMap.get(paramName).setValue(map); i++; continue; @@ -169,7 +164,9 @@ public class OnapCliArgsParser { // it is positional option // Positional arg is missing from the params if (positionalIdx >= positionArgs.size()) { - throw new OnapCliInvalidArgument(args.get(i)); + throw new OnapCliInvalidArgument( + args.get(i), + "No positional argument is defined for this one"); } paramMap.get(positionArgs.get(positionalIdx)).setValue(args.get(i)); 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 f7401904..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 { @@ -152,6 +155,13 @@ public class OnapCliMainTest { } cli = new OnapCli(new String[] {}); + mockConsole("profile"); + try { + cli.handleInteractive(); + } catch (Exception e) { + } + + cli = new OnapCli(new String[] {}); mockConsole("version"); try { cli.handleInteractive(); 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 eb2ca294..c3627ca2 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 @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Optional; import java.util.Set; import org.junit.Assert; @@ -255,7 +257,7 @@ public class OnapCliUtilsTest { OnapCliArgsParser.populateParams(paramslist, Arrays.asList("show", "--map", "param1=value1", "--map", "param2=value2")); - Assert.assertEquals("{\"param1\":\"value1\",\"param2\":\"value2\"}", paramslist.iterator().next().getValue().toString()); + Assert.assertEquals("{param1=value1, param2=value2}", paramslist.iterator().next().getValue().toString()); } @Test(expected = OnapCliInvalidArgument.class) @@ -360,4 +362,19 @@ public class OnapCliUtilsTest { Assert.assertEquals("--json-param", paramslist.iterator().next().getValue()); } + + @Test + public void testArrayCommandArg() throws OnapCommandException { + OnapCommandParameter arrParam = new OnapCommandParameter(); + arrParam.setShortOption("q"); + arrParam.setParameterType(OnapCommandParameterType.ARRAY); + arrParam.setName("array-param"); + Set<OnapCommandParameter> paramslist = new HashSet<>(); + paramslist.add(arrParam); + String[] args = new String[] { "sample-create", "-q", "test1", "-q", "test2" }; + + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); + Assert.assertTrue(((List<String>) arrParam.getValue()) + .containsAll(Arrays.asList("test1", "test2"))); + } }
\ No newline at end of file diff --git a/main/src/test/resources/schema-validate-param-file.yaml b/main/src/test/resources/schema-validate-param-file.yaml new file mode 100644 index 00000000..0da20116 --- /dev/null +++ b/main/src/test/resources/schema-validate-param-file.yaml @@ -0,0 +1,3 @@ +one: + - internal-schema: true + - schema-location: catalog.yaml
\ No newline at end of file |