blob: 39298552db196e96e7253aaef6b0b999418902bd [file] [log] [blame]
sonu gupta1bb37b82016-11-11 16:51:18 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sonu gupta1bb37b82016-11-11 16:51:18 +05303 *
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
17package org.onosproject.yms.app.ydt;
18
19import org.junit.Rule;
20import org.junit.Test;
21import org.junit.rules.ExpectedException;
sonu gupta1bb37b82016-11-11 16:51:18 +053022import org.onosproject.yms.app.ydt.exceptions.YdtException;
23
24import java.util.ArrayList;
25import java.util.HashSet;
26import java.util.List;
27import java.util.Set;
28
29import static org.junit.Assert.assertEquals;
30import static org.onosproject.yms.app.ydt.YdtTestConstants.E_LEAF;
31import static org.onosproject.yms.app.ydt.YdtTestConstants.E_LIST;
32import static org.onosproject.yms.app.ydt.YdtTestConstants.E_TOPARENT;
33import static org.onosproject.yms.app.ydt.YdtTestConstants.INV;
34import static org.onosproject.yms.app.ydt.YdtTestConstants.LISTNS;
35import static org.onosproject.yms.app.ydt.YdtTestConstants.LWC;
36import static org.onosproject.yms.app.ydt.YdtTestUtils.getTestYdtBuilder;
37import static org.onosproject.yms.app.ydt.YdtTestUtils.getYdtBuilder;
38import static org.onosproject.yms.app.ydt.YdtTestUtils.listWithContainer1Ydt;
39import static org.onosproject.yms.app.ydt.YdtTestUtils.listWithContainer2Ydt;
40import static org.onosproject.yms.app.ydt.YdtTestUtils.listWithContainerYdt;
41import static org.onosproject.yms.app.ydt.YdtTestUtils.listWithoutContainerYdt;
42import static org.onosproject.yms.app.ydt.YdtTestUtils.validateLeafContents;
43import static org.onosproject.yms.app.ydt.YdtTestUtils.validateLeafListContents;
44import static org.onosproject.yms.app.ydt.YdtTestUtils.validateNodeContents;
45import static org.onosproject.yms.app.ydt.YdtTestUtils.walkINTree;
46import static org.onosproject.yms.ydt.YdtContextOperationType.MERGE;
sonu guptaeff184b2016-11-24 12:43:49 +053047import static org.onosproject.yms.ydt.YdtType.SINGLE_INSTANCE_NODE;
sonu gupta1bb37b82016-11-11 16:51:18 +053048
49public class ListTest {
50
51 @Rule
52 public ExpectedException thrown = ExpectedException.none();
53
54 private Set<String> valueSet = new HashSet();
55
56 private static final String[] ERROR = {
57 "rootlist is missing some of the keys of listwithcontainer.",
58 "Duplicate entry with name invalid.",
59 "Some of the key elements are not unique in listwithcontainer.",
60 "Too few key parameters in listwithcontainer." +
61 " Expected 2; actual 1.",
62 "Too many key parameters in listwithcontainer." +
63 " Expected 2; actual 3.",
64 "Application with name \"" + "invalid\" doesn't exist.",
65 "Too many instances of listwithcontainer. Expected maximum " +
66 "instances 3.",
67 "Duplicate entry found under invalidinterval leaf-list node.",
sonu guptaeff184b2016-11-24 12:43:49 +053068 "YANG file error : Input value \"string\" is not a valid uint16.",
69 "Schema node with name listwithcontainer doesn't exist.",
70 "Duplicate entry with name rootlist."
sonu gupta1bb37b82016-11-11 16:51:18 +053071 };
72
73 private static final String[] EXPECTED = {
74 "Entry Node is list.",
75 "Entry Node is rootlist.",
76 "Entry Node is listwithoutcontainer.",
77 "Entry Node is invalidinterval.",
78 "Exit Node is invalidinterval.",
79 "Exit Node is listwithoutcontainer.",
80 "Exit Node is rootlist.",
81 "Exit Node is list."
82 };
83
84 List<String> keysValueList = new ArrayList<>();
85
86 /**
87 * Creates and validates rootlist module with listwithoutcontainer node.
88 */
89 @Test
90 public void listwithoutcontainerTest() throws YdtException {
91 YangRequestWorkBench ydtBuilder = listWithoutContainerYdt();
92 validateTree(ydtBuilder);
93 // walker test
94 walkINTree(ydtBuilder, EXPECTED);
95 }
96
97 /**
98 * Creates and validates rootlist module with listwithcontainer node
99 * using addMultiInstanceChild interface for adding multi instance node.
100 */
101 @Test
102 public void listwithcontainerTest() throws YdtException {
103 YangRequestWorkBench ydtBuilder = listWithContainerYdt();
104 validateListwithcontainerTree(ydtBuilder);
105 }
106
107 /**
108 * Creates and validates rootlist module with listwithcontainer
109 * node using addChild interface for adding multi instance node.
110 */
111 @Test
112 public void listwithcontainer1Test() throws YdtException {
113 YangRequestWorkBench ydtBuilder = listWithContainer1Ydt();
114 validateListwithcontainerTree(ydtBuilder);
115 }
116
117 /**
118 * Creates and validates rootlist module with multiple instances of
119 * listwithcontainer node using addMultiInstanceChild interface for adding
120 * multi instance node.
121 */
122 @Test
123 public void listwithcontainer2Test() throws YdtException {
124 YangRequestWorkBench ydtBuilder = listWithContainer2Ydt();
125 }
126
127 /**
128 * Validates the given built ydt.
129 */
130 private void validateTree(YangRequestWorkBench ydtBuilder) {
131
132 // assign root node to ydtNode for validating purpose.
133 YdtNode ydtNode = (YdtNode) ydtBuilder.getRootNode();
134 // Logical root node does not have operation type
135 validateNodeContents(ydtNode, "list", null);
136
137 ydtNode = ydtNode.getFirstChild();
138 validateNodeContents(ydtNode, "rootlist", MERGE);
139 ydtNode = ydtNode.getFirstChild();
140 validateNodeContents(ydtNode, "listwithoutcontainer", MERGE);
141 ydtNode = ydtNode.getFirstChild();
142 validateLeafContents(ydtNode, INV, "12");
143 }
144
145 /**
146 * Validates the given list with container built ydt.
147 */
148 private void validateListwithcontainerTree(
149 YangRequestWorkBench ydtBuilder) {
150
151 valueSet.add("1");
152 valueSet.add("2");
153 // assign root node to ydtNode for validating purpose.
154 YdtNode ydtNode = (YdtNode) ydtBuilder.getRootNode();
155 // Logical root node does not have operation type
156 validateNodeContents(ydtNode, "list", null);
157
158 ydtNode = ydtNode.getFirstChild();
159 validateNodeContents(ydtNode, "rootlist", MERGE);
160 ydtNode = ydtNode.getFirstChild();
161 validateNodeContents(ydtNode, LWC, MERGE);
162 ydtNode = ydtNode.getFirstChild();
163 validateLeafContents(ydtNode, "invalid", "12");
164 ydtNode = ydtNode.getNextSibling();
165 validateLeafContents(ydtNode, "invalid1", "12");
166 ydtNode = ydtNode.getNextSibling();
167 validateLeafListContents(ydtNode, INV, valueSet);
168 ydtNode = ydtNode.getNextSibling();
169 validateNodeContents(ydtNode, "interface", MERGE);
170 ydtNode = ydtNode.getFirstChild();
171 validateLeafContents(ydtNode, INV, "12");
172 ydtNode = ydtNode.getNextSibling();
173 validateLeafContents(ydtNode, "invalid", "121");
174 }
175
176 /**
177 * Tests the negative error scenario when application name for ydt is
178 * invalid.
179 */
180 @Test
181 public void negative1Test() {
sonu guptaeff184b2016-11-24 12:43:49 +0530182 thrown.expect(IllegalArgumentException.class);
sonu gupta1bb37b82016-11-11 16:51:18 +0530183 thrown.expectMessage(ERROR[5]);
184 getYdtBuilder("list", "invalid", "ydt.invalid", MERGE);
185 }
186
187 /**
188 * Tests the negative error scenario when list node is not having all
189 * key elements.
190 */
191 @Test
192 public void negative2Test() throws YdtException {
193
194 YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
195 ydtBuilder.addChild(LWC, LISTNS);
196 ydtBuilder.addLeaf("invalid", LISTNS, "12");
197 ydtBuilder.traverseToParent();
198
199 traversToParentErrorMsgValidator(ydtBuilder, ERROR[0]);
200 }
201
202 /**
203 * Tests the negative error scenario when duplicate entry of list node
204 * is created.
205 */
206 @Test
207 public void negative3Test() throws YdtException {
208 YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
209 ydtBuilder.addChild(LWC, LISTNS);
210 ydtBuilder.addLeaf("invalid", LISTNS, "12");
211 ydtBuilder.traverseToParent();
212 leafErrorMsgValidator(ydtBuilder, "invalid", "12", ERROR[1]);
213 }
214
215 /**
216 * Tests the negative error scenario when key elements of list node
217 * are not unique.
218 */
219 @Test
220 public void negative4Test() throws YdtException {
221 YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
222 ydtBuilder.addChild(LWC, LISTNS);
223 ydtBuilder.addLeaf("invalid", LISTNS, "12");
224 ydtBuilder.traverseToParent();
225 ydtBuilder.addLeaf("invalid1", LISTNS, "12");
226 ydtBuilder.traverseToParent();
227 ydtBuilder.traverseToParent();
228
229 ydtBuilder.addChild(LWC, LISTNS);
230 ydtBuilder.addLeaf("invalid", LISTNS, "12");
231 ydtBuilder.traverseToParent();
232 ydtBuilder.addLeaf("invalid1", LISTNS, "12");
233 ydtBuilder.traverseToParent();
234 ydtBuilder.traverseToParent();
235
236 traversToParentErrorMsgValidator(ydtBuilder, ERROR[2]);
237 }
238
239 /**
240 * Tests the negative error scenario when all key elements of list node
241 * are not supplied.
242 */
243 @Test
244 public void negative5Test() throws YdtException {
245 keysValueList.clear();
246 keysValueList.add("1");
247 keysValueList.add("2");
248 keysValueList.add("2");
249 YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
250 listNodeErrorMsgValidator(ydtBuilder, keysValueList, ERROR[4]);
251
252 keysValueList.clear();
253 keysValueList.add("1");
254 ydtBuilder = getTestYdtBuilder(LISTNS);
255 listNodeErrorMsgValidator(ydtBuilder, keysValueList, ERROR[3]);
256 }
257
258 /**
259 * Tests the negative error scenario when instances of a list node are
260 * created above the allowed limit.
261 */
262 @Test
263 public void negative6Test() throws YdtException {
264 keysValueList.clear();
265 keysValueList.add("1");
266 keysValueList.add("1");
267 YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
268 ydtBuilder.addMultiInstanceChild(LWC, LISTNS, keysValueList, null);
269 ydtBuilder.traverseToParent();
270
271 ydtBuilder.addChild(LWC, LISTNS);
272 ydtBuilder.addLeaf("invalid", LISTNS, "12");
273 ydtBuilder.traverseToParent();
274 ydtBuilder.addLeaf("invalid1", LISTNS, "121");
275 ydtBuilder.traverseToParent();
276 ydtBuilder.traverseToParent();
277
278 ydtBuilder.addChild(LWC, LISTNS);
279 ydtBuilder.addLeaf("invalid", LISTNS, "12");
280 ydtBuilder.traverseToParent();
281 ydtBuilder.addLeaf("invalid1", LISTNS, "1211");
282 ydtBuilder.traverseToParent();
283 ydtBuilder.traverseToParent();
284
285 ydtBuilder.addChild(LWC, LISTNS);
286 ydtBuilder.addLeaf("invalid", LISTNS, "12");
287 ydtBuilder.traverseToParent();
288 ydtBuilder.addLeaf("invalid1", LISTNS, "21");
289 ydtBuilder.traverseToParent();
290 ydtBuilder.traverseToParent();
291 traversToParentErrorMsgValidator(ydtBuilder, ERROR[6]);
292 }
293
294 /**
295 * Tests the negative error scenario when list node is not having all
296 * key elements.
297 */
298 @Test
299 public void negative7Test() throws YdtException {
300 YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
301 ydtBuilder.addChild(LWC, LISTNS);
302 traversToParentErrorMsgValidator(ydtBuilder, ERROR[0]);
303 }
304
305 /**
306 * Tests the negative error scenario when duplicate key entry is created
307 * inside leaf-list node.
308 */
309 @Test
310 public void negative8Test() throws YdtException {
311 YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
312 ydtBuilder.addChild(LWC, LISTNS);
313 ydtBuilder.addLeaf("invalid", LISTNS, "12");
314 ydtBuilder.traverseToParent();
315 ydtBuilder.addLeaf("invalid1", LISTNS, "12");
316 ydtBuilder.traverseToParent();
317 ydtBuilder.addLeaf(INV, LISTNS, "12");
318 ydtBuilder.traverseToParent();
319 leafErrorMsgValidator(ydtBuilder, INV, "12", ERROR[7]);
320 }
321
sonu guptaeff184b2016-11-24 12:43:49 +0530322 //TODO negative scenario will be handled later
323// /**
324// * Tests the negative error scenario when string is passed for uint16 type
325// * leaf node.
326// */
327// @Test
328// public void negative9Test() throws YdtException {
329// YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
330// ydtBuilder.addChild(LWC, LISTNS);
331// ydtBuilder.addLeaf("invalid", LISTNS, "12");
332// ydtBuilder.traverseToParent();
333// ydtBuilder.addLeaf("invalid1", LISTNS, "12");
334// ydtBuilder.traverseToParent();
335// leafErrorMsgValidator(ydtBuilder, INV, "string", ERROR[8]);
336// }
337//
338// /**
339// * Tests the negative error scenario when duplicate key entry created
340// * inside a leaf-list node.
341// */
342// @Test
343// public void negative10Test() throws YdtException {
344// valueSet.clear();
345// valueSet.add("1");
346// valueSet.add("2");
347// valueSet.add("12");
348// YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
349// ydtBuilder.addChild(LWC, LISTNS);
350// ydtBuilder.addLeaf("invalid", LISTNS, "12");
351// ydtBuilder.traverseToParent();
352// ydtBuilder.addLeaf("invalid1", LISTNS, "12");
353// ydtBuilder.traverseToParent();
354// ydtBuilder.addLeaf(INV, LISTNS, "12");
355// ydtBuilder.traverseToParent();
356// thrown.expect(IllegalArgumentException.class);
357// thrown.expectMessage(ERROR[7]);
358// ydtBuilder.addLeaf(INV, LISTNS, valueSet);
359// }
360
361// /**
362// * Tests the negative error scenario when string is passed for uint16 type
363// * key entry inside a leaf-list node.
364// */
365// @Test
366// public void negative11Test() throws YdtException {
367// valueSet.clear();
368// valueSet.add("string");
369// YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
370// ydtBuilder.addChild(LWC, LISTNS);
371// ydtBuilder.addLeaf("invalid", LISTNS, "12");
372// ydtBuilder.traverseToParent();
373// ydtBuilder.addLeaf("invalid1", LISTNS, "12");
374// ydtBuilder.traverseToParent();
375// ydtBuilder.addLeaf(INV, LISTNS, "12");
376// ydtBuilder.traverseToParent();
377// thrown.expect(DataTypeException.class);
378// thrown.expectMessage(ERROR[8]);
379// ydtBuilder.addLeaf(INV, LISTNS, valueSet);
380// }
381
sonu gupta1bb37b82016-11-11 16:51:18 +0530382 /**
sonu guptaeff184b2016-11-24 12:43:49 +0530383 * Tests the negative error scenario when list node addition requested
384 * with single instance request type.
sonu gupta1bb37b82016-11-11 16:51:18 +0530385 */
386 @Test
sonu guptaeff184b2016-11-24 12:43:49 +0530387 public void negative12Test() {
sonu gupta1bb37b82016-11-11 16:51:18 +0530388 YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
sonu guptaeff184b2016-11-24 12:43:49 +0530389 thrown.expect(IllegalArgumentException.class);
390 thrown.expectMessage(ERROR[9]);
391 ydtBuilder.addChild(LWC, LISTNS, SINGLE_INSTANCE_NODE, MERGE);
sonu gupta1bb37b82016-11-11 16:51:18 +0530392 }
393
394 /**
sonu guptaeff184b2016-11-24 12:43:49 +0530395 * Tests the negative error scenario when application with requested
396 * name is already part of tree.
sonu gupta1bb37b82016-11-11 16:51:18 +0530397 */
398 @Test
sonu guptaeff184b2016-11-24 12:43:49 +0530399 public void negative13Test() {
sonu gupta1bb37b82016-11-11 16:51:18 +0530400 YangRequestWorkBench ydtBuilder = getTestYdtBuilder(LISTNS);
sonu gupta1bb37b82016-11-11 16:51:18 +0530401 ydtBuilder.traverseToParent();
sonu guptaeff184b2016-11-24 12:43:49 +0530402 thrown.expect(IllegalArgumentException.class);
403 thrown.expectMessage(ERROR[10]);
404 ydtBuilder.addChild("rootlist", LISTNS, MERGE);
sonu gupta1bb37b82016-11-11 16:51:18 +0530405 }
406
407 /**
408 * Validate the error message obtained by adding multi instance node in
409 * current context against the given error string.
410 *
411 * @param bldr ydt builder
412 * @param list list of key values
413 * @param error error string
414 */
415 private void listNodeErrorMsgValidator(YangRequestWorkBench bldr,
416 List<String> list, String error) {
417 /*
418 * This try catch is explicitly written to use as utility in other
419 * test cases.
420 */
421 boolean isExpOccurred = false;
422 try {
423 bldr.addMultiInstanceChild(LWC, LISTNS, list, null);
sonu guptaeff184b2016-11-24 12:43:49 +0530424 } catch (IllegalArgumentException e) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530425 isExpOccurred = true;
426 assertEquals(e.getMessage(), error);
427 }
428 assertEquals(E_LIST + LWC, isExpOccurred, true);
429 }
430
431 /**
432 * Validate the error message obtained by traversing back to parent of
433 * current context against the given error string.
434 *
435 * @param ydtBuilder ydt builder
436 * @param error error string
437 */
438 private void traversToParentErrorMsgValidator(
439 YangRequestWorkBench ydtBuilder, String error) {
440 /*
441 * This try catch is explicitly written to use as utility in other
442 * test cases.
443 */
444 boolean isExpOccurred = false;
445 try {
446 ydtBuilder.traverseToParent();
sonu guptaeff184b2016-11-24 12:43:49 +0530447 } catch (IllegalStateException e) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530448 isExpOccurred = true;
449 assertEquals(e.getMessage(), error);
450 }
451 assertEquals(E_TOPARENT, isExpOccurred, true);
452 }
453
454 /**
455 * Validate the error message obtained by adding leaf node in
456 * current context against the given error string.
457 *
458 * @param bldr ydt builder
459 * @param name name of the leaf
460 * @param val leaf value
461 * @param error error string
462 */
463 private void leafErrorMsgValidator(
464 YangRequestWorkBench bldr, String name, String val, String error) {
465 /*
466 * This try catch is explicitly written to use as utility in other
467 * test cases.
468 */
469 boolean isExpOccurred = false;
470 try {
471 bldr.addLeaf(name, LISTNS, val);
sonu guptaeff184b2016-11-24 12:43:49 +0530472 } catch (IllegalArgumentException e) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530473 isExpOccurred = true;
474 assertEquals(e.getMessage(), error);
475 }
476 assertEquals(E_LEAF + name, isExpOccurred, true);
477 }
478}