Fixed Sonar Bug and Code Smell Blockers
[cli.git] / main / src / test / java / org / onap / cli / main / OnapCliMainTest.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.main;
18
19 import static org.junit.Assert.fail;
20
21 import java.io.IOException;
22
23
24 import jline.console.ConsoleReader;
25 import mockit.Invocation;
26 import mockit.Mock;
27 import mockit.MockUp;
28 import org.junit.Test;
29 import org.onap.cli.fw.error.OnapCommandException;
30 import org.onap.cli.fw.error.OnapCommandHelpFailed;
31 import org.onap.cli.fw.registrar.OnapCommandRegistrar;
32
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Arrays;
38 import org.onap.cli.fw.error.OnapCommandInvalidSchema;
39 import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
42
43
44 public class OnapCliMainTest {
45
46     OnapCli cli = null;
47
48     private void handle(String[] args) {
49         cli = new OnapCli(args);
50         cli.handle();
51     }
52
53     @Test
54     public void testHelp() {
55         assertDoesNotThrow(() -> this.handle(new String[] { "--help" }));
56     }
57
58     @Test
59     public void testHelpShort() {
60         assertDoesNotThrow(() -> this.handle(new String[] { "-h" }));
61     }
62
63     @Test
64     public void testVersion() {
65         assertDoesNotThrow(() -> this.handle(new String[] { "--version" }));
66     }
67
68     @Test
69     public void testVersionShort() {
70         assertDoesNotThrow(() -> this.handle(new String[] { "-v" }));
71     }
72
73     @Test
74     public void testHelpSampleCommand() {
75         assertDoesNotThrow(() -> this.handle(new String[] { "sample-test", "--help" }));
76     }
77
78     @Test
79     public void testHelpSampleCommandShort() {
80         assertDoesNotThrow(() -> this.handle(new String[] { "sample-test", "-h" }));
81     }
82
83     @Test
84     public void testVersionSampleCommandShort() {
85         assertDoesNotThrow(() -> this.handle(new String[] { "sample-test", "-v" }));
86     }
87
88     @Test
89     public void testHandleSampleCommandSet() throws OnapCommandException {
90         OnapCommandRegistrar.getRegistrar().addParamCache("sample:string-param", "paramValue");
91         OnapCommandRegistrar.getRegistrar().addParamCache("host-username", "paramValue");
92         OnapCommandRegistrar.getRegistrar().addParamCache("host-password", "paramValue");
93         OnapCommandRegistrar.getRegistrar().addParamCache("host-url", "paramValue");
94         assertDoesNotThrow(() -> this.handle(new String[] { "sample-test", "--string-param", "test"}));
95     }
96
97     @Test
98     public void testHandleSampleCommandFailure() throws OnapCommandException {
99         assertDoesNotThrow(() -> this.handle(new String[] { "sample-test", "--string-param"}));
100     }
101
102     @Test
103     public void interactiveTest() {
104         cli = new OnapCli(new String[] {});
105
106         mockConsole("exit");
107         cli.handleInteractive();
108
109         mockConsole("clear");
110         try {
111             cli.handleInteractive();
112         } catch (Exception e) {
113         }
114
115         cli = new OnapCli(new String[] {});
116         mockConsole("sample-test -h");
117
118         try {
119             cli.handleInteractive();
120         } catch (Exception e) {
121         }
122
123         cli = new OnapCli(new String[] {});
124         mockConsole("use open-cli");
125         try {
126             cli.handleInteractive();
127         } catch (Exception e) {
128         }
129
130         cli = new OnapCli(new String[] {});
131         mockConsole("set a=b");
132         try {
133             cli.handleInteractive();
134         } catch (Exception e) {
135         }
136
137         cli = new OnapCli(new String[] {});
138         mockConsole("set");
139         try {
140             cli.handleInteractive();
141         } catch (Exception e) {
142         }
143
144         cli = new OnapCli(new String[] {});
145         mockConsole("set a=");
146         try {
147             cli.handleInteractive();
148         } catch (Exception e) {
149         }
150
151         cli = new OnapCli(new String[] {});
152         mockConsole("unset a");
153         try {
154             cli.handleInteractive();
155         } catch (Exception e) {
156         }
157
158         cli = new OnapCli(new String[] {});
159         mockConsole("profile test");
160         try {
161             cli.handleInteractive();
162         } catch (Exception e) {
163         }
164
165         cli = new OnapCli(new String[] {});
166         mockConsole("profile");
167         try {
168             cli.handleInteractive();
169         } catch (Exception e) {
170         }
171
172         cli = new OnapCli(new String[] {});
173         mockConsole("version");
174         try {
175             cli.handleInteractive();
176         } catch (Exception e) {
177         }
178
179         cli = new OnapCli(new String[] {});
180         mockConsole("help");
181         try {
182             cli.handleInteractive();
183         } catch (Exception e) {
184         }
185
186         cli = new OnapCli(new String[] {});
187         mockConsoleReader();
188         assertDoesNotThrow(() -> cli.handleInteractive());
189
190     }
191
192     private static void mockConsoleReader() {
193         new MockUp<OnapCli>() {
194             @Mock
195             public ConsoleReader createConsoleReader() throws IOException {
196                 throw new IOException("Exception mock");
197             }
198         };
199     }
200
201     private static void mockConsole(String input) {
202         new MockUp<ConsoleReader>() {
203             boolean isMock = true;
204
205             @Mock
206             public String readLine(Invocation inv) throws IOException {
207                 if (isMock) {
208                     isMock = false;
209                     return input;
210                 } else {
211                     return inv.proceed(input);
212                 }
213             }
214         };
215     }
216
217     @Test
218     public void testDirectiveHelp() {
219         try {
220             OnapCli.getDirectiveHelp();
221         } catch (OnapCommandHelpFailed e) {
222             fail("Directive help failed to run");
223         }
224     }
225     @Test
226     public void testLoadYamlForYamlReader() throws OnapCommandInvalidSchema {
227         Map<String,?> map = OnapCommandDiscoveryUtils.loadYaml("src/test/resources/open-cli-schema/sample-test-schema.yaml");
228         assertFalse(map.isEmpty());
229     }
230     @Test
231     public void testverifyCommand() throws OnapCommandException {
232         cli = new OnapCli(new String[] {"schema-validate","--verify" });
233       new MockUp<OnapCommandRegistrar>(){
234             @Mock
235             public List<Map<String, Object>> getTestSuite(String cmd, String product) throws OnapCommandException {
236                 List<Map<String, Object>> list=new ArrayList<>();
237                 Map<String,Object>map=new HashMap<>();
238                 map.put("output","output");
239                 map.put("input", Arrays.asList(new String[]{"--verify"}));
240                 map.put("sampleid","sample1");
241                 map.put("samplefileid","schema-validate-sample.yaml");
242                 map.put("moco","schema-validate-moco.json");
243                 assertDoesNotThrow(() -> list.add(map));
244                 return list;
245             }
246         };
247         cli.handleCommand();
248     }
249 }