Fix the style warings reported in registrar
[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 import java.util.ArrayList;
27
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;
35
36 public class OnapCommandRegistrarTest {
37
38     OnapCommandRegistrar registerar;
39
40     @Before
41     public void setup() throws OnapCommandException {
42         registerar = OnapCommandRegistrar.getRegistrar();
43         createDir();
44     }
45
46     private void createDir() {
47         URL url = OnapCommandRegistrarTest.class.getClassLoader().getResource("open-cli-schema");
48         if (url != null) {
49             String path = url.getPath();
50             path = path.replaceFirst("open-cli-schema", "data");
51             File file = new File(path);
52             if (!file.exists()) {
53                 file.mkdir();
54             } else {
55                 File f1 = new File(path + "/cli-schema.json");
56                 f1.delete();
57             }
58         }
59     }
60
61     @Test
62     public void oclipCommandNotFoundTest() throws OnapCommandException {
63         try {
64             registerar = OnapCommandRegistrar.getRegistrar();
65             registerar.get("Test1");
66             fail("This should have thrown an exception");
67         } catch (OnapCommandNotFound e) {
68             //pass  // NOSONAR
69         } catch (Exception e) {
70             fail("This should have thrown an OnapCommandNotFound exception");
71         }
72     }
73
74     @Test
75     public void helpTest() throws OnapCommandException {
76         String help = registerar.getHelp();
77         assertNotNull(help);
78     }
79
80     @Test
81     public void versionTest() throws OnapCommandHelpFailed {
82         String version = registerar.getVersion();
83         assertNotNull(version);
84     }
85
86     @Test
87     public void listTest() {
88         registerar.listCommands();
89     }
90
91     @Test
92     public void testProfile() throws OnapCommandException {
93         try {
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();
103             assertTrue(
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");
108         }
109     }
110
111
112     @Test
113     public void test() throws OnapCommandException {
114         OnapCommandRegistrar registrar = OnapCommandRegistrar.getRegistrar();
115         OnapCommand cmd = registrar.get("sample-test");
116         cmd.printVersion();
117         registrar.listCommands();
118     }
119 }
120
121 @OnapCommandSchema(schema = "sample-test-schema.yaml")
122 final class OnapCommandTest extends OnapCommand {
123
124     public OnapCommandTest() {
125
126     }
127
128     public static final String CMD_NAME = "test";
129
130     protected void run() throws OnapCommandException {
131
132     }
133
134 }
135
136 @OnapCommandSchema(schema = "test-schema.yaml")
137 final class OnapCommandTest1 extends OnapCommand {
138
139     public OnapCommandTest1() {
140
141     }
142
143     public static final String CMD_NAME = "test1";
144
145     protected void run() throws OnapCommandException {
146
147     }
148
149 }