2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.yang.stmt;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
16 import com.google.common.collect.ImmutableSet;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
27 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
30 public class IfFeatureResolutionTest {
32 private static final StatementStreamSource FOO_MODULE = sourceForResource("/if-feature-resolution-test/foo.yang");
33 private static final StatementStreamSource BAR_MODULE = sourceForResource("/if-feature-resolution-test/bar.yang");
36 public void testSomeFeaturesSupported() throws ReactorException {
37 final Set<QName> supportedFeatures = ImmutableSet.of(
38 QName.create("foo-namespace", "1970-01-01", "test-feature-1"),
39 QName.create("foo-namespace", "1970-01-01", "test-feature-2"),
40 QName.create("foo-namespace", "1970-01-01", "test-feature-3"),
41 QName.create("bar-namespace", "1970-01-01", "imp-feature"));
43 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
44 reactor.addSources(FOO_MODULE, BAR_MODULE);
45 reactor.setSupportedFeatures(supportedFeatures);
47 final SchemaContext schemaContext = reactor.buildEffective();
48 assertNotNull(schemaContext);
50 final Module testModule = schemaContext.findModuleByName("foo", null);
51 assertNotNull(testModule);
53 assertEquals(9, testModule.getChildNodes().size());
55 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) testModule.getDataChildByName(
56 QName.create(testModule.getQNameModule(), "test-container-a"));
57 assertNull(testContainerA);
59 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) testModule.getDataChildByName(
60 QName.create(testModule.getQNameModule(), "test-container-b"));
61 assertNotNull(testContainerB);
62 final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
63 QName.create(testModule.getQNameModule(), "test-leaf-b"));
64 assertNotNull(testLeafB);
66 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) testModule.getDataChildByName(
67 QName.create(testModule.getQNameModule(), "test-container-c"));
68 assertNotNull(testContainerC);
69 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
70 QName.create(testModule.getQNameModule(), "test-leaf-c"));
71 assertNotNull(testLeafC);
73 final ContainerSchemaNode testContainerD = (ContainerSchemaNode) testModule.getDataChildByName(
74 QName.create(testModule.getQNameModule(), "test-container-d"));
75 assertNull(testContainerD);
77 final ContainerSchemaNode testContainerE = (ContainerSchemaNode) testModule.getDataChildByName(
78 QName.create(testModule.getQNameModule(), "test-container-e"));
79 assertNotNull(testContainerE);
80 final ContainerSchemaNode testSubContainerE = (ContainerSchemaNode) testContainerE.getDataChildByName(
81 QName.create(testModule.getQNameModule(), "test-subcontainer-e"));
82 assertNotNull(testSubContainerE);
83 final LeafSchemaNode testLeafE = (LeafSchemaNode) testSubContainerE.getDataChildByName(
84 QName.create(testModule.getQNameModule(), "test-leaf-e"));
85 assertNull(testLeafE);
87 final ContainerSchemaNode testContainerF = (ContainerSchemaNode) testModule.getDataChildByName(
88 QName.create(testModule.getQNameModule(), "test-container-f"));
89 assertNotNull(testContainerF);
90 final ContainerSchemaNode testSubContainerF = (ContainerSchemaNode) testContainerF.getDataChildByName(
91 QName.create(testModule.getQNameModule(), "test-subcontainer-f"));
92 assertNull(testSubContainerF);
94 final ContainerSchemaNode testContainerG = (ContainerSchemaNode) testModule.getDataChildByName(
95 QName.create(testModule.getQNameModule(), "test-container-g"));
96 assertNotNull(testContainerG);
97 assertEquals(1, testContainerG.getAvailableAugmentations().size());
98 final LeafSchemaNode testLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
99 QName.create(testModule.getQNameModule(), "test-leaf-g"));
100 assertNotNull(testLeafG);
101 final LeafSchemaNode augmentingTestLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
102 QName.create(testModule.getQNameModule(), "augmenting-test-leaf-g"));
103 assertNull(augmentingTestLeafG);
104 final AnyXmlSchemaNode augmentingTestAnyxmlG = (AnyXmlSchemaNode) testContainerG.getDataChildByName(
105 QName.create(testModule.getQNameModule(), "augmenting-test-anyxml-g"));
106 assertNotNull(augmentingTestAnyxmlG);
108 final ContainerSchemaNode testContainerH = (ContainerSchemaNode) testModule.getDataChildByName(
109 QName.create(testModule.getQNameModule(), "test-container-h"));
110 assertNotNull(testContainerH);
111 assertEquals(0, testContainerH.getChildNodes().size());
112 assertEquals(0, testContainerH.getUses().size());
114 final ContainerSchemaNode testContainerI = (ContainerSchemaNode) testModule.getDataChildByName(
115 QName.create(testModule.getQNameModule(), "test-container-i"));
116 assertNotNull(testContainerI);
117 assertEquals(1, testContainerI.getUses().size());
118 ContainerSchemaNode testGroupingSubContainer = (ContainerSchemaNode) testContainerI.getDataChildByName(
119 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
120 assertNotNull(testGroupingSubContainer);
121 final LeafSchemaNode testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
122 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
123 assertNull(testGroupingLeaf);
125 final ContainerSchemaNode testContainerJ = (ContainerSchemaNode) testModule.getDataChildByName(
126 QName.create(testModule.getQNameModule(), "test-container-j"));
127 assertNotNull(testContainerJ);
128 final LeafSchemaNode testLeafJ = (LeafSchemaNode) testContainerJ.getDataChildByName(
129 QName.create(testModule.getQNameModule(), "test-leaf-j"));
130 assertNotNull(testLeafJ);
132 final ContainerSchemaNode testContainerK = (ContainerSchemaNode) testModule.getDataChildByName(
133 QName.create(testModule.getQNameModule(), "test-container-k"));
134 assertNotNull(testContainerK);
135 assertEquals(1, testContainerK.getUses().size());
136 testGroupingSubContainer = (ContainerSchemaNode) testContainerK.getDataChildByName(
137 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
138 assertNotNull(testGroupingSubContainer);
139 assertEquals(1, testGroupingSubContainer.getAvailableAugmentations().size());
140 final LeafSchemaNode augmentingTestGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
141 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf"));
142 assertNotNull(augmentingTestGroupingLeaf);
143 final LeafSchemaNode augmentingTestGroupingLeaf2 = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
144 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf-2"));
145 assertNull(augmentingTestGroupingLeaf2);
149 public void testAllFeaturesSupported() throws ReactorException {
150 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
151 reactor.addSources(FOO_MODULE, BAR_MODULE);
153 final SchemaContext schemaContext = reactor.buildEffective();
154 assertNotNull(schemaContext);
156 final Module testModule = schemaContext.findModuleByName("foo", null);
157 assertNotNull(testModule);
159 assertEquals(11, testModule.getChildNodes().size());
161 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) testModule.getDataChildByName(
162 QName.create(testModule.getQNameModule(), "test-container-a"));
163 assertNotNull(testContainerA);
164 final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
165 QName.create(testModule.getQNameModule(), "test-leaf-a"));
166 assertNotNull(testLeafA);
169 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) testModule.getDataChildByName(
170 QName.create(testModule.getQNameModule(), "test-container-b"));
171 assertNotNull(testContainerB);
172 final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
173 QName.create(testModule.getQNameModule(), "test-leaf-b"));
174 assertNotNull(testLeafB);
176 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) testModule.getDataChildByName(
177 QName.create(testModule.getQNameModule(), "test-container-c"));
178 assertNotNull(testContainerC);
179 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
180 QName.create(testModule.getQNameModule(), "test-leaf-c"));
181 assertNotNull(testLeafC);
183 final ContainerSchemaNode testContainerD = (ContainerSchemaNode) testModule.getDataChildByName(
184 QName.create(testModule.getQNameModule(), "test-container-d"));
185 assertNotNull(testContainerD);
186 final LeafSchemaNode testLeafD = (LeafSchemaNode) testContainerD.getDataChildByName(
187 QName.create(testModule.getQNameModule(), "test-leaf-d"));
188 assertNotNull(testLeafD);
190 final ContainerSchemaNode testContainerE = (ContainerSchemaNode) testModule.getDataChildByName(
191 QName.create(testModule.getQNameModule(), "test-container-e"));
192 assertNotNull(testContainerE);
193 final ContainerSchemaNode testSubContainerE = (ContainerSchemaNode) testContainerE.getDataChildByName(
194 QName.create(testModule.getQNameModule(), "test-subcontainer-e"));
195 assertNotNull(testSubContainerE);
196 final LeafSchemaNode testLeafE = (LeafSchemaNode) testSubContainerE.getDataChildByName(
197 QName.create(testModule.getQNameModule(), "test-leaf-e"));
198 assertNotNull(testLeafE);
200 final ContainerSchemaNode testContainerF = (ContainerSchemaNode) testModule.getDataChildByName(
201 QName.create(testModule.getQNameModule(), "test-container-f"));
202 assertNotNull(testContainerF);
203 final ContainerSchemaNode testSubContainerF = (ContainerSchemaNode) testContainerF.getDataChildByName(
204 QName.create(testModule.getQNameModule(), "test-subcontainer-f"));
205 assertNotNull(testSubContainerF);
206 final ContainerSchemaNode testSubSubContainerF = (ContainerSchemaNode) testSubContainerF.getDataChildByName(
207 QName.create(testModule.getQNameModule(), "test-subsubcontainer-f"));
208 assertNotNull(testSubSubContainerF);
209 final LeafSchemaNode testLeafF = (LeafSchemaNode) testSubSubContainerF.getDataChildByName(
210 QName.create(testModule.getQNameModule(), "test-leaf-f"));
211 assertNotNull(testLeafF);
213 final ContainerSchemaNode testContainerG = (ContainerSchemaNode) testModule.getDataChildByName(
214 QName.create(testModule.getQNameModule(), "test-container-g"));
215 assertNotNull(testContainerG);
216 assertEquals(2, testContainerG.getAvailableAugmentations().size());
217 final LeafSchemaNode testLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
218 QName.create(testModule.getQNameModule(), "test-leaf-g"));
219 assertNotNull(testLeafG);
220 final LeafSchemaNode augmentingTestLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
221 QName.create(testModule.getQNameModule(), "augmenting-test-leaf-g"));
222 assertNotNull(augmentingTestLeafG);
223 final AnyXmlSchemaNode augmentingTestAnyxmlG = (AnyXmlSchemaNode) testContainerG.getDataChildByName(
224 QName.create(testModule.getQNameModule(), "augmenting-test-anyxml-g"));
225 assertNotNull(augmentingTestAnyxmlG);
227 final ContainerSchemaNode testContainerH = (ContainerSchemaNode) testModule.getDataChildByName(
228 QName.create(testModule.getQNameModule(), "test-container-h"));
229 assertNotNull(testContainerH);
230 assertEquals(1, testContainerH.getUses().size());
231 ContainerSchemaNode testGroupingSubContainer = (ContainerSchemaNode) testContainerH.getDataChildByName(
232 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
233 assertNotNull(testGroupingSubContainer);
234 LeafSchemaNode testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
235 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
236 assertNotNull(testGroupingLeaf);
238 final ContainerSchemaNode testContainerI = (ContainerSchemaNode) testModule.getDataChildByName(
239 QName.create(testModule.getQNameModule(), "test-container-i"));
240 assertNotNull(testContainerI);
241 assertEquals(1, testContainerI.getUses().size());
242 testGroupingSubContainer = (ContainerSchemaNode) testContainerI.getDataChildByName(
243 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
244 assertNotNull(testGroupingSubContainer);
245 testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
246 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
247 assertNotNull(testGroupingLeaf);
249 final ContainerSchemaNode testContainerJ = (ContainerSchemaNode) testModule.getDataChildByName(
250 QName.create(testModule.getQNameModule(), "test-container-j"));
251 assertNotNull(testContainerJ);
252 final LeafSchemaNode testLeafJ = (LeafSchemaNode) testContainerJ.getDataChildByName(
253 QName.create(testModule.getQNameModule(), "test-leaf-j"));
254 assertNotNull(testLeafJ);
256 final ContainerSchemaNode testContainerK = (ContainerSchemaNode) testModule.getDataChildByName(
257 QName.create(testModule.getQNameModule(), "test-container-k"));
258 assertNotNull(testContainerK);
259 assertEquals(1, testContainerK.getUses().size());
260 testGroupingSubContainer = (ContainerSchemaNode) testContainerK.getDataChildByName(
261 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
262 assertNotNull(testGroupingSubContainer);
263 assertEquals(1, testGroupingSubContainer.getAvailableAugmentations().size());
264 final LeafSchemaNode augmentingTestGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
265 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf"));
266 assertNotNull(augmentingTestGroupingLeaf);
267 final LeafSchemaNode augmentingTestGroupingLeaf2 = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
268 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf-2"));
269 assertNotNull(augmentingTestGroupingLeaf2);
270 testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
271 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
272 assertNotNull(testGroupingLeaf);
276 public void testNoFeaturesSupported() throws ReactorException {
277 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
278 reactor.addSources(FOO_MODULE, BAR_MODULE);
279 reactor.setSupportedFeatures(ImmutableSet.of());
281 final SchemaContext schemaContext = reactor.buildEffective();
282 assertNotNull(schemaContext);
284 final Module testModule = schemaContext.findModuleByName("foo", null);
285 assertNotNull(testModule);
287 assertEquals(6, testModule.getChildNodes().size());
289 final ContainerSchemaNode testContainerE = (ContainerSchemaNode) testModule.getDataChildByName(
290 QName.create(testModule.getQNameModule(), "test-container-e"));
291 assertNotNull(testContainerE);
292 final ContainerSchemaNode testSubContainerE = (ContainerSchemaNode) testContainerE.getDataChildByName(
293 QName.create(testModule.getQNameModule(), "test-subcontainer-e"));
294 assertNotNull(testSubContainerE);
295 final LeafSchemaNode testLeafE = (LeafSchemaNode) testSubContainerE.getDataChildByName(
296 QName.create(testModule.getQNameModule(), "test-leaf-e"));
297 assertNull(testLeafE);
299 final ContainerSchemaNode testContainerF = (ContainerSchemaNode) testModule.getDataChildByName(
300 QName.create(testModule.getQNameModule(), "test-container-f"));
301 assertNotNull(testContainerF);
302 final ContainerSchemaNode testSubContainerF = (ContainerSchemaNode) testContainerF.getDataChildByName(
303 QName.create(testModule.getQNameModule(), "test-subcontainer-f"));
304 assertNull(testSubContainerF);
306 final ContainerSchemaNode testContainerG = (ContainerSchemaNode) testModule.getDataChildByName(
307 QName.create(testModule.getQNameModule(), "test-container-g"));
308 assertNotNull(testContainerG);
309 assertEquals(1, testContainerG.getAvailableAugmentations().size());
310 final LeafSchemaNode testLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
311 QName.create(testModule.getQNameModule(), "test-leaf-g"));
312 assertNotNull(testLeafG);
313 final LeafSchemaNode augmentingTestLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
314 QName.create(testModule.getQNameModule(), "augmenting-test-leaf-g"));
315 assertNull(augmentingTestLeafG);
316 final AnyXmlSchemaNode augmentingTestAnyxmlG = (AnyXmlSchemaNode) testContainerG.getDataChildByName(
317 QName.create(testModule.getQNameModule(), "augmenting-test-anyxml-g"));
318 assertNotNull(augmentingTestAnyxmlG);
320 final ContainerSchemaNode testContainerH = (ContainerSchemaNode) testModule.getDataChildByName(
321 QName.create(testModule.getQNameModule(), "test-container-h"));
322 assertNotNull(testContainerH);
323 assertEquals(0, testContainerH.getChildNodes().size());
324 assertEquals(0, testContainerH.getUses().size());
326 final ContainerSchemaNode testContainerI = (ContainerSchemaNode) testModule.getDataChildByName(
327 QName.create(testModule.getQNameModule(), "test-container-i"));
328 assertNotNull(testContainerI);
329 assertEquals(1, testContainerI.getUses().size());
330 ContainerSchemaNode testGroupingSubContainer = (ContainerSchemaNode) testContainerI.getDataChildByName(
331 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
332 assertNotNull(testGroupingSubContainer);
333 LeafSchemaNode testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
334 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
335 assertNull(testGroupingLeaf);
337 final ContainerSchemaNode testContainerK = (ContainerSchemaNode) testModule.getDataChildByName(
338 QName.create(testModule.getQNameModule(), "test-container-k"));
339 assertNotNull(testContainerK);
340 assertEquals(1, testContainerK.getUses().size());
341 testGroupingSubContainer = (ContainerSchemaNode) testContainerK.getDataChildByName(
342 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
343 assertNotNull(testGroupingSubContainer);
344 assertEquals(1, testGroupingSubContainer.getAvailableAugmentations().size());
345 final LeafSchemaNode augmentingTestGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
346 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf"));
347 assertNull(augmentingTestGroupingLeaf);
348 final LeafSchemaNode augmentingTestGroupingLeaf2 = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
349 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf-2"));
350 assertNull(augmentingTestGroupingLeaf2);
351 testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
352 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
353 assertNull(testGroupingLeaf);