ValidateSchemaTest sonar issue, Replace these 3 tests with a single Parameterized one
[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         OnapCommand cmd = new OnapCommand() {
39             @Override
40             protected void run() throws OnapCommandException {}
41         };
42         try {
43             OnapCommandSchemaLoader.loadSchema(cmd, "fdsfds", true, true);
44         } catch(Exception e) {
45             assertEquals(e.getClass(), OnapCommandInvalidSchema.class);
46         }
47         OnapCommandSchemaLoader.loadSchema(cmd, "schema-invalid-file.yaml", true, true);
48     }
49
50     @Test
51     public void validateTestMerge() throws OnapCommandException {
52
53         OnapCommand cmd = new OnapCommand() {
54             @Override
55             protected void run() throws OnapCommandException {}
56         };
57         assertTrue(cmd.initializeSchema("test-command-to-valdiate-merge.yaml", true).isEmpty());
58     }
59
60     @Test(expected = OnapCommandInvalidSchema.class)
61     public void invalidateTest2() throws OnapCommandException {
62         OnapCommand cmd = new OnapCommand() {
63             @Override
64             protected void run() throws OnapCommandException {}
65         };
66         OnapCommandSchemaLoader.loadSchema(cmd,
67                 ValidateSchemaTest.class.getClassLoader().getResource("open-cli.properties").getFile(),
68                 true, true);
69     }
70
71     @Test(expected = OnapCommandInvalidSchema.class)
72     public void invalidateTest3() throws OnapCommandException {
73         OnapCommand cmd = new OnapCommand() {
74             @Override
75             protected void run() throws OnapCommandException {}
76         };
77         OnapCommandSchemaLoader.loadSchema(cmd, "schema-invalid-file-null.yaml", true, true);
78         fail("OnapCommandInvalidSchema exception occurs");
79     }
80
81     @Test
82     public void invalidate1Test5() throws OnapCommandException {
83         OnapCommand cmd = new OnapCommand() {
84             @Override
85             protected void run() throws OnapCommandException {}
86         };
87         OnapCommandSchemaLoader.loadSchema(cmd, "schema-validate-pass.yaml", true, true);
88         List<String> list = OnapCommandSchemaLoader.loadSchema(cmd, "schema-validate-pass.yaml", true, true);
89         assertTrue(list.isEmpty());
90     }
91
92     @Test
93     public void validateTest() throws OnapCommandException {
94
95         OnapCommand cmd2 = new OnapCommand() {
96             @Override
97             protected void run() throws OnapCommandException {}
98         };
99         List<String> errorList2 = OnapCommandSchemaLoader.loadSchema(cmd2, "schema-validate-basic.yaml", true, true);
100         assertTrue(errorList2.size() > 0);
101
102         OnapCommand cmd3 = new OnapCommand() {
103             @Override
104             protected void run() throws OnapCommandException {}
105         };
106         List<String> errorList3 = OnapCommandSchemaLoader.loadSchema(cmd2,
107             "schema-validate-invalidschematype.yaml", true, true);
108         assertTrue(errorList3.size() > 0);
109
110
111         OnapCommand cmd5 = new OnapCommand() {
112             @Override
113             protected void run() throws OnapCommandException {}
114         };
115         List<String> errorList5 = OnapCommandSchemaLoader.loadSchema(cmd5, "schema-validate-pass.yaml", true, true);
116         assertEquals(0, errorList5.size());
117
118     }
119
120     @Test
121     public void validateAfterRemovingIfElseTest() throws OnapCommandException {
122         OnapCommand cmd2 = new OnapCommand() {
123             @Override
124             protected void run() throws OnapCommandException {
125
126             }
127         };
128         List < String > errorList2 = OnapCommandSchemaLoader.loadSchema(cmd2,
129             "schema-validate-basic-default-attr.yaml", true, true);
130         assertTrue(errorList2.size() > 0);
131     }
132
133     @Test
134     public void parseSchemaTest() throws OnapCommandException {
135         OnapCommand cmd = new OnapCommand() {
136             @Override
137             protected void run() throws OnapCommandException {}
138         };
139         Map<String, Object> values=new HashMap<>();
140         List<Map<String, Object>> list=new ArrayList<>();
141         Map<String,Object> paraValues=new HashMap<>();
142         paraValues.put("is_secured","yes");
143         paraValues.put("is_default_param","yes");
144         list.add(paraValues);
145         values.put("parameters",list);
146         assertEquals(2, OnapCommandSchemaLoader.parseSchema(cmd,values,true).size());
147
148     }
149     @Test
150     public void parseSchema2Test() throws OnapCommandException {
151         OnapCommand cmd = new OnapCommand() {
152             @Override
153             protected void run() throws OnapCommandException {}
154         };
155         Map<String, Object> values=new HashMap<>();
156         List<Map<String, Object>> list=new ArrayList<>();
157         Map<String,Object> paraValues=new HashMap<>();
158         Map<String, Object> attributesValues=new HashMap<>();
159         paraValues.put("is_secured","yes");
160         paraValues.put("is_default_attr","yes");
161         list.add(paraValues);
162         attributesValues.put("attributes",list);
163         values.put("results",attributesValues);
164         assertEquals(2, OnapCommandSchemaLoader.parseSchema(cmd,values,true).size());
165
166     }
167 }