00c311fcbafb7bbb23ca02661223218d2893d997
[cli.git] / framework / src / test / java / org / onap / cli / fw / schema / ValidateSchemaTest.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.schema;
18
19 import static org.junit.Assert.assertTrue;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.junit.Test;
27 import org.onap.cli.fw.cmd.OnapCommand;
28 import org.onap.cli.fw.error.OnapCommandException;
29 import org.onap.cli.fw.error.OnapCommandInvalidSchema;
30 import static org.junit.Assert.assertEquals;
31
32 import static org.junit.Assert.fail;
33
34 public class ValidateSchemaTest {
35
36     @Test(expected = OnapCommandInvalidSchema.class)
37     public void invalidateTest1() throws OnapCommandException {
38
39         OnapCommand cmd = new OnapCommand() {
40             @Override
41             protected void run() throws OnapCommandException {}
42         };
43         OnapCommandSchemaLoader.loadSchema(cmd, "fdsfds.yaml", true, true);
44     }
45
46
47     @Test
48     public void validateTestMerge() throws OnapCommandException {
49
50         OnapCommand cmd = new OnapCommand() {
51             @Override
52             protected void run() throws OnapCommandException {}
53         };
54         assertTrue(cmd.initializeSchema("test-command-to-valdiate-merge.yaml", true).isEmpty());
55     }
56
57     @Test(expected = OnapCommandInvalidSchema.class)
58     public void invalidateTest2() throws OnapCommandException {
59         OnapCommand cmd = new OnapCommand() {
60             @Override
61             protected void run() throws OnapCommandException {}
62         };
63         OnapCommandSchemaLoader.loadSchema(cmd, "fdsfds", true, true);
64     }
65
66     @Test(expected = OnapCommandInvalidSchema.class)
67     public void invalidateTest4() throws OnapCommandException {
68         OnapCommand cmd = new OnapCommand() {
69             @Override
70             protected void run() throws OnapCommandException {}
71         };
72         OnapCommandSchemaLoader.loadSchema(cmd,
73                 ValidateSchemaTest.class.getClassLoader().getResource("open-cli.properties").getFile(),
74                 true, true);
75     }
76
77     @Test(expected = OnapCommandInvalidSchema.class)
78     public void invalidateTest5() throws OnapCommandException {
79         OnapCommand cmd = new OnapCommand() {
80             @Override
81             protected void run() throws OnapCommandException {}
82         };
83         OnapCommandSchemaLoader.loadSchema(cmd, "schema-invalid-file-null.yaml", true, true);
84         fail("OnapCommandInvalidSchema exception occurs");
85     }
86
87     @Test
88     public void invalidate1Test5() throws OnapCommandException {
89         OnapCommand cmd = new OnapCommand() {
90             @Override
91             protected void run() throws OnapCommandException {}
92         };
93         OnapCommandSchemaLoader.loadSchema(cmd, "schema-validate-pass.yaml", true, true);
94         List<String> list = OnapCommandSchemaLoader.loadSchema(cmd, "schema-validate-pass.yaml", true, true);
95         assertTrue(list.isEmpty());
96     }
97
98     @Test(expected = OnapCommandInvalidSchema.class)
99     public void invalidateTest3() throws OnapCommandException {
100         OnapCommand cmd = new OnapCommand() {
101             @Override
102             protected void run() throws OnapCommandException {}
103         };
104         OnapCommandSchemaLoader.loadSchema(cmd, "schema-invalid-file.yaml", true, true);
105     }
106
107     @Test
108     public void validateTest() throws OnapCommandException {
109
110         OnapCommand cmd2 = new OnapCommand() {
111             @Override
112             protected void run() throws OnapCommandException {}
113         };
114         List<String> errorList2 = OnapCommandSchemaLoader.loadSchema(cmd2, "schema-validate-basic.yaml", true, true);
115         assertTrue(errorList2.size() > 0);
116
117         OnapCommand cmd3 = new OnapCommand() {
118             @Override
119             protected void run() throws OnapCommandException {}
120         };
121         List<String> errorList3 = OnapCommandSchemaLoader.loadSchema(cmd2,
122             "schema-validate-invalidschematype.yaml", true, true);
123         assertTrue(errorList3.size() > 0);
124
125
126         OnapCommand cmd5 = new OnapCommand() {
127             @Override
128             protected void run() throws OnapCommandException {}
129         };
130         List<String> errorList5 = OnapCommandSchemaLoader.loadSchema(cmd5, "schema-validate-pass.yaml", true, true);
131         assertEquals(0, errorList5.size());
132
133     }
134
135     @Test
136     public void validateAfterRemovingIfElseTest() throws OnapCommandException {
137         OnapCommand cmd2 = new OnapCommand() {
138             @Override
139             protected void run() throws OnapCommandException {
140
141             }
142         };
143         List < String > errorList2 = OnapCommandSchemaLoader.loadSchema(cmd2,
144             "schema-validate-basic-default-attr.yaml", true, true);
145         assertTrue(errorList2.size() > 0);
146     }
147
148     @Test
149     public void parseSchemaTest() throws OnapCommandException {
150         OnapCommand cmd = new OnapCommand() {
151             @Override
152             protected void run() throws OnapCommandException {}
153         };
154         Map<String, Object> values=new HashMap<>();
155         List<Map<String, Object>> list=new ArrayList<>();
156         Map<String,Object> paraValues=new HashMap<>();
157         paraValues.put("is_secured","yes");
158         paraValues.put("is_default_param","yes");
159         list.add(paraValues);
160         values.put("parameters",list);
161         assertEquals(2, OnapCommandSchemaLoader.parseSchema(cmd,values,true).size());
162
163     }
164     @Test
165     public void parseSchema2Test() throws OnapCommandException {
166         OnapCommand cmd = new OnapCommand() {
167             @Override
168             protected void run() throws OnapCommandException {}
169         };
170         Map<String, Object> values=new HashMap<>();
171         List<Map<String, Object>> list=new ArrayList<>();
172         Map<String,Object> paraValues=new HashMap<>();
173         Map<String, Object> attributesValues=new HashMap<>();
174         paraValues.put("is_secured","yes");
175         paraValues.put("is_default_attr","yes");
176         list.add(paraValues);
177         attributesValues.put("attributes",list);
178         values.put("results",attributesValues);
179         assertEquals(2, OnapCommandSchemaLoader.parseSchema(cmd,values,true).size());
180
181     }
182 }