blob: 9a0373e72ebac40881906b0ebf4f07d292bbcc19 [file] [log] [blame]
Mahesh Poojary Huawei2cd44332016-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
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +053019import org.junit.Rule;
20import org.junit.Test;
21import org.junit.rules.ExpectedException;
22import org.onosproject.yangutils.datamodel.YangDecimal64;
23import org.onosproject.yangutils.datamodel.YangDerivedInfo;
24import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.datamodel.YangModule;
26import org.onosproject.yangutils.datamodel.YangNode;
27import org.onosproject.yangutils.datamodel.YangNodeType;
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +053028import org.onosproject.yangutils.datamodel.YangRangeInterval;
Bharat saraswal0663aff2016-10-18 23:16:14 +053029import org.onosproject.yangutils.datamodel.YangRangeRestriction;
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +053030import org.onosproject.yangutils.datamodel.YangType;
31import org.onosproject.yangutils.datamodel.YangTypeDef;
32import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal0663aff2016-10-18 23:16:14 +053033import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +053034import org.onosproject.yangutils.linker.exceptions.LinkerException;
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +053035import org.onosproject.yangutils.parser.exceptions.ParserException;
36import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +053037
38import java.io.IOException;
39import java.math.BigDecimal;
40import java.util.ListIterator;
41
Bharat saraswal0663aff2016-10-18 23:16:14 +053042import static org.hamcrest.MatcherAssert.assertThat;
43import static org.hamcrest.core.Is.is;
44
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +053045/**
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 Huawei41547ad2016-07-14 17:49:50 +053060 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeStatement.yang");
Mahesh Poojary Huawei2cd44332016-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(),
Bharat saraswal0663aff2016-10-18 23:16:14 +053079 is(2));
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +053080 }
81
82 /**
83 * Checks decimal64 statement with range statement.
84 */
85 @Test
86 public void processDecimal64TypeWithRangeStatement() throws IOException, ParserException {
87
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +053088 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeWithRangeStatement.yang");
Mahesh Poojary Huawei2cd44332016-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 Huawei41547ad2016-07-14 17:49:50 +0530126 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeValidation.yang");
Mahesh Poojary Huawei2cd44332016-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));
Mahesh Poojary S6986df32016-07-19 10:58:07 +0530148 decimal64.validateDecimal64();
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530149 decimal64.setValue(new BigDecimal(9.223372036854775807));
Mahesh Poojary S6986df32016-07-19 10:58:07 +0530150 decimal64.validateDecimal64();
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530151 }
152
153 /**
154 * Failure validation of decimal64 statement.
155 */
156 @Test
157 public void processDecimal64ValueFailureValidation() throws IOException, ParserException, DataModelException {
158 thrown.expect(DataModelException.class);
sonu gupta7303cf92016-11-15 17:08:35 +0530159 thrown.expectMessage(
160 "YANG file error : value is not in decimal64 range.");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530161
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530162 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeValidation.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530163
164 // Check whether the data model tree returned is of type module.
165 assertThat((node instanceof YangModule), is(true));
166
167 // Check whether the node type is set properly to module.
168 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
169
170 // Check whether the module name is set correctly.
171 YangModule yangNode = (YangModule) node;
172 assertThat(yangNode.getName(), is("Test"));
173
174 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
175 YangLeaf leafInfo = leafIterator.next();
176 YangDecimal64 decimal64 = (YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo();
177
178 assertThat(leafInfo.getName(), is("validDecimal"));
179 assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
180 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
181 assertThat(decimal64.getFractionDigit(), is(18));
182
183 decimal64.setValue(new BigDecimal(-92233720368547758.08));
184 // validation should fail
Mahesh Poojary S6986df32016-07-19 10:58:07 +0530185 decimal64.validateDecimal64();
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530186 }
187
188 /**
189 * Validation of invalid maximum value limit of fraction-digits.
190 */
191 @Test
192 public void processDecimal64InvalidMaxFraction() throws IOException, ParserException, DataModelException {
193 thrown.expect(ParserException.class);
194 thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
195
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530196 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidMaxValueFraction.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530197 }
198
199 /**
200 * Validation of invalid (0) minimum value limit of fraction-digits.
201 */
202 @Test
203 public void processDecimal64InvalidMinFraction1() throws IOException, ParserException, DataModelException {
204 thrown.expect(ParserException.class);
205 thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
206
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530207 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidMinValueFraction1.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530208 }
209
210 /**
211 * Validation of invalid (-1) minimum value limit of fraction-digits.
212 */
213 @Test
214 public void processDecimal64InvalidMinFraction2() throws IOException, ParserException, DataModelException {
215 thrown.expect(ParserException.class);
216 thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18.");
217
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530218 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidMinValueFraction2.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530219 }
220
221 /**
222 * Validation of decimal64 range statement.
223 */
224 @Test
225 public void processDecimal64TypeWithMultiValueRangeStatement() throws IOException, ParserException {
226
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530227 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeWithMultiValueRangeStmnt.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530228
229 // Check whether the data model tree returned is of type module.
230 assertThat((node instanceof YangModule), is(true));
231
232 // Check whether the node type is set properly to module.
233 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
234
235 // Check whether the module name is set correctly.
236 YangModule yangNode = (YangModule) node;
237 assertThat(yangNode.getName(), is("Test"));
238
239 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
240 YangLeaf leafInfo = leafIterator.next();
241
242 assertThat(leafInfo.getName(), is("validDecimal"));
243 assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64"));
244 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64));
245 assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(),
246 is(18));
247
248 YangRangeRestriction rangeRestriction = ((YangDecimal64<YangRangeRestriction>) leafInfo.getDataType()
249 .getDataTypeExtendedInfo())
250 .getRangeRestrictedExtendedInfo();
251
252 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
253 .listIterator();
254 // range "1 .. 3.14 | 10 | 20..max";
255 // check first range 1 .. 3.14
256 YangRangeInterval rangeInterval = rangeListIterator.next();
257 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(-9.22));
258 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(7.22));
259 // check second range 10
260 rangeInterval = rangeListIterator.next();
261 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(8.0));
262 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(8.0));
263 // check third range 20..max
264 rangeInterval = rangeListIterator.next();
265 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(9.0));
266 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(9.223372036854776));
267 }
268
269 /**
270 * Validation of decimal64 with invalid range.
271 */
272 @Test
273 public void processDecimal64InvalidRange() throws IOException, ParserException, DataModelException {
274 thrown.expect(ParserException.class);
Bharat saraswal0663aff2016-10-18 23:16:14 +0530275 thrown.expectMessage(
276 "YANG file error : decimal64 validation failed.decimal64 in 7 at 12" +
277 " in src/test/resources/decimal64/Decimal64TypeInvalidRangeStmnt.yang\"");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530278
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530279 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidRangeStmnt.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530280 }
281
282 /**
283 * Validation of decimal64 without fraction-digits. Fraction-digits must be present for decimal64.
284 */
285 @Test
286 public void processDecimal64WithoutFraction() throws IOException, ParserException, DataModelException {
287 thrown.expect(ParserException.class);
288 thrown.expectMessage("YANG file error : a type decimal64 must have fraction-digits statement.");
289
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530290 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeWithoutFraction.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530291 }
292
293 /**
294 * Checks decimal64 with typedef statement.
295 */
296 @Test
297 public void processDecimal64TypedefStatement() throws IOException, ParserException {
298
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530299 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypedefStatement.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530300
301 // Check whether the data model tree returned is of type module.
302 assertThat((node instanceof YangModule), is(true));
303
304 // Check whether the node type is set properly to module.
305 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
306
307 // Check whether the module name is set correctly.
308 YangModule yangNode = (YangModule) node;
309 assertThat(yangNode.getName(), is("Test"));
310
311 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
312 YangLeaf leafInfo = leafIterator.next();
313
314 assertThat(leafInfo.getName(), is("setFourDecimal"));
315 assertThat(leafInfo.getDataType().getDataTypeName(), is("validDecimal"));
316 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
317 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
318 YangTypeDef typedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
319 assertThat(typedef.getName(), is("validDecimal"));
320
321 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
322 YangType type = typedef.getTypeList().iterator().next();
323 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
324 assertThat(type.getDataTypeName(), is("decimal64"));
325 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) typedef.getTypeList().iterator().next();
326 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
327 assertThat(decimal64.getFractionDigit(), is(4));
328 }
329
330 /**
331 * Checks decimal64 with multiple typedef statement.
332 */
333 @Test
334 public void processDecimal64MultiTypedefStatement() throws IOException, ParserException {
335
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530336 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefStatement.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530337
338 // Check whether the data model tree returned is of type module.
339 assertThat((node instanceof YangModule), is(true));
340
341 // Check whether the node type is set properly to module.
342 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
343
344 // Check whether the module name is set correctly.
345 YangModule yangNode = (YangModule) node;
346 assertThat(yangNode.getName(), is("Test"));
347
348 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
349 YangLeaf leafInfo = leafIterator.next();
350
351 // check leaf type
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530352 assertThat(leafInfo.getName(), is("lowerDecimal"));
353 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530354 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
355 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
356 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
357
358 // check previous typedef
359 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530360 assertThat(prevTypedef.getName(), is("midDecimal"));
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530361 YangType type = prevTypedef.getTypeList().iterator().next();
362 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
363 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
364
365 // check top typedef
366 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
367 assertThat(topTypedef.getName(), is("topDecimal"));
368 type = topTypedef.getTypeList().iterator().next();
369 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
370 assertThat(type.getDataTypeName(), is("decimal64"));
371 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
372 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
373 assertThat(decimal64.getFractionDigit(), is(4));
374 }
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530375
376 /**
377 * Checks decimal64 with multiple typedef with single range statement.
378 * Range value in typedef statement.
379 */
380 @Test
381 public void processDecimal64MultiTypedefRangeStatement() throws IOException, ParserException {
382
383 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefRangeStatement.yang");
384
385 // Check whether the data model tree returned is of type module.
386 assertThat((node instanceof YangModule), is(true));
387
388 // Check whether the node type is set properly to module.
389 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
390
391 // Check whether the module name is set correctly.
392 YangModule yangNode = (YangModule) node;
393 assertThat(yangNode.getName(), is("Test"));
394
395 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
396 YangLeaf leafInfo = leafIterator.next();
397
398 // check leaf type
399 assertThat(leafInfo.getName(), is("lowerDecimal"));
400 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
401 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
402 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
403 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
404
405 // Check range restriction
406 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
407 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
408 .listIterator();
409 YangRangeInterval rangeInterval = rangeListIterator.next();
410 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
411 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
412
413 // check previous typedef
414 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
415 assertThat(prevTypedef.getName(), is("midDecimal"));
416 YangType type = prevTypedef.getTypeList().iterator().next();
417 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
418 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
419
420 // check top typedef
421 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
422 assertThat(topTypedef.getName(), is("topDecimal"));
423 type = topTypedef.getTypeList().iterator().next();
424 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
425 assertThat(type.getDataTypeName(), is("decimal64"));
426 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
427 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
428 assertThat(decimal64.getFractionDigit(), is(4));
429
430 // Check range restriction
431 rangeRestriction = (YangRangeRestriction) decimal64.getRangeRestrictedExtendedInfo();
432 rangeListIterator = rangeRestriction.getAscendingRangeIntervals().listIterator();
433 rangeInterval = rangeListIterator.next();
434 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
435 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
436 }
437
438 /**
439 * Checks decimal64 with multiple typedef with single range statement.
440 * Range value in leaf statement.
441 */
442 @Test
443 public void processDecimal64MultiTypedefRangeInLeafStatement() throws IOException, ParserException {
444
445 YangNode node = manager
446 .getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefRangeInLeafStatement.yang");
447
448 // Check whether the data model tree returned is of type module.
449 assertThat((node instanceof YangModule), is(true));
450
451 // Check whether the node type is set properly to module.
452 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
453
454 // Check whether the module name is set correctly.
455 YangModule yangNode = (YangModule) node;
456 assertThat(yangNode.getName(), is("Test"));
457
458 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
459 YangLeaf leafInfo = leafIterator.next();
460
461 // check leaf type
462 assertThat(leafInfo.getName(), is("lowerDecimal"));
463 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
464 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
465 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
466 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
467
468 // Check range restriction
469 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
470 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
471 .listIterator();
472 YangRangeInterval rangeInterval = rangeListIterator.next();
473 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
474 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
475
476 // check previous typedef
477 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
478 assertThat(prevTypedef.getName(), is("midDecimal"));
479 YangType type = prevTypedef.getTypeList().iterator().next();
480 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
481 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
482
483 // check top typedef
484 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
485 assertThat(topTypedef.getName(), is("topDecimal"));
486 type = topTypedef.getTypeList().iterator().next();
487 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
488 assertThat(type.getDataTypeName(), is("decimal64"));
489 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
490 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
491 assertThat(decimal64.getFractionDigit(), is(4));
492 }
493
494 /**
495 * Checks decimal64 with multiple typedef with multiple range statement.
496 * Having more restricted range at leaf.
497 */
498 @Test
499 public void processDecimal64MultiTypedefMultipleRangeStatement() throws IOException, ParserException {
500
501 YangNode node = manager
502 .getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefMultiRangeStatement.yang");
503
504 // Check whether the data model tree returned is of type module.
505 assertThat((node instanceof YangModule), is(true));
506
507 // Check whether the node type is set properly to module.
508 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
509
510 // Check whether the module name is set correctly.
511 YangModule yangNode = (YangModule) node;
512 assertThat(yangNode.getName(), is("Test"));
513
514 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
515 YangLeaf leafInfo = leafIterator.next();
516
517 // check leaf type
518 assertThat(leafInfo.getName(), is("lowerDecimal"));
519 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
520 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
521 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
522 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
523
524 // Check range restriction
525 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
526
527 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
528 .listIterator();
529 YangRangeInterval rangeInterval = rangeListIterator.next();
530 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(4.0));
531 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(11.0));
532
533 // check previous typedef
534 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
535 assertThat(prevTypedef.getName(), is("midDecimal"));
536 YangType type = prevTypedef.getTypeList().iterator().next();
537 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
538 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
539
540 // check top typedef
541 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
542 assertThat(topTypedef.getName(), is("topDecimal"));
543 type = topTypedef.getTypeList().iterator().next();
544 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
545 assertThat(type.getDataTypeName(), is("decimal64"));
546 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
547 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
548 assertThat(decimal64.getFractionDigit(), is(4));
549
550 // Check range restriction
551 rangeRestriction = (YangRangeRestriction) decimal64.getRangeRestrictedExtendedInfo();
552 rangeListIterator = rangeRestriction.getAscendingRangeIntervals().listIterator();
553 rangeInterval = rangeListIterator.next();
554 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
555 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
556 }
557
558 /**
559 * Checks decimal64 with multiple typedef with multiple range statement.
560 * But having more restricted range at top of typedef.
561 */
562 @Test
563 public void processDecimal64MultiTypedefMultiInvalidRangeStatement() throws IOException, LinkerException {
564 thrown.expect(LinkerException.class);
Bharat saraswal0663aff2016-10-18 23:16:14 +0530565 thrown.expectMessage(
566 "Range interval doesn't fall within the referred restriction ranges" +
567 " restriction ranges. in 0 at 0 in src/test/resources/decimal64/" +
568 "Decimal64MultiTypedefMultiInvalidRangeStatement.yang\"type." +
569 " in 19 at 12 in src/test/resources/decimal64/Decimal64MultiTypedef" +
570 "MultiInvalidRangeStatement.yang");
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530571
572 manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefMultiInvalidRangeStatement.yang");
573 }
574
575 /**
576 * Checks decimal64 with multiple typedef with max range statement.
577 */
578 @Test
579 public void processDecimal64MultiTypedefWithMaxRange() throws IOException, ParserException {
580
581 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefWithMaxRange.yang");
582
583 // Check whether the data model tree returned is of type module.
584 assertThat((node instanceof YangModule), is(true));
585
586 // Check whether the node type is set properly to module.
587 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
588
589 // Check whether the module name is set correctly.
590 YangModule yangNode = (YangModule) node;
591 assertThat(yangNode.getName(), is("Test"));
592
593 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
594 YangLeaf leafInfo = leafIterator.next();
595
596 // check leaf type
597 assertThat(leafInfo.getName(), is("lowerDecimal"));
598 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
599 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
600 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
601 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
602
603 // Check range restriction
604 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
605
606 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
607 .listIterator();
608 YangRangeInterval rangeInterval = rangeListIterator.next();
609 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(4.0));
610 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
611
612 // check previous typedef
613 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
614 assertThat(prevTypedef.getName(), is("midDecimal"));
615 YangType type = prevTypedef.getTypeList().iterator().next();
616 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
617 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
618
619 // check top typedef
620 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
621 assertThat(topTypedef.getName(), is("topDecimal"));
622 type = topTypedef.getTypeList().iterator().next();
623 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
624 assertThat(type.getDataTypeName(), is("decimal64"));
625 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
626 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
627 assertThat(decimal64.getFractionDigit(), is(4));
628
629 // Check range restriction
630 rangeRestriction = (YangRangeRestriction) decimal64.getRangeRestrictedExtendedInfo();
631 rangeListIterator = rangeRestriction.getAscendingRangeIntervals().listIterator();
632 rangeInterval = rangeListIterator.next();
633 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
634 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
635 }
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530636}