dd956b931b2ec513e9109f2224d2111f7b924094
[cli.git] / framework / src / test / java / org / onap / cli / fw / utils / OnapCommandUtilsTest.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.utils;
18
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
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;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.Set;
33
34 import mockit.Invocation;
35 import mockit.Mock;
36 import mockit.MockUp;
37
38 import org.junit.FixMethodOrder;
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.junit.runners.MethodSorters;
42
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;
57
58
59 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
60 public class OnapCommandUtilsTest {
61     @Test
62     public void externalSchemaTest() {
63         OnapCommandSchemaInfo schema = new OnapCommandSchemaInfo();
64         schema.setCmdName("cmdName");
65         schema.setSchemaName("schemaName");
66         schema.setVersion("version");
67
68         assertTrue("cmdName".equals(schema.getCmdName()) && "schemaName".equals(schema.getSchemaName())
69                 && "version".equals(schema.getVersion()));
70     }
71
72     @Test
73     public void schemaFileNotFoundTest() throws OnapCommandException {
74
75         Map<String, ?> map = OnapCommandSchemaLoader.validateSchemaVersion("sample-test-schema.yaml", "1.0");
76         assertTrue(map.size() > 0);
77     }
78
79     @Test
80     @Ignore
81     public void invalidSchemaFileTest() throws OnapCommandException {
82         Map<String, ?> map = null;
83         try {
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());
91         }
92     }
93
94     @Test
95     public void validateWrongSchemaVersionTest() throws OnapCommandException {
96         Map<String, ?> map = null;
97         try {
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());
105         }
106     }
107
108     @Test
109     public void validateSchemaVersionTest() throws OnapCommandException {
110         Map<String, ?> map = null;
111         try {
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());
119         }
120     }
121
122     @Test
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);
127     }
128
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);
133     }
134
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);
139     }
140
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);
145     }
146
147     @Test
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);
152
153         for (OnapCommandParameter com : cmd.getParameters()) {
154             switch (com.getParameterType()) {
155                 case STRING:
156                     com.setValue("value");
157                     break;
158
159                 case ARRAY:
160                     com.setValue(Collections.EMPTY_LIST);
161                     break;
162
163                 case MAP:
164                     com.setValue(new HashMap<String, String>());
165                     break;
166
167                 case BOOL:
168                     com.setValue(true);
169                     break;
170
171                 case TEXT:
172                     com.setValue("value");
173                     break;
174
175                 case URL:
176                     com.setValue("http:localhost/test");
177                     break;
178
179                 case JSON:
180                     com.setValue("json");
181                     break;
182
183                 default:
184                     break;
185             }
186         }
187
188         Map<String, OnapCommandParameter> map = OnapCommandUtils.getInputMap(cmd.getParameters());
189         assertTrue(map.size() == 18);
190     }
191
192     @Test
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"))
198                 .findFirst();
199
200         if (contextOpt.isPresent()) {
201             OnapCommandParameter context = contextOpt.get();
202             assertTrue(context.getDefaultValue() instanceof HashMap);
203         } else {
204             fail("context parameter is not available");
205         }
206     }
207
208     @Test
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"))
214                 .findFirst();
215
216         if (contextOpt.isPresent()) {
217             OnapCommandParameter context = contextOpt.get();
218             HashMap<String, String> map = new HashMap();
219             map.put("a", "b");
220             context.setValue(map);
221
222             map = (HashMap<String, String>) context.getValue();
223             assertTrue(map.keySet().contains("a"));
224             assertTrue(map.values().contains("b"));
225         } else {
226             fail("context parameter is not available");
227         }
228     }
229
230     @Test
231     public void helpCommandTest() throws IOException, OnapCommandException {
232         OnapCommand cmd = new OnapCommandSample();
233         OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema.yaml", true, false);
234
235         String actualResult = OnapCommandHelperUtils.help(cmd);
236
237         String expectedHelp = FileUtil.loadResource("sample-cmd-test-help.txt");
238
239         //mrkanag compare the result
240     }
241
242     @Test
243     public void findOnapCommandsTest() {
244         List<Class<OnapCommand>> cmds = OnapCommandDiscoveryUtils.discoverCommandPlugins();
245         assertTrue(cmds.size() == 4);
246     }
247
248     @Test
249     public void sortTest() {
250         Set<String> set = new HashSet<String>();
251         set.add("dbvc");
252         set.add("bbvcb");
253         set.add("aaa");
254         set.add("c");
255         set.add("z");
256         List<String> list = OnapCommandUtils.sort(set);
257         assertEquals("[aaa, bbvcb, c, dbvc, z]", list.toString());
258     }
259
260     @Test
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());
266
267     }
268
269     @Test
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());
275
276     }
277
278     @Test(expected = OnapCommandHelpFailed.class)
279     public void zendExceptionHelpTest1() throws OnapCommandException {
280
281         mockPrintMethodException();
282         OnapCommand cmd = new OnapCommandSample();
283         OnapCommandSchemaLoader.loadSchema(cmd, "sample-test-schema.yaml", true, false);
284
285         OnapCommandHelperUtils.help(cmd);
286
287     }
288
289
290     @Test
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();
295         assert info != null;
296     }
297
298     @OnapCommandSchema(schema = "sample-test-info.yaml")
299     class OnapCommandSampleInfo extends OnapCommand {
300         @Override
301         protected void run() throws OnapCommandException {
302         }
303     }
304
305     @OnapCommandSchema(schema = "sample-test-schema.yaml")
306     class OnapCommandSample extends OnapCommand {
307         @Override
308         protected void run() throws OnapCommandException {
309         }
310     }
311
312     private void mockPrintMethodException() {
313         new MockUp<OnapCommandResult>() {
314             boolean isMock = true;
315
316             @Mock
317             public String print(Invocation inv) throws OnapCommandException {
318                 if (isMock) {
319                     isMock = false;
320                     throw new OnapCommandException("", "");
321                 } else {
322                     return inv.proceed();
323                 }
324             }
325         };
326     }
327 }