blob: bd634bb36d19e8f528a4c11806837f9a8984bf5a [file] [log] [blame]
Vidyashree Rama49abe712016-02-13 22:22:12 +05301/*
2 * Copyright 2016 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.yangutils.parser.impl.listeners;
18
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053019import java.io.IOException;
20import java.util.ListIterator;
21
Vidyashree Rama49abe712016-02-13 22:22:12 +053022import org.junit.Rule;
23import org.junit.Test;
Vidyashree Rama49abe712016-02-13 22:22:12 +053024import org.junit.rules.ExpectedException;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053025import org.onosproject.yangutils.datamodel.YangContainer;
26import org.onosproject.yangutils.datamodel.YangDataTypes;
Vidyashree Rama49abe712016-02-13 22:22:12 +053027import org.onosproject.yangutils.datamodel.YangLeaf;
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053028import org.onosproject.yangutils.datamodel.YangLeafList;
29import org.onosproject.yangutils.datamodel.YangList;
Vidyashree Rama49abe712016-02-13 22:22:12 +053030import org.onosproject.yangutils.datamodel.YangModule;
31import org.onosproject.yangutils.datamodel.YangNode;
32import org.onosproject.yangutils.datamodel.YangNodeType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053033import org.onosproject.yangutils.datamodel.YangStatusType;
Vidyashree Rama49abe712016-02-13 22:22:12 +053034import org.onosproject.yangutils.parser.exceptions.ParserException;
35import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
36
Vidyashree Rama49abe712016-02-13 22:22:12 +053037import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.core.Is.is;
39
40/**
41 * Test cases for config listener.
42 */
43public class ConfigListenerTest {
44
45 @Rule
46 public ExpectedException thrown = ExpectedException.none();
47
48 private final YangUtilsParserManager manager = new YangUtilsParserManager();
49
50 /**
51 * Checks valid config statement.
52 */
53 @Test
54 public void processConfigTrue() throws IOException, ParserException {
55
56 YangNode node = manager.getDataModel("src/test/resources/ConfigTrue.yang");
57
58 // Check whether the data model tree returned is of type module.
59 assertThat((node instanceof YangModule), is(true));
60
61 // Check whether the node type is set properly to module.
62 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
63
64 // Check whether the module name is set correctly.
65 YangModule yangNode = (YangModule) node;
66 assertThat(yangNode.getName(), is("Test"));
67
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053068 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
69 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053070
71 // Check whether the Config value is set correctly.
72 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
73 assertThat(leafInfo.isConfig(), is(true));
74 }
75
76 /**
77 * Checks valid config statement.
78 */
79 @Test
80 public void processConfigFalse() throws IOException, ParserException {
81
82 YangNode node = manager.getDataModel("src/test/resources/ConfigFalse.yang");
83
84 // Check whether the data model tree returned is of type module.
85 assertThat((node instanceof YangModule), is(true));
86
87 // Check whether the node type is set properly to module.
88 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
89
90 // Check whether the module name is set correctly.
91 YangModule yangNode = (YangModule) node;
92 assertThat(yangNode.getName(), is("Test"));
93
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053094 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
95 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +053096
97 // Check whether the Config value is set correctly.
98 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
99 assertThat(leafInfo.isConfig(), is(false));
100 }
101
102 /**
103 * Checks invalid config statement and expects parser exception.
104 */
105 @Test
106 public void processConfigWithoutStatementEnd() throws IOException, ParserException {
107 thrown.expect(ParserException.class);
108 thrown.expectMessage("missing ';' at '}'");
109 YangNode node = manager.getDataModel("src/test/resources/ConfigWithoutStatementEnd.yang");
110 }
111
112 /**
113 * Checks invalid config statement and expects parser exception.
114 */
115 @Test
116 public void processConfigInvalidValue() throws IOException, ParserException {
117 thrown.expect(ParserException.class);
118 thrown.expectMessage("mismatched input 'invalid' expecting {'false', 'true'}");
119 YangNode node = manager.getDataModel("src/test/resources/ConfigInvalidValue.yang");
120 }
121
122 /**
123 * Checks invalid config statement and expects parser exception.
124 */
125 @Test
126 public void processConfigEmptyValue() throws IOException, ParserException {
127 thrown.expect(ParserException.class);
128 thrown.expectMessage("missing {'false', 'true'} at ';'");
129 YangNode node = manager.getDataModel("src/test/resources/ConfigEmptyValue.yang");
130 }
131
132 /**
133 * Checks config statement as sub-statement of module.
134 */
135 @Test
136 public void processModuleSubStatementConfig() throws IOException, ParserException {
137 thrown.expect(ParserException.class);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530138 thrown.expectMessage("mismatched input 'config' expecting {'augment', 'choice', 'contact', 'container',"
139 + " 'description', 'extension', 'deviation', 'feature', 'grouping', 'identity', 'import', 'include', "
140 + "'leaf', 'leaf-list', 'list', 'namespace', 'notification', 'organization', 'prefix', 'reference',"
141 + " 'revision', 'rpc', 'typedef', 'uses', 'yang-version', '}'}");
Vidyashree Rama49abe712016-02-13 22:22:12 +0530142 YangNode node = manager.getDataModel("src/test/resources/ModuleSubStatementConfig.yang");
143 }
144
145 /**
146 * Checks config statement as sub-statement of container.
147 */
148 @Test
149 public void processContainerSubStatementConfig() throws IOException, ParserException {
150
151 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementConfig.yang");
152
153 // Check whether the data model tree returned is of type module.
154 assertThat((node instanceof YangModule), is(true));
155
156 // Check whether the node type is set properly to module.
157 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
158
159 // Check whether the module name is set correctly.
160 YangModule yangNode = (YangModule) node;
161 assertThat(yangNode.getName(), is("Test"));
162
163 // Check whether the config value is set correctly.
164 YangContainer container = (YangContainer) yangNode.getChild();
165 assertThat(container.getName(), is("valid"));
166 assertThat(container.isConfig(), is(true));
167
168 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530169 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
170 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530171
172 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
173 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
174 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
175 assertThat(leafInfo.getUnits(), is("\"seconds\""));
176 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
177 assertThat(leafInfo.isMandatory(), is(true));
178 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
179 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
180 }
181
182 /**
183 * Checks config statement as sub-statement of list.
184 */
185 @Test
186 public void processListSubStatementConfig() throws IOException, ParserException {
187
188 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementConfig.yang");
189
190 assertThat((node instanceof YangModule), is(true));
191
192 // Check whether the node type is set properly to module.
193 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
194
195 // Check whether the module name is set correctly.
196 YangModule yangNode = (YangModule) node;
197 assertThat(yangNode.getName(), is("Test"));
198
199 // Check whether the list is child of module and config value is set.
200 YangList yangList = (YangList) yangNode.getChild();
201 assertThat(yangList.getName(), is("valid"));
202 assertThat(yangList.isConfig(), is(true));
203
204 // Check whether leaf properties as set correctly.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530205 ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator();
206 YangLeaf leafInfo = leafIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530207
208 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
209 assertThat(leafInfo.getDataType().getDataTypeName(), is("\"uint16\""));
210 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UINT16));
211 assertThat(leafInfo.getUnits(), is("\"seconds\""));
212 assertThat(leafInfo.getDescription(), is("\"Interval before a route is declared invalid\""));
213 assertThat(leafInfo.isMandatory(), is(true));
214 assertThat(leafInfo.getStatus(), is(YangStatusType.CURRENT));
215 assertThat(leafInfo.getReference(), is("\"RFC 6020\""));
216 }
217
218 /**
219 * Checks valid config statement as sub-statement of leaf-list.
220 */
221 @Test
222 public void processLeafListSubStatementConfig() throws IOException, ParserException {
223
224 YangNode node = manager.getDataModel("src/test/resources/LeafListSubStatementConfig.yang");
225
226 // Check whether the data model tree returned is of type module.
227 assertThat((node instanceof YangModule), is(true));
228
229 // Check whether the node type is set properly to module.
230 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
231
232 // Check whether the module name is set correctly.
233 YangModule yangNode = (YangModule) node;
234 assertThat(yangNode.getName(), is("Test"));
235
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530236 ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator();
237 YangLeafList leafListInfo = leafListIterator.next();
Vidyashree Rama49abe712016-02-13 22:22:12 +0530238
239 // Check whether config value is set correctly.
240 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
241 assertThat(leafListInfo.isConfig(), is(true));
242 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530243
244 /**
245 * Checks config statement's default Value.
246 */
247 @Test
248 public void processConfigDefaultValue() throws IOException, ParserException {
249
250 YangNode node = manager.getDataModel("src/test/resources/ConfigDefaultValue.yang");
251
252 assertThat((node instanceof YangModule), is(true));
253 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
254 YangModule yangNode = (YangModule) node;
255 assertThat(yangNode.getName(), is("Test"));
256
257 // Check whether the config value is set correctly.
258 YangContainer container = (YangContainer) yangNode.getChild();
259 assertThat(container.getName(), is("valid"));
260 assertThat(container.isConfig(), is(true));
261
262 // Check whether leaf properties as set correctly.
263 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
264 YangLeaf leafInfo = leafIterator.next();
265
266 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
267 assertThat(leafInfo.isConfig(), is(true));
268 }
269
270 /**
271 * Checks whether exception is throw when node's parent config set to false,
272 * no node underneath it can have config set to true.
273 */
274 @Test
275 public void processConfigFalseParentContainerChildLeafList() throws IOException, ParserException {
276 thrown.expect(ParserException.class);
277 thrown.expectMessage("Internal parser error detected: Unhandled parsed data at container \"valid\" after "
278 + "processing.\nError Information: If a container has \"config\" set to \"false\", no node underneath "
279 + "it can have \"config\" set to \"true\".");
280 YangNode node = manager.getDataModel("src/test/resources/ConfigFalseParentContainerChildLeafList.yang");
281 }
282
283 /**
284 * Checks whether exception is throw when node's parent config set to false,
285 * no node underneath it can have config set to true.
286 */
287 @Test
288 public void processConfigFalseParentContainerChildLeaf() throws IOException, ParserException {
289 thrown.expect(ParserException.class);
290 thrown.expectMessage("Internal parser error detected: Unhandled parsed data at container \"valid\" after "
291 + "processing.\nError Information: If a container has \"config\" set to \"false\", no node underneath "
292 + "it can have \"config\" set to \"true\".");
293 YangNode node = manager.getDataModel("src/test/resources/ConfigFalseParentContainerChildLeaf.yang");
294 }
295
296 /**
297 * Checks whether exception is throw when node's parent config set to false,
298 * no node underneath it can have config set to true.
299 */
300 @Test
301 public void processConfigFalseParentListChildLeafList() throws IOException, ParserException {
302 thrown.expect(ParserException.class);
303 thrown.expectMessage("Internal parser error detected: Unhandled parsed data at list \"valid\" after"
304 + " processing.\nError Information: If a list has \"config\" set to \"false\", no node underneath"
305 + " it can have \"config\" set to \"true\".");
306 YangNode node = manager.getDataModel("src/test/resources/ConfigFalseParentListChildLeafList.yang");
307 }
308
309 /**
310 * Checks whether exception is throw when node's parent config set to false,
311 * no node underneath it can have config set to true.
312 */
313 @Test
314 public void processConfigFalseParentListChildLeaf() throws IOException, ParserException {
315 thrown.expect(ParserException.class);
316 thrown.expectMessage("Internal parser error detected: Unhandled parsed data at list \"valid\" after"
317 + " processing.\nError Information: If a list has \"config\" set to \"false\", no node underneath"
318 + " it can have \"config\" set to \"true\".");
319 YangNode node = manager.getDataModel("src/test/resources/ConfigFalseParentListChildLeaf.yang");
320 }
321
322 /**
323 * Checks when config is not specified, the default is same as the parent's schema node's
324 * config statement's value.
325 */
326 @Test
327 public void processNoConfigContainerSubStatementContainer() throws IOException, ParserException {
328
329 YangNode node = manager.getDataModel("src/test/resources/NoConfigContainerSubStatementContainer.yang");
330
331 assertThat((node instanceof YangModule), is(true));
332 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
333 YangModule yangNode = (YangModule) node;
334 assertThat(yangNode.getName(), is("Test"));
335
336 // Check whether the config value is set correctly.
337 YangContainer container = (YangContainer) yangNode.getChild();
338 assertThat(container.getName(), is("hello"));
339 assertThat(container.isConfig(), is(true));
340
341 YangNode containerNode = container.getChild();
342 assertThat(containerNode instanceof YangContainer, is(true));
343 YangContainer childContainer = (YangContainer) containerNode;
344 assertThat(childContainer.getName(), is("valid"));
345 assertThat(childContainer.isConfig(), is(true));
346 }
347
348 /**
349 * Checks when config is not specified, the default is same as the parent's schema node's
350 * config statement's value.
351 */
352 @Test
353 public void processNoConfigContainerSubStatementLeafList() throws IOException, ParserException {
354
355 YangNode node = manager.getDataModel("src/test/resources/NoConfigContainerSubStatementLeafList.yang");
356
357 assertThat((node instanceof YangModule), is(true));
358 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
359 YangModule yangNode = (YangModule) node;
360 assertThat(yangNode.getName(), is("Test"));
361
362 // Check whether the config value is set correctly.
363 YangContainer container = (YangContainer) yangNode.getChild();
364 assertThat(container.getName(), is("valid"));
365 assertThat(container.isConfig(), is(true));
366
367 ListIterator<YangLeafList> leafListIterator = container.getListOfLeafList().listIterator();
368 YangLeafList leafListInfo = leafListIterator.next();
369
370 // Check whether config value is set correctly.
371 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
372 assertThat(leafListInfo.isConfig(), is(true));
373
374 }
375
376 /**
377 * Checks when config is not specified, the default is same as the parent's schema node's
378 * config statement's value.
379 */
380 @Test
381 public void processNoConfigContainerSubStatementLeaf() throws IOException, ParserException {
382
383 YangNode node = manager.getDataModel("src/test/resources/NoConfigContainerSubStatementLeaf.yang");
384
385 assertThat((node instanceof YangModule), is(true));
386 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
387 YangModule yangNode = (YangModule) node;
388 assertThat(yangNode.getName(), is("Test"));
389
390 // Check whether the config value is set correctly.
391 YangContainer container = (YangContainer) yangNode.getChild();
392 assertThat(container.getName(), is("valid"));
393 assertThat(container.isConfig(), is(true));
394
395 // Check whether leaf properties as set correctly.
396 ListIterator<YangLeaf> leafIterator = container.getListOfLeaf().listIterator();
397 YangLeaf leafInfo = leafIterator.next();
398
399 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
400 assertThat(leafInfo.isConfig(), is(true));
401 }
402
403 /**
404 * Checks when config is not specified, the default is same as the parent's schema node's
405 * config statement's value.
406 */
407 @Test
408 public void processNoConfigContainerSubStatementList() throws IOException, ParserException {
409
410 YangNode node = manager.getDataModel("src/test/resources/NoConfigContainerSubStatementList.yang");
411
412 assertThat((node instanceof YangModule), is(true));
413 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
414 YangModule yangNode = (YangModule) node;
415 assertThat(yangNode.getName(), is("Test"));
416
417 // Check whether the config value is set correctly.
418 YangContainer container = (YangContainer) yangNode.getChild();
419 assertThat(container.getName(), is("hello"));
420 assertThat(container.isConfig(), is(true));
421
422 YangNode listNode = container.getChild();
423 assertThat(listNode instanceof YangList, is(true));
424 YangList childList = (YangList) listNode;
425 assertThat(childList.getName(), is("valid"));
426 assertThat(childList.isConfig(), is(true));
427
428 }
429
430 /**
431 * Checks when config is not specified, the default is same as the parent's schema node's
432 * config statement's value.
433 */
434 @Test
435 public void processNoConfigListSubStatementContainer() throws IOException, ParserException {
436
437 YangNode node = manager.getDataModel("src/test/resources/NoConfigListSubStatementContainer.yang");
438
439 assertThat((node instanceof YangModule), is(true));
440 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
441 YangModule yangNode = (YangModule) node;
442 assertThat(yangNode.getName(), is("Test"));
443
444 // Check whether the config value is set correctly.
445 YangList list1 = (YangList) yangNode.getChild();
446 assertThat(list1.getName(), is("list1"));
447 assertThat(list1.isConfig(), is(true));
448
449 YangNode containerNode = list1.getChild();
450 assertThat(containerNode instanceof YangContainer, is(true));
451 YangContainer childContainer = (YangContainer) containerNode;
452 assertThat(childContainer.getName(), is("container1"));
453 assertThat(childContainer.isConfig(), is(true));
454 }
455
456 /**
457 * Checks when config is not specified, the default is same as the parent's schema node's
458 * config statement's value.
459 */
460 @Test
461 public void processNoConfigListSubStatementLeafList() throws IOException, ParserException {
462
463 YangNode node = manager.getDataModel("src/test/resources/NoConfigListSubStatementLeafList.yang");
464
465 assertThat((node instanceof YangModule), is(true));
466 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
467 YangModule yangNode = (YangModule) node;
468 assertThat(yangNode.getName(), is("Test"));
469
470 // Check whether the config value is set correctly.
471 YangList list1 = (YangList) yangNode.getChild();
472 assertThat(list1.getName(), is("valid"));
473 assertThat(list1.isConfig(), is(true));
474
475 ListIterator<YangLeafList> leafListIterator = list1.getListOfLeafList().listIterator();
476 YangLeafList leafListInfo = leafListIterator.next();
477
478 // Check whether config value is set correctly.
479 assertThat(leafListInfo.getLeafName(), is("invalid-interval"));
480 assertThat(leafListInfo.isConfig(), is(true));
481 }
482
483 /**
484 * Checks when config is not specified, the default is same as the parent's schema node's
485 * config statement's value.
486 */
487 @Test
488 public void processNoConfigListSubStatementLeaf() throws IOException, ParserException {
489
490 YangNode node = manager.getDataModel("src/test/resources/NoConfigListSubStatementLeaf.yang");
491
492 assertThat((node instanceof YangModule), is(true));
493 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
494 YangModule yangNode = (YangModule) node;
495 assertThat(yangNode.getName(), is("Test"));
496
497 // Check whether the config value is set correctly.
498 YangList list1 = (YangList) yangNode.getChild();
499 assertThat(list1.getName(), is("valid"));
500 assertThat(list1.isConfig(), is(true));
501
502 // Check whether leaf properties as set correctly.
503 ListIterator<YangLeaf> leafIterator = list1.getListOfLeaf().listIterator();
504 YangLeaf leafInfo = leafIterator.next();
505
506 assertThat(leafInfo.getLeafName(), is("invalid-interval"));
507 assertThat(leafInfo.isConfig(), is(true));
508 }
509
510 /**
511 * Checks when config is not specified, the default is same as the parent's schema node's
512 * config statement's value.
513 */
514 @Test
515 public void processNoConfigListSubStatementList() throws IOException, ParserException {
516
517 YangNode node = manager.getDataModel("src/test/resources/NoConfigListSubStatementList.yang");
518
519 assertThat((node instanceof YangModule), is(true));
520 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
521 YangModule yangNode = (YangModule) node;
522 assertThat(yangNode.getName(), is("Test"));
523
524 // Check whether the config value is set correctly.
525 YangList list1 = (YangList) yangNode.getChild();
526 assertThat(list1.getName(), is("valid"));
527 assertThat(list1.isConfig(), is(true));
528
529 YangNode listNode = list1.getChild();
530 assertThat(listNode instanceof YangList, is(true));
531 YangList childList = (YangList) listNode;
532 assertThat(childList.getName(), is("list1"));
533 assertThat(childList.isConfig(), is(true));
534 }
Vidyashree Rama49abe712016-02-13 22:22:12 +0530535}