Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 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 | import org.junit.Test; |
| 22 | import org.onosproject.yangutils.datamodel.YangBit; |
| 23 | import org.onosproject.yangutils.datamodel.YangBits; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 26 | import org.onosproject.yangutils.datamodel.YangModule; |
| 27 | import org.onosproject.yangutils.datamodel.YangNode; |
| 28 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 29 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 30 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 31 | |
| 32 | import java.io.IOException; |
| 33 | import java.util.ListIterator; |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 34 | import java.util.Map; |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Test cases for position listener. |
| 38 | */ |
| 39 | public class PositionListenerTest { |
| 40 | |
| 41 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 42 | |
| 43 | /** |
| 44 | * Checks explicitly configured value. |
| 45 | */ |
| 46 | @Test |
| 47 | public void processPositionStatement() throws IOException, ParserException { |
| 48 | |
| 49 | YangNode node = manager.getDataModel("src/test/resources/PositionStatement.yang"); |
| 50 | |
| 51 | // Check whether the data model tree returned is of type module. |
| 52 | assertThat((node instanceof YangModule), is(true)); |
| 53 | |
| 54 | // Check whether the node type is set properly to module. |
| 55 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 56 | |
| 57 | // Check whether the module name is set correctly. |
| 58 | YangModule yangNode = (YangModule) node; |
| 59 | assertThat(yangNode.getName(), is("Test")); |
| 60 | |
| 61 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 62 | YangLeaf leafInfo = leafIterator.next(); |
| 63 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 64 | assertThat(leafInfo.getName(), is("mybits")); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 65 | assertThat(leafInfo.getDataType().getDataTypeName(), is("bits")); |
| 66 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS)); |
| 67 | assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(), |
| 68 | is("mybits")); |
| 69 | |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 70 | // Check bit name map |
| 71 | Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap(); |
| 72 | assertThat(bitNameMap.size(), is(3)); |
| 73 | for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) { |
| 74 | String bitName = element.getKey(); |
| 75 | YangBit yangBit = element.getValue(); |
| 76 | if (bitName.equals("disable-nagle")) { |
| 77 | assertThat(yangBit.getPosition(), is(0)); |
| 78 | } else if (bitName.equals("auto-sense-speed")) { |
| 79 | assertThat(yangBit.getPosition(), is(1)); |
| 80 | } else if (bitName.equals("Ten-Mb-only")) { |
| 81 | assertThat(yangBit.getPosition(), is(2)); |
| 82 | } else { |
| 83 | throw new IOException("Invalid bit name: " + bitName); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Check bit position map |
| 88 | Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()) |
| 89 | .getBitPositionMap(); |
| 90 | assertThat(bitPositionMap.size(), is(3)); |
| 91 | for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) { |
| 92 | int position = element.getKey(); |
| 93 | YangBit yangBit = element.getValue(); |
| 94 | if (position == 0) { |
| 95 | assertThat(yangBit.getBitName(), is("disable-nagle")); |
| 96 | } else if (position == 1) { |
| 97 | assertThat(yangBit.getBitName(), is("auto-sense-speed")); |
| 98 | } else if (position == 2) { |
| 99 | assertThat(yangBit.getBitName(), is("Ten-Mb-only")); |
| 100 | } else { |
| 101 | throw new IOException("Invalid bit position: " + position); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 107 | * Checks position value with double quotes. |
| 108 | */ |
| 109 | @Test |
| 110 | public void processPositionWithDoubleQuotes() throws IOException, ParserException { |
| 111 | |
| 112 | YangNode node = manager.getDataModel("src/test/resources/PositionWithDoubleQuotes.yang"); |
| 113 | |
| 114 | // Check whether the data model tree returned is of type module. |
| 115 | assertThat((node instanceof YangModule), is(true)); |
| 116 | |
| 117 | // Check whether the node type is set properly to module. |
| 118 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 119 | |
| 120 | // Check whether the module name is set correctly. |
| 121 | YangModule yangNode = (YangModule) node; |
| 122 | assertThat(yangNode.getName(), is("Test")); |
| 123 | |
| 124 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 125 | YangLeaf leafInfo = leafIterator.next(); |
| 126 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 127 | assertThat(leafInfo.getName(), is("mybits")); |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 128 | assertThat(leafInfo.getDataType().getDataTypeName(), is("bits")); |
| 129 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS)); |
| 130 | assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(), |
| 131 | is("mybits")); |
| 132 | |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 133 | // Check bit name map |
| 134 | Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap(); |
| 135 | assertThat(bitNameMap.size(), is(3)); |
| 136 | for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) { |
| 137 | String bitName = element.getKey(); |
| 138 | YangBit yangBit = element.getValue(); |
| 139 | if (bitName.equals("disable-nagle")) { |
| 140 | assertThat(yangBit.getPosition(), is(0)); |
| 141 | } else if (bitName.equals("auto-sense-speed")) { |
| 142 | assertThat(yangBit.getPosition(), is(1)); |
| 143 | } else if (bitName.equals("Ten-Mb-only")) { |
| 144 | assertThat(yangBit.getPosition(), is(2)); |
| 145 | } else { |
| 146 | throw new IOException("Invalid bit name: " + bitName); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Check bit position map |
| 151 | Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()) |
| 152 | .getBitPositionMap(); |
| 153 | assertThat(bitPositionMap.size(), is(3)); |
| 154 | for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) { |
| 155 | int position = element.getKey(); |
| 156 | YangBit yangBit = element.getValue(); |
| 157 | if (position == 0) { |
| 158 | assertThat(yangBit.getBitName(), is("disable-nagle")); |
| 159 | } else if (position == 1) { |
| 160 | assertThat(yangBit.getBitName(), is("auto-sense-speed")); |
| 161 | } else if (position == 2) { |
| 162 | assertThat(yangBit.getBitName(), is("Ten-Mb-only")); |
| 163 | } else { |
| 164 | throw new IOException("Invalid bit position: " + position); |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 170 | * Checks explicit value and auto generated value. |
| 171 | */ |
| 172 | @Test |
| 173 | public void processPositionImplicitAndExplicit() throws IOException, ParserException { |
| 174 | |
| 175 | YangNode node = manager.getDataModel("src/test/resources/PositionImplicitAndExplicit.yang"); |
| 176 | |
| 177 | // Check whether the data model tree returned is of type module. |
| 178 | assertThat((node instanceof YangModule), is(true)); |
| 179 | |
| 180 | // Check whether the node type is set properly to module. |
| 181 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 182 | |
| 183 | // Check whether the module name is set correctly. |
| 184 | YangModule yangNode = (YangModule) node; |
| 185 | assertThat(yangNode.getName(), is("Test")); |
| 186 | |
| 187 | ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator(); |
| 188 | YangLeaf leafInfo = leafIterator.next(); |
| 189 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 190 | assertThat(leafInfo.getName(), is("mybits")); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 191 | assertThat(leafInfo.getDataType().getDataTypeName(), is("bits")); |
| 192 | assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS)); |
| 193 | assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(), |
| 194 | is("mybits")); |
| 195 | |
Mahesh Poojary Huawei | 46fb4db | 2016-07-14 12:38:17 +0530 | [diff] [blame] | 196 | // Check bit name map |
| 197 | Map<String, YangBit> bitNameMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitNameMap(); |
| 198 | assertThat(bitNameMap.size(), is(3)); |
| 199 | for (Map.Entry<String, YangBit> element : bitNameMap.entrySet()) { |
| 200 | String bitName = element.getKey(); |
| 201 | YangBit yangBit = element.getValue(); |
| 202 | if (bitName.equals("disable-nagle")) { |
| 203 | assertThat(yangBit.getPosition(), is(0)); |
| 204 | } else if (bitName.equals("auto-sense-speed")) { |
| 205 | assertThat(yangBit.getPosition(), is(1)); |
| 206 | } else if (bitName.equals("Ten-Mb-only")) { |
| 207 | assertThat(yangBit.getPosition(), is(2)); |
| 208 | } else { |
| 209 | throw new IOException("Invalid bit name: " + bitName); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Check bit position map |
| 214 | Map<Integer, YangBit> bitPositionMap = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()) |
| 215 | .getBitPositionMap(); |
| 216 | assertThat(bitPositionMap.size(), is(3)); |
| 217 | for (Map.Entry<Integer, YangBit> element : bitPositionMap.entrySet()) { |
| 218 | int position = element.getKey(); |
| 219 | YangBit yangBit = element.getValue(); |
| 220 | if (position == 0) { |
| 221 | assertThat(yangBit.getBitName(), is("disable-nagle")); |
| 222 | } else if (position == 1) { |
| 223 | assertThat(yangBit.getBitName(), is("auto-sense-speed")); |
| 224 | } else if (position == 2) { |
| 225 | assertThat(yangBit.getBitName(), is("Ten-Mb-only")); |
| 226 | } else { |
| 227 | throw new IOException("Invalid bit position: " + position); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Checks explicit value should not be repeated. |
| 234 | */ |
| 235 | @Test(expected = ParserException.class) |
| 236 | public void processPositionDuplication() throws IOException, ParserException { |
| 237 | |
| 238 | YangNode node = manager.getDataModel("src/test/resources/PositionDuplication.yang"); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Checks explicit or auto generated value should not be repeated. |
| 243 | */ |
| 244 | @Test(expected = ParserException.class) |
| 245 | public void processPositionImplicitAndExplicitDuplication() throws IOException, ParserException { |
| 246 | |
| 247 | YangNode node = manager.getDataModel("src/test/resources/PositionImplicitAndExplicitDuplication.yang"); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Checks if negative value of position is not allowed. |
| 252 | */ |
| 253 | @Test(expected = ParserException.class) |
| 254 | public void processPositionNegativeValue() throws IOException, ParserException { |
| 255 | |
| 256 | YangNode node = manager.getDataModel("src/test/resources/PositionNegativeValue.yang"); |
| 257 | } |
| 258 | } |