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 {
38 OnapCommand cmd = new OnapCommand() {
40 protected void run() throws OnapCommandException {}
43 OnapCommandSchemaLoader.loadSchema(cmd, "fdsfds", true, true);
44 } catch(Exception e) {
45 assertEquals(e.getClass(), OnapCommandInvalidSchema.class);
47 OnapCommandSchemaLoader.loadSchema(cmd, "schema-invalid-file.yaml", true, true);
51 public void validateTestMerge() throws OnapCommandException {
53 OnapCommand cmd = new OnapCommand() {
55 protected void run() throws OnapCommandException {}
57 assertTrue(cmd.initializeSchema("test-command-to-valdiate-merge.yaml", true).isEmpty());
60 @Test(expected = OnapCommandInvalidSchema.class)
61 public void invalidateTest2() throws OnapCommandException {
62 OnapCommand cmd = new OnapCommand() {
64 protected void run() throws OnapCommandException {}
66 OnapCommandSchemaLoader.loadSchema(cmd,
67 ValidateSchemaTest.class.getClassLoader().getResource("open-cli.properties").getFile(),
71 @Test(expected = OnapCommandInvalidSchema.class)
72 public void invalidateTest3() throws OnapCommandException {
73 OnapCommand cmd = new OnapCommand() {
75 protected void run() throws OnapCommandException {}
77 OnapCommandSchemaLoader.loadSchema(cmd, "schema-invalid-file-null.yaml", true, true);
78 fail("OnapCommandInvalidSchema exception occurs");
82 public void invalidate1Test5() throws OnapCommandException {
83 OnapCommand cmd = new OnapCommand() {
85 protected void run() throws OnapCommandException {}
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());
93 public void validateTest() throws OnapCommandException {
95 OnapCommand cmd2 = new OnapCommand() {
97 protected void run() throws OnapCommandException {}
99 List<String> errorList2 = OnapCommandSchemaLoader.loadSchema(cmd2, "schema-validate-basic.yaml", true, true);
100 assertTrue(errorList2.size() > 0);
102 OnapCommand cmd3 = new OnapCommand() {
104 protected void run() throws OnapCommandException {}
106 List<String> errorList3 = OnapCommandSchemaLoader.loadSchema(cmd2,
107 "schema-validate-invalidschematype.yaml", true, true);
108 assertTrue(errorList3.size() > 0);
111 OnapCommand cmd5 = new OnapCommand() {
113 protected void run() throws OnapCommandException {}
115 List<String> errorList5 = OnapCommandSchemaLoader.loadSchema(cmd5, "schema-validate-pass.yaml", true, true);
116 assertEquals(0, errorList5.size());
121 public void validateAfterRemovingIfElseTest() throws OnapCommandException {
122 OnapCommand cmd2 = new OnapCommand() {
124 protected void run() throws OnapCommandException {
128 List < String > errorList2 = OnapCommandSchemaLoader.loadSchema(cmd2,
129 "schema-validate-basic-default-attr.yaml", true, true);
130 assertTrue(errorList2.size() > 0);
134 public void parseSchemaTest() throws OnapCommandException {
135 OnapCommand cmd = new OnapCommand() {
137 protected void run() throws OnapCommandException {}
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());
150 public void parseSchema2Test() throws OnapCommandException {
151 OnapCommand cmd = new OnapCommand() {
153 protected void run() throws OnapCommandException {}
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());