blob: 81e973f6143205a73bf6c3f075f5aba38c0a74d9 [file] [log] [blame]
Vidyashree Rama1db15562016-05-17 16:16:15 +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.yangutils.parser.impl.listeners;
18
19import java.io.IOException;
20import java.util.ListIterator;
21
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053022import org.junit.Rule;
Vidyashree Rama1db15562016-05-17 16:16:15 +053023import org.junit.Test;
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053024import org.junit.rules.ExpectedException;
25import org.onosproject.yangutils.datamodel.YangDerivedInfo;
Vidyashree Rama1db15562016-05-17 16:16:15 +053026import org.onosproject.yangutils.datamodel.YangNode;
27import org.onosproject.yangutils.datamodel.YangModule;
28import org.onosproject.yangutils.datamodel.YangLeaf;
29import org.onosproject.yangutils.datamodel.YangChoice;
30import org.onosproject.yangutils.datamodel.YangContainer;
31import org.onosproject.yangutils.datamodel.YangNodeType;
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053032import org.onosproject.yangutils.datamodel.YangType;
33import org.onosproject.yangutils.datamodel.YangTypeDef;
34import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException;
35import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
36import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangInt64;
Vidyashree Rama1db15562016-05-17 16:16:15 +053037import org.onosproject.yangutils.parser.exceptions.ParserException;
38import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
39
40import static org.hamcrest.core.Is.is;
41import static org.junit.Assert.assertThat;
42
43/**
44 * Test cases for testing default listener functionality.
45 */
46public class DefaultListenerTest {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053047 @Rule
48 public ExpectedException thrown = ExpectedException.none();
Vidyashree Rama1db15562016-05-17 16:16:15 +053049
50 private final YangUtilsParserManager manager = new YangUtilsParserManager();
51
52 /**
53 * Checks if default value is set correctly.
54 */
55 @Test
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053056 public void processDefaultValueInLeafSubStatement() throws IOException, ParserException {
Vidyashree Rama1db15562016-05-17 16:16:15 +053057
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053058 YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueInLeafSubStatement.yang");
Vidyashree Rama1db15562016-05-17 16:16:15 +053059 // Check whether the data model tree returned is of type module.
60 assertThat((node instanceof YangModule), is(true));
61
62 // Check whether the node type is set properly to module.
63 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
64
65 // Check whether the module name is set correctly.
66 YangModule yangNode = (YangModule) node;
67 assertThat(yangNode.getName(), is("Test"));
68
69 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
70 YangLeaf leafInfo = leafIterator.next();
71
72 assertThat(leafInfo.getName(), is("invalid-interval"));
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053073 assertThat(leafInfo.getDefaultValueInString(), is("1"));
Vidyashree Rama1db15562016-05-17 16:16:15 +053074 }
75
76 /**
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053077 * Validates default invalid value in leaf.
Vidyashree Rama1db15562016-05-17 16:16:15 +053078 */
79 @Test
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053080 public void processDefaultInalueInLeafSubStatement() throws IOException, ParserException {
Vidyashree Rama1db15562016-05-17 16:16:15 +053081
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053082 thrown.expect(DataTypeException.class);
83 thrown.expectMessage("YANG file error : Input value \"x\" is not a valid uint16.");
84
85 manager.getDataModel("src/test/resources/default/DefaultInvalidValueInLeafSubStatement.yang");
86 }
87
88 /**
89 * Validates default case value in choice statement.
90 */
91 @Test
92 public void processDefaultCaseInChoiceSubStatement() throws IOException, ParserException {
93
94 YangNode node = manager.getDataModel("src/test/resources/default/DefaultCaseInChoiceSubStatement.yang");
Vidyashree Rama1db15562016-05-17 16:16:15 +053095 // Check whether the data model tree returned is of type module.
96 assertThat((node instanceof YangModule), is(true));
97
98 // Check whether the node type is set properly to module.
99 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
100
101 // Check whether the module name is set correctly.
102 YangModule yangNode = (YangModule) node;
103 assertThat(yangNode.getName(), is("Test"));
104
105 YangContainer yangContainer = (YangContainer) yangNode.getChild();
106 assertThat(yangContainer.getName(), is("food"));
107
108 YangChoice yangChoice = (YangChoice) yangContainer.getChild();
109 assertThat(yangChoice.getName(), is("snack"));
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530110 assertThat(yangChoice.getDefaultValueInString(), is("sports-arena"));
111 }
112
113 /**
114 * Validates default invalide case in choice statement.
115 */
116 @Test
117 public void processDefaultInvalidCaseInChoiceSubStatement() throws IOException, ParserException {
118
119 thrown.expect(ParserException.class);
120 thrown.expectMessage("Internal parser error detected: Invalid content in choice \"snack\" after processing.");
121
122 manager.getDataModel("src/test/resources/default/DefaultInvalidValueInChoiceSubStmt.yang");
123 }
124
125 /**
126 * Validates default value in typedef.
127 */
128 @Test
129 public void processDefaultInTypedef() throws IOException, ParserException {
130
131 YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueInTypeDef.yang");
132 // Check whether the data model tree returned is of type module.
133 assertThat((node instanceof YangModule), is(true));
134
135 // Check whether the node type is set properly to module.
136 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
137
138 // Check whether the module name is set correctly.
139 YangModule yangNode = (YangModule) node;
140 assertThat(yangNode.getName(), is("Test"));
141
142 // check typedef
143 YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
144 assertThat(typedef.getName(), is("topInt"));
145 assertThat(typedef.getDefaultValueInString(), is("10"));
146
147 YangType type = typedef.getTypeList().iterator().next();
148 assertThat(type.getDataType(), is(YangDataTypes.INT64));
149 assertThat(type.getDataTypeName(), is("int64"));
150
151 // check leaf
152 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
153 YangLeaf leafInfo = leafIterator.next();
154 assertThat(leafInfo.getName(), is("myValue"));
155
156 // Check leaf reffered typedef
157 assertThat(leafInfo.getDataType().getDataTypeName(), is("topInt"));
158 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
159 YangType<YangDerivedInfo> typeDerived = (YangType<YangDerivedInfo>) leafInfo.getDataType();
160 YangDerivedInfo derivedInfo = (YangDerivedInfo) typeDerived.getDataTypeExtendedInfo();
161 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
162 assertThat(prevTypedef.getName(), is("topInt"));
163 assertThat(prevTypedef.getDefaultValueInString(), is("10"));
164 YangType topType = prevTypedef.getTypeList().iterator().next();
165 assertThat(topType.getDataType(), is(YangDataTypes.INT64));
166 assertThat(topType.getDataTypeName(), is("int64"));
167 YangType<YangInt64> typeInt64 = (YangType<YangInt64>) topType;
168 YangInt64 int64Obj = typeInt64.getDataTypeExtendedInfo();
169 }
170
171 /**
172 * Validates invalid default value in typedef.
173 */
174 @Test
175 public void processInvalidDefaultValueInTypdeDef() throws IOException, ParserException {
176 thrown.expect(DataTypeException.class);
177 thrown.expectMessage("YANG file error : Input value \"x\" is not a valid int64.");
178
179 manager.getDataModel("src/test/resources/default/DefaultInvalidValueInTypeDef.yang");
180 }
181
182 /**
183 * Validates default value decimal64 in leaf.
184 */
185 @Test
186 public void processDefaultValueDecimal64InLeaf() throws IOException, ParserException {
187
188 YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueDecimal64InLeaf.yang");
189 // Check whether the data model tree returned is of type module.
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 leaf
200 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
201 YangLeaf leafInfo = leafIterator.next();
202
203 // check the default value
204 // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
205 assertThat(leafInfo.getName(), is("mydecimal"));
206 assertThat(leafInfo.getDefaultValueInString(), is("5"));
207 }
208
209 /**
210 * Validates default invalid value decimal64 in leaf.
211 */
212 @Test
213 public void processDefaultInvalidValueDecimal64InLeaf() throws IOException, ParserException {
214 thrown.expect(DataTypeException.class);
215 thrown.expectMessage("YANG file error : Input value \"x\" is not a valid decimal64.");
216
217 manager.getDataModel("src/test/resources/default/DefaultInvalidValueDecimal64InLeaf.yang");
218 }
219
220 /**
221 * Validates default value string in leaf.
222 */
223 @Test
224 public void processDefaultValueStringInLeaf() throws IOException, ParserException {
225
226 YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueStringInLeaf.yang");
227 // Check whether the data model tree returned is of type module.
228 assertThat((node instanceof YangModule), is(true));
229
230 // Check whether the node type is set properly to module.
231 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
232
233 // Check whether the module name is set correctly.
234 YangModule yangNode = (YangModule) node;
235 assertThat(yangNode.getName(), is("Test"));
236
237 // check leaf
238 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
239 YangLeaf leafInfo = leafIterator.next();
240
241 // check the default value
242 // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
243 assertThat(leafInfo.getName(), is("MyString"));
244 assertThat(leafInfo.getDefaultValueInString(), is("2bB"));
245 }
246
247 /**
248 * Validates default invalid value string in leaf.
249 */
250 @Test
251 public void processDefaultInvalidValueStringInLeaf() throws IOException, ParserException {
252 thrown.expect(DataTypeException.class);
253 thrown.expectMessage("YANG file error : Input value \"2bB2bB\" is not a valid string");
254
255 manager.getDataModel("src/test/resources/default/DefaultInvalidValueStringInLeaf.yang");
256 }
257
258 /**
259 * Validates default value boolean in leaf.
260 */
261 @Test
262 public void processDefaultValueBooleanInLeaf() throws IOException, ParserException {
263
264 YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueBooleanInLeaf.yang");
265 // Check whether the data model tree returned is of type module.
266 assertThat((node instanceof YangModule), is(true));
267
268 // Check whether the node type is set properly to module.
269 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
270
271 // Check whether the module name is set correctly.
272 YangModule yangNode = (YangModule) node;
273 assertThat(yangNode.getName(), is("Test"));
274
275 // check leaf
276 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
277 YangLeaf leafInfo = leafIterator.next();
278
279 // check the default value
280 // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
281 assertThat(leafInfo.getName(), is("myboolean"));
282 assertThat(leafInfo.getDefaultValueInString(), is("true"));
283 }
284
285 /**
286 * Validates default invalid value boolean in leaf.
287 */
288 @Test
289 public void processDefaultInvalidValueBooleanInLeaf() throws IOException, ParserException {
290 thrown.expect(DataTypeException.class);
291 thrown.expectMessage("YANG file error : Input value \"yes\" is not a valid boolean");
292
293 manager.getDataModel("src/test/resources/default/DefaultInvalidValueBooleanInLeaf.yang");
294 }
295
296 /**
297 * Validates default value enumeration in leaf.
298 */
299 @Test
300 public void processDefaultValueEnumberationInLeaf() throws IOException, ParserException {
301
302 YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueEnumerationInLeaf.yang");
303 // Check whether the data model tree returned is of type module.
304 assertThat((node instanceof YangModule), is(true));
305
306 // Check whether the node type is set properly to module.
307 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
308
309 // Check whether the module name is set correctly.
310 YangModule yangNode = (YangModule) node;
311 assertThat(yangNode.getName(), is("Test"));
312
313 // check leaf
314 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
315 YangLeaf leafInfo = leafIterator.next();
316
317 // check the default value
318 // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
319 assertThat(leafInfo.getName(), is("myenum"));
320 assertThat(leafInfo.getDefaultValueInString(), is("one"));
321 }
322
323 /**
324 * Validates default invalid value enumeration in leaf.
325 */
326 @Test
327 public void processDefaultInvalidValueEnumberationInLeaf() throws IOException, ParserException {
328 thrown.expect(DataTypeException.class);
329 thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid union");
330
331 manager.getDataModel("src/test/resources/default/DefaultInvalidValueEnumerationInLeaf.yang");
332 }
333
334 /**
335 * Validates default value bits in leaf.
336 */
337 @Test
338 public void processDefaultValueBitsInLeaf() throws IOException, ParserException {
339
340 YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueBitsInLeaf.yang");
341 // Check whether the data model tree returned is of type module.
342 assertThat((node instanceof YangModule), is(true));
343
344 // Check whether the node type is set properly to module.
345 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
346
347 // Check whether the module name is set correctly.
348 YangModule yangNode = (YangModule) node;
349 assertThat(yangNode.getName(), is("Test"));
350
351 // check leaf
352 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
353 YangLeaf leafInfo = leafIterator.next();
354
355 // check the default value
356 // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
357 assertThat(leafInfo.getName(), is("mybits"));
358 assertThat(leafInfo.getDefaultValueInString(), is("auto-sense-speed"));
359 }
360
361 /**
362 * Validates default invalid value bits in leaf.
363 */
364 @Test
365 public void processDefaultInvalidValueBitsInLeaf() throws IOException, ParserException {
366 thrown.expect(DataTypeException.class);
367 thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid bits");
368
369 manager.getDataModel("src/test/resources/default/DefaultInvalidValueBitsInLeaf.yang");
370 }
371
372 /**
373 * Validates default value binary in leaf.
374 */
375 @Test
376 public void processDefaultValueBinaryInLeaf() throws IOException, ParserException {
377
378 YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueBinaryInLeaf.yang");
379 // Check whether the data model tree returned is of type module.
380 assertThat((node instanceof YangModule), is(true));
381
382 // Check whether the node type is set properly to module.
383 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
384
385 // Check whether the module name is set correctly.
386 YangModule yangNode = (YangModule) node;
387 assertThat(yangNode.getName(), is("Test"));
388
389 // check leaf
390 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
391 YangLeaf leafInfo = leafIterator.next();
392
393 // check the default value
394 // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
395 assertThat(leafInfo.getName(), is("message"));
396 assertThat(leafInfo.getDefaultValueInString(), is("10010010"));
397 }
398
399 /**
400 * Validates default invalid value binary in leaf.
401 */
402 @Test
403 public void processDefaultInvlaidValueBinaryInLeaf() throws IOException, ParserException {
404 thrown.expect(DataTypeException.class);
405 thrown.expectMessage("YANG file error : Input value \"000\" is not a valid binary");
406
407 manager.getDataModel("src/test/resources/default/DefaultInvalidValueBinaryInLeaf.yang");
408 }
409
410 /**
411 * Validates default value empty in leaf.
412 */
413 @Test
414 public void processDefaultValueEmptyInLeaf() throws IOException, ParserException {
415 thrown.expect(DataTypeException.class);
416 thrown.expectMessage("YANG file error : Input value \"something\" is not a allowed for a data type empty");
417
418 manager.getDataModel("src/test/resources/default/DefaultValueEmptyInLeaf.yang");
419 }
420
421 /**
422 * Validates default value union in leaf.
423 */
424 @Test
425 public void processDefaultValueUnionInLeaf() throws IOException, ParserException {
426
427 YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueUnionInLeaf.yang");
428 // Check whether the data model tree returned is of type module.
429 assertThat((node instanceof YangModule), is(true));
430
431 // Check whether the node type is set properly to module.
432 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
433
434 // Check whether the module name is set correctly.
435 YangModule yangNode = (YangModule) node;
436 assertThat(yangNode.getName(), is("Test"));
437
438 // check leaf
439 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
440 YangLeaf leafInfo = leafIterator.next();
441
442 // check the default value
443 // This default value is verified by YangType.isValidValue() called from LeafListener.processLeafExit()
444 assertThat(leafInfo.getName(), is("message"));
445 assertThat(leafInfo.getDefaultValueInString(), is("unbounded"));
446 }
447
448 /**
449 * Validates default invalid value union in leaf.
450 */
451 @Test
452 public void processDefaultInvalidValueUnionInLeaf() throws IOException, ParserException {
453 thrown.expect(DataTypeException.class);
454 thrown.expectMessage("YANG file error : Input value \"xyz\" is not a valid union");
455
456 manager.getDataModel("src/test/resources/default/DefaultInvalidValueUnionInLeaf.yang");
457 }
458
459 /**
460 * Validates default value in multiple typedef.
461 */
462 @Test
463 public void processDefaultInMultiTypedef() throws IOException, ParserException {
464
465 YangNode node = manager.getDataModel("src/test/resources/default/DefaultValueInMultiTypeDef.yang");
466 // Check whether the data model tree returned is of type module.
467 assertThat((node instanceof YangModule), is(true));
468
469 // Check whether the node type is set properly to module.
470 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
471
472 // Check whether the module name is set correctly.
473 YangModule yangNode = (YangModule) node;
474 assertThat(yangNode.getName(), is("Test"));
475
476 // check typedef
477 YangTypeDef typedef = (YangTypeDef) yangNode.getChild();
478 assertThat(typedef.getName(), is("topInt"));
479 assertThat(typedef.getDefaultValueInString(), is("10"));
480
481 YangType type = typedef.getTypeList().iterator().next();
482 assertThat(type.getDataType(), is(YangDataTypes.INT64));
483 assertThat(type.getDataTypeName(), is("int64"));
484
485 // check leaf
486 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
487 YangLeaf leafInfo = leafIterator.next();
488 assertThat(leafInfo.getName(), is("lowInt"));
489
490 // check leaf type
491 assertThat(leafInfo.getName(), is("lowInt"));
492 assertThat(leafInfo.getDataType().getDataTypeName(), is("midInt"));
493 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
494 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
495 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
496
497 // check previous typedef
498 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
499 assertThat(prevTypedef.getName(), is("midInt"));
500 type = prevTypedef.getTypeList().iterator().next();
501 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
502 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
503
504 // check top typedef
505 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
506 assertThat(topTypedef.getName(), is("topInt"));
507 assertThat(topTypedef.getDefaultValueInString(), is("10"));
508 YangType topType = topTypedef.getTypeList().iterator().next();
509 assertThat(topType.getDataType(), is(YangDataTypes.INT64));
510 assertThat(topType.getDataTypeName(), is("int64"));
511 YangType<YangInt64> typeInt64 = (YangType<YangInt64>) topType;
512 YangInt64 int64Obj = typeInt64.getDataTypeExtendedInfo();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530513 }
514}