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;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.cli.fw.cmd.OnapCommand;
30 import org.onap.cli.fw.error.OnapCommandException;
31 import org.onap.cli.fw.error.OnapCommandHelpFailed;
32 import org.onap.cli.fw.error.OnapCommandNotFound;
33 import org.onap.cli.fw.schema.OnapCommandSchema;
35 public class OnapCommandRegistrarTest {
37 OnapCommandRegistrar registerar;
40 public void setup() throws OnapCommandException {
41 registerar = OnapCommandRegistrar.getRegistrar();
45 private void createDir() {
46 URL url = OnapCommandRegistrarTest.class.getClassLoader().getResource("open-cli-schema");
48 String path = url.getPath();
49 path = path.replaceFirst("open-cli-schema", "data");
50 File file = new File(path);
54 File f1 = new File(path + "/cli-schema.json");
61 public void oclipCommandNotFoundTest() throws OnapCommandException {
63 registerar = OnapCommandRegistrar.getRegistrar();
64 registerar.get("Test1");
65 fail("This should have thrown an exception");
66 } catch (OnapCommandNotFound e) {
68 } catch (Exception e) {
69 fail("This should have thrown an OnapCommandNotFound exception");
74 public void helpTest() throws OnapCommandException {
75 String help = registerar.getHelp();
80 public void versionTest() throws OnapCommandHelpFailed {
81 String version = registerar.getVersion();
82 assertNotNull(version);
86 public void listTest() {
87 registerar.listCommands();
91 public void testProfile() throws OnapCommandException {
93 OnapCommandRegistrar.getRegistrar().setProfile("test");
94 OnapCommandRegistrar.getRegistrar().addParamCache("a", "b");
95 OnapCommandRegistrar.getRegistrar().getParamCache();
96 OnapCommandRegistrar.getRegistrar().removeParamCache("a");
98 OnapCommandRegistrar.getRegistrar().setInteractiveMode(false);
99 assertTrue(!OnapCommandRegistrar.getRegistrar().isInteractiveMode());
101 OnapCommandRegistrar.getRegistrar().setEnabledProductVersion("open-cli");
102 assertEquals("open-cli", OnapCommandRegistrar.getRegistrar().getEnabledProductVersion());
103 OnapCommandRegistrar.getRegistrar().getAvailableProductVersions();
104 assertTrue(OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion().contains("sample-test"));
106 assertTrue(OnapCommandRegistrar.getRegistrar().listCommandInfo().size() > 2);
107 } catch (Exception e) {
108 fail("failed to test the profile");
114 public void test() throws OnapCommandException {
115 OnapCommandRegistrar registrar = OnapCommandRegistrar.getRegistrar();
116 OnapCommand cmd = registrar.get("sample-test");
118 registrar.listCommands();
122 @OnapCommandSchema(schema = "sample-test-schema.yaml")
123 class OnapCommandTest extends OnapCommand {
125 public OnapCommandTest() {
129 public static final String CMD_NAME = "test";
131 protected void run() throws OnapCommandException {
137 @OnapCommandSchema(schema = "test-schema.yaml")
138 class OnapCommandTest1 extends OnapCommand {
140 public OnapCommandTest1() {
144 public static final String CMD_NAME = "test1";
146 protected void run() throws OnapCommandException {