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.main;
19 import static org.junit.Assert.fail;
21 import java.io.IOException;
24 import jline.console.ConsoleReader;
25 import mockit.Invocation;
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;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
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;
44 public class OnapCliMainTest {
48 private void handle(String[] args) {
49 cli = new OnapCli(args);
54 public void testHelp() {
55 assertDoesNotThrow(() -> this.handle(new String[] { "--help" }));
59 public void testHelpShort() {
60 assertDoesNotThrow(() -> this.handle(new String[] { "-h" }));
64 public void testVersion() {
65 assertDoesNotThrow(() -> this.handle(new String[] { "--version" }));
69 public void testVersionShort() {
70 assertDoesNotThrow(() -> this.handle(new String[] { "-v" }));
74 public void testHelpSampleCommand() {
75 assertDoesNotThrow(() -> this.handle(new String[] { "sample-test", "--help" }));
79 public void testHelpSampleCommandShort() {
80 assertDoesNotThrow(() -> this.handle(new String[] { "sample-test", "-h" }));
84 public void testVersionSampleCommandShort() {
85 assertDoesNotThrow(() -> this.handle(new String[] { "sample-test", "-v" }));
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"}));
98 public void testHandleSampleCommandFailure() throws OnapCommandException {
99 assertDoesNotThrow(() -> this.handle(new String[] { "sample-test", "--string-param"}));
103 public void interactiveTest() {
104 cli = new OnapCli(new String[] {});
107 cli.handleInteractive();
109 mockConsole("clear");
111 cli.handleInteractive();
112 } catch (Exception e) {
115 cli = new OnapCli(new String[] {});
116 mockConsole("sample-test -h");
119 cli.handleInteractive();
120 } catch (Exception e) {
123 cli = new OnapCli(new String[] {});
124 mockConsole("use open-cli");
126 cli.handleInteractive();
127 } catch (Exception e) {
130 cli = new OnapCli(new String[] {});
131 mockConsole("set a=b");
133 cli.handleInteractive();
134 } catch (Exception e) {
137 cli = new OnapCli(new String[] {});
140 cli.handleInteractive();
141 } catch (Exception e) {
144 cli = new OnapCli(new String[] {});
145 mockConsole("set a=");
147 cli.handleInteractive();
148 } catch (Exception e) {
151 cli = new OnapCli(new String[] {});
152 mockConsole("unset a");
154 cli.handleInteractive();
155 } catch (Exception e) {
158 cli = new OnapCli(new String[] {});
159 mockConsole("profile test");
161 cli.handleInteractive();
162 } catch (Exception e) {
165 cli = new OnapCli(new String[] {});
166 mockConsole("profile");
168 cli.handleInteractive();
169 } catch (Exception e) {
172 cli = new OnapCli(new String[] {});
173 mockConsole("version");
175 cli.handleInteractive();
176 } catch (Exception e) {
179 cli = new OnapCli(new String[] {});
182 cli.handleInteractive();
183 } catch (Exception e) {
186 cli = new OnapCli(new String[] {});
188 assertDoesNotThrow(() -> cli.handleInteractive());
192 private static void mockConsoleReader() {
193 new MockUp<OnapCli>() {
195 public ConsoleReader createConsoleReader() throws IOException {
196 throw new IOException("Exception mock");
201 private static void mockConsole(String input) {
202 new MockUp<ConsoleReader>() {
203 boolean isMock = true;
206 public String readLine(Invocation inv) throws IOException {
211 return inv.proceed(input);
218 public void testDirectiveHelp() {
220 OnapCli.getDirectiveHelp();
221 } catch (OnapCommandHelpFailed e) {
222 fail("Directive help failed to run");
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());
231 public void testverifyCommand() throws OnapCommandException {
232 cli = new OnapCli(new String[] {"schema-validate","--verify" });
233 new MockUp<OnapCommandRegistrar>(){
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));