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