diff options
Diffstat (limited to 'main')
-rw-r--r-- | main/src/main/java/org/onap/cli/main/OnapCli.java | 4 | ||||
-rw-r--r-- | main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java (renamed from main/src/main/java/org/onap/cli/main/utils/OnapCliUtils.java) | 119 | ||||
-rw-r--r-- | main/src/test/java/org/onap/cli/main/OnapCliMainTest.java | 4 | ||||
-rw-r--r-- | main/src/test/java/org/onap/cli/main/utils/OnapCliUtilsTest.java | 36 |
4 files changed, 50 insertions, 113 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 77de4408..15f845b6 100644 --- a/main/src/main/java/org/onap/cli/main/OnapCli.java +++ b/main/src/main/java/org/onap/cli/main/OnapCli.java @@ -38,7 +38,7 @@ import org.onap.cli.fw.output.PrintDirection; import org.onap.cli.fw.output.ResultType; import org.onap.cli.main.conf.OnapCliConstants; import org.onap.cli.main.interactive.StringCompleter; -import org.onap.cli.main.utils.OnapCliUtils; +import org.onap.cli.main.utils.OnapCliArgsParser; import org.onap.cli.sample.yaml.SampleYamlGenerator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -356,7 +356,7 @@ public class OnapCli { } } - OnapCliUtils.populateParams(cmd.getParameters(), args); + OnapCliArgsParser.populateParams(cmd.getParameters(), args); OnapCommandResult result = cmd.execute(); this.print(result.getDebugInfo()); diff --git a/main/src/main/java/org/onap/cli/main/utils/OnapCliUtils.java b/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java index 99cfa7dd..725459e9 100644 --- a/main/src/main/java/org/onap/cli/main/utils/OnapCliUtils.java +++ b/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java @@ -41,12 +41,12 @@ import java.util.Map; * Oclip CLI utilities. * */ -public class OnapCliUtils { +public class OnapCliArgsParser { /** * private Constructor. */ - private OnapCliUtils() { + private OnapCliArgsParser() { } @@ -93,52 +93,56 @@ public class OnapCliUtils { int positionalIdx = 0; // Skip the first args oclip cmd name, so start from 1 for (int i = 1; i < args.size(); i++) { - // check if short option exist - // (mrkanag) Optimize the below code to handle short and long options in one iteration - // now its redundant - if (shortOptionMap.containsKey(args.get(i))) { + String paramName = null; + if (shortOptionMap.containsKey(args.get(i))) { + paramName = shortOptionMap.get(args.get(i)); + } else if (longOptionMap.containsKey(args.get(i))) { + paramName = longOptionMap.get(args.get(i)); + } + + if (paramName != null) { // 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(shortOptionMap.get(args.get(i))).getParameterType().equals(ParameterType.BOOL)) { - paramMap.get(shortOptionMap.get(args.get(i))).setValue("true"); + if (paramMap.get(paramName).getParameterType().equals(ParameterType.BOOL)) { + paramMap.get(paramName).setValue("true"); continue; } throw new OnapCliArgumentValueMissing(args.get(i)); } - if (paramMap.get(shortOptionMap.get(args.get(i))).getParameterType().equals(ParameterType.JSON)) { - paramMap.get(shortOptionMap.get(args.get(i))).setValue(readJsonStringFromUrl(args.get(i + 1), - paramMap.get(shortOptionMap.get(args.get(i))).getName())); + if (paramMap.get(paramName).getParameterType().equals(ParameterType.JSON)) { + paramMap.get(paramName).setValue(readJsonStringFromUrl(args.get(i + 1), + paramMap.get(paramName).getName())); i++; continue; - } if (paramMap.get(shortOptionMap.get(args.get(i))).getParameterType().equals(ParameterType.TEXT)) { - paramMap.get(shortOptionMap.get(args.get(i))).setValue(readTextStringFromUrl(args.get(i + 1), - paramMap.get(shortOptionMap.get(args.get(i))).getName())); + } if (paramMap.get(paramName).getParameterType().equals(ParameterType.TEXT)) { + paramMap.get(paramName).setValue(readTextStringFromUrl(args.get(i + 1), + paramMap.get(paramName).getName())); i++; continue; - } else if (paramMap.get(shortOptionMap.get(args.get(i))).getParameterType() + } else if (paramMap.get(paramName).getParameterType() .equals(ParameterType.ARRAY)) { - Object value = paramMap.get(shortOptionMap.get(args.get(i))).getValue(); + Object value = paramMap.get(paramName).getValue(); List<String> list; if (value == "") { list = new ArrayList<>(); } else { - list = convertJsonToListString(paramMap.get(shortOptionMap.get(args.get(i))).getName(), + list = convertJsonToListString(paramMap.get(paramName).getName(), value.toString()); } list.add(args.get(i + 1)); - paramMap.get(shortOptionMap.get(args.get(i))).setValue(list); + paramMap.get(paramName).setValue(list); i++; continue; - } else if (paramMap.get(shortOptionMap.get(args.get(i))).getParameterType().equals(ParameterType.MAP)) { - Object value = paramMap.get(shortOptionMap.get(args.get(i))).getValue(); + } else if (paramMap.get(paramName).getParameterType().equals(ParameterType.MAP)) { + Object value = paramMap.get(paramName).getValue(); Map<String, String> map; if (value == "") { map = new HashMap<>(); } else { - map = convertJsonToMapString(paramMap.get(shortOptionMap.get(args.get(i))).getName(), + map = convertJsonToMapString(paramMap.get(paramName).getName(), value.toString()); } @@ -146,83 +150,16 @@ public class OnapCliUtils { String[] argArr = arg.split("="); if (argArr.length != 2) { - throw new OnapCliInvalidArgument(paramMap.get(shortOptionMap.get(args.get(i))).getName()); + throw new OnapCliInvalidArgument(paramMap.get(paramName).getName()); } map.put(argArr[0], argArr[1]); - paramMap.get(shortOptionMap.get(args.get(i))).setValue(map); + paramMap.get(paramName).setValue(map); i++; continue; } - paramMap.get(shortOptionMap.get(args.get(i))).setValue(args.get(i + 1)); - - i++; - continue; - } - - // check if long option exist - if (longOptionMap.containsKey(args.get(i))) { - // 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(longOptionMap.get(args.get(i))).getParameterType().equals(ParameterType.BOOL)) { - paramMap.get(longOptionMap.get(args.get(i))).setValue("true"); - continue; - } - throw new OnapCliArgumentValueMissing(args.get(i)); - } - - if (paramMap.get(longOptionMap.get(args.get(i))).getParameterType().equals(ParameterType.JSON)) { - paramMap.get(longOptionMap.get(args.get(i))).setValue(readJsonStringFromUrl(args.get(i + 1), - paramMap.get(longOptionMap.get(args.get(i))).getName())); - i++; - continue; - } else if (paramMap.get(longOptionMap.get(args.get(i))).getParameterType().equals(ParameterType.TEXT)) { - paramMap.get(longOptionMap.get(args.get(i))).setValue(readTextStringFromUrl(args.get(i + 1), - paramMap.get(longOptionMap.get(args.get(i))).getName())); - i++; - continue; - } else if (paramMap.get(longOptionMap.get(args.get(i))).getParameterType() - .equals(ParameterType.ARRAY)) { - Object value = paramMap.get(longOptionMap.get(args.get(i))).getValue(); - List<String> list; - if (value == "") { - list = new ArrayList<>(); - } else { - list = convertJsonToListString(paramMap.get(longOptionMap.get(args.get(i))).getName(), - value.toString()); - } - list.add(args.get(i + 1)); - paramMap.get(longOptionMap.get(args.get(i))).setValue(list); - i++; - continue; - } else if (paramMap.get(longOptionMap.get(args.get(i))).getParameterType().equals(ParameterType.MAP)) { - - Object value = paramMap.get(longOptionMap.get(args.get(i))).getValue(); - - Map<String, String> map; - - if (value == "") { - map = new HashMap<>(); - } else { - map = convertJsonToMapString(paramMap.get(longOptionMap.get(args.get(i))).getName(), - value.toString()); - } - - String arg = args.get(i + 1); - String[] argArr = arg.split("="); - - if (argArr.length != 2) { - throw new OnapCliInvalidArgument(paramMap.get(longOptionMap.get(args.get(i))).getName()); - } - - map.put(argArr[0], argArr[1]); - paramMap.get(longOptionMap.get(args.get(i))).setValue(map); - i++; - continue; - } - - paramMap.get(longOptionMap.get(args.get(i))).setValue(args.get(i + 1)); + paramMap.get(paramName).setValue(args.get(i + 1)); i++; continue; 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 6eb2e11f..d720ed95 100644 --- a/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java +++ b/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java @@ -31,7 +31,7 @@ import org.onap.cli.fw.OnapCommand; import org.onap.cli.fw.OnapCommandRegistrar; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandHelpFailed; -import org.onap.cli.main.utils.OnapCliUtils; +import org.onap.cli.main.utils.OnapCliArgsParser; import jline.console.ConsoleReader; import mockit.Invocation; @@ -120,7 +120,7 @@ public class OnapCliMainTest { OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get("sample-create"); List<String> args = new ArrayList<>(Arrays.asList(new String[] { "sample-create", "--service-name", "test-service", "-i", "ip1", "-i", "ip2", "-o", "port1=value1", "-o", "port2=value2" })); - OnapCliUtils.populateParams(cmd.getParameters(), args); + OnapCliArgsParser.populateParams(cmd.getParameters(), args); } @Test 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 1ea1a22d..ad1a7ccc 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 @@ -63,7 +63,7 @@ public class OnapCliUtilsTest { String[] args = new String[] { "sample-create", "--host-username", "admin", "--host-password", "123", "--host-url", "a@b.com", "--string-param", "blah", "--long-opt", "10" }; - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertEquals("host-username", expectedList.get(2), param1.getValue()); @@ -82,7 +82,7 @@ public class OnapCliUtilsTest { paramslist.add(paramargs); String[] args = new String[] { "positional-args", "http://localhost:8082/file.txt" }; paramargs.setParameterType(ParameterType.STRING); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertEquals("positional-args", expectedList.get(1), paramslist.get(0).getValue()); } @@ -97,7 +97,7 @@ public class OnapCliUtilsTest { String[] args = new String[] { "sample-create", "--bool" }; boolparam.setParameterType(ParameterType.BOOL); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue()); @@ -113,7 +113,7 @@ public class OnapCliUtilsTest { String[] args = new String[] { "sample-create", "-b", }; boolparam.setParameterType(ParameterType.BOOL); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue()); } @@ -128,7 +128,7 @@ public class OnapCliUtilsTest { String[] args = new String[] { "sample-create", "--text-param" , "text"}; boolparam.setParameterType(ParameterType.TEXT); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue()); @@ -144,7 +144,7 @@ public class OnapCliUtilsTest { String[] args = new String[] { "sample-create", "-e" , "text"}; boolparam.setParameterType(ParameterType.TEXT); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue()); } @@ -159,7 +159,7 @@ public class OnapCliUtilsTest { File resourcesDirectory = new File("src/test/resources/sampletest.json"); String[] args = new String[] { "sample-create", "-j", "file:" + resourcesDirectory }; jsonparam.setParameterType(ParameterType.JSON); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue()); } @@ -174,7 +174,7 @@ public class OnapCliUtilsTest { File resourcesDirectory = new File("src/test/resources/sampletest.json"); String[] args = new String[] { "sample-create", "--json-param", "file:" + resourcesDirectory }; jsonparam.setParameterType(ParameterType.JSON); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue()); } @@ -212,7 +212,7 @@ public class OnapCliUtilsTest { String[] args11 = new String[] { "sample-create", "-u", "admin", "-p", "123", "-r", "a@b.com", "-c", "blah", "-l", "10", }; - OnapCliUtils.populateParams(paramslist, Arrays.asList(args11)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args11)); List<String> expectedList = Arrays.asList(args11); @@ -234,7 +234,7 @@ public class OnapCliUtilsTest { paramslist.add(arrayval); arrayval.setParameterType(ParameterType.ARRAY); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue()); @@ -250,7 +250,7 @@ public class OnapCliUtilsTest { paramslist.add(param1); param1.setParameterType(ParameterType.MAP); - OnapCliUtils.populateParams(paramslist, + OnapCliArgsParser.populateParams(paramslist, Arrays.asList("show", "--map", "param1=value1", "--map", "param2=value2")); Assert.assertEquals("{\"param1\":\"value1\",\"param2\":\"value2\"}", paramslist.get(0).getValue().toString()); @@ -266,7 +266,7 @@ public class OnapCliUtilsTest { paramslist.add(param1); param1.setParameterType(ParameterType.MAP); - OnapCliUtils.populateParams(paramslist, Arrays.asList("show", "--map", "param1=value1", "--map", "param2")); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList("show", "--map", "param1=value1", "--map", "param2")); Assert.assertEquals("{\"param1\":\"value1\",\"param2\"}", paramslist.get(0).getValue().toString()); } @@ -279,7 +279,7 @@ public class OnapCliUtilsTest { List<OnapCommandParameter> paramslist = new ArrayList<>(); paramslist.add(param1); param1.setParameterType(ParameterType.MAP); - OnapCliUtils.populateParams(paramslist, Arrays.asList("show", "-o", "param1=value1", "-o", "param2")); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList("show", "-o", "param1=value1", "-o", "param2")); Assert.assertEquals("{\"param1\":\"value1\",\"param2\"}", paramslist.get(0).getValue().toString()); } @@ -292,7 +292,7 @@ public class OnapCliUtilsTest { String[] args = new String[] { "positional-args", "http://localhost:8082/file.txt", "http://localhost:8082/file.txt" }; paramargs.setParameterType(ParameterType.STRING); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertEquals("positional-args", expectedList.get(1), paramslist.get(0).getValue()); } @@ -307,7 +307,7 @@ public class OnapCliUtilsTest { String[] args = new String[] { "sample-create", "-b", "-b", "-h" }; boolparam.setParameterType(ParameterType.BOOL); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertEquals("true", paramslist.get(0).getValue()); } @@ -322,7 +322,7 @@ public class OnapCliUtilsTest { String[] args = new String[] { "sample-create", "--bool", "--bool", "--help" }; boolparam.setParameterType(ParameterType.BOOL); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertEquals("true", paramslist.get(0).getValue()); } @@ -337,7 +337,7 @@ public class OnapCliUtilsTest { File resourcesDirectory = new File("src/test/resources/sampletest.json"); String[] args = new String[] { "sample-create", "--json-param", "file:" + resourcesDirectory, "--json-param" }; jsonparam.setParameterType(ParameterType.JSON); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertEquals("--json-param", paramslist.get(0).getValue()); @@ -353,7 +353,7 @@ public class OnapCliUtilsTest { File resourcesDirectory = new File("src/test/resources/sampletest.json"); String[] args = new String[] { "sample-create", "-j", "file:" + resourcesDirectory, "-j" }; jsonparam.setParameterType(ParameterType.JSON); - OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + OnapCliArgsParser.populateParams(paramslist, Arrays.asList(args)); List<String> expectedList = Arrays.asList(args); Assert.assertEquals("--json-param", paramslist.get(0).getValue()); |