Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 java.io.IOException; |
| 20 | import java.math.BigInteger; |
| 21 | import java.util.ListIterator; |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 22 | import org.junit.Rule; |
| 23 | import org.junit.Test; |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 24 | import org.junit.rules.ExpectedException; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 27 | import org.onosproject.yangutils.datamodel.YangLeafList; |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangModule; |
| 29 | import org.onosproject.yangutils.datamodel.YangNode; |
| 30 | import org.onosproject.yangutils.datamodel.YangNodeType; |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.YangRangeInterval; |
| 32 | import org.onosproject.yangutils.datamodel.YangRangeRestriction; |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.datamodel.YangStringRestriction; |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 35 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 36 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint64; |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 38 | |
| 39 | import static org.hamcrest.MatcherAssert.assertThat; |
| 40 | import static org.hamcrest.core.Is.is; |
| 41 | |
| 42 | /** |
| 43 | * Test cases for length restriction listener. |
| 44 | */ |
| 45 | public class LengthRestrictionListenerTest { |
| 46 | |
| 47 | @Rule |
| 48 | public ExpectedException thrown = ExpectedException.none(); |
| 49 | |
| 50 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 51 | |
| 52 | /** |
| 53 | * Checks valid length statement as sub-statement of leaf statement. |
| 54 | */ |
| 55 | @Test |
| 56 | public void processValidLengthStatement() throws IOException, ParserException { |
| 57 | |
| 58 | YangNode node = manager.getDataModel("src/test/resources/ValidLengthStatement.yang"); |
| 59 | |
| 60 | assertThat((node instanceof YangModule), is(true)); |
| 61 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 62 | YangModule yangNode = (YangModule) node; |
| 63 | assertThat(yangNode.getName(), is("Test")); |
| 64 | |
| 65 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 66 | YangLeaf leafInfo = leafIterator.next(); |
| 67 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 68 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 69 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
| 70 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 71 | YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 72 | .getDataType().getDataTypeExtendedInfo(); |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 73 | YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction(); |
| 74 | |
| 75 | ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals() |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 76 | .listIterator(); |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 77 | |
| 78 | YangRangeInterval rangeInterval = lengthListIterator.next(); |
| 79 | |
| 80 | assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0))); |
| 81 | assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100))); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Checks valid length statement as sub-statement of leaf-list. |
| 86 | */ |
| 87 | @Test |
| 88 | public void processLengthStatementInsideLeafList() throws IOException, ParserException { |
| 89 | |
| 90 | YangNode node = manager.getDataModel("src/test/resources/LengthStatementInsideLeafList.yang"); |
| 91 | |
| 92 | assertThat((node instanceof YangModule), is(true)); |
| 93 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 94 | YangModule yangNode = (YangModule) node; |
| 95 | assertThat(yangNode.getName(), is("Test")); |
| 96 | |
| 97 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 98 | YangLeafList leafListInfo = leafListIterator.next(); |
| 99 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 100 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 101 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("string")); |
| 102 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 103 | YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo |
| 104 | .getDataType().getDataTypeExtendedInfo(); |
| 105 | YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction(); |
| 106 | |
| 107 | ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals() |
| 108 | .listIterator(); |
| 109 | |
| 110 | YangRangeInterval rangeInterval = lengthListIterator.next(); |
| 111 | assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(1))); |
| 112 | assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100))); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Checks valid length statement as sub-statement of typedef. |
| 117 | */ |
| 118 | @Test |
| 119 | public void processLengthStatementInsideTypeDef() throws IOException, ParserException { |
| 120 | |
| 121 | YangNode node = manager.getDataModel("src/test/resources/LengthStatementInsideTypeDef.yang"); |
| 122 | |
| 123 | assertThat((node instanceof YangModule), is(true)); |
| 124 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 125 | YangModule yangNode = (YangModule) node; |
| 126 | assertThat(yangNode.getName(), is("Test")); |
| 127 | |
| 128 | YangTypeDef typedef = (YangTypeDef) yangNode.getChild(); |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 129 | YangStringRestriction stringRestriction = (YangStringRestriction) typedef.getTypeDefBaseType() |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 130 | .getDataTypeExtendedInfo(); |
| 131 | |
| 132 | YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction(); |
| 133 | ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals() |
| 134 | .listIterator(); |
| 135 | YangRangeInterval rangeInterval = lengthListIterator.next(); |
| 136 | assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(1))); |
| 137 | assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100))); |
| 138 | } |
| 139 | |
| 140 | /** |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 141 | * Checks valid length statement as sub-statement of binary statement. |
| 142 | */ |
| 143 | @Test |
| 144 | public void processValidBinaryLengthStatement() throws IOException, ParserException { |
| 145 | |
| 146 | YangNode node = manager.getDataModel("src/test/resources/ValidBinaryLengthStatement.yang"); |
| 147 | |
| 148 | assertThat((node instanceof YangModule), is(true)); |
| 149 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 150 | YangModule yangNode = (YangModule) node; |
| 151 | assertThat(yangNode.getName(), is("Test")); |
| 152 | |
| 153 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 154 | YangLeaf leafInfo = leafIterator.next(); |
| 155 | |
| 156 | assertThat(leafInfo.getName(), is("message")); |
| 157 | assertThat(leafInfo.getDataType().getDataTypeName(), is("binary")); |
| 158 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BINARY)); |
| 159 | YangRangeRestriction lengthRestriction = (YangRangeRestriction) leafInfo |
| 160 | .getDataType().getDataTypeExtendedInfo(); |
| 161 | |
| 162 | ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals() |
| 163 | .listIterator(); |
| 164 | |
| 165 | YangRangeInterval rangeInterval = lengthListIterator.next(); |
| 166 | |
| 167 | assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(4))); |
| 168 | assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(4))); |
| 169 | } |
| 170 | |
| 171 | /** |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 172 | * Checks length statement with invalid type. |
| 173 | */ |
| 174 | @Test |
| 175 | public void processLengthWithInvalidType() throws IOException, ParserException { |
| 176 | thrown.expect(ParserException.class); |
| 177 | thrown.expectMessage("YANG file error : length name \"1..100\" can be used to restrict the built-in type" + |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 178 | " string/binary or types derived from string/binary."); |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 179 | YangNode node = manager.getDataModel("src/test/resources/LengthWithInvalidType.yang"); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Checks length statement with only start interval. |
| 184 | */ |
| 185 | @Test |
| 186 | public void processLengthWithOneInterval() throws IOException, ParserException { |
| 187 | |
| 188 | |
| 189 | YangNode node = manager.getDataModel("src/test/resources/LengthWithOneInterval.yang"); |
| 190 | |
| 191 | assertThat((node instanceof YangModule), is(true)); |
| 192 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 193 | YangModule yangNode = (YangModule) node; |
| 194 | assertThat(yangNode.getName(), is("Test")); |
| 195 | |
| 196 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 197 | YangLeafList leafListInfo = leafListIterator.next(); |
| 198 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 199 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 200 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("string")); |
| 201 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 202 | YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo |
| 203 | .getDataType().getDataTypeExtendedInfo(); |
| 204 | YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction(); |
| 205 | |
| 206 | ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals() |
| 207 | .listIterator(); |
| 208 | |
| 209 | YangRangeInterval rangeInterval = lengthListIterator.next(); |
| 210 | assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(1))); |
| 211 | assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(1))); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Checks length statement with min and max. |
| 216 | */ |
| 217 | @Test |
| 218 | public void processLengthWithMinMax() throws IOException, ParserException { |
| 219 | |
| 220 | |
| 221 | YangNode node = manager.getDataModel("src/test/resources/LengthWithMinMax.yang"); |
| 222 | |
| 223 | assertThat((node instanceof YangModule), is(true)); |
| 224 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 225 | YangModule yangNode = (YangModule) node; |
| 226 | assertThat(yangNode.getName(), is("Test")); |
| 227 | |
| 228 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 229 | YangLeafList leafListInfo = leafListIterator.next(); |
| 230 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 231 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 232 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("string")); |
| 233 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 234 | YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo |
| 235 | .getDataType().getDataTypeExtendedInfo(); |
| 236 | YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction(); |
| 237 | |
| 238 | ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals() |
| 239 | .listIterator(); |
| 240 | |
| 241 | YangRangeInterval rangeInterval = lengthListIterator.next(); |
| 242 | assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0))); |
| 243 | assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(new BigInteger("18446744073709551615"))); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Checks length statement with invalid integer pattern. |
| 248 | */ |
| 249 | @Test |
| 250 | public void processLengthWithInvalidIntegerPattern() throws IOException, ParserException { |
| 251 | thrown.expect(ParserException.class); |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 252 | thrown.expectMessage("YANG file error : Input value \"a\" is not a valid uint64."); |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 253 | YangNode node = manager.getDataModel("src/test/resources/LengthWithInvalidIntegerPattern.yang"); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Checks length statement with invalid interval. |
| 258 | */ |
| 259 | @Test |
| 260 | public void processLengthWithInvalidInterval() throws IOException, ParserException { |
| 261 | thrown.expect(ParserException.class); |
| 262 | thrown.expectMessage("YANG file error : 18446744073709551617 is greater than maximum value" + |
| 263 | " 18446744073709551615."); |
| 264 | YangNode node = manager.getDataModel("src/test/resources/LengthWithInvalidInterval.yang"); |
| 265 | } |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 266 | |
| 267 | /** |
| 268 | * Checks valid length substatements. |
| 269 | */ |
| 270 | @Test |
| 271 | public void processLengthSubStatements() throws IOException, ParserException { |
| 272 | |
| 273 | YangNode node = manager.getDataModel("src/test/resources/LengthSubStatements.yang"); |
| 274 | |
| 275 | assertThat((node instanceof YangModule), is(true)); |
| 276 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 277 | YangModule yangNode = (YangModule) node; |
| 278 | assertThat(yangNode.getName(), is("Test")); |
| 279 | |
| 280 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 281 | YangLeaf leafInfo = leafIterator.next(); |
| 282 | |
| 283 | assertThat(leafInfo.getName(), is("invalid-interval")); |
| 284 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
| 285 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 286 | YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo |
| 287 | .getDataType().getDataTypeExtendedInfo(); |
| 288 | YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction(); |
| 289 | |
| 290 | assertThat(lengthRestriction.getDescription(), is("\"length description\"")); |
| 291 | assertThat(lengthRestriction.getReference(), is("\"length reference\"")); |
| 292 | |
| 293 | ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals() |
| 294 | .listIterator(); |
| 295 | |
| 296 | YangRangeInterval rangeInterval = lengthListIterator.next(); |
| 297 | |
| 298 | assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0))); |
| 299 | assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100))); |
| 300 | } |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * Checks whether space can be allowed when length statement is present. |
| 304 | */ |
| 305 | @Test |
| 306 | public void processLengthStatementWithSpace() throws IOException, ParserException { |
| 307 | |
| 308 | YangNode node = manager.getDataModel("src/test/resources/LengthStatementWithSpace.yang"); |
| 309 | |
| 310 | assertThat((node instanceof YangModule), is(true)); |
| 311 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 312 | YangModule yangNode = (YangModule) node; |
| 313 | assertThat(yangNode.getName(), is("Test")); |
| 314 | |
| 315 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 316 | YangLeaf leafInfo = leafIterator.next(); |
| 317 | |
| 318 | assertThat(leafInfo.getName(), is("invalid-interval")); |
| 319 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
| 320 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 321 | YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo |
| 322 | .getDataType().getDataTypeExtendedInfo(); |
| 323 | YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction(); |
| 324 | |
| 325 | ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals() |
| 326 | .listIterator(); |
| 327 | |
| 328 | YangRangeInterval rangeInterval = lengthListIterator.next(); |
| 329 | |
| 330 | assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0))); |
| 331 | assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100))); |
| 332 | } |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 333 | } |