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.schema;
19 import static org.junit.Assert.assertTrue;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
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;
32 import static org.junit.Assert.fail;
34 public class ValidateSchemaTest {
36 @Test(expected = OnapCommandInvalidSchema.class)
37 public void invalidateTest1() throws OnapCommandException {
39 OnapCommand cmd = new OnapCommand() {
41 protected void run() throws OnapCommandException {}
43 OnapCommandSchemaLoader.loadSchema(cmd, "fdsfds.yaml", true, true);
48 public void validateTestMerge() throws OnapCommandException {
50 OnapCommand cmd = new OnapCommand() {
52 protected void run() throws OnapCommandException {}
54 assertTrue(cmd.initializeSchema("test-command-to-valdiate-merge.yaml", true).isEmpty());
57 @Test(expected = OnapCommandInvalidSchema.class)
58 public void invalidateTest2() throws OnapCommandException {
59 OnapCommand cmd = new OnapCommand() {
61 protected void run() throws OnapCommandException {}
63 OnapCommandSchemaLoader.loadSchema(cmd, "fdsfds", true, true);
66 @Test(expected = OnapCommandInvalidSchema.class)
67 public void invalidateTest4() throws OnapCommandException {
68 OnapCommand cmd = new OnapCommand() {
70 protected void run() throws OnapCommandException {}
72 OnapCommandSchemaLoader.loadSchema(cmd,
73 ValidateSchemaTest.class.getClassLoader().getResource("open-cli.properties").getFile(),
77 @Test(expected = OnapCommandInvalidSchema.class)
78 public void invalidateTest5() throws OnapCommandException {
79 OnapCommand cmd = new OnapCommand() {
81 protected void run() throws OnapCommandException {}
83 OnapCommandSchemaLoader.loadSchema(cmd, "schema-invalid-file-null.yaml", true, true);
84 fail("OnapCommandInvalidSchema exception occurs");
88 public void invalidate1Test5() throws OnapCommandException {
89 OnapCommand cmd = new OnapCommand() {
91 protected void run() throws OnapCommandException {}
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());
98 @Test(expected = OnapCommandInvalidSchema.class)
99 public void invalidateTest3() throws OnapCommandException {
100 OnapCommand cmd = new OnapCommand() {
102 protected void run() throws OnapCommandException {}
104 OnapCommandSchemaLoader.loadSchema(cmd, "schema-invalid-file.yaml", true, true);
108 public void validateTest() throws OnapCommandException {
110 OnapCommand cmd2 = new OnapCommand() {
112 protected void run() throws OnapCommandException {}
114 List<String> errorList2 = OnapCommandSchemaLoader.loadSchema(cmd2, "schema-validate-basic.yaml", true, true);
115 assertTrue(errorList2.size() > 0);
117 OnapCommand cmd3 = new OnapCommand() {
119 protected void run() throws OnapCommandException {}
121 List<String> errorList3 = OnapCommandSchemaLoader.loadSchema(cmd2,
122 "schema-validate-invalidschematype.yaml", true, true);
123 assertTrue(errorList3.size() > 0);
126 OnapCommand cmd5 = new OnapCommand() {
128 protected void run() throws OnapCommandException {}
130 List<String> errorList5 = OnapCommandSchemaLoader.loadSchema(cmd5, "schema-validate-pass.yaml", true, true);
131 assertEquals(0, errorList5.size());
136 public void validateAfterRemovingIfElseTest() throws OnapCommandException {
137 OnapCommand cmd2 = new OnapCommand() {
139 protected void run() throws OnapCommandException {
143 List < String > errorList2 = OnapCommandSchemaLoader.loadSchema(cmd2,
144 "schema-validate-basic-default-attr.yaml", true, true);
145 assertTrue(errorList2.size() > 0);
149 public void parseSchemaTest() throws OnapCommandException {
150 OnapCommand cmd = new OnapCommand() {
152 protected void run() throws OnapCommandException {}
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());
165 public void parseSchema2Test() throws OnapCommandException {
166 OnapCommand cmd = new OnapCommand() {
168 protected void run() throws OnapCommandException {}
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());