Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +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.plugin.manager; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.util.List; |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 21 | import java.util.ListIterator; |
| 22 | |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 23 | import org.junit.Test; |
| 24 | import org.onosproject.yangutils.datamodel.YangContainer; |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangFeature; |
| 27 | import org.onosproject.yangutils.datamodel.YangIfFeature; |
| 28 | import org.onosproject.yangutils.datamodel.YangLeaf; |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangLeafRef; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.YangModule; |
| 31 | import org.onosproject.yangutils.datamodel.YangNode; |
| 32 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 33 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 34 | import org.onosproject.yangutils.datamodel.utils.ResolvableStatus; |
| 35 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 36 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 37 | |
| 38 | import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | import static org.hamcrest.core.Is.is; |
| 40 | |
| 41 | /** |
| 42 | * Test cases for testing if-feature intra file linking. |
| 43 | */ |
| 44 | public class IntraFileIfFeatureLinkingTest { |
| 45 | |
| 46 | private final YangUtilsParserManager manager = new YangUtilsParserManager(); |
| 47 | |
| 48 | /** |
| 49 | * Checks self resolution when feature defined in same file. |
| 50 | */ |
| 51 | @Test |
| 52 | public void processSelfFileLinkingWithFeature() |
| 53 | throws IOException, ParserException { |
| 54 | |
| 55 | YangNode node = manager |
| 56 | .getDataModel("src/test/resources/SelfFileLinkingWithFeature.yang"); |
| 57 | |
| 58 | // Check whether the data model tree returned is of type module. |
| 59 | assertThat((node instanceof YangModule), is(true)); |
| 60 | |
| 61 | // Check whether the node type is set properly to module. |
| 62 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 63 | |
| 64 | // Check whether the module name is set correctly. |
| 65 | YangModule yangNode = (YangModule) node; |
| 66 | assertThat(yangNode.getName(), is("syslog")); |
| 67 | |
| 68 | List<YangFeature> featureList = yangNode.getFeatureList(); |
| 69 | YangFeature feature = featureList.iterator().next(); |
| 70 | assertThat(feature.getName(), is("local-storage")); |
| 71 | |
| 72 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 73 | assertThat(container.getName(), is("speed")); |
| 74 | |
| 75 | List<YangLeaf> listOfLeaf = container.getListOfLeaf(); |
| 76 | YangLeaf leaf = listOfLeaf.iterator().next(); |
| 77 | assertThat(leaf.getName(), is("local-storage-limit")); |
| 78 | |
| 79 | List<YangIfFeature> ifFeatureList = leaf.getIfFeatureList(); |
| 80 | YangIfFeature ifFeature = ifFeatureList.iterator().next(); |
| 81 | assertThat(ifFeature.getName().getName(), is("local-storage")); |
| 82 | assertThat(ifFeature.getResolvableStatus(), is(ResolvableStatus.RESOLVED)); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Checks self resolution when feature is undefined. |
| 87 | */ |
| 88 | @Test |
| 89 | public void processSelfFileLinkingWithFeatureUndefined() |
| 90 | throws IOException, ParserException { |
| 91 | |
| 92 | YangNode node = manager |
| 93 | .getDataModel("src/test/resources/SelfFileLinkingWithFeatureUndefined.yang"); |
| 94 | |
| 95 | // Check whether the data model tree returned is of type module. |
| 96 | assertThat((node instanceof YangModule), is(true)); |
| 97 | |
| 98 | // Check whether the node type is set properly to module. |
| 99 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 100 | |
| 101 | // Check whether the module name is set correctly. |
| 102 | YangModule yangNode = (YangModule) node; |
| 103 | assertThat(yangNode.getName(), is("syslog")); |
| 104 | |
| 105 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 106 | assertThat(container.getName(), is("speed")); |
| 107 | |
| 108 | List<YangLeaf> listOfLeaf = container.getListOfLeaf(); |
| 109 | YangLeaf leaf = listOfLeaf.iterator().next(); |
| 110 | assertThat(leaf.getName(), is("local-storage-limit")); |
| 111 | |
| 112 | List<YangIfFeature> ifFeatureList = leaf.getIfFeatureList(); |
| 113 | YangIfFeature ifFeature = ifFeatureList.iterator().next(); |
| 114 | assertThat(ifFeature.getName().getName(), is("local-storage")); |
| 115 | assertThat(ifFeature.getResolvableStatus(), is(ResolvableStatus.INTRA_FILE_RESOLVED)); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Checks self resolution of feature with multiple dependency. |
| 120 | */ |
| 121 | @Test |
| 122 | public void processSelfFileLinkingWithMultipleDependency() throws IOException, ParserException { |
| 123 | YangNode node = manager |
| 124 | .getDataModel("src/test/resources/SelfFileLinkingWithMultipleDependency.yang"); |
| 125 | |
| 126 | // Check whether the data model tree returned is of type module. |
| 127 | assertThat((node instanceof YangModule), is(true)); |
| 128 | |
| 129 | // Check whether the node type is set properly to module. |
| 130 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 131 | |
| 132 | // Check whether the module name is set correctly. |
| 133 | YangModule yangNode = (YangModule) node; |
| 134 | assertThat(yangNode.getName(), is("syslog")); |
| 135 | |
| 136 | List<YangFeature> featureList = yangNode.getFeatureList(); |
| 137 | YangFeature feature = featureList.iterator().next(); |
| 138 | assertThat(feature.getName(), is("p2mp-te")); |
| 139 | |
| 140 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 141 | assertThat(container.getName(), is("speed")); |
| 142 | |
| 143 | List<YangLeaf> listOfLeaf = container.getListOfLeaf(); |
| 144 | YangLeaf leaf = listOfLeaf.iterator().next(); |
| 145 | assertThat(leaf.getName(), is("local-storage-limit")); |
| 146 | |
| 147 | List<YangIfFeature> ifFeatureList = leaf.getIfFeatureList(); |
| 148 | YangIfFeature ifFeature = ifFeatureList.iterator().next(); |
| 149 | assertThat(ifFeature.getName().getName(), is("frr-te")); |
| 150 | assertThat(ifFeature.getResolvableStatus(), is(ResolvableStatus.RESOLVED)); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Checks self resolution of feature with multiple dependency undefined. |
| 155 | */ |
| 156 | @Test |
| 157 | public void processSelfFileLinkingWithMultipleDependencyUnresolved() throws IOException, ParserException { |
| 158 | YangNode node = manager |
| 159 | .getDataModel("src/test/resources/SelfFileLinkingWithMultipleDependencyUnresolved.yang"); |
| 160 | |
| 161 | // Check whether the data model tree returned is of type module. |
| 162 | assertThat((node instanceof YangModule), is(true)); |
| 163 | |
| 164 | // Check whether the node type is set properly to module. |
| 165 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 166 | |
| 167 | // Check whether the module name is set correctly. |
| 168 | YangModule yangNode = (YangModule) node; |
| 169 | assertThat(yangNode.getName(), is("syslog")); |
| 170 | |
| 171 | List<YangFeature> featureList = yangNode.getFeatureList(); |
| 172 | YangFeature feature = featureList.iterator().next(); |
| 173 | assertThat(feature.getName(), is("frr-te")); |
| 174 | |
| 175 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 176 | assertThat(container.getName(), is("speed")); |
| 177 | |
| 178 | List<YangLeaf> listOfLeaf = container.getListOfLeaf(); |
| 179 | YangLeaf leaf = listOfLeaf.iterator().next(); |
| 180 | assertThat(leaf.getName(), is("local-storage-limit")); |
| 181 | |
| 182 | List<YangIfFeature> ifFeatureList = leaf.getIfFeatureList(); |
| 183 | YangIfFeature ifFeature = ifFeatureList.iterator().next(); |
| 184 | assertThat(ifFeature.getName().getName(), is("frr-te")); |
| 185 | assertThat(ifFeature.getResolvableStatus(), is(ResolvableStatus.INTRA_FILE_RESOLVED)); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Checks self resolution when feature is defined in same file in submodule. |
| 190 | */ |
| 191 | @Test |
| 192 | public void processSelfFileLinkingWithFeatureInSubModule() |
| 193 | throws IOException, ParserException { |
| 194 | |
| 195 | YangNode node = manager |
| 196 | .getDataModel("src/test/resources/SelfFileLinkingWithFeatureInSubModule.yang"); |
| 197 | |
| 198 | // Check whether the data model tree returned is of type module. |
| 199 | assertThat((node instanceof YangSubModule), is(true)); |
| 200 | |
| 201 | // Check whether the node type is set properly to module. |
| 202 | assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE)); |
| 203 | |
| 204 | // Check whether the module name is set correctly. |
| 205 | YangSubModule yangNode = (YangSubModule) node; |
| 206 | assertThat(yangNode.getName(), is("syslog")); |
| 207 | |
| 208 | List<YangFeature> featureList = yangNode.getFeatureList(); |
| 209 | YangFeature feature = featureList.iterator().next(); |
| 210 | assertThat(feature.getName(), is("local-storage")); |
| 211 | |
| 212 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 213 | assertThat(container.getName(), is("speed")); |
| 214 | |
| 215 | List<YangLeaf> listOfLeaf = container.getListOfLeaf(); |
| 216 | YangLeaf leaf = listOfLeaf.iterator().next(); |
| 217 | assertThat(leaf.getName(), is("local-storage-limit")); |
| 218 | |
| 219 | List<YangIfFeature> ifFeatureList = leaf.getIfFeatureList(); |
| 220 | YangIfFeature ifFeature = ifFeatureList.iterator().next(); |
| 221 | assertThat(ifFeature.getName().getName(), is("local-storage")); |
| 222 | assertThat(ifFeature.getResolvableStatus(), is(ResolvableStatus.RESOLVED)); |
| 223 | } |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * Checks addition of if-feature list to leafref. |
| 227 | */ |
| 228 | @Test |
| 229 | public void processSelfFileLinkingWithFeatureReferredByLeafref() |
| 230 | throws IOException, ParserException { |
| 231 | |
| 232 | YangNode node = manager |
| 233 | .getDataModel("src/test/resources/SelfFileLinkingWithFeatureReferredByLeafref.yang"); |
| 234 | |
| 235 | // Check whether the data model tree returned is of type module. |
| 236 | assertThat((node instanceof YangModule), is(true)); |
| 237 | |
| 238 | // Check whether the node type is set properly to module. |
| 239 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 240 | |
| 241 | // Check whether the module name is set correctly. |
| 242 | YangModule yangNode = (YangModule) node; |
| 243 | assertThat(yangNode.getName(), is("syslog")); |
| 244 | |
| 245 | List<YangFeature> featureList = yangNode.getFeatureList(); |
| 246 | YangFeature feature = featureList.iterator().next(); |
| 247 | assertThat(feature.getName(), is("local-storage")); |
| 248 | |
| 249 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 250 | assertThat(container.getName(), is("speed")); |
| 251 | |
| 252 | List<YangLeaf> listOfLeaf = container.getListOfLeaf(); |
| 253 | YangLeaf leaf = listOfLeaf.iterator().next(); |
| 254 | assertThat(leaf.getName(), is("local-storage-limit")); |
| 255 | |
| 256 | List<YangIfFeature> ifFeatureList = leaf.getIfFeatureList(); |
| 257 | YangIfFeature ifFeature = ifFeatureList.iterator().next(); |
| 258 | assertThat(ifFeature.getName().getName(), is("local-storage")); |
| 259 | assertThat(ifFeature.getResolvableStatus(), is(ResolvableStatus.RESOLVED)); |
| 260 | |
| 261 | ListIterator<YangLeaf> listOfLeafInModule = yangNode.getListOfLeaf().listIterator(); |
| 262 | YangLeaf yangLeaf = listOfLeafInModule.next(); |
| 263 | assertThat(yangLeaf.getName(), is("storage-value")); |
| 264 | |
| 265 | YangLeafRef leafRef = (YangLeafRef) yangLeaf.getDataType().getDataTypeExtendedInfo(); |
| 266 | |
| 267 | assertThat(leafRef.getEffectiveDataType().getDataType(), is(YangDataTypes.UINT64)); |
| 268 | |
| 269 | List<YangIfFeature> ifFeatureListInLeafref = leafRef.getIfFeatureList(); |
| 270 | YangIfFeature ifFeatureInLeafref = ifFeatureListInLeafref.iterator().next(); |
| 271 | assertThat(ifFeatureInLeafref.getName().getName(), is("local-storage")); |
| 272 | assertThat(ifFeatureInLeafref.getResolvableStatus(), is(ResolvableStatus.RESOLVED)); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Checks addition of if-feature list to leafref when referred leaf is again having leafref in it. |
| 277 | */ |
| 278 | @Test |
| 279 | public void processSelfFileLinkingWithFeatureReferredByMultiLeafref() |
| 280 | throws IOException, ParserException { |
| 281 | |
| 282 | YangNode node = manager |
| 283 | .getDataModel("src/test/resources/SelfFileLinkingWithFeatureReferredByMultiLeafref.yang"); |
| 284 | |
| 285 | // Check whether the data model tree returned is of type module. |
| 286 | assertThat((node instanceof YangModule), is(true)); |
| 287 | |
| 288 | // Check whether the node type is set properly to module. |
| 289 | assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); |
| 290 | |
| 291 | // Check whether the module name is set correctly. |
| 292 | YangModule yangNode = (YangModule) node; |
| 293 | assertThat(yangNode.getName(), is("syslog")); |
| 294 | |
| 295 | List<YangFeature> featureList = yangNode.getFeatureList(); |
| 296 | YangFeature feature = featureList.iterator().next(); |
| 297 | assertThat(feature.getName(), is("local-storage")); |
| 298 | |
| 299 | YangContainer container = (YangContainer) yangNode.getChild(); |
| 300 | assertThat(container.getName(), is("speed")); |
| 301 | |
| 302 | List<YangLeaf> listOfLeaf = container.getListOfLeaf(); |
| 303 | YangLeaf leaf = listOfLeaf.iterator().next(); |
| 304 | assertThat(leaf.getName(), is("local-storage-limit")); |
| 305 | |
| 306 | List<YangIfFeature> ifFeatureList = leaf.getIfFeatureList(); |
| 307 | YangIfFeature ifFeature = ifFeatureList.iterator().next(); |
| 308 | assertThat(ifFeature.getName().getName(), is("local-storage")); |
| 309 | assertThat(ifFeature.getResolvableStatus(), is(ResolvableStatus.RESOLVED)); |
| 310 | |
| 311 | ListIterator<YangLeaf> listOfLeafInModule = yangNode.getListOfLeaf().listIterator(); |
| 312 | YangLeaf yangLeaf = listOfLeafInModule.next(); |
| 313 | assertThat(yangLeaf.getName(), is("storage-value")); |
| 314 | |
| 315 | YangLeafRef leafRef = (YangLeafRef) yangLeaf.getDataType().getDataTypeExtendedInfo(); |
| 316 | |
| 317 | assertThat(leafRef.getEffectiveDataType().getDataType(), is(YangDataTypes.UINT64)); |
| 318 | |
| 319 | List<YangIfFeature> ifFeatureListInLeafref = leafRef.getIfFeatureList(); |
| 320 | YangIfFeature ifFeatureInLeafref = ifFeatureListInLeafref.iterator().next(); |
| 321 | |
| 322 | assertThat(ifFeatureInLeafref.getName().getName(), is("main-storage")); |
| 323 | assertThat(ifFeatureInLeafref.getResolvableStatus(), is(ResolvableStatus.RESOLVED)); |
| 324 | |
| 325 | YangIfFeature ifFeatureInLeafref1 = ifFeatureListInLeafref.iterator().next(); |
| 326 | |
| 327 | assertThat(ifFeatureInLeafref1.getName().getName(), is("main-storage")); |
| 328 | assertThat(ifFeatureInLeafref1.getResolvableStatus(), is(ResolvableStatus.RESOLVED)); |
| 329 | |
| 330 | } |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 331 | } |