blob: 30e61e3b63fae8763b14345eab114c563aa5682f [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);
159 thrown.expectMessage("YANG file error : decimal64 validation failed.");
160
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530161 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeValidation.yang");
Mahesh Poojary Huawei2cd44332016-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
Mahesh Poojary S6986df32016-07-19 10:58:07 +0530184 decimal64.validateDecimal64();
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530185 }
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 Huawei41547ad2016-07-14 17:49:50 +0530195 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidMaxValueFraction.yang");
Mahesh Poojary Huawei2cd44332016-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 Huawei41547ad2016-07-14 17:49:50 +0530206 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidMinValueFraction1.yang");
Mahesh Poojary Huawei2cd44332016-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 Huawei41547ad2016-07-14 17:49:50 +0530217 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidMinValueFraction2.yang");
Mahesh Poojary Huawei2cd44332016-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 Huawei41547ad2016-07-14 17:49:50 +0530226 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypeWithMultiValueRangeStmnt.yang");
Mahesh Poojary Huawei2cd44332016-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);
Bharat saraswal0663aff2016-10-18 23:16:14 +0530274 thrown.expectMessage(
275 "YANG file error : decimal64 validation failed.decimal64 in 7 at 12" +
276 " in src/test/resources/decimal64/Decimal64TypeInvalidRangeStmnt.yang\"");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530277
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530278 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeInvalidRangeStmnt.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530279 }
280
281 /**
282 * Validation of decimal64 without fraction-digits. Fraction-digits must be present for decimal64.
283 */
284 @Test
285 public void processDecimal64WithoutFraction() throws IOException, ParserException, DataModelException {
286 thrown.expect(ParserException.class);
287 thrown.expectMessage("YANG file error : a type decimal64 must have fraction-digits statement.");
288
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530289 manager.getDataModel("src/test/resources/decimal64/Decimal64TypeWithoutFraction.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530290 }
291
292 /**
293 * Checks decimal64 with typedef statement.
294 */
295 @Test
296 public void processDecimal64TypedefStatement() throws IOException, ParserException {
297
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530298 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64TypedefStatement.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530299
300 // Check whether the data model tree returned is of type module.
301 assertThat((node instanceof YangModule), is(true));
302
303 // Check whether the node type is set properly to module.
304 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
305
306 // Check whether the module name is set correctly.
307 YangModule yangNode = (YangModule) node;
308 assertThat(yangNode.getName(), is("Test"));
309
310 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
311 YangLeaf leafInfo = leafIterator.next();
312
313 assertThat(leafInfo.getName(), is("setFourDecimal"));
314 assertThat(leafInfo.getDataType().getDataTypeName(), is("validDecimal"));
315 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
316 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
317 YangTypeDef typedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
318 assertThat(typedef.getName(), is("validDecimal"));
319
320 assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED));
321 YangType type = typedef.getTypeList().iterator().next();
322 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
323 assertThat(type.getDataTypeName(), is("decimal64"));
324 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) typedef.getTypeList().iterator().next();
325 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
326 assertThat(decimal64.getFractionDigit(), is(4));
327 }
328
329 /**
330 * Checks decimal64 with multiple typedef statement.
331 */
332 @Test
333 public void processDecimal64MultiTypedefStatement() throws IOException, ParserException {
334
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530335 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefStatement.yang");
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530336
337 // Check whether the data model tree returned is of type module.
338 assertThat((node instanceof YangModule), is(true));
339
340 // Check whether the node type is set properly to module.
341 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
342
343 // Check whether the module name is set correctly.
344 YangModule yangNode = (YangModule) node;
345 assertThat(yangNode.getName(), is("Test"));
346
347 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
348 YangLeaf leafInfo = leafIterator.next();
349
350 // check leaf type
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530351 assertThat(leafInfo.getName(), is("lowerDecimal"));
352 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530353 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
354 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
355 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
356
357 // check previous typedef
358 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530359 assertThat(prevTypedef.getName(), is("midDecimal"));
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530360 YangType type = prevTypedef.getTypeList().iterator().next();
361 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
362 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
363
364 // check top typedef
365 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
366 assertThat(topTypedef.getName(), is("topDecimal"));
367 type = topTypedef.getTypeList().iterator().next();
368 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
369 assertThat(type.getDataTypeName(), is("decimal64"));
370 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
371 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
372 assertThat(decimal64.getFractionDigit(), is(4));
373 }
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530374
375 /**
376 * Checks decimal64 with multiple typedef with single range statement.
377 * Range value in typedef statement.
378 */
379 @Test
380 public void processDecimal64MultiTypedefRangeStatement() throws IOException, ParserException {
381
382 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefRangeStatement.yang");
383
384 // Check whether the data model tree returned is of type module.
385 assertThat((node instanceof YangModule), is(true));
386
387 // Check whether the node type is set properly to module.
388 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
389
390 // Check whether the module name is set correctly.
391 YangModule yangNode = (YangModule) node;
392 assertThat(yangNode.getName(), is("Test"));
393
394 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
395 YangLeaf leafInfo = leafIterator.next();
396
397 // check leaf type
398 assertThat(leafInfo.getName(), is("lowerDecimal"));
399 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
400 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
401 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
402 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
403
404 // Check range restriction
405 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
406 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
407 .listIterator();
408 YangRangeInterval rangeInterval = rangeListIterator.next();
409 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
410 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
411
412 // check previous typedef
413 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
414 assertThat(prevTypedef.getName(), is("midDecimal"));
415 YangType type = prevTypedef.getTypeList().iterator().next();
416 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
417 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
418
419 // check top typedef
420 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
421 assertThat(topTypedef.getName(), is("topDecimal"));
422 type = topTypedef.getTypeList().iterator().next();
423 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
424 assertThat(type.getDataTypeName(), is("decimal64"));
425 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
426 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
427 assertThat(decimal64.getFractionDigit(), is(4));
428
429 // Check range restriction
430 rangeRestriction = (YangRangeRestriction) decimal64.getRangeRestrictedExtendedInfo();
431 rangeListIterator = rangeRestriction.getAscendingRangeIntervals().listIterator();
432 rangeInterval = rangeListIterator.next();
433 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
434 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
435 }
436
437 /**
438 * Checks decimal64 with multiple typedef with single range statement.
439 * Range value in leaf statement.
440 */
441 @Test
442 public void processDecimal64MultiTypedefRangeInLeafStatement() throws IOException, ParserException {
443
444 YangNode node = manager
445 .getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefRangeInLeafStatement.yang");
446
447 // Check whether the data model tree returned is of type module.
448 assertThat((node instanceof YangModule), is(true));
449
450 // Check whether the node type is set properly to module.
451 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
452
453 // Check whether the module name is set correctly.
454 YangModule yangNode = (YangModule) node;
455 assertThat(yangNode.getName(), is("Test"));
456
457 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
458 YangLeaf leafInfo = leafIterator.next();
459
460 // check leaf type
461 assertThat(leafInfo.getName(), is("lowerDecimal"));
462 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
463 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
464 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
465 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
466
467 // Check range restriction
468 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
469 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
470 .listIterator();
471 YangRangeInterval rangeInterval = rangeListIterator.next();
472 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
473 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
474
475 // check previous typedef
476 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
477 assertThat(prevTypedef.getName(), is("midDecimal"));
478 YangType type = prevTypedef.getTypeList().iterator().next();
479 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
480 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
481
482 // check top typedef
483 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
484 assertThat(topTypedef.getName(), is("topDecimal"));
485 type = topTypedef.getTypeList().iterator().next();
486 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
487 assertThat(type.getDataTypeName(), is("decimal64"));
488 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
489 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
490 assertThat(decimal64.getFractionDigit(), is(4));
491 }
492
493 /**
494 * Checks decimal64 with multiple typedef with multiple range statement.
495 * Having more restricted range at leaf.
496 */
497 @Test
498 public void processDecimal64MultiTypedefMultipleRangeStatement() throws IOException, ParserException {
499
500 YangNode node = manager
501 .getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefMultiRangeStatement.yang");
502
503 // Check whether the data model tree returned is of type module.
504 assertThat((node instanceof YangModule), is(true));
505
506 // Check whether the node type is set properly to module.
507 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
508
509 // Check whether the module name is set correctly.
510 YangModule yangNode = (YangModule) node;
511 assertThat(yangNode.getName(), is("Test"));
512
513 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
514 YangLeaf leafInfo = leafIterator.next();
515
516 // check leaf type
517 assertThat(leafInfo.getName(), is("lowerDecimal"));
518 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
519 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
520 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
521 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
522
523 // Check range restriction
524 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
525
526 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
527 .listIterator();
528 YangRangeInterval rangeInterval = rangeListIterator.next();
529 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(4.0));
530 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(11.0));
531
532 // check previous typedef
533 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
534 assertThat(prevTypedef.getName(), is("midDecimal"));
535 YangType type = prevTypedef.getTypeList().iterator().next();
536 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
537 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
538
539 // check top typedef
540 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
541 assertThat(topTypedef.getName(), is("topDecimal"));
542 type = topTypedef.getTypeList().iterator().next();
543 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
544 assertThat(type.getDataTypeName(), is("decimal64"));
545 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
546 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
547 assertThat(decimal64.getFractionDigit(), is(4));
548
549 // Check range restriction
550 rangeRestriction = (YangRangeRestriction) decimal64.getRangeRestrictedExtendedInfo();
551 rangeListIterator = rangeRestriction.getAscendingRangeIntervals().listIterator();
552 rangeInterval = rangeListIterator.next();
553 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
554 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
555 }
556
557 /**
558 * Checks decimal64 with multiple typedef with multiple range statement.
559 * But having more restricted range at top of typedef.
560 */
561 @Test
562 public void processDecimal64MultiTypedefMultiInvalidRangeStatement() throws IOException, LinkerException {
563 thrown.expect(LinkerException.class);
Bharat saraswal0663aff2016-10-18 23:16:14 +0530564 thrown.expectMessage(
565 "Range interval doesn't fall within the referred restriction ranges" +
566 " restriction ranges. in 0 at 0 in src/test/resources/decimal64/" +
567 "Decimal64MultiTypedefMultiInvalidRangeStatement.yang\"type." +
568 " in 19 at 12 in src/test/resources/decimal64/Decimal64MultiTypedef" +
569 "MultiInvalidRangeStatement.yang");
Mahesh Poojary Huawei41547ad2016-07-14 17:49:50 +0530570
571 manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefMultiInvalidRangeStatement.yang");
572 }
573
574 /**
575 * Checks decimal64 with multiple typedef with max range statement.
576 */
577 @Test
578 public void processDecimal64MultiTypedefWithMaxRange() throws IOException, ParserException {
579
580 YangNode node = manager.getDataModel("src/test/resources/decimal64/Decimal64MultiTypedefWithMaxRange.yang");
581
582 // Check whether the data model tree returned is of type module.
583 assertThat((node instanceof YangModule), is(true));
584
585 // Check whether the node type is set properly to module.
586 assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
587
588 // Check whether the module name is set correctly.
589 YangModule yangNode = (YangModule) node;
590 assertThat(yangNode.getName(), is("Test"));
591
592 ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
593 YangLeaf leafInfo = leafIterator.next();
594
595 // check leaf type
596 assertThat(leafInfo.getName(), is("lowerDecimal"));
597 assertThat(leafInfo.getDataType().getDataTypeName(), is("midDecimal"));
598 YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType();
599 assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED));
600 YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo();
601
602 // Check range restriction
603 YangRangeRestriction rangeRestriction = (YangRangeRestriction) derivedInfo.getResolvedExtendedInfo();
604
605 ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
606 .listIterator();
607 YangRangeInterval rangeInterval = rangeListIterator.next();
608 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(4.0));
609 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
610
611 // check previous typedef
612 YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
613 assertThat(prevTypedef.getName(), is("midDecimal"));
614 YangType type = prevTypedef.getTypeList().iterator().next();
615 assertThat(type.getDataType(), is(YangDataTypes.DERIVED));
616 derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo();
617
618 // check top typedef
619 YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef();
620 assertThat(topTypedef.getName(), is("topDecimal"));
621 type = topTypedef.getTypeList().iterator().next();
622 assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64));
623 assertThat(type.getDataTypeName(), is("decimal64"));
624 YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type;
625 YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo();
626 assertThat(decimal64.getFractionDigit(), is(4));
627
628 // Check range restriction
629 rangeRestriction = (YangRangeRestriction) decimal64.getRangeRestrictedExtendedInfo();
630 rangeListIterator = rangeRestriction.getAscendingRangeIntervals().listIterator();
631 rangeInterval = rangeListIterator.next();
632 assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(1.0));
633 assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(12.0));
634 }
Mahesh Poojary Huawei2cd44332016-07-14 12:38:17 +0530635}