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.utils;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
24 import java.io.IOException;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
31 import java.util.Optional;
34 import mockit.Invocation;
38 import org.junit.FixMethodOrder;
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.junit.runners.MethodSorters;
43 import org.onap.cli.fw.cmd.OnapCommand;
44 import org.onap.cli.fw.error.OnapCommandException;
45 import org.onap.cli.fw.error.OnapCommandHelpFailed;
46 import org.onap.cli.fw.error.OnapCommandInvalidSchema;
47 import org.onap.cli.fw.error.OnapCommandInvalidSchemaVersion;
48 import org.onap.cli.fw.error.OnapCommandParameterNameConflict;
49 import org.onap.cli.fw.error.OnapCommandParameterOptionConflict;
50 import org.onap.cli.fw.error.OnapCommandSchemaNotFound;
51 import org.onap.cli.fw.info.OnapCommandInfo;
52 import org.onap.cli.fw.input.OnapCommandParameter;
53 import org.onap.cli.fw.output.OnapCommandResult;
54 import org.onap.cli.fw.schema.OnapCommandSchema;
55 import org.onap.cli.fw.schema.OnapCommandSchemaInfo;
56 import org.onap.cli.fw.schema.OnapCommandSchemaLoader;
59 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
60 public class OnapCommandUtilsTest {
62 public void externalSchemaTest() {
63 OnapCommandSchemaInfo schema = new OnapCommandSchemaInfo();
64 schema.setCmdName("cmdName");
65 schema.setSchemaName("schemaName");
66 schema.setVersion("version");
68 assertTrue("cmdName".equals(schema.getCmdName()) && "schemaName".equals(schema.getSchemaName())
69 && "version".equals(schema.getVersion()));
73 public void schemaFileNotFoundTest() throws OnapCommandException {
75 Map<String, ?> map = OnapCommandSchemaLoader.validateSchemaVersion("sample-test-schema.yaml", "1.0");
76 assertTrue(map.size() > 0);
81 public void invalidSchemaFileTest() throws OnapCommandException {
82 Map<String, ?> map = null;
84 map = OnapCommandSchemaLoader.validateSchemaVersion("sample-test-schema1.yaml", "1.0");
85 } catch (OnapCommandInvalidSchemaVersion e) {
86 fail("Test should not have thrown this exception : " + e.getMessage());
87 } catch (OnapCommandInvalidSchema e) {
88 fail("Test should not have thrown this exception : " + e.getMessage());
89 } catch (OnapCommandSchemaNotFound e) {
90 assertEquals("0xb002::Command schema sample-test-schema1.yaml is not found", e.getMessage());
95 public void validateWrongSchemaVersionTest() throws OnapCommandException {
96 Map<String, ?> map = null;
98 map = OnapCommandSchemaLoader.validateSchemaVersion("sample-test-invalid-schema.yaml", "1.0");
99 } catch (OnapCommandInvalidSchemaVersion e) {
100 fail("Test should not have thrown this exception : " + e.getMessage());
101 } catch (OnapCommandInvalidSchema e) {
102 assertTrue(e.getMessage().contains("0xb001::Command schema sample-test-invalid-schema.yaml is invalid"));
103 } catch (OnapCommandSchemaNotFound e) {
104 fail("Test should not have thrown this exception : " + e.getMessage());
109 public void validateSchemaVersionTest() throws OnapCommandException {
110 Map<String, ?> map = null;
112 map = OnapCommandSchemaLoader.validateSchemaVersion("sample-test-schema.yaml", "1.1");
113 } catch (OnapCommandInvalidSchemaVersion e) {
114 assertEquals("0xb003::Command schema open_cli_schema_version 1.0 is invalid or missing", e.getMessage());
115 } catch (OnapCommandInvalidSchema e) {
116 fail("Test should not have thrown this exception : " + e.getMessage());
117 } catch (OnapCommandSchemaNotFound e) {
118 fail("Test should not have thrown this exception : " + e.getMessage());
123 public void loadOnapCommandSchemaWithOutDefaultTest() throws OnapCommandException {
124 OnapCommand cmd = new OnapCommandSample();
125 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema.yaml", false, false);
126 assertTrue("sample-test".equals(cmd.getName()) && cmd.getParameters().size() == 10);
129 @Test(expected = OnapCommandParameterNameConflict.class)
130 public void loadOnapCommandSchemaWithDuplicateNameTest() throws OnapCommandException {
131 OnapCommand cmd = new OnapCommandSample();
132 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-invalid-schema-duplicate-name.yaml", false, false);
135 @Test(expected = OnapCommandParameterOptionConflict.class)
136 public void loadOnapCommandSchemaWithDuplicateShortOptionTest() throws OnapCommandException {
137 OnapCommand cmd = new OnapCommandSample();
138 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-invalid-schema-duplicate-shortoption.yaml", false, false);
141 @Test(expected = OnapCommandParameterOptionConflict.class)
142 public void loadOnapCommandSchemaWithDuplicateLongOptionTest() throws OnapCommandException {
143 OnapCommand cmd = new OnapCommandSample();
144 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-invalid-schema-duplicate-longoption.yaml", false, false);
148 public void loadOnapCommandSchemaWithDefaultTest() throws OnapCommandException {
149 OnapCommand cmd = new OnapCommandSample();
150 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema.yaml", true, false);
151 assertTrue("sample-test".equals(cmd.getName()) && cmd.getParameters().size() > 9);
153 for (OnapCommandParameter com : cmd.getParameters()) {
154 switch (com.getParameterType()) {
156 com.setValue("value");
160 com.setValue(Collections.EMPTY_LIST);
164 com.setValue(new HashMap<String, String>());
172 com.setValue("value");
176 com.setValue("http:localhost/test");
180 com.setValue("json");
188 Map<String, OnapCommandParameter> map = OnapCommandUtils.getInputMap(cmd.getParameters());
189 assertTrue(map.size() == 18);
193 public void contextParameterTest() throws OnapCommandException {
194 OnapCommand cmd = new OnapCommandSample();
195 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema.yaml", true, false);
196 Optional<OnapCommandParameter> contextOpt = cmd.getParameters().stream()
197 .filter(e -> e.getName().equals("context"))
200 if (contextOpt.isPresent()) {
201 OnapCommandParameter context = contextOpt.get();
202 assertTrue(context.getDefaultValue() instanceof HashMap);
204 fail("context parameter is not available");
209 public void contextParameterSetAndGetTest() throws OnapCommandException {
210 OnapCommand cmd = new OnapCommandSample();
211 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema.yaml", true, false);
212 Optional<OnapCommandParameter> contextOpt = cmd.getParameters().stream()
213 .filter(e -> e.getName().equals("context"))
216 if (contextOpt.isPresent()) {
217 OnapCommandParameter context = contextOpt.get();
218 HashMap<String, String> map = new HashMap();
220 context.setValue(map);
222 map = (HashMap<String, String>) context.getValue();
223 assertTrue(map.keySet().contains("a"));
224 assertTrue(map.values().contains("b"));
226 fail("context parameter is not available");
231 public void helpCommandTest() throws IOException, OnapCommandException {
232 OnapCommand cmd = new OnapCommandSample();
233 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema.yaml", true, false);
235 String actualResult = OnapCommandHelperUtils.help(cmd);
237 String expectedHelp = FileUtil.loadResource("sample-cmd-test-help.txt");
239 //mrkanag compare the result
243 public void findOnapCommandsTest() {
244 List<Class<OnapCommand>> cmds = OnapCommandDiscoveryUtils.discoverCommandPlugins();
245 assertTrue(cmds.size() == 4);
249 public void sortTest() {
250 Set<String> set = new HashSet<String>();
256 List<String> list = OnapCommandUtils.sort(set);
257 assertEquals("[aaa, bbvcb, c, dbvc, z]", list.toString());
261 public void jsonFlattenTest() {
262 List<String> list = Arrays.asList(new String[] { "{\"menu1\": {\"id\": \"file1\",\"value\": \"File1\"}}" });
263 List<String> list1 = OnapCommandUtils.jsonFlatten(list);
264 String expected = "[{\"menu1\":{\"id\":\"file1\",\"value\":\"File1\"}}]";
265 assertEquals(expected, list1.toString());
270 public void jsonFlattenExceptionTest() {
271 List<String> list = Arrays.asList(new String[] { "{\"menu1\"::{\"id\":\"file1\",\"value\":\"File1\"}}" });
272 List<String> list1 = OnapCommandUtils.jsonFlatten(list);
273 String expected = "[{\"menu1\"::{\"id\":\"file1\",\"value\":\"File1\"}}]";
274 assertEquals(expected, list1.toString());
278 @Test(expected = OnapCommandHelpFailed.class)
279 public void zendExceptionHelpTest1() throws OnapCommandException {
281 mockPrintMethodException();
282 OnapCommand cmd = new OnapCommandSample();
283 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema.yaml", true, false);
285 OnapCommandHelperUtils.help(cmd);
291 public void test() throws OnapCommandException {
292 OnapCommandSampleInfo cmd = new OnapCommandSampleInfo();
293 OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-info.yaml", true, false);
294 OnapCommandInfo info = cmd.getInfo();
298 @OnapCommandSchema(schema = "sample-test-info.yaml")
299 class OnapCommandSampleInfo extends OnapCommand {
301 protected void run() throws OnapCommandException {
305 @OnapCommandSchema(schema = "sample-test-schema.yaml")
306 class OnapCommandSample extends OnapCommand {
308 protected void run() throws OnapCommandException {
312 private void mockPrintMethodException() {
313 new MockUp<OnapCommandResult>() {
314 boolean isMock = true;
317 public String print(Invocation inv) throws OnapCommandException {
320 throw new OnapCommandException("", "");
322 return inv.proceed();