Mahesh Poojary Huawei | 2cd4433 | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.parser.impl.listeners; |
| 18 | |
| 19 | import static org.hamcrest.MatcherAssert.assertThat; |
| 20 | import static org.hamcrest.core.Is.is; |
| 21 | |
| 22 | import org.junit.Rule; |
| 23 | import org.junit.Test; |
| 24 | import org.junit.rules.ExpectedException; |
| 25 | import org.onosproject.yangutils.datamodel.YangDecimal64; |
| 26 | import org.onosproject.yangutils.datamodel.YangDerivedInfo; |
| 27 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 28 | import org.onosproject.yangutils.datamodel.YangModule; |
| 29 | import org.onosproject.yangutils.datamodel.YangNode; |
| 30 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 31 | import org.onosproject.yangutils.datamodel.YangRangeRestriction; |
| 32 | import org.onosproject.yangutils.datamodel.YangRangeInterval; |
| 33 | import org.onosproject.yangutils.datamodel.YangType; |
| 34 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 35 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 36 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 37 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 38 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
| 39 | |
| 40 | import java.io.IOException; |
| 41 | import java.math.BigDecimal; |
| 42 | import java.util.ListIterator; |
| 43 | |
| 44 | /** |
| 45 | * Test cases for decimal64 listener. |
| 46 | */ |
| 47 | public class Decimal64ListenerTest { |
| 48 | @Rule |
| 49 | public ExpectedException thrown = ExpectedException.none(); |
| 50 | |
| 51 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 52 | |
| 53 | /** |
| 54 | * Checks decimal64 statement with fraction-digits. |
| 55 | */ |
| 56 | @Test |
| 57 | public void processDecimal64TypeStatement() throws IOException, ParserException { |
| 58 | |
| 59 | YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeStatement.yang"); |
| 60 | |
| 61 | // Check whether the data model tree returned is of type module. |
| 62 | assertThat((node instanceof YangModule), is(true)); |
| 63 | |
| 64 | // Check whether the node type is set properly to module. |
| 65 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 66 | |
| 67 | // Check whether the module name is set correctly. |
| 68 | YangModule yangNode = (YangModule) node; |
| 69 | assertThat(yangNode.getName(), is("Test")); |
| 70 | |
| 71 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 72 | YangLeaf leafInfo = leafIterator.next(); |
| 73 | |
| 74 | assertThat(leafInfo.getName(), is("validDecimal")); |
| 75 | assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64")); |
| 76 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64)); |
| 77 | assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(), |
| 78 | is(2)); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Checks decimal64 statement with range statement. |
| 83 | */ |
| 84 | @Test |
| 85 | public void processDecimal64TypeWithRangeStatement() throws IOException, ParserException { |
| 86 | |
| 87 | YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeWithRangeStatement.yang"); |
| 88 | |
| 89 | // Check whether the data model tree returned is of type module. |
| 90 | assertThat((node instanceof YangModule), is(true)); |
| 91 | |
| 92 | // Check whether the node type is set properly to module. |
| 93 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 94 | |
| 95 | // Check whether the module name is set correctly. |
| 96 | YangModule yangNode = (YangModule) node; |
| 97 | assertThat(yangNode.getName(), is("Test")); |
| 98 | |
| 99 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 100 | YangLeaf leafInfo = leafIterator.next(); |
| 101 | |
| 102 | assertThat(leafInfo.getName(), is("validDecimal")); |
| 103 | assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64")); |
| 104 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64)); |
| 105 | assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(), |
| 106 | is(8)); |
| 107 | |
| 108 | YangRangeRestriction rangeRestriction = ((YangDecimal64<YangRangeRestriction>) leafInfo.getDataType() |
| 109 | .getDataTypeExtendedInfo()) |
| 110 | .getRangeRestrictedExtendedInfo(); |
| 111 | |
| 112 | ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals() |
| 113 | .listIterator(); |
| 114 | YangRangeInterval rangeInterval = rangeListIterator.next(); |
| 115 | assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(-92233720368.54775808)); |
| 116 | assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(92233720368.54775807)); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Successful validation of decimal64 statement. |
| 121 | */ |
| 122 | @Test |
| 123 | public void processDecimal64ValueSuccessfulValidation() throws IOException, ParserException, DataModelException { |
| 124 | |
| 125 | YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeValidation.yang"); |
| 126 | |
| 127 | // Check whether the data model tree returned is of type module. |
| 128 | assertThat((node instanceof YangModule), is(true)); |
| 129 | |
| 130 | // Check whether the node type is set properly to module. |
| 131 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 132 | |
| 133 | // Check whether the module name is set correctly. |
| 134 | YangModule yangNode = (YangModule) node; |
| 135 | assertThat(yangNode.getName(), is("Test")); |
| 136 | |
| 137 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 138 | YangLeaf leafInfo = leafIterator.next(); |
| 139 | YangDecimal64 decimal64 = (YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo(); |
| 140 | |
| 141 | assertThat(leafInfo.getName(), is("validDecimal")); |
| 142 | assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64")); |
| 143 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64)); |
| 144 | assertThat(decimal64.getFractionDigit(), is(18)); |
| 145 | |
| 146 | decimal64.setValue(new BigDecimal(-9.223372036854775808)); |
| 147 | decimal64.validateValue(); |
| 148 | decimal64.setValue(new BigDecimal(9.223372036854775807)); |
| 149 | decimal64.validateValue(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Failure validation of decimal64 statement. |
| 154 | */ |
| 155 | @Test |
| 156 | public void processDecimal64ValueFailureValidation() throws IOException, ParserException, DataModelException { |
| 157 | thrown.expect(DataModelException.class); |
| 158 | thrown.expectMessage("YANG file error : decimal64 validation failed."); |
| 159 | |
| 160 | YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeValidation.yang"); |
| 161 | |
| 162 | // Check whether the data model tree returned is of type module. |
| 163 | assertThat((node instanceof YangModule), is(true)); |
| 164 | |
| 165 | // Check whether the node type is set properly to module. |
| 166 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 167 | |
| 168 | // Check whether the module name is set correctly. |
| 169 | YangModule yangNode = (YangModule) node; |
| 170 | assertThat(yangNode.getName(), is("Test")); |
| 171 | |
| 172 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 173 | YangLeaf leafInfo = leafIterator.next(); |
| 174 | YangDecimal64 decimal64 = (YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo(); |
| 175 | |
| 176 | assertThat(leafInfo.getName(), is("validDecimal")); |
| 177 | assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64")); |
| 178 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64)); |
| 179 | assertThat(decimal64.getFractionDigit(), is(18)); |
| 180 | |
| 181 | decimal64.setValue(new BigDecimal(-92233720368547758.08)); |
| 182 | // validation should fail |
| 183 | decimal64.validateValue(); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Validation of invalid maximum value limit of fraction-digits. |
| 188 | */ |
| 189 | @Test |
| 190 | public void processDecimal64InvalidMaxFraction() throws IOException, ParserException, DataModelException { |
| 191 | thrown.expect(ParserException.class); |
| 192 | thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18."); |
| 193 | |
| 194 | manager.getDataModel("src/test/resources/Decimal64TypeInvalidMaxValueFraction.yang"); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Validation of invalid (0) minimum value limit of fraction-digits. |
| 199 | */ |
| 200 | @Test |
| 201 | public void processDecimal64InvalidMinFraction1() throws IOException, ParserException, DataModelException { |
| 202 | thrown.expect(ParserException.class); |
| 203 | thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18."); |
| 204 | |
| 205 | manager.getDataModel("src/test/resources/Decimal64TypeInvalidMinValueFraction1.yang"); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Validation of invalid (-1) minimum value limit of fraction-digits. |
| 210 | */ |
| 211 | @Test |
| 212 | public void processDecimal64InvalidMinFraction2() throws IOException, ParserException, DataModelException { |
| 213 | thrown.expect(ParserException.class); |
| 214 | thrown.expectMessage("YANG file error : fraction-digits value should be between 1 and 18."); |
| 215 | |
| 216 | manager.getDataModel("src/test/resources/Decimal64TypeInvalidMinValueFraction2.yang"); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Validation of decimal64 range statement. |
| 221 | */ |
| 222 | @Test |
| 223 | public void processDecimal64TypeWithMultiValueRangeStatement() throws IOException, ParserException { |
| 224 | |
| 225 | YangNode node = manager.getDataModel("src/test/resources/Decimal64TypeWithMultiValueRangeStmnt.yang"); |
| 226 | |
| 227 | // Check whether the data model tree returned is of type module. |
| 228 | assertThat((node instanceof YangModule), is(true)); |
| 229 | |
| 230 | // Check whether the node type is set properly to module. |
| 231 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 232 | |
| 233 | // Check whether the module name is set correctly. |
| 234 | YangModule yangNode = (YangModule) node; |
| 235 | assertThat(yangNode.getName(), is("Test")); |
| 236 | |
| 237 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 238 | YangLeaf leafInfo = leafIterator.next(); |
| 239 | |
| 240 | assertThat(leafInfo.getName(), is("validDecimal")); |
| 241 | assertThat(leafInfo.getDataType().getDataTypeName(), is("decimal64")); |
| 242 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DECIMAL64)); |
| 243 | assertThat(((YangDecimal64) leafInfo.getDataType().getDataTypeExtendedInfo()).getFractionDigit(), |
| 244 | is(18)); |
| 245 | |
| 246 | YangRangeRestriction rangeRestriction = ((YangDecimal64<YangRangeRestriction>) leafInfo.getDataType() |
| 247 | .getDataTypeExtendedInfo()) |
| 248 | .getRangeRestrictedExtendedInfo(); |
| 249 | |
| 250 | ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals() |
| 251 | .listIterator(); |
| 252 | // range "1 .. 3.14 | 10 | 20..max"; |
| 253 | // check first range 1 .. 3.14 |
| 254 | YangRangeInterval rangeInterval = rangeListIterator.next(); |
| 255 | assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(-9.22)); |
| 256 | assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(7.22)); |
| 257 | // check second range 10 |
| 258 | rangeInterval = rangeListIterator.next(); |
| 259 | assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(8.0)); |
| 260 | assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(8.0)); |
| 261 | // check third range 20..max |
| 262 | rangeInterval = rangeListIterator.next(); |
| 263 | assertThat(((YangDecimal64) rangeInterval.getStartValue()).getValue().doubleValue(), is(9.0)); |
| 264 | assertThat(((YangDecimal64) rangeInterval.getEndValue()).getValue().doubleValue(), is(9.223372036854776)); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Validation of decimal64 with invalid range. |
| 269 | */ |
| 270 | @Test |
| 271 | public void processDecimal64InvalidRange() throws IOException, ParserException, DataModelException { |
| 272 | thrown.expect(ParserException.class); |
| 273 | thrown.expectMessage("YANG file error : decimal64 validation failed."); |
| 274 | |
| 275 | manager.getDataModel("src/test/resources/Decimal64TypeInvalidRangeStmnt.yang"); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Validation of decimal64 without fraction-digits. Fraction-digits must be present for decimal64. |
| 280 | */ |
| 281 | @Test |
| 282 | public void processDecimal64WithoutFraction() throws IOException, ParserException, DataModelException { |
| 283 | thrown.expect(ParserException.class); |
| 284 | thrown.expectMessage("YANG file error : a type decimal64 must have fraction-digits statement."); |
| 285 | |
| 286 | manager.getDataModel("src/test/resources/Decimal64TypeWithoutFraction.yang"); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Checks decimal64 with typedef statement. |
| 291 | */ |
| 292 | @Test |
| 293 | public void processDecimal64TypedefStatement() throws IOException, ParserException { |
| 294 | |
| 295 | YangNode node = manager.getDataModel("src/test/resources/Decimal64TypedefStatement.yang"); |
| 296 | |
| 297 | // Check whether the data model tree returned is of type module. |
| 298 | assertThat((node instanceof YangModule), is(true)); |
| 299 | |
| 300 | // Check whether the node type is set properly to module. |
| 301 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 302 | |
| 303 | // Check whether the module name is set correctly. |
| 304 | YangModule yangNode = (YangModule) node; |
| 305 | assertThat(yangNode.getName(), is("Test")); |
| 306 | |
| 307 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 308 | YangLeaf leafInfo = leafIterator.next(); |
| 309 | |
| 310 | assertThat(leafInfo.getName(), is("setFourDecimal")); |
| 311 | assertThat(leafInfo.getDataType().getDataTypeName(), is("validDecimal")); |
| 312 | YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType(); |
| 313 | YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo(); |
| 314 | YangTypeDef typedef = (YangTypeDef) derivedInfo.getReferredTypeDef(); |
| 315 | assertThat(typedef.getName(), is("validDecimal")); |
| 316 | |
| 317 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.DERIVED)); |
| 318 | YangType type = typedef.getTypeList().iterator().next(); |
| 319 | assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64)); |
| 320 | assertThat(type.getDataTypeName(), is("decimal64")); |
| 321 | YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) typedef.getTypeList().iterator().next(); |
| 322 | YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo(); |
| 323 | assertThat(decimal64.getFractionDigit(), is(4)); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Checks decimal64 with multiple typedef statement. |
| 328 | */ |
| 329 | @Test |
| 330 | public void processDecimal64MultiTypedefStatement() throws IOException, ParserException { |
| 331 | |
| 332 | YangNode node = manager.getDataModel("src/test/resources/Decimal64MultiTypedefStatement.yang"); |
| 333 | |
| 334 | // Check whether the data model tree returned is of type module. |
| 335 | assertThat((node instanceof YangModule), is(true)); |
| 336 | |
| 337 | // Check whether the node type is set properly to module. |
| 338 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 339 | |
| 340 | // Check whether the module name is set correctly. |
| 341 | YangModule yangNode = (YangModule) node; |
| 342 | assertThat(yangNode.getName(), is("Test")); |
| 343 | |
| 344 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 345 | YangLeaf leafInfo = leafIterator.next(); |
| 346 | |
| 347 | // check leaf type |
| 348 | assertThat(leafInfo.getName(), is("setFourDecimal")); |
| 349 | assertThat(leafInfo.getDataType().getDataTypeName(), is("validDecimal")); |
| 350 | YangType<YangDerivedInfo> derivedInfoType = (YangType<YangDerivedInfo>) leafInfo.getDataType(); |
| 351 | assertThat(derivedInfoType.getDataType(), is(YangDataTypes.DERIVED)); |
| 352 | YangDerivedInfo derivedInfo = (YangDerivedInfo) derivedInfoType.getDataTypeExtendedInfo(); |
| 353 | |
| 354 | // check previous typedef |
| 355 | YangTypeDef prevTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef(); |
| 356 | assertThat(prevTypedef.getName(), is("validDecimal")); |
| 357 | YangType type = prevTypedef.getTypeList().iterator().next(); |
| 358 | assertThat(type.getDataType(), is(YangDataTypes.DERIVED)); |
| 359 | derivedInfo = (YangDerivedInfo) type.getDataTypeExtendedInfo(); |
| 360 | |
| 361 | // check top typedef |
| 362 | YangTypeDef topTypedef = (YangTypeDef) derivedInfo.getReferredTypeDef(); |
| 363 | assertThat(topTypedef.getName(), is("topDecimal")); |
| 364 | type = topTypedef.getTypeList().iterator().next(); |
| 365 | assertThat(type.getDataType(), is(YangDataTypes.DECIMAL64)); |
| 366 | assertThat(type.getDataTypeName(), is("decimal64")); |
| 367 | YangType<YangDecimal64> decimal64Type = (YangType<YangDecimal64>) type; |
| 368 | YangDecimal64 decimal64 = decimal64Type.getDataTypeExtendedInfo(); |
| 369 | assertThat(decimal64.getFractionDigit(), is(4)); |
| 370 | } |
| 371 | } |