aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>2017-07-19 15:14:29 +0530
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>2017-07-19 15:31:19 +0530
commitb610d2dbfa445e2ed8fd7f9976ae7a776666d630 (patch)
tree6d58385dc197aada4c6a832c922f04d0f4da61ad /main/src
parent20173579de1cd7d8f70fc2cdf17a4f9c0fc6e946 (diff)
Add seed code from Open-Orelease-1.0.0
It migrated the code from Open-O into onap namespace and adds the required framework, main, deployment and some sample plugins for msb. CLI-7 CLI-8 CLI-11 Change-Id: I499e34237daccb971ef74bd10e50f50707baa4d3 Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'main/src')
-rw-r--r--main/src/main/java/org/onap/cli/main/OnapCli.java267
-rw-r--r--main/src/main/java/org/onap/cli/main/conf/OnapCliConstants.java39
-rw-r--r--main/src/main/java/org/onap/cli/main/error/OnapCliArgumentValueMissing.java32
-rw-r--r--main/src/main/java/org/onap/cli/main/error/OnapCliInvalidArgument.java42
-rw-r--r--main/src/main/java/org/onap/cli/main/interactive/StringCompleter.java79
-rw-r--r--main/src/main/java/org/onap/cli/main/utils/OnapCliUtils.java270
-rw-r--r--main/src/main/resources/onap-readme.txt15
-rw-r--r--main/src/test/java/org/onap/cli/main/OnapCliMainTest.java347
-rw-r--r--main/src/test/java/org/onap/cli/main/OnapCommandSampleTest.java35
-rw-r--r--main/src/test/java/org/onap/cli/main/conf/OnapCliConstantsTest.java33
-rw-r--r--main/src/test/java/org/onap/cli/main/error/OnapCliArgumentTest.java39
-rw-r--r--main/src/test/java/org/onap/cli/main/interactive/StringCompleterTest.java48
-rw-r--r--main/src/test/java/org/onap/cli/main/utils/OnapCliUtilsTest.java330
-rw-r--r--main/src/test/resources/META-INF/services/org.onap.cli.fw.OnapCommand1
-rw-r--r--main/src/test/resources/onap-cli-schema/sample-create-schema.yaml71
-rw-r--r--main/src/test/resources/sample-test-schema.yaml79
-rw-r--r--main/src/test/resources/sampletest.json8
17 files changed, 1735 insertions, 0 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
new file mode 100644
index 00000000..05daf116
--- /dev/null
+++ b/main/src/main/java/org/onap/cli/main/OnapCli.java
@@ -0,0 +1,267 @@
+/*
+ * 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.main;
+
+import org.apache.commons.io.IOUtils;
+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.OnapCommandWarning;
+import org.onap.cli.fw.input.OnapCommandParameter;
+import org.onap.cli.fw.output.OnapCommandResult;
+import org.onap.cli.main.conf.OnapCliConstants;
+import org.onap.cli.main.interactive.StringCompleter;
+import org.onap.cli.main.utils.OnapCliUtils;
+
+import jline.TerminalFactory;
+import jline.console.ConsoleReader;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Onap Command Line Interface (CLI).
+ *
+ */
+public class OnapCli {
+
+ private List<String> args = new ArrayList<>();
+
+ private int exitCode = -1;
+
+ public OnapCli(String[] args) {
+ this.args = Arrays.asList(args);
+ }
+
+ private void exitSuccessfully() {
+ this.exitCode = OnapCliConstants.EXIT_SUCCESS;
+ }
+
+ private void exitFailure() {
+ this.exitCode = OnapCliConstants.EXIT_FAILURE;
+ }
+
+ private void print(String msg) {
+ System.out.println(msg);
+ }
+
+ private void print(Throwable throwable) {
+ System.out.println(throwable.getMessage());
+ }
+
+ private String getShortOption(String opt) {
+ return OnapCommandParameter.printShortOption(opt);
+ }
+
+ private String getLongOption(String opt) {
+ return OnapCommandParameter.printLongOption(opt);
+ }
+
+ public int getExitCode() {
+ return this.exitCode;
+ }
+
+ /**
+ * Handles help. --help or -h
+ */
+ public void handleHelp() {
+ try {
+ // By default, it prints help
+ if ((args.isEmpty())
+ || ((args.size() == 1) && (this.getLongOption(OnapCliConstants.PARAM_HELP_LOGN).equals(args.get(0))
+ || this.getShortOption(OnapCliConstants.PARAM_HELP_SHORT).equals(args.get(0))))) {
+ this.print(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("onap-readme.txt")));
+ String help = OnapCommandRegistrar.getRegistrar().getHelp();
+ this.print(help);
+ this.exitSuccessfully();
+ }
+ } catch (Exception e) {
+ this.print(e);
+ this.exitFailure();
+ }
+ }
+
+ /**
+ * Handles version. --version or -v
+ */
+ public void handleVersion() {
+ try {
+ if ((args.size() == 1) && (this.getLongOption(OnapCliConstants.PARAM_VERSION_LONG).equals(args.get(0))
+ || this.getShortOption(OnapCliConstants.PARAM_VERSION_SHORT).equals(args.get(0)))) {
+ String version = OnapCommandRegistrar.getRegistrar().getVersion();
+ this.print(version);
+ this.exitSuccessfully();
+ }
+ } catch (Exception e) {
+ this.print(e);
+ this.exitFailure();
+ }
+ }
+
+ /**
+ * Handles Interactive Mode. --interactive or -i
+ */
+ public void handleInteractive() { // NOSONAR
+ if (isInteractive()) {
+ ConsoleReader console = null;
+ try {
+ console = createConsoleReader();
+ String line = null;
+ while ((line = console.readLine()) != null) {
+ if (OnapCliConstants.PARAM_INTERACTIVE_EXIT.equalsIgnoreCase(line)
+ || OnapCliConstants.PARAM_INTERACTIVE_BYE.equalsIgnoreCase(line)) {
+ break;
+ } else if (OnapCliConstants.PARAM_INTERACTIVE_CLEAR.equalsIgnoreCase(line)) {
+ console.clearScreen();
+ continue;
+ }
+ this.args = Arrays.asList(line.split(OnapCliConstants.PARAM_INTERACTIVE_ARG_SPLIT_PATTERN));
+ handleCommand();
+ }
+ } catch (IOException e) { // NOSONAR
+ this.print("Failed to read console, " + e.getMessage());
+ } finally {
+ try {
+ TerminalFactory.get().restore();
+ } catch (Exception e) { // NOSONAR
+ }
+ if (console != null) {
+ console.close();
+ }
+ this.exitSuccessfully();
+ }
+ }
+ }
+
+ /**
+ * Checks if the command is interactive.
+ *
+ * @return boolean
+ */
+ public boolean isInteractive() {
+ if ((args.size() == 1) && (this.getLongOption(OnapCliConstants.PARAM_INTERACTIVE_LONG).equals(args.get(0))
+ || this.getShortOption(OnapCliConstants.PARAM_INTERACTIVE_SHORT).equals(args.get(0)))) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Creates console reader object.
+ *
+ * @return ConsoleReader
+ * @throws IOException
+ * exception
+ */
+ private ConsoleReader createConsoleReader() throws IOException {
+ ConsoleReader console = new ConsoleReader();
+ try {
+ StringCompleter strCompleter = new StringCompleter(OnapCommandRegistrar.getRegistrar().listCommands());
+ strCompleter.add(OnapCliConstants.PARAM_INTERACTIVE_EXIT,
+ OnapCliConstants.PARAM_INTERACTIVE_CLEAR);
+ console.addCompleter(strCompleter);
+ console.setPrompt(OnapCliConstants.PARAM_INTERACTIVE_PROMPT);
+ } catch (OnapCommandException e) { // NOSONAR
+ this.print("Failed to load onap commands," + e.getMessage());
+ }
+
+ return console;
+ }
+
+ /**
+ * Handles command.
+ */
+ public void handleCommand() {
+ OnapCommand cmd;
+ if (!args.isEmpty()) {
+ try {
+ cmd = OnapCommandRegistrar.getRegistrar().get(args.get(0));
+ } catch (Exception e) {
+ this.print(e);
+ this.exitFailure();
+ return;
+ }
+ try {
+ // check for help or version
+ if (args.size() == 2) {
+ if (this.getLongOption(OnapCliConstants.PARAM_HELP_LOGN).equals(args.get(1))
+ || this.getShortOption(OnapCliConstants.PARAM_HELP_SHORT).equals(args.get(1))) {
+ String help = cmd.printHelp();
+ this.print(help);
+ this.exitSuccessfully();
+ return;
+ } else if (this.getLongOption(OnapCliConstants.PARAM_VERSION_LONG).equals(args.get(1))
+ || this.getShortOption(OnapCliConstants.PARAM_VERSION_SHORT).equals(args.get(1))) {
+ String version = cmd.printVersion();
+ this.print(version);
+ this.exitSuccessfully();
+ return;
+ }
+ }
+
+ OnapCliUtils.populateParams(cmd.getParameters(), args);
+ OnapCommandResult result = cmd.execute();
+ this.print(result.getDebugInfo());
+ this.print(result.print());
+ this.exitSuccessfully();
+ } catch (Exception e) {
+ this.print(cmd.getResult().getDebugInfo());
+ if (e instanceof OnapCommandWarning) {
+ this.exitSuccessfully();
+ } else {
+ this.print(e);
+ this.exitFailure();
+ }
+ }
+ }
+ }
+
+ /**
+ * Handles all client input.
+ */
+ public void handle() {
+ this.handleHelp();
+
+ if (this.exitCode == -1) {
+ this.handleVersion();
+ }
+
+ if (this.exitCode == -1) {
+ this.handleInteractive();
+ }
+
+ if (this.exitCode == -1) {
+ this.handleCommand();
+ }
+ }
+
+ /**
+ * Main method.
+ *
+ * @param args
+ * array
+ */
+ public static void main(String[] args) {
+ OnapCli cli = new OnapCli(args);
+ cli.handle();
+ System.exit(cli.getExitCode());
+ }
+
+}
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
new file mode 100644
index 00000000..7fb51cbe
--- /dev/null
+++ b/main/src/main/java/org/onap/cli/main/conf/OnapCliConstants.java
@@ -0,0 +1,39 @@
+/*
+ * 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.main.conf;
+
+public final class OnapCliConstants {
+
+ public static final String PARAM_HELP_SHORT = "h";
+ public static final String PARAM_HELP_LOGN = "help";
+
+ public static final String PARAM_VERSION_SHORT = "v";
+ public static final String PARAM_VERSION_LONG = "version";
+
+ public static final int EXIT_SUCCESS = 0;
+ public static final int EXIT_FAILURE = 1;
+
+ public static final String PARAM_INTERACTIVE_SHORT = "i";
+ public static final String PARAM_INTERACTIVE_LONG = "interactive";
+ public static final String PARAM_INTERACTIVE_PROMPT = "onap>";
+ public static final String PARAM_INTERACTIVE_EXIT = "exit";
+ public static final String PARAM_INTERACTIVE_BYE = "bye";
+ public static final String PARAM_INTERACTIVE_CLEAR = "clear";
+ public static final String PARAM_INTERACTIVE_ARG_SPLIT_PATTERN = "\\s+";
+
+ private OnapCliConstants(){}
+}
diff --git a/main/src/main/java/org/onap/cli/main/error/OnapCliArgumentValueMissing.java b/main/src/main/java/org/onap/cli/main/error/OnapCliArgumentValueMissing.java
new file mode 100644
index 00000000..f8f86e46
--- /dev/null
+++ b/main/src/main/java/org/onap/cli/main/error/OnapCliArgumentValueMissing.java
@@ -0,0 +1,32 @@
+/*
+ * 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.main.error;
+
+import org.onap.cli.fw.error.OnapCommandException;
+
+/**
+ * CLI argument missing.
+ *
+ */
+public class OnapCliArgumentValueMissing extends OnapCommandException {
+
+ private static final long serialVersionUID = -6362824415803063442L;
+
+ public OnapCliArgumentValueMissing(String arg) {
+ super("0x1001", "Value for argument " + arg + " is missing");
+ }
+}
diff --git a/main/src/main/java/org/onap/cli/main/error/OnapCliInvalidArgument.java b/main/src/main/java/org/onap/cli/main/error/OnapCliInvalidArgument.java
new file mode 100644
index 00000000..32d8ef6f
--- /dev/null
+++ b/main/src/main/java/org/onap/cli/main/error/OnapCliInvalidArgument.java
@@ -0,0 +1,42 @@
+/*
+ * 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.main.error;
+
+import org.onap.cli.fw.error.OnapCommandException;
+
+/**
+ * CLI argument missing.
+ *
+ */
+public class OnapCliInvalidArgument extends OnapCommandException {
+
+ private static final long serialVersionUID = -1438492553530993246L;
+ private static final String ERROR_CODE = "0x1000";
+ private static final String ERROR_MESSAGE1 = "Invalid argument ";
+
+ public OnapCliInvalidArgument(String arg) {
+ super(ERROR_CODE, ERROR_MESSAGE1 + arg);
+ }
+
+ public OnapCliInvalidArgument(String arg, String errorMessage) {
+ super(ERROR_CODE, ERROR_MESSAGE1 + arg + " , " + errorMessage);
+ }
+
+ public OnapCliInvalidArgument(String arg, Throwable throwable) {
+ this(arg , throwable.getMessage());
+ }
+}
diff --git a/main/src/main/java/org/onap/cli/main/interactive/StringCompleter.java b/main/src/main/java/org/onap/cli/main/interactive/StringCompleter.java
new file mode 100644
index 00000000..cf2a6c55
--- /dev/null
+++ b/main/src/main/java/org/onap/cli/main/interactive/StringCompleter.java
@@ -0,0 +1,79 @@
+/*
+ * 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.main.interactive;
+
+import jline.console.completer.Completer;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+/**
+ * String completer for autocomplete commands.
+ *
+ */
+public class StringCompleter implements Completer {
+ private final SortedSet<String> strings = new TreeSet<String>();
+
+ /**
+ * Public Constructor takes collection of strings.
+ *
+ * @param strings collection
+ */
+ public StringCompleter(Collection<String> strings) {
+ this.strings.addAll(strings);
+ }
+
+ /**
+ * Public Constructor takes array of strings.
+ *
+ * @param strings array of strings
+ */
+ public void add(String... strings) {
+ this.strings.addAll(Arrays.asList(strings));
+ }
+
+ /**
+ * String completion.
+ *
+ * @param buffer
+ * string
+ * @param cursor
+ * int
+ * @param candidates
+ * list
+ * @return int
+ */
+ public int complete(String buffer, int cursor, List<CharSequence> candidates) {
+ if (buffer == null) {
+ candidates.addAll(strings);
+ } else {
+ for (String match : strings.tailSet(buffer)) {
+ if (!match.startsWith(buffer)) {
+ break;
+ }
+ candidates.add(match);
+ }
+ }
+ if (candidates.size() == 1) {
+ candidates.set(0, candidates.get(0) + " ");
+ }
+ return candidates.isEmpty() ? -1 : 0;
+ }
+}
diff --git a/main/src/main/java/org/onap/cli/main/utils/OnapCliUtils.java b/main/src/main/java/org/onap/cli/main/utils/OnapCliUtils.java
new file mode 100644
index 00000000..ed47bc68
--- /dev/null
+++ b/main/src/main/java/org/onap/cli/main/utils/OnapCliUtils.java
@@ -0,0 +1,270 @@
+/*
+ * 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.main.utils;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import net.minidev.json.JSONObject;
+
+import org.onap.cli.fw.error.OnapCommandException;
+import org.onap.cli.fw.error.OnapCommandInvalidParameterValue;
+import org.onap.cli.fw.input.OnapCommandParameter;
+import org.onap.cli.fw.input.ParameterType;
+import org.onap.cli.main.error.OnapCliArgumentValueMissing;
+import org.onap.cli.main.error.OnapCliInvalidArgument;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Onap CLI utilities.
+ *
+ */
+public class OnapCliUtils {
+
+ /**
+ * private Constructor.
+ */
+ private OnapCliUtils() {
+
+ }
+
+ /**
+ * It read thru the args and populate the given params for short optional, long option and postional args the idx of
+ * positional args, is calculated based on the position at which it present in the params and args.
+ *
+ * @param params
+ * List of command paramters
+ * @param args
+ * Array of arguments
+ * @throws OnapCliArgumentValueMissing
+ * ArgumentValueMissing exception
+ * @throws OnapCliInvalidArgument
+ * Invalid argument exception
+ * @throws OnapCommandInvalidParameterValue
+ * exception
+ */
+ public static void populateParams(List<OnapCommandParameter> params, List<String> args)
+ throws OnapCommandException {
+ Map<String, String> shortOptionMap = new HashMap<>();
+ Map<String, String> longOptionMap = new HashMap<>();
+ List<String> positionArgs = new ArrayList<>();
+ Map<String, OnapCommandParameter> paramMap = new HashMap<>();
+
+ for (OnapCommandParameter param : params) {
+ boolean positional = true;
+ if (param.getShortOption() != null) {
+ shortOptionMap.put(OnapCommandParameter.printShortOption(param.getShortOption()), param.getName());
+ positional = false;
+ }
+ if (param.getLongOption() != null) {
+ longOptionMap.put(OnapCommandParameter.printLongOption(param.getLongOption()), param.getName());
+ positional = false;
+ }
+
+ if (positional) {
+ positionArgs.add(param.getName());
+ }
+
+ paramMap.put(param.getName(), param);
+ }
+
+ int positionalIdx = 0;
+ // Skip the first args onap 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))) {
+ // 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");
+ 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()));
+ i++;
+ continue;
+ } else if (paramMap.get(shortOptionMap.get(args.get(i))).getParameterType()
+ .equals(ParameterType.ARRAY)) {
+ Object value = paramMap.get(shortOptionMap.get(args.get(i))).getValue();
+ List<String> list;
+ if (value == "") {
+ list = new ArrayList<>();
+ } else {
+ list = convertJsonToListString(paramMap.get(shortOptionMap.get(args.get(i))).getName(),
+ value.toString());
+ }
+ list.add(args.get(i + 1));
+ paramMap.get(shortOptionMap.get(args.get(i))).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();
+
+ Map<String, String> map;
+
+ if (value == "") {
+ map = new HashMap<>();
+ } else {
+ map = convertJsonToMapString(paramMap.get(shortOptionMap.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(shortOptionMap.get(args.get(i))).getName());
+ }
+
+ map.put(argArr[0], argArr[1]);
+ paramMap.get(shortOptionMap.get(args.get(i))).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.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));
+
+ i++;
+ continue;
+ }
+
+ // it is positional option
+ // Positional arg is missing from the params
+ if (positionalIdx >= positionArgs.size()) {
+ throw new OnapCliInvalidArgument(args.get(i));
+ }
+
+ paramMap.get(positionArgs.get(positionalIdx)).setValue(args.get(i));
+ positionalIdx++;
+ }
+
+ params.clear();
+ params.addAll(paramMap.values());
+ }
+
+ private static String readJsonStringFromUrl(String input, String argName) throws OnapCliInvalidArgument {
+ ObjectMapper mapper = new ObjectMapper();
+ try {
+ File file = new File(input);
+ if (file.isFile()) {
+ return mapper.readValue(file, JSONObject.class).toJSONString();
+ } else if (input.startsWith("file:") || input.startsWith("http:") || input.startsWith("ftp:")) {
+ URL jsonUrl = new URL(input);
+ return mapper.readValue(jsonUrl, JSONObject.class).toJSONString();
+ } else {
+ return mapper.readValue(input, JSONObject.class).toJSONString();
+ }
+ } catch (IOException e) {
+ throw new OnapCliInvalidArgument(argName, e);
+ }
+ }
+
+ private static List<String> convertJsonToListString(String arg, String json) throws OnapCliInvalidArgument {
+ TypeReference<List<String>> mapType = new TypeReference<List<String>>() {
+ };
+ try {
+ return new ObjectMapper().readValue(json, mapType);
+ } catch (IOException e) {
+ throw new OnapCliInvalidArgument(arg, e);
+ }
+ }
+
+ private static Map<String, String> convertJsonToMapString(String arg, String json) throws OnapCliInvalidArgument {
+ TypeReference<Map<String, String>> mapType = new TypeReference<Map<String, String>>() {
+ };
+ try {
+ return new ObjectMapper().readValue(json, mapType);
+ } catch (IOException e) {
+ throw new OnapCliInvalidArgument(arg, e);
+ }
+ }
+}
diff --git a/main/src/main/resources/onap-readme.txt b/main/src/main/resources/onap-readme.txt
new file mode 100644
index 00000000..737cd04d
--- /dev/null
+++ b/main/src/main/resources/onap-readme.txt
@@ -0,0 +1,15 @@
+Onap Command-line interface (CLI).
+
+One Command to command whole Onap !! It provides the unified commands
+to access and operate Onap functionalities. To use this CLI, please
+configure the following environment variables:
+
+1. ONAP_MSB_URL - Onap Micro service bus(MSB) URL
+2. ONAP_USERNAME - Onap user name
+3. ONAP_PASSWORD - Onap user password
+
+To know the CLI version, type onap [-v|--version]
+To know the CLI usage, type onap [-h|--help]
+To know the usage of sub commands, type onap <command> [-h|--help]
+
+To know more, please refer the Onap wiki https://wiki.onap.org \ 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
new file mode 100644
index 00000000..ca955cc9
--- /dev/null
+++ b/main/src/test/java/org/onap/cli/main/OnapCliMainTest.java
@@ -0,0 +1,347 @@
+/*
+ * 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.main;
+
+import static org.junit.Assert.assertTrue;
+
+import jline.console.ConsoleReader;
+import mockit.Invocation;
+import mockit.Mock;
+import mockit.MockUp;
+
+import org.aspectj.lang.annotation.After;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.onap.cli.fw.OnapCommand;
+import org.onap.cli.fw.OnapCommandRegistrar;
+import org.onap.cli.fw.error.OnapCommandException;
+import org.onap.cli.main.utils.OnapCliUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class OnapCliMainTest {
+
+ OnapCli cli = null;
+
+ /**
+ * Clean up.
+ */
+ @After(value = "")
+ public void cleanup() {
+ if (this.cli != null) {
+ if (cli.getExitCode() != 0) {
+ // Fail test case
+ }
+ }
+ }
+
+ private void handle(String[] args) {
+ cli = new OnapCli(args);
+ cli.handle();
+ }
+
+ @Test
+ public void testHelp() {
+ this.handle(new String[] { "--help" });
+ }
+
+ @Test
+ public void testHelpShort() {
+ this.handle(new String[] { "-h" });
+ }
+
+ @Test
+ public void testVersion() {
+ this.handle(new String[] { "--version" });
+ }
+
+ @Test
+ public void testVersionShort() {
+ this.handle(new String[] { "--v" });
+ }
+
+ @Test
+ public void testHelpSampleCommand() {
+ this.handle(new String[] { "sample-test", "--help" });
+ }
+
+ @Test
+ public void testHelpSampleCreateCommand() throws OnapCommandException {
+ ClassLoader cl = ClassLoader.getSystemClassLoader();
+ URL[] urls = ((URLClassLoader) cl).getURLs();
+ for (URL url : urls) {
+ if (url.getPath().contains("main/target/test-classes")) {
+ File file = new File(url.getPath() + "data");
+ if (!file.exists()) {
+ file.mkdirs();
+ }
+ break;
+ }
+ }
+ this.handle(new String[] { "sample-create", "--help" });
+ OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get("sample-create");
+ List<String> args = new ArrayList<>(Arrays.asList(new String[] { "sample-create", "-u", "admin", "-p",
+ "Changeme_123", "-m", "http://192.168.99.100:80", "--service-name", "test-service", "-i", "ip1", "-i",
+ "ip2", "-o", "port1=value1", "-o", "port2=value2" }));
+ OnapCliUtils.populateParams(cmd.getParameters(), args);
+ }
+
+ @Test
+ public void testHelpSampleCommandShort() {
+ this.handle(new String[] { "sample-test", "-h" });
+ }
+
+ @Test
+ public void testHelpServiceListCommandShort() {
+ this.handle(new String[] { "microservice-list", "-h" });
+ }
+
+ @Test
+ public void testHelpUserCreateCommand() {
+ this.handle(new String[] { "user-create", "--help" });
+ }
+
+ @Test
+ @Ignore
+ public void testUserCreateCommand() {
+ this.handle(new String[] { "user-create", "-u", "admin", "-p", "Changeme_123", "-m", "http://192.168.99.100:80",
+ "--username", "test", "--password", "sss", "--description", "test user", "-d" });
+ }
+
+ @Test
+ @Ignore
+ public void testHelpVimShowCommand() {
+ this.handle(new String[] { "vim-show", "--help" });
+ }
+
+ @Test
+ @Ignore
+ public void testVimListCommand() {
+ this.handle(new String[] { "vim-list", "-u", "admin", "-p", "Changeme_123", "-m", "http://192.168.99.100:80",
+ "--long", "--format", "table" });
+ }
+
+ @Test
+ public void tesVersionServiceListCommand() {
+ this.handle(new String[] { "microservice-list", "--version" });
+ }
+
+ @Test
+ public void tesVersionServiceListCommandShort() {
+ this.handle(new String[] { "microservice-list", "-v" });
+ }
+
+ @Test
+ @Ignore
+ public void testServiceListCommand() {
+ this.handle(new String[] { "microservice-list", "-u", "root1", "-p", "root123", "-m", "http://192.168.4.47:80",
+ "--long", "--no-title", "true", "-d" });
+ }
+
+ @Test
+ @Ignore
+ public void testServiceCreateCommand() {
+ this.handle(new String[] { "microservice-create", "-u", "admin", "-p", "Changeme_123", "-m",
+ "http://192.168.99.100:80", "--service-name", "test-service", "--service-version", "v1",
+ "--service-url", "/openapi/sampletest/v1", "127.0.0.1", "8080", "--debug", "--long" });
+ }
+
+ @Test
+ @Ignore
+ public void testServiceShowCommand() {
+ this.handle(new String[] { "microservice-show", "-u", "admin", "-p", "Changeme_123", "-m",
+ "http://192.168.99.100:80", "--service-name", "test-service", "--service-version", "v1", "--debug",
+ "--long" });
+ }
+
+ @Test
+ @Ignore
+ public void testServiceDeleteCommand() {
+ this.handle(new String[] { "microservice-delete", "-u", "admin", "-p", "Changeme_123", "-m",
+ "http://192.168.99.100:80", "--service-name", "test-service", "--service-version", "v1", "--debug",
+ "--long" });
+ }
+
+ @Test
+ @Ignore
+ public void testSdncCreate() {
+
+ this.handle(new String[] { "sdnc-create", "-u", "root1", "-p", "root123", "-m", "http://192.168.4.47:80",
+ "--name", "testcontroller", "--vendor", "testvendor", "--sdnc-version", "v1", "--description",
+ "testingSDNC", "--type", "string", "--url", "onapapi/extsys/v1", "--username", "test", "--password",
+ "test", "--product-name", "testproduct", "--protocol", "http", "-d" });
+ }
+
+ @Test
+ @Ignore
+ public void testSdnclist() {
+
+ this.handle(new String[] { "sdnc-list", "-p", "root123", "--msb-url", "http://192.168.99.100", "-u", "root1",
+ "-d" });
+ }
+
+ @Test
+ @Ignore
+ public void testNFVResourcelist() {
+ this.handle(new String[] { "resource-datacenter-show", "--id", "test", "-u", "root", "-p", "root123",
+ "--msb-url", "http://192.168.99.100", "-a", "-d" });
+ }
+
+ @Test
+ @Ignore
+ public void testNFVResourceShow() {
+
+ this.handle(new String[] { "resource-datacenter-show", "--id", "test", "-u", "root", "-p", "root123",
+ "--msb-url", "http://192.168.99.100", "-a", "-d" });
+ }
+
+ @Test
+ @Ignore
+ public void testSdncdelete() {
+
+ this.handle(new String[] { "sdnc-delete", "-p", "root123", "--msb-url", "http://192.168.4.47", "-u", "root1",
+ "--id", "053104c1-0f8b-481d-9456-f7b02e87c0e7" });
+ }
+
+ @Test
+ @Ignore
+ public void testServiceCreateHelpCommand() {
+ this.handle(new String[] { "microservice-create", "--help" });
+ }
+
+ @Test
+ @Ignore
+ public void testGsoServiceCreateCommand() {
+ this.handle(new String[] { "service-create", "-m", "http://192.168.4.47:80", "-u", "root1", "-p", "root123",
+ "-x", "test", "-z", "test", "-n", "test", "-r", "123", "-j",
+ "D:/workspace/onap/integration/test/csit/plans/gso/sanity-check/jsoninput/lcm_CreateServiceReq.json",
+ "-d" });
+ }
+
+ @Test
+ @Ignore
+ public void testCsrUpload() {
+ this.handle(new String[] { "catalog-csar-create", "-u", "root", "-p", "root123", "--msb-url",
+ "http://192.168.4.47", "-d", "-a", "-z", "D:\\enterprise2DC.csar" });
+ }
+
+ @Test
+ @Ignore
+ public void testCsrDelete() {
+ this.handle(new String[] { "catalog-csar-delete", "-u", "root", "-p", "root123", "--msb-url",
+ "http://192.168.4.47", "-d", "-a", "-i", "7aa791f9-4e5f-433a-afeb-3555bcbabb47" });
+ }
+
+ @Test
+ @Ignore
+ public void testPortCreate() {
+ this.handle(new String[] { "resource-link-create", "-u", "root", "-p", "root123", "--msb-url",
+ "http://192.168.4.213", "-d", "-n", "PradeepLink1", "-b", "19.5.6.13", "-c", "193.4.57.13", "-g",
+ "193.5.6.13", "-q", "189.78.6.13", "-y", "fiberLink" });
+ }
+
+ @Test
+ public void validateCommands() throws IOException, OnapCommandException {
+ Map<String, String> cmdSchemaMap = OnapCommandRegistrar.getRegistrar().getAllCommandToSchemaMap();
+ for (String cmdName : cmdSchemaMap.keySet()) {
+ System.out.println(
+ "************************* '" + cmdSchemaMap.get(cmdName) + "' *******************************");
+ this.handle(new String[] { "schema-validate", "-l", cmdSchemaMap.get(cmdName), "-i", "true", "-m",
+ "http://192.168.4.47:80", "-u", "root1", "-p", "root123" });
+ }
+ }
+
+ @Test
+ public void commandHelpTest() throws OnapCommandException {
+ Set<String> cmds = OnapCommandRegistrar.getRegistrar().listCommands();
+
+ for (String cmdName : cmds) {
+ System.out.println("************************* '" + cmdName + "' *******************************");
+ this.handle(new String[] { cmdName, "-h" });
+ }
+
+ }
+
+ @Test
+ public void interactiveTest() {
+ cli = new OnapCli(new String[] { "-i" });
+ boolean isInter = cli.isInteractive();
+
+ assertTrue(isInter);
+ cli = new OnapCli(new String[] { "--interactive" });
+ isInter = cli.isInteractive();
+ assertTrue(isInter);
+ cli.getExitCode();
+
+ mockConsole("exit");
+ cli.handleInteractive();
+
+ mockConsole("bye");
+ cli.handleInteractive();
+
+ mockConsole("clear");
+ try {
+ cli.handleInteractive();
+ } catch (Exception e) {
+ }
+
+ mockConsole("microservice-create -h");
+
+ try {
+ cli.handleInteractive();
+ } catch (Exception e) {
+ }
+
+ mockConsoleReader();
+ cli.handleInteractive();
+
+ }
+
+ private static void mockConsoleReader() {
+ new MockUp<OnapCli>() {
+ @Mock
+ public ConsoleReader createConsoleReader() throws IOException {
+ throw new IOException("Exception mock");
+ }
+ };
+ }
+
+ private static void mockConsole(String input) {
+ new MockUp<ConsoleReader>() {
+ boolean isMock = true;
+
+ @Mock
+ public String readLine(Invocation inv) throws IOException {
+ if (isMock) {
+ isMock = false;
+ return input;
+ } else {
+ return inv.proceed(input);
+ }
+ }
+ };
+ }
+
+}
diff --git a/main/src/test/java/org/onap/cli/main/OnapCommandSampleTest.java b/main/src/test/java/org/onap/cli/main/OnapCommandSampleTest.java
new file mode 100644
index 00000000..517d6c4f
--- /dev/null
+++ b/main/src/test/java/org/onap/cli/main/OnapCommandSampleTest.java
@@ -0,0 +1,35 @@
+/*
+ * 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.main;
+
+import org.onap.cli.fw.OnapCommand;
+import org.onap.cli.fw.OnapCommandSchema;
+import org.onap.cli.fw.error.OnapCommandException;
+
+/**
+ * This command helps to test the Command functionalities.
+ *
+ */
+@OnapCommandSchema(name = "sample-test", schema = "sample-test-schema.yaml")
+public class OnapCommandSampleTest extends OnapCommand {
+
+ @Override
+ protected void run() throws OnapCommandException {
+
+ }
+
+}
diff --git a/main/src/test/java/org/onap/cli/main/conf/OnapCliConstantsTest.java b/main/src/test/java/org/onap/cli/main/conf/OnapCliConstantsTest.java
new file mode 100644
index 00000000..af19836a
--- /dev/null
+++ b/main/src/test/java/org/onap/cli/main/conf/OnapCliConstantsTest.java
@@ -0,0 +1,33 @@
+/*
+ * 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.main.conf;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class OnapCliConstantsTest {
+
+ @Test
+ public void test() {
+ assertTrue(1 == OnapCliConstants.EXIT_FAILURE && 0 == OnapCliConstants.EXIT_SUCCESS
+ && "help".equals(OnapCliConstants.PARAM_HELP_LOGN) && "h".equals(OnapCliConstants.PARAM_HELP_SHORT)
+ && "v".equals(OnapCliConstants.PARAM_VERSION_SHORT)
+ && "version".equals(OnapCliConstants.PARAM_VERSION_LONG));
+ }
+
+}
diff --git a/main/src/test/java/org/onap/cli/main/error/OnapCliArgumentTest.java b/main/src/test/java/org/onap/cli/main/error/OnapCliArgumentTest.java
new file mode 100644
index 00000000..3916c630
--- /dev/null
+++ b/main/src/test/java/org/onap/cli/main/error/OnapCliArgumentTest.java
@@ -0,0 +1,39 @@
+/*
+ * 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.main.error;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class OnapCliArgumentTest {
+
+ @Test
+ public void onapCliArgumentValueMissingTest() {
+ OnapCliArgumentValueMissing failed = new OnapCliArgumentValueMissing("Argument value missing");
+ assertEquals("0x1001::Value for argument Argument value missing is missing", failed.getMessage());
+ }
+
+ @Test
+ public void onapCliInvalidArgumentTest() {
+ OnapCliInvalidArgument failed = new OnapCliInvalidArgument("Invalid Argument");
+ assertEquals("0x1000::Invalid argument Invalid Argument", failed.getMessage());
+ failed = new OnapCliInvalidArgument("Invalid Argument", new Exception(""));
+ assertEquals("0x1000::Invalid argument Invalid Argument , ", failed.getMessage());
+ }
+
+}
diff --git a/main/src/test/java/org/onap/cli/main/interactive/StringCompleterTest.java b/main/src/test/java/org/onap/cli/main/interactive/StringCompleterTest.java
new file mode 100644
index 00000000..9d5cbf17
--- /dev/null
+++ b/main/src/test/java/org/onap/cli/main/interactive/StringCompleterTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.main.interactive;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+public class StringCompleterTest {
+
+ @Test
+ public void completeTest() {
+ StringCompleter com = new StringCompleter(Arrays.asList(new String[] { "test", "testing1", "testing2" }));
+ int result = com.complete("test", 1, new ArrayList<CharSequence>());
+ assertTrue(result == 0);
+ result = com.complete(null, 1, new ArrayList<CharSequence>());
+ assertTrue(result == 0);
+
+ com.add("test", "testing1", "testing2");
+ result = com.complete("test", 1, new ArrayList<CharSequence>());
+ assertTrue(result == 0);
+
+ result = com.complete("sfds", 1, new ArrayList<CharSequence>());
+ assertTrue(result == -1);
+
+
+ result = com.complete("test", 1, new ArrayList<CharSequence>());
+ assertTrue(result == 0);
+ }
+
+}
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
new file mode 100644
index 00000000..c7fb7985
--- /dev/null
+++ b/main/src/test/java/org/onap/cli/main/utils/OnapCliUtilsTest.java
@@ -0,0 +1,330 @@
+/*
+ * 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.main.utils;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.onap.cli.fw.error.OnapCommandException;
+import org.onap.cli.fw.input.OnapCommandParameter;
+import org.onap.cli.fw.input.ParameterType;
+import org.onap.cli.main.error.OnapCliArgumentValueMissing;
+import org.onap.cli.main.error.OnapCliInvalidArgument;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class OnapCliUtilsTest {
+
+ @Test
+ public void testpopulateParamsLong() throws OnapCommandException {
+ OnapCommandParameter param1 = new OnapCommandParameter();
+ param1.setLongOption("onap-username");
+ param1.setName("onap-username");
+ param1.setParameterType(ParameterType.STRING);
+ OnapCommandParameter param2 = new OnapCommandParameter();
+ param2.setLongOption("onap-password");
+ param2.setName("onap-password");
+ param2.setParameterType(ParameterType.STRING);
+ OnapCommandParameter param3 = new OnapCommandParameter();
+ param3.setLongOption("msb-url");
+ param3.setName("msb-url");
+ param3.setParameterType(ParameterType.STRING);
+ OnapCommandParameter param4 = new OnapCommandParameter();
+ param4.setLongOption("string-param");
+ param4.setName("string-param");
+ param4.setParameterType(ParameterType.STRING);
+ OnapCommandParameter param5 = new OnapCommandParameter();
+ param5.setLongOption("long-opt");
+ param5.setName("long-opt");
+ param5.setParameterType(ParameterType.STRING);
+
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(param1);
+ paramslist.add(param2);
+ paramslist.add(param3);
+ paramslist.add(param4);
+ paramslist.add(param5);
+
+ String[] args = new String[] { "sample-create", "--onap-username", "admin", "--onap-password", "123",
+ "--msb-url", "a@b.com", "--string-param", "blah", "--long-opt", "10" };
+ OnapCliUtils.populateParams(paramslist, Arrays.asList(args));
+ List<String> expectedList = Arrays.asList(args);
+
+ Assert.assertEquals("onap-username", expectedList.get(2), param1.getValue());
+ Assert.assertEquals("onap-password", expectedList.get(4), param2.getValue());
+ Assert.assertEquals("msb-url", expectedList.get(6), param3.getValue());
+ Assert.assertEquals("string-param", expectedList.get(8), param4.getValue());
+ Assert.assertEquals("long-opt", expectedList.get(10), param5.getValue());
+
+ }
+
+ @Test
+ public void testpositionalargs() throws OnapCommandException {
+ OnapCommandParameter paramargs = new OnapCommandParameter();
+ paramargs.setName("http://localhost:8082/file.txt");
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(paramargs);
+ String[] args = new String[] { "positional-args", "http://localhost:8082/file.txt" };
+ paramargs.setParameterType(ParameterType.STRING);
+ OnapCliUtils.populateParams(paramslist, Arrays.asList(args));
+ List<String> expectedList = Arrays.asList(args);
+ Assert.assertEquals("positional-args", expectedList.get(1), paramslist.get(0).getValue());
+ }
+
+ @Test
+ public void testboolparamslong() throws OnapCommandException {
+ OnapCommandParameter boolparam = new OnapCommandParameter();
+ boolparam.setLongOption("bool");
+ boolparam.setName("bool-param");
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(boolparam);
+ String[] args = new String[] { "sample-create", "--bool" };
+
+ boolparam.setParameterType(ParameterType.BOOL);
+ OnapCliUtils.populateParams(paramslist, Arrays.asList(args));
+ List<String> expectedList = Arrays.asList(args);
+ Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue());
+
+ }
+
+ @Test
+ public void testboolparamsshort() throws OnapCommandException {
+ OnapCommandParameter boolparam = new OnapCommandParameter();
+ boolparam.setShortOption("b");
+ boolparam.setName("bool-param");
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(boolparam);
+ String[] args = new String[] { "sample-create", "-b", };
+
+ boolparam.setParameterType(ParameterType.BOOL);
+ OnapCliUtils.populateParams(paramslist, Arrays.asList(args));
+ List<String> expectedList = Arrays.asList(args);
+ Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue());
+ }
+
+ @Test
+ public void testjsonparamsshort() throws OnapCommandException {
+ OnapCommandParameter jsonparam = new OnapCommandParameter();
+ jsonparam.setShortOption("j");
+ jsonparam.setName("json-param");
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(jsonparam);
+ 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));
+ List<String> expectedList = Arrays.asList(args);
+ Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue());
+ }
+
+ @Test
+ public void testjsonparamslong() throws OnapCommandException {
+ OnapCommandParameter jsonparam = new OnapCommandParameter();
+ jsonparam.setLongOption("json-param");
+ jsonparam.setName("json-param");
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(jsonparam);
+ 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));
+ List<String> expectedList = Arrays.asList(args);
+ Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue());
+ }
+
+ @Test
+ public void testpopulateParamsShort() throws OnapCommandException {
+
+ OnapCommandParameter param1 = new OnapCommandParameter();
+ param1.setShortOption("u");
+ param1.setName("onap-username");
+ param1.setParameterType(ParameterType.STRING);
+ OnapCommandParameter param2 = new OnapCommandParameter();
+ param2.setShortOption("p");
+ param2.setName("onap-password");
+ param2.setParameterType(ParameterType.STRING);
+ OnapCommandParameter param3 = new OnapCommandParameter();
+ param3.setShortOption("r");
+ param3.setName("msb-url");
+ param3.setParameterType(ParameterType.STRING);
+ OnapCommandParameter param4 = new OnapCommandParameter();
+ param4.setShortOption("c");
+ param4.setName("string-param");
+ param4.setParameterType(ParameterType.STRING);
+ OnapCommandParameter param5 = new OnapCommandParameter();
+ param5.setShortOption("l");
+ param5.setName("long-opt");
+ param5.setParameterType(ParameterType.STRING);
+
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(param1);
+ paramslist.add(param2);
+ paramslist.add(param3);
+ paramslist.add(param4);
+ paramslist.add(param5);
+
+ String[] args11 = new String[] { "sample-create", "-u", "admin", "-p", "123", "-r", "a@b.com", "-c", "blah",
+ "-l", "10", };
+ OnapCliUtils.populateParams(paramslist, Arrays.asList(args11));
+
+ List<String> expectedList = Arrays.asList(args11);
+
+ Assert.assertEquals("u", expectedList.get(2), param1.getValue());
+ Assert.assertEquals("-p", expectedList.get(4), param2.getValue());
+ Assert.assertEquals("r", expectedList.get(6), param3.getValue());
+ Assert.assertEquals("c", expectedList.get(8), param4.getValue());
+ Assert.assertEquals("l", expectedList.get(10), param5.getValue());
+ }
+
+ @Test
+ public void testArrayparamslong() throws OnapCommandException {
+ OnapCommandParameter arrayval = new OnapCommandParameter();
+ arrayval.setLongOption("node-ip");
+ arrayval.setName("node-ip");
+
+ String[] args = new String[] { "sample-create", "--node-ip", "{}" };
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(arrayval);
+
+ arrayval.setParameterType(ParameterType.ARRAY);
+ OnapCliUtils.populateParams(paramslist, Arrays.asList(args));
+
+ List<String> expectedList = Arrays.asList(args);
+ Assert.assertNotNull(expectedList.get(1), paramslist.get(0).getValue());
+ }
+
+ @Test
+ public void testMapparamsShort() throws OnapCommandException {
+ OnapCommandParameter param1 = new OnapCommandParameter();
+ param1.setLongOption("map");
+ param1.setName("MAP");
+ param1.setParameterType(ParameterType.MAP);
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(param1);
+
+ param1.setParameterType(ParameterType.MAP);
+ OnapCliUtils.populateParams(paramslist,
+ Arrays.asList("show", "--map", "param1=value1", "--map", "param2=value2"));
+
+ Assert.assertEquals("{\"param1\":\"value1\",\"param2\":\"value2\"}", paramslist.get(0).getValue().toString());
+ }
+
+ @Test(expected = OnapCliInvalidArgument.class)
+ public void testMapparamsLongfail() throws OnapCommandException {
+ OnapCommandParameter param1 = new OnapCommandParameter();
+ param1.setLongOption("map");
+ param1.setName("MAP");
+ param1.setParameterType(ParameterType.MAP);
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(param1);
+
+ param1.setParameterType(ParameterType.MAP);
+ OnapCliUtils.populateParams(paramslist, Arrays.asList("show", "--map", "param1=value1", "--map", "param2"));
+ Assert.assertEquals("{\"param1\":\"value1\",\"param2\"}", paramslist.get(0).getValue().toString());
+ }
+
+ @Test(expected = OnapCliInvalidArgument.class)
+ public void testMapparamsShortfail() throws OnapCommandException {
+ OnapCommandParameter param1 = new OnapCommandParameter();
+ param1.setShortOption("o");
+ param1.setName("node-port");
+ param1.setParameterType(ParameterType.MAP);
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(param1);
+ param1.setParameterType(ParameterType.MAP);
+ OnapCliUtils.populateParams(paramslist, Arrays.asList("show", "-o", "param1=value1", "-o", "param2"));
+ Assert.assertEquals("{\"param1\":\"value1\",\"param2\"}", paramslist.get(0).getValue().toString());
+ }
+
+ @Test(expected = OnapCliInvalidArgument.class)
+ public void testpositionalargsfails() throws OnapCommandException {
+ OnapCommandParameter paramargs = new OnapCommandParameter();
+ paramargs.setName("http://localhost:8082/file.txt");
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(paramargs);
+ 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));
+ List<String> expectedList = Arrays.asList(args);
+ Assert.assertEquals("positional-args", expectedList.get(1), paramslist.get(0).getValue());
+ }
+
+ @Test(expected = OnapCliInvalidArgument.class)
+ public void testboolparamsshortfails() throws OnapCommandException {
+ OnapCommandParameter boolparam = new OnapCommandParameter();
+ boolparam.setShortOption("b");
+ boolparam.setName("bool-param");
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(boolparam);
+ String[] args = new String[] { "sample-create", "-b", "-b", "-h" };
+
+ boolparam.setParameterType(ParameterType.BOOL);
+ OnapCliUtils.populateParams(paramslist, Arrays.asList(args));
+ List<String> expectedList = Arrays.asList(args);
+ Assert.assertEquals("true", paramslist.get(0).getValue());
+ }
+
+ @Test(expected = OnapCliInvalidArgument.class)
+ public void testboolparamsLongfails() throws OnapCommandException {
+ OnapCommandParameter boolparam = new OnapCommandParameter();
+ boolparam.setShortOption("bool");
+ boolparam.setName("bool-param");
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(boolparam);
+ String[] args = new String[] { "sample-create", "--bool", "--bool", "--help" };
+
+ boolparam.setParameterType(ParameterType.BOOL);
+ OnapCliUtils.populateParams(paramslist, Arrays.asList(args));
+ List<String> expectedList = Arrays.asList(args);
+ Assert.assertEquals("true", paramslist.get(0).getValue());
+ }
+
+ @Test(expected = OnapCliArgumentValueMissing.class)
+ public void testjsonparamslongfails() throws OnapCommandException {
+ OnapCommandParameter jsonparam = new OnapCommandParameter();
+ jsonparam.setLongOption("json-param");
+ jsonparam.setName("json-param");
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(jsonparam);
+ 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));
+ List<String> expectedList = Arrays.asList(args);
+ Assert.assertEquals("--json-param", paramslist.get(0).getValue());
+
+ }
+
+ @Test(expected = OnapCliArgumentValueMissing.class)
+ public void testjsonparamsshortfails() throws OnapCommandException {
+ OnapCommandParameter jsonparam = new OnapCommandParameter();
+ jsonparam.setShortOption("j");
+ jsonparam.setName("json-param");
+ List<OnapCommandParameter> paramslist = new ArrayList<>();
+ paramslist.add(jsonparam);
+ 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));
+ List<String> expectedList = Arrays.asList(args);
+ Assert.assertEquals("--json-param", paramslist.get(0).getValue());
+
+ }
+} \ No newline at end of file
diff --git a/main/src/test/resources/META-INF/services/org.onap.cli.fw.OnapCommand b/main/src/test/resources/META-INF/services/org.onap.cli.fw.OnapCommand
new file mode 100644
index 00000000..16dd879a
--- /dev/null
+++ b/main/src/test/resources/META-INF/services/org.onap.cli.fw.OnapCommand
@@ -0,0 +1 @@
+org.onap.cli.main.OnapCommandSampleTest \ No newline at end of file
diff --git a/main/src/test/resources/onap-cli-schema/sample-create-schema.yaml b/main/src/test/resources/onap-cli-schema/sample-create-schema.yaml
new file mode 100644
index 00000000..6e775334
--- /dev/null
+++ b/main/src/test/resources/onap-cli-schema/sample-create-schema.yaml
@@ -0,0 +1,71 @@
+onap_cmd_schema_version: 1.0
+name: sample-create
+description: Sample create into Onap
+service:
+ name: sample
+ version: v1
+ no-auth: true
+parameters:
+ - name: service-name
+ description: Onap service name
+ type: string
+ short_option: x
+ long_option: service-name
+ is_optional: false
+ - name: node-ip
+ description: Onap service running node IP
+ type: array
+ short_option: i
+ long_option: node-ip
+ - name: node-port
+ description: Onap service running node port
+ type: map
+ short_option: o
+ long_option: node-port
+results:
+ direction: portrait
+ attributes:
+ - name: name
+ description: Onap service name
+ scope: short
+ type: string
+ - name: version
+ description: Onap service version
+ scope: short
+ type: string
+ - name: url
+ description: Onap service base url
+ scope: short
+ type: url
+ - name: status
+ description: Onap service status
+ scope: short
+ type: long
+ - name: nodes
+ description: Onap service running nodes
+ scope: long
+ type: string
+ - name: location
+ description: Onap service location
+ scope: long
+ type: url
+http:
+ request:
+ uri: /services
+ method: POST
+ body: '{"serviceName":"${service-name}","nodeIp":"${node-ip}","nodePort":"${node-port}"}'
+ headers:
+ queries:
+ success_codes:
+ - 201
+ - 200
+ result_map:
+ name: $b{$.serviceName}
+ version: $b{$.version}
+ url: $b{$.url}
+ status: $b{$.status}
+ nodes: $b{$.nodes[*].ip}:$b{$.nodes[*].port}
+ location: $h{Location}
+
+ sample_response:
+ body: {"serviceName":"test","version":"v1","url":"/api/test/v1","protocol":"REST","visualRange":"1","lb_policy":"hash","nodes":[{"ip":"127.0.0.1","port":"8012","ttl":0,"nodeId":"test_127.0.0.1_8012","expiration":"2017-02-10T05:33:25Z","created_at":"2017-02-10T05:33:25Z","updated_at":"2017-02-10T05:33:25Z"}],"status":"1"} \ No newline at end of file
diff --git a/main/src/test/resources/sample-test-schema.yaml b/main/src/test/resources/sample-test-schema.yaml
new file mode 100644
index 00000000..419f416c
--- /dev/null
+++ b/main/src/test/resources/sample-test-schema.yaml
@@ -0,0 +1,79 @@
+onap_cmd_schema_version: 1.0
+name: sample-test
+description: Onap sample command to test the command features
+service:
+ name: sample
+ version: v1
+parameters:
+ - name: bool-param
+ type: bool
+ description: Onap boolean param, by default its always false.
+ short_option: b
+ long_option: bool
+ is_optional: true
+ default_value: false
+ - name: secure-param
+ type: string
+ description: Onap secure param such as password
+ short_option: x
+ long_option: secure
+ is_secured: true
+ is_optional: false
+ default_Value: pass123#
+ - name: string-param
+ type: string
+ description: Onap string param
+ long_option: string-param
+ short_option: c
+ is_optional: false
+ default_Value: test
+ - name: yaml-param
+ type: json
+ description: Onap yaml file location param
+ long_option: yaml-param
+ short_option: y
+ is_optional: false
+ - name: json-param
+ type: json
+ description: Onap json file location param
+ long_option: json-param
+ short_option: j
+ is_optional: false
+ - name: long-param
+ type: long
+ description: Onap long param
+ short_option: l
+ long_option: long-opt
+ is_optional: false
+ default_value: 10
+ - name: url-param
+ type: url
+ description: Onap url param
+ short_option: r
+ long_option: url
+ is_optional: false
+ default_value: http://localhost:8082/file.txt
+ - name: env-param
+ type: string
+ description: Onap env param.
+ short_option: z
+ long_option: env
+ is_optional: false
+ default_value: ${ENV_VAR}
+ default_value: http://localhost:8082/file.txt
+ - name: positional-args
+ type: string
+ description: Onap positional args, if no short option and no long option given for it
+ is_optional: false
+ default_value: http://localhost:8082/file.txt
+results:
+ direction: portrait
+ attributes:
+ - name: output-1
+ description: Onap output attribute marked in short
+ scope: short
+ type: string
+ - name: output-2
+ description: Onap output attribute marked in long
+ scope: short
+ type: string \ No newline at end of file
diff --git a/main/src/test/resources/sampletest.json b/main/src/test/resources/sampletest.json
new file mode 100644
index 00000000..44c101ce
--- /dev/null
+++ b/main/src/test/resources/sampletest.json
@@ -0,0 +1,8 @@
+{
+ "name": "test",
+ "messages": [
+ "hello jackson 1",
+ "hello jackson 2",
+ "hello jackson 3"
+ ]
+} \ No newline at end of file