2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.fw.registrar;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
26 import java.util.ArrayList;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.cli.fw.cmd.OnapCommand;
31 import org.onap.cli.fw.error.OnapCommandException;
32 import org.onap.cli.fw.error.OnapCommandHelpFailed;
33 import org.onap.cli.fw.error.OnapCommandNotFound;
34 import org.onap.cli.fw.schema.OnapCommandSchema;
36 public class OnapCommandRegistrarTest {
38 OnapCommandRegistrar registerar;
41 public void setup() throws OnapCommandException {
42 registerar = OnapCommandRegistrar.getRegistrar();
46 private void createDir() {
47 URL url = OnapCommandRegistrarTest.class.getClassLoader().getResource("open-cli-schema");
49 String path = url.getPath();
50 path = path.replaceFirst("open-cli-schema", "data");
51 File file = new File(path);
55 File f1 = new File(path + "/cli-schema.json");
62 public void oclipCommandNotFoundTest() throws OnapCommandException {
64 registerar = OnapCommandRegistrar.getRegistrar();
65 registerar.get("Test1");
66 fail("This should have thrown an exception");
67 } catch (OnapCommandNotFound e) {
69 } catch (Exception e) {
70 fail("This should have thrown an OnapCommandNotFound exception");
75 public void helpTest() throws OnapCommandException {
76 String help = registerar.getHelp();
81 public void versionTest() throws OnapCommandHelpFailed {
82 String version = registerar.getVersion();
83 assertNotNull(version);
87 public void listTest() {
88 registerar.listCommands();
92 public void testProfile() throws OnapCommandException {
94 OnapCommandRegistrar.getRegistrar().setProfile("test234", new ArrayList<String>(), new ArrayList<String>());
95 OnapCommandRegistrar.getRegistrar().addParamCache("a", "b");
96 OnapCommandRegistrar.getRegistrar().getParamCache();
97 OnapCommandRegistrar.getRegistrar().removeParamCache("a");
98 OnapCommandRegistrar.getRegistrar().setInteractiveMode(false);
99 assertTrue(!OnapCommandRegistrar.getRegistrar().isInteractiveMode());
100 OnapCommandRegistrar.getRegistrar().setEnabledProductVersion("open-cli");
101 assertEquals("open-cli", OnapCommandRegistrar.getRegistrar().getEnabledProductVersion());
102 OnapCommandRegistrar.getRegistrar().getAvailableProductVersions();
104 OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion().contains("sample-test"));
105 assertTrue(OnapCommandRegistrar.getRegistrar().listCommandInfo().size() > 2);
106 } catch (Exception e) {
107 fail("failed to test the profile");
113 public void test() throws OnapCommandException {
114 OnapCommandRegistrar registrar = OnapCommandRegistrar.getRegistrar();
115 OnapCommand cmd = registrar.get("sample-test");
117 registrar.listCommands();
121 @OnapCommandSchema(schema = "sample-test-schema.yaml")
122 final class OnapCommandTest extends OnapCommand {
124 public OnapCommandTest() {
128 public static final String CMD_NAME = "test";
130 protected void run() throws OnapCommandException {
136 @OnapCommandSchema(schema = "test-schema.yaml")
137 final class OnapCommandTest1 extends OnapCommand {
139 public OnapCommandTest1() {
143 public static final String CMD_NAME = "test1";
145 protected void run() throws OnapCommandException {