blob: 303a3e64e263a1ff7272950f6305db1331bd28a3 [file] [log] [blame]
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +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 static org.hamcrest.MatcherAssert.assertThat;
20import static org.hamcrest.core.Is.is;
21
22import org.junit.Rule;
23import org.junit.Test;
24import org.junit.rules.ExpectedException;
25import org.onosproject.yangutils.datamodel.YangDecimal64;
26import org.onosproject.yangutils.datamodel.YangDerivedInfo;
27import org.onosproject.yangutils.datamodel.YangLeaf;
28import org.onosproject.yangutils.datamodel.YangModule;
29import org.onosproject.yangutils.datamodel.YangNode;
30import org.onosproject.yangutils.datamodel.YangNodeType;
31import org.onosproject.yangutils.datamodel.YangRangeRestriction;
32import org.onosproject.yangutils.datamodel.YangRangeInterval;
33import org.onosproject.yangutils.datamodel.YangType;
34import org.onosproject.yangutils.datamodel.YangTypeDef;
35import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +053036import org.onosproject.yangutils.linker.exceptions.LinkerException;
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053037import org.onosproject.yangutils.parser.exceptions.ParserException;
38import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
39import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
40
41import java.io.IOException;
42import java.math.BigDecimal;
43import java.util.ListIterator;
44
45/**
46 * Test cases for decimal64 listener.
47 */
48public class Decimal64ListenerTest {
49 @Rule
50 public ExpectedException thrown = ExpectedException.none();
51
52 private final YangUtilsParserManager manager = new YangUtilsParserManager();
53
54 /**
55 * Checks decimal64 statement with fraction-digits.
56 */
57 @Test
58 public void processDecimal64TypeStatement() throws IOException, ParserException {
59
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +053060 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeStatement.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053061
62 // Check whether the data model tree returned is of type module.
63 assertThat((node instanceof YangModule), is(true));
64
65 // Check whether the node type is set properly to module.
66 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
67
68 // Check whether the module name is set correctly.
69 YangModule yangNode = (YangModule) node;
70 assertThat(yangNode.getName(), is("Test"));
71
72 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
73 YangLeaf leafInfo = leafIterator.next();
74
75 assertThat(leafInfo.getName(), is("validDecimal"));
76 assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
77 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
78 assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(),
79 is(2));
80 }
81
82 /**
83 * Checks decimal64 statement with range statement.
84 */
85 @Test
86 public void processDecimal64TypeWithRangeStatement() throws IOException, ParserException {
87
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +053088 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeWithRangeStatement.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053089
90 // Check whether the data model tree returned is of type module.
91 assertThat((node instanceof YangModule), is(true));
92
93 // Check whether the node type is set properly to module.
94 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
95
96 // Check whether the module name is set correctly.
97 YangModule yangNode = (YangModule) node;
98 assertThat(yangNode.getName(), is("Test"));
99
100 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
101 YangLeaf leafInfo = leafIterator.next();
102
103 assertThat(leafInfo.getName(), is("validDecimal"));
104 assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
105 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
106 assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(),
107 is(8));
108
109 YangRangeRestriction rangeRestriction = ((YangDecimal64<YangRangeRestriction>) leafInfo.getDataType()
110 .getDataTypeExtendedInfo())
111 .getRangeRestrictedExtendedInfo();
112
113 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
114 .listIterator();
115 YangRangeInterval rangeInterval = rangeListIterator.next();
116 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(-92233720368.54775808));
117 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(92233720368.54775807));
118 }
119
120 /**
121 * Successful validation of decimal64 statement.
122 */
123 @Test
124 public void processDecimal64ValueSuccessfulValidation() throws IOException, ParserException, DataModelException {
125
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530126 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeValidation.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530127
128 // Check whether the data model tree returned is of type module.
129 assertThat((node instanceof YangModule), is(true));
130
131 // Check whether the node type is set properly to module.
132 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
133
134 // Check whether the module name is set correctly.
135 YangModule yangNode = (YangModule) node;
136 assertThat(yangNode.getName(), is("Test"));
137
138 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
139 YangLeaf leafInfo = leafIterator.next();
140 YangDecimal64 decimal64 = (YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo();
141
142 assertThat(leafInfo.getName(), is("validDecimal"));
143 assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
144 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
145 assertThat(decimal64.getFractionDigit(), is(18));
146
147 decimal64.setValue(new BigDecimal(-9.223372036854775808));
148 decimal64.validateValue();
149 decimal64.setValue(new BigDecimal(9.223372036854775807));
150 decimal64.validateValue();
151 }
152
153 /**
154 * Failure validation of decimal64 statement.
155 */
156 @Test
157 public void processDecimal64ValueFailureValidation() throws IOException, ParserException, DataModelException {
158 thrown.expect(DataModelException.class);
159 thrown.expectMessage("YANG file error : decimal64 validation failed.");
160
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530161 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeValidation.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530162
163 // Check whether the data model tree returned is of type module.
164 assertThat((node instanceof YangModule), is(true));
165
166 // Check whether the node type is set properly to module.
167 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
168
169 // Check whether the module name is set correctly.
170 YangModule yangNode = (YangModule) node;
171 assertThat(yangNode.getName(), is("Test"));
172
173 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
174 YangLeaf leafInfo = leafIterator.next();
175 YangDecimal64 decimal64 = (YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo();
176
177 assertThat(leafInfo.getName(), is("validDecimal"));
178 assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
179 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
180 assertThat(decimal64.getFractionDigit(), is(18));
181
182 decimal64.setValue(new BigDecimal(-92233720368547758.08));
183 // validation should fail
184 decimal64.validateValue();
185 }
186
187 /**
188 * Validation of invalid maximum value limit of fraction-digits.
189 */
190 @Test
191 public void processDecimal64InvalidMaxFraction() throws IOException, ParserException, DataModelException {
192 thrown.expect(ParserException.class);
193 thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
194
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530195 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidMaxValueFraction.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530196 }
197
198 /**
199 * Validation of invalid (0) minimum value limit of fraction-digits.
200 */
201 @Test
202 public void processDecimal64InvalidMinFraction1() throws IOException, ParserException, DataModelException {
203 thrown.expect(ParserException.class);
204 thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
205
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530206 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidMinValueFraction1.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530207 }
208
209 /**
210 * Validation of invalid (-1) minimum value limit of fraction-digits.
211 */
212 @Test
213 public void processDecimal64InvalidMinFraction2() throws IOException, ParserException, DataModelException {
214 thrown.expect(ParserException.class);
215 thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
216
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530217 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidMinValueFraction2.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530218 }
219
220 /**
221 * Validation of decimal64 range statement.
222 */
223 @Test
224 public void processDecimal64TypeWithMultiValueRangeStatement() throws IOException, ParserException {
225
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530226 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeWithMultiValueRangeStmnt.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530227
228 // Check whether the data model tree returned is of type module.
229 assertThat((node instanceof YangModule), is(true));
230
231 // Check whether the node type is set properly to module.
232 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
233
234 // Check whether the module name is set correctly.
235 YangModule yangNode = (YangModule) node;
236 assertThat(yangNode.getName(), is("Test"));
237
238 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
239 YangLeaf leafInfo = leafIterator.next();
240
241 assertThat(leafInfo.getName(), is("validDecimal"));
242 assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
243 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
244 assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(),
245 is(18));
246
247 YangRangeRestriction rangeRestriction = ((YangDecimal64<YangRangeRestriction>) leafInfo.getDataType()
248 .getDataTypeExtendedInfo())
249 .getRangeRestrictedExtendedInfo();
250
251 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
252 .listIterator();
253 // range "1 .. 3.14 | 10 | 20..max";
254 // check first range 1 .. 3.14
255 YangRangeInterval rangeInterval = rangeListIterator.next();
256 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(-9.22));
257 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(7.22));
258 // check second range 10
259 rangeInterval = rangeListIterator.next();
260 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(8.0));
261 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(8.0));
262 // check third range 20..max
263 rangeInterval = rangeListIterator.next();
264 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(9.0));
265 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(9.223372036854776));
266 }
267
268 /**
269 * Validation of decimal64 with invalid range.
270 */
271 @Test
272 public void processDecimal64InvalidRange() throws IOException, ParserException, DataModelException {
273 thrown.expect(ParserException.class);
274 thrown.expectMessage("YANG file error : decimal64 validation failed.");
275
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530276 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidRangeStmnt.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530277 }
278
279 /**
280 * Validation of decimal64 without fraction-digits. Fraction-digits must be present for decimal64.
281 */
282 @Test
283 public void processDecimal64WithoutFraction() throws IOException, ParserException, DataModelException {
284 thrown.expect(ParserException.class);
285 thrown.expectMessage("YANG file error : a type decimal64 must have fraction-digits statement.");
286
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530287 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeWithoutFraction.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530288 }
289
290 /**
291 * Checks decimal64 with typedef statement.
292 */
293 @Test
294 public void processDecimal64TypedefStatement() throws IOException, ParserException {
295
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530296 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypedefStatement.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530297
298 // Check whether the data model tree returned is of type module.
299 assertThat((node instanceof YangModule), is(true));
300
301 // Check whether the node type is set properly to module.
302 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
303
304 // Check whether the module name is set correctly.
305 YangModule yangNode = (YangModule) node;
306 assertThat(yangNode.getName(), is("Test"));
307
308 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
309 YangLeaf leafInfo = leafIterator.next();
310
311 assertThat(leafInfo.getName(), is("setFourDecimal"));
312 assertThat(leafInfo.getDataType().getDataTypeName(), is("validDecimal"));
313 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
314 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
315 YangTypeDef typedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
316 assertThat(typedef.getName(), is("validDecimal"));
317
318 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
319 YangType type = typedef.getTypeList().iterator().next();
320 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
321 assertThat(type.getDataTypeName(), is("decimal64"));
322 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) typedef.getTypeList().iterator().next();
323 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
324 assertThat(decimal64.getFractionDigit(), is(4));
325 }
326
327 /**
328 * Checks decimal64 with multiple typedef statement.
329 */
330 @Test
331 public void processDecimal64MultiTypedefStatement() throws IOException, ParserException {
332
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530333 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefStatement.yang");
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530334
335 // Check whether the data model tree returned is of type module.
336 assertThat((node instanceof YangModule), is(true));
337
338 // Check whether the node type is set properly to module.
339 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
340
341 // Check whether the module name is set correctly.
342 YangModule yangNode = (YangModule) node;
343 assertThat(yangNode.getName(), is("Test"));
344
345 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
346 YangLeaf leafInfo = leafIterator.next();
347
348 // check leaf type
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530349 assertThat(leafInfo.getName(), is("lowerDecimal"));
350 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530351 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
352 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
353 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
354
355 // check previous typedef
356 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530357 assertThat(prevTypedef.getName(), is("midDecimal"));
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530358 YangType type = prevTypedef.getTypeList().iterator().next();
359 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
360 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
361
362 // check top typedef
363 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
364 assertThat(topTypedef.getName(), is("topDecimal"));
365 type = topTypedef.getTypeList().iterator().next();
366 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
367 assertThat(type.getDataTypeName(), is("decimal64"));
368 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
369 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
370 assertThat(decimal64.getFractionDigit(), is(4));
371 }
Mahesh Poojary Huawei30c116a2016-07-14 17:49:50 +0530372
373 /**
374 * Checks decimal64 with multiple typedef with single range statement.
375 * Range value in typedef statement.
376 */
377 @Test
378 public void processDecimal64MultiTypedefRangeStatement() throws IOException, ParserException {
379
380 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefRangeStatement.yang");
381
382 // Check whether the data model tree returned is of type module.
383 assertThat((node instanceof YangModule), is(true));
384
385 // Check whether the node type is set properly to module.
386 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
387
388 // Check whether the module name is set correctly.
389 YangModule yangNode = (YangModule) node;
390 assertThat(yangNode.getName(), is("Test"));
391
392 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
393 YangLeaf leafInfo = leafIterator.next();
394
395 // check leaf type
396 assertThat(leafInfo.getName(), is("lowerDecimal"));
397 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
398 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
399 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
400 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
401
402 // Check range restriction
403 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
404 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
405 .listIterator();
406 YangRangeInterval rangeInterval = rangeListIterator.next();
407 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
408 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
409
410 // check previous typedef
411 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
412 assertThat(prevTypedef.getName(), is("midDecimal"));
413 YangType type = prevTypedef.getTypeList().iterator().next();
414 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
415 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
416
417 // check top typedef
418 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
419 assertThat(topTypedef.getName(), is("topDecimal"));
420 type = topTypedef.getTypeList().iterator().next();
421 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
422 assertThat(type.getDataTypeName(), is("decimal64"));
423 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
424 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
425 assertThat(decimal64.getFractionDigit(), is(4));
426
427 // Check range restriction
428 rangeRestriction = (YangRangeRestriction) decimal64.getRangeRestrictedExtendedInfo();
429 rangeListIterator = rangeRestriction.getAscendingRangeIntervals().listIterator();
430 rangeInterval = rangeListIterator.next();
431 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
432 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
433 }
434
435 /**
436 * Checks decimal64 with multiple typedef with single range statement.
437 * Range value in leaf statement.
438 */
439 @Test
440 public void processDecimal64MultiTypedefRangeInLeafStatement() throws IOException, ParserException {
441
442 YangNode node = manager
443 .getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefRangeInLeafStatement.yang");
444
445 // Check whether the data model tree returned is of type module.
446 assertThat((node instanceof YangModule), is(true));
447
448 // Check whether the node type is set properly to module.
449 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
450
451 // Check whether the module name is set correctly.
452 YangModule yangNode = (YangModule) node;
453 assertThat(yangNode.getName(), is("Test"));
454
455 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
456 YangLeaf leafInfo = leafIterator.next();
457
458 // check leaf type
459 assertThat(leafInfo.getName(), is("lowerDecimal"));
460 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
461 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
462 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
463 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
464
465 // Check range restriction
466 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
467 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
468 .listIterator();
469 YangRangeInterval rangeInterval = rangeListIterator.next();
470 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
471 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
472
473 // check previous typedef
474 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
475 assertThat(prevTypedef.getName(), is("midDecimal"));
476 YangType type = prevTypedef.getTypeList().iterator().next();
477 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
478 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
479
480 // check top typedef
481 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
482 assertThat(topTypedef.getName(), is("topDecimal"));
483 type = topTypedef.getTypeList().iterator().next();
484 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
485 assertThat(type.getDataTypeName(), is("decimal64"));
486 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
487 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
488 assertThat(decimal64.getFractionDigit(), is(4));
489 }
490
491 /**
492 * Checks decimal64 with multiple typedef with multiple range statement.
493 * Having more restricted range at leaf.
494 */
495 @Test
496 public void processDecimal64MultiTypedefMultipleRangeStatement() throws IOException, ParserException {
497
498 YangNode node = manager
499 .getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefMultiRangeStatement.yang");
500
501 // Check whether the data model tree returned is of type module.
502 assertThat((node instanceof YangModule), is(true));
503
504 // Check whether the node type is set properly to module.
505 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
506
507 // Check whether the module name is set correctly.
508 YangModule yangNode = (YangModule) node;
509 assertThat(yangNode.getName(), is("Test"));
510
511 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
512 YangLeaf leafInfo = leafIterator.next();
513
514 // check leaf type
515 assertThat(leafInfo.getName(), is("lowerDecimal"));
516 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
517 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
518 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
519 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
520
521 // Check range restriction
522 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
523
524 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
525 .listIterator();
526 YangRangeInterval rangeInterval = rangeListIterator.next();
527 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(4.0));
528 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(11.0));
529
530 // check previous typedef
531 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
532 assertThat(prevTypedef.getName(), is("midDecimal"));
533 YangType type = prevTypedef.getTypeList().iterator().next();
534 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
535 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
536
537 // check top typedef
538 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
539 assertThat(topTypedef.getName(), is("topDecimal"));
540 type = topTypedef.getTypeList().iterator().next();
541 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
542 assertThat(type.getDataTypeName(), is("decimal64"));
543 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
544 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
545 assertThat(decimal64.getFractionDigit(), is(4));
546
547 // Check range restriction
548 rangeRestriction = (YangRangeRestriction) decimal64.getRangeRestrictedExtendedInfo();
549 rangeListIterator = rangeRestriction.getAscendingRangeIntervals().listIterator();
550 rangeInterval = rangeListIterator.next();
551 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
552 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
553 }
554
555 /**
556 * Checks decimal64 with multiple typedef with multiple range statement.
557 * But having more restricted range at top of typedef.
558 */
559 @Test
560 public void processDecimal64MultiTypedefMultiInvalidRangeStatement() throws IOException, LinkerException {
561 thrown.expect(LinkerException.class);
562 thrown.expectMessage(" Range interval doesn't fall within the referred restriction ranges");
563
564 manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefMultiInvalidRangeStatement.yang");
565 }
566
567 /**
568 * Checks decimal64 with multiple typedef with max range statement.
569 */
570 @Test
571 public void processDecimal64MultiTypedefWithMaxRange() throws IOException, ParserException {
572
573 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefWithMaxRange.yang");
574
575 // Check whether the data model tree returned is of type module.
576 assertThat((node instanceof YangModule), is(true));
577
578 // Check whether the node type is set properly to module.
579 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
580
581 // Check whether the module name is set correctly.
582 YangModule yangNode = (YangModule) node;
583 assertThat(yangNode.getName(), is("Test"));
584
585 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
586 YangLeaf leafInfo = leafIterator.next();
587
588 // check leaf type
589 assertThat(leafInfo.getName(), is("lowerDecimal"));
590 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
591 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
592 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
593 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
594
595 // Check range restriction
596 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
597
598 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
599 .listIterator();
600 YangRangeInterval rangeInterval = rangeListIterator.next();
601 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(4.0));
602 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
603
604 // check previous typedef
605 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
606 assertThat(prevTypedef.getName(), is("midDecimal"));
607 YangType type = prevTypedef.getTypeList().iterator().next();
608 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
609 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
610
611 // check top typedef
612 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
613 assertThat(topTypedef.getName(), is("topDecimal"));
614 type = topTypedef.getTypeList().iterator().next();
615 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
616 assertThat(type.getDataTypeName(), is("decimal64"));
617 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
618 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
619 assertThat(decimal64.getFractionDigit(), is(4));
620
621 // Check range restriction
622 rangeRestriction = (YangRangeRestriction) decimal64.getRangeRestrictedExtendedInfo();
623 rangeListIterator = rangeRestriction.getAscendingRangeIntervals().listIterator();
624 rangeInterval = rangeListIterator.next();
625 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
626 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
627 }
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530628}