Add profile list support
[cli.git] / framework / src / test / java / org / onap / cli / fw / registrar / OnapCommandRegistrarTest.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.cli.fw.registrar;
18
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;
23
24 import java.io.File;
25 import java.net.URL;
26
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;
34
35 public class OnapCommandRegistrarTest {
36
37     OnapCommandRegistrar registerar;
38
39     @Before
40     public void setup() throws OnapCommandException {
41         registerar = OnapCommandRegistrar.getRegistrar();
42         createDir();
43     }
44
45     private void createDir() {
46         URL url = OnapCommandRegistrarTest.class.getClassLoader().getResource("open-cli-schema");
47         if (url != null) {
48             String path = url.getPath();
49             path = path.replaceFirst("open-cli-schema", "data");
50             File file = new File(path);
51             if (!file.exists()) {
52                 file.mkdir();
53             } else {
54                 File f1 = new File(path + "/cli-schema.json");
55                 f1.delete();
56             }
57         }
58     }
59
60     @Test
61     public void oclipCommandNotFoundTest() throws OnapCommandException {
62         try {
63             registerar = OnapCommandRegistrar.getRegistrar();
64             registerar.get("Test1");
65             fail("This should have thrown an exception");
66         } catch (OnapCommandNotFound e) {
67             //pass  // NOSONAR
68         } catch (Exception e) {
69             fail("This should have thrown an OnapCommandNotFound exception");
70         }
71     }
72
73     @Test
74     public void helpTest() throws OnapCommandException {
75         String help = registerar.getHelp();
76         assertNotNull(help);
77     }
78
79     @Test
80     public void versionTest() throws OnapCommandHelpFailed {
81         String version = registerar.getVersion();
82         assertNotNull(version);
83     }
84
85     @Test
86     public void listTest() {
87         registerar.listCommands();
88     }
89
90     @Test
91     public void testProfile() throws OnapCommandException {
92         try {
93                 OnapCommandRegistrar.getRegistrar().setProfile("test12312");
94                 OnapCommandRegistrar.getRegistrar().addParamCache("a", "b");
95                 OnapCommandRegistrar.getRegistrar().getParamCache();
96                 OnapCommandRegistrar.getRegistrar().removeParamCache("a");
97
98                 OnapCommandRegistrar.getRegistrar().setInteractiveMode(false);
99                 assertTrue(!OnapCommandRegistrar.getRegistrar().isInteractiveMode());
100
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"));
105
106                 assertTrue(OnapCommandRegistrar.getRegistrar().listCommandInfo().size() > 2);
107         } catch (Exception e) {
108             fail("failed to test the profile");
109         }
110     }
111
112
113     @Test
114     public void test() throws OnapCommandException {
115         OnapCommandRegistrar registrar = OnapCommandRegistrar.getRegistrar();
116         OnapCommand cmd = registrar.get("sample-test");
117         cmd.printVersion();
118         registrar.listCommands();
119     }
120 }
121
122 @OnapCommandSchema(schema = "sample-test-schema.yaml")
123 class OnapCommandTest extends OnapCommand {
124
125     public OnapCommandTest() {
126
127     }
128
129     public static final String CMD_NAME = "test";
130
131     protected void run() throws OnapCommandException {
132
133     }
134
135 }
136
137 @OnapCommandSchema(schema = "test-schema.yaml")
138 class OnapCommandTest1 extends OnapCommand {
139
140     public OnapCommandTest1() {
141
142     }
143
144     public static final String CMD_NAME = "test1";
145
146     protected void run() throws OnapCommandException {
147
148     }
149
150 }