Vidyashree Rama | bc9611f | 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 | |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 19 | import org.junit.Test; |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 21 | import org.onosproject.yangutils.datamodel.YangLeafList; |
Gaurav Agrawal | 92598c5 | 2016-05-20 14:40:50 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangModule; |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
| 24 | import org.onosproject.yangutils.datamodel.YangNodeType; |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangPatternRestriction; |
| 26 | import org.onosproject.yangutils.datamodel.YangStringRestriction; |
Gaurav Agrawal | 92598c5 | 2016-05-20 14:40:50 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 30 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 31 | |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 32 | import java.io.IOException; |
| 33 | import java.util.ListIterator; |
| 34 | |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 35 | import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | import static org.hamcrest.core.Is.is; |
| 37 | |
| 38 | /** |
| 39 | * Test cases for pattern restriction listener. |
| 40 | */ |
| 41 | public class PatternRestrictionListenerTest { |
| 42 | |
| 43 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 44 | |
| 45 | /** |
| 46 | * Checks valid pattern statement as sub-statement of leaf statement. |
| 47 | */ |
| 48 | @Test |
| 49 | public void processValidPatternStatement() throws IOException, ParserException { |
| 50 | |
| 51 | YangNode node = manager.getDataModel("src/test/resources/ValidPatternStatement.yang"); |
| 52 | |
| 53 | assertThat((node instanceof YangModule), is(true)); |
| 54 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 55 | YangModule yangNode = (YangModule) node; |
| 56 | assertThat(yangNode.getName(), is("Test")); |
| 57 | |
| 58 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 59 | YangLeaf leafInfo = leafIterator.next(); |
| 60 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 61 | assertThat(leafInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 62 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
| 63 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 64 | YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo |
| 65 | .getDataType().getDataTypeExtendedInfo(); |
| 66 | ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction() |
| 67 | .getPatternList().listIterator(); |
| 68 | assertThat(patternListIterator.next(), is("[a-zA-Z]")); |
Bharat saraswal | a5c2851 | 2016-11-10 21:09:23 +0530 | [diff] [blame] | 69 | |
| 70 | leafInfo = leafIterator.next(); |
| 71 | |
| 72 | assertThat(leafInfo.getName(), is("ipv4-address")); |
| 73 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
| 74 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 75 | stringRestriction = (YangStringRestriction) leafInfo |
| 76 | .getDataType().getDataTypeExtendedInfo(); |
| 77 | patternListIterator = stringRestriction.getPatternRestriction() |
| 78 | .getPatternList().listIterator(); |
| 79 | assertThat(patternListIterator.next(), is( |
| 80 | "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}" + |
| 81 | "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + |
| 82 | "(%[\\p{N}\\p{L}]+)?")); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Checks valid pattern statement as sub-statement of leaf-list. |
| 87 | */ |
| 88 | @Test |
| 89 | public void processPatternStatementInsideLeafList() throws IOException, ParserException { |
| 90 | |
| 91 | YangNode node = manager.getDataModel("src/test/resources/PatternStatementInsideLeafList.yang"); |
| 92 | |
| 93 | assertThat((node instanceof YangModule), is(true)); |
| 94 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 95 | YangModule yangNode = (YangModule) node; |
| 96 | assertThat(yangNode.getName(), is("Test")); |
| 97 | |
| 98 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 99 | YangLeafList leafListInfo = leafListIterator.next(); |
| 100 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 101 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 102 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("string")); |
| 103 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 104 | YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo |
| 105 | .getDataType().getDataTypeExtendedInfo(); |
| 106 | ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction() |
| 107 | .getPatternList().listIterator(); |
| 108 | assertThat(patternListIterator.next(), is("[a-zA-Z]")); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Checks valid pattern statement as sub-statement of typedef. |
| 113 | */ |
| 114 | @Test |
| 115 | public void processPatternStatementInsideTypeDef() throws IOException, ParserException { |
| 116 | |
| 117 | YangNode node = manager.getDataModel("src/test/resources/PatternStatementInsideTypeDef.yang"); |
| 118 | |
| 119 | assertThat((node instanceof YangModule), is(true)); |
| 120 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 121 | YangModule yangNode = (YangModule) node; |
| 122 | assertThat(yangNode.getName(), is("Test")); |
| 123 | |
| 124 | YangTypeDef typedef = (YangTypeDef) yangNode.getChild(); |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 125 | YangStringRestriction stringRestriction = (YangStringRestriction) typedef.getTypeDefBaseType() |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 126 | .getDataTypeExtendedInfo(); |
| 127 | |
| 128 | YangPatternRestriction yangPatternRestriction = stringRestriction.getPatternRestriction(); |
| 129 | assertThat(yangPatternRestriction.getPatternList().listIterator().next(), is("[a-zA-Z]")); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Checks valid multiple pattern statements. |
| 134 | */ |
| 135 | @Test |
| 136 | public void processMultiplePatternStatement() throws IOException, ParserException { |
| 137 | |
| 138 | YangNode node = manager.getDataModel("src/test/resources/MultiplePatternStatement.yang"); |
| 139 | |
| 140 | assertThat((node instanceof YangModule), is(true)); |
| 141 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 142 | YangModule yangNode = (YangModule) node; |
| 143 | assertThat(yangNode.getName(), is("Test")); |
| 144 | |
| 145 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 146 | YangLeafList leafListInfo = leafListIterator.next(); |
| 147 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 148 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 149 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("string")); |
| 150 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 151 | YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo |
| 152 | .getDataType().getDataTypeExtendedInfo(); |
| 153 | ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction() |
| 154 | .getPatternList().listIterator(); |
| 155 | assertThat(patternListIterator.next(), is("[a-zA-Z]")); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Checks valid pattern statement with plus symbol in pattern. |
| 160 | */ |
| 161 | @Test |
| 162 | public void processPatternStatementWithPlus() throws IOException, ParserException { |
| 163 | |
| 164 | YangNode node = manager.getDataModel("src/test/resources/PatternStatementWithPlus.yang"); |
| 165 | |
| 166 | assertThat((node instanceof YangModule), is(true)); |
| 167 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 168 | YangModule yangNode = (YangModule) node; |
| 169 | assertThat(yangNode.getName(), is("Test")); |
| 170 | |
| 171 | ListIterator<YangLeafList> leafListIterator = yangNode.getListOfLeafList().listIterator(); |
| 172 | YangLeafList leafListInfo = leafListIterator.next(); |
| 173 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 174 | assertThat(leafListInfo.getName(), is("invalid-interval")); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 175 | assertThat(leafListInfo.getDataType().getDataTypeName(), is("string")); |
| 176 | assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 177 | YangStringRestriction stringRestriction = (YangStringRestriction) leafListInfo |
| 178 | .getDataType().getDataTypeExtendedInfo(); |
| 179 | ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction() |
| 180 | .getPatternList().listIterator(); |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 181 | //FIXME: + should not be remove from the end. |
| 182 | //assertThat(patternListIterator.next(), is("-[0-9]+|[0-9]+")); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 183 | } |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * Checks valid pattern substatement. |
| 187 | */ |
| 188 | @Test |
| 189 | public void processPatternSubStatements() throws IOException, ParserException { |
| 190 | |
| 191 | YangNode node = manager.getDataModel("src/test/resources/PatternSubStatements.yang"); |
| 192 | |
| 193 | assertThat((node instanceof YangModule), is(true)); |
| 194 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 195 | YangModule yangNode = (YangModule) node; |
| 196 | assertThat(yangNode.getName(), is("Test")); |
| 197 | |
| 198 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 199 | YangLeaf leafInfo = leafIterator.next(); |
| 200 | |
| 201 | assertThat(leafInfo.getName(), is("invalid-interval")); |
| 202 | assertThat(leafInfo.getDataType().getDataTypeName(), is("string")); |
| 203 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING)); |
| 204 | YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo |
| 205 | .getDataType().getDataTypeExtendedInfo(); |
| 206 | assertThat(stringRestriction.getDescription(), is("\"pattern description\"")); |
| 207 | assertThat(stringRestriction.getReference(), is("\"pattern reference\"")); |
| 208 | ListIterator<String> patternListIterator = stringRestriction.getPatternRestriction() |
| 209 | .getPatternList().listIterator(); |
| 210 | assertThat(patternListIterator.next(), is("[a-zA-Z]")); |
| 211 | } |
Gaurav Agrawal | 92598c5 | 2016-05-20 14:40:50 +0530 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * Checks invalid pattern sub-statement. |
| 215 | */ |
| 216 | @Test(expected = ParserException.class) |
| 217 | public void processInvalidPatternSubStatements() throws IOException, ParserException { |
| 218 | YangNode node = manager.getDataModel("src/test/resources/InvalidPatternSubStatements.yang"); |
| 219 | } |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 220 | } |