From b610d2dbfa445e2ed8fd7f9976ae7a776666d630 Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam k00365106 Date: Wed, 19 Jul 2017 15:14:29 +0530 Subject: Add seed code from Open-O 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 --- .../java/org/onap/cli/main/OnapCliMainTest.java | 347 +++++++++++++++++++++ .../org/onap/cli/main/OnapCommandSampleTest.java | 35 +++ .../onap/cli/main/conf/OnapCliConstantsTest.java | 33 ++ .../onap/cli/main/error/OnapCliArgumentTest.java | 39 +++ .../cli/main/interactive/StringCompleterTest.java | 48 +++ .../org/onap/cli/main/utils/OnapCliUtilsTest.java | 330 ++++++++++++++++++++ .../META-INF/services/org.onap.cli.fw.OnapCommand | 1 + .../onap-cli-schema/sample-create-schema.yaml | 71 +++++ main/src/test/resources/sample-test-schema.yaml | 79 +++++ main/src/test/resources/sampletest.json | 8 + 10 files changed, 991 insertions(+) create mode 100644 main/src/test/java/org/onap/cli/main/OnapCliMainTest.java create mode 100644 main/src/test/java/org/onap/cli/main/OnapCommandSampleTest.java create mode 100644 main/src/test/java/org/onap/cli/main/conf/OnapCliConstantsTest.java create mode 100644 main/src/test/java/org/onap/cli/main/error/OnapCliArgumentTest.java create mode 100644 main/src/test/java/org/onap/cli/main/interactive/StringCompleterTest.java create mode 100644 main/src/test/java/org/onap/cli/main/utils/OnapCliUtilsTest.java create mode 100644 main/src/test/resources/META-INF/services/org.onap.cli.fw.OnapCommand create mode 100644 main/src/test/resources/onap-cli-schema/sample-create-schema.yaml create mode 100644 main/src/test/resources/sample-test-schema.yaml create mode 100644 main/src/test/resources/sampletest.json (limited to 'main/src/test') 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 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 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 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() { + @Mock + public ConsoleReader createConsoleReader() throws IOException { + throw new IOException("Exception mock"); + } + }; + } + + private static void mockConsole(String input) { + new MockUp() { + 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()); + assertTrue(result == 0); + result = com.complete(null, 1, new ArrayList()); + assertTrue(result == 0); + + com.add("test", "testing1", "testing2"); + result = com.complete("test", 1, new ArrayList()); + assertTrue(result == 0); + + result = com.complete("sfds", 1, new ArrayList()); + assertTrue(result == -1); + + + result = com.complete("test", 1, new ArrayList()); + 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 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 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 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 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 paramslist = new ArrayList<>(); + paramslist.add(boolparam); + String[] args = new String[] { "sample-create", "--bool" }; + + boolparam.setParameterType(ParameterType.BOOL); + OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + List 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 paramslist = new ArrayList<>(); + paramslist.add(boolparam); + String[] args = new String[] { "sample-create", "-b", }; + + boolparam.setParameterType(ParameterType.BOOL); + OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + List 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 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 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 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 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 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 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 paramslist = new ArrayList<>(); + paramslist.add(arrayval); + + arrayval.setParameterType(ParameterType.ARRAY); + OnapCliUtils.populateParams(paramslist, Arrays.asList(args)); + + List 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 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 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 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 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 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 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 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 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 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 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 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 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 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 -- cgit 1.2.3-korg