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;
17 import java.util.Optional;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
31 public class IfFeatureResolutionTest {
33 private static final StatementStreamSource FOO_MODULE = sourceForResource("/if-feature-resolution-test/foo.yang");
34 private static final StatementStreamSource BAR_MODULE = sourceForResource("/if-feature-resolution-test/bar.yang");
37 public void testSomeFeaturesSupported() throws ReactorException {
38 final Set<QName> supportedFeatures = ImmutableSet.of(
39 QName.create("foo-namespace", "1970-01-01", "test-feature-1"),
40 QName.create("foo-namespace", "1970-01-01", "test-feature-2"),
41 QName.create("foo-namespace", "1970-01-01", "test-feature-3"),
42 QName.create("bar-namespace", "1970-01-01", "imp-feature"));
44 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
45 Optional.of(supportedFeatures));
46 reactor.addSources(FOO_MODULE, BAR_MODULE);
48 final SchemaContext schemaContext = reactor.buildEffective();
49 assertNotNull(schemaContext);
51 final Module testModule = schemaContext.findModuleByName("foo", null);
52 assertNotNull(testModule);
54 assertEquals(9, testModule.getChildNodes().size());
56 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) testModule.getDataChildByName(
57 QName.create(testModule.getQNameModule(), "test-container-a"));
58 assertNull(testContainerA);
60 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) testModule.getDataChildByName(
61 QName.create(testModule.getQNameModule(), "test-container-b"));
62 assertNotNull(testContainerB);
63 final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
64 QName.create(testModule.getQNameModule(), "test-leaf-b"));
65 assertNotNull(testLeafB);
67 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) testModule.getDataChildByName(
68 QName.create(testModule.getQNameModule(), "test-container-c"));
69 assertNotNull(testContainerC);
70 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
71 QName.create(testModule.getQNameModule(), "test-leaf-c"));
72 assertNotNull(testLeafC);
74 final ContainerSchemaNode testContainerD = (ContainerSchemaNode) testModule.getDataChildByName(
75 QName.create(testModule.getQNameModule(), "test-container-d"));
76 assertNull(testContainerD);
78 final ContainerSchemaNode testContainerE = (ContainerSchemaNode) testModule.getDataChildByName(
79 QName.create(testModule.getQNameModule(), "test-container-e"));
80 assertNotNull(testContainerE);
81 final ContainerSchemaNode testSubContainerE = (ContainerSchemaNode) testContainerE.getDataChildByName(
82 QName.create(testModule.getQNameModule(), "test-subcontainer-e"));
83 assertNotNull(testSubContainerE);
84 final LeafSchemaNode testLeafE = (LeafSchemaNode) testSubContainerE.getDataChildByName(
85 QName.create(testModule.getQNameModule(), "test-leaf-e"));
86 assertNull(testLeafE);
88 final ContainerSchemaNode testContainerF = (ContainerSchemaNode) testModule.getDataChildByName(
89 QName.create(testModule.getQNameModule(), "test-container-f"));
90 assertNotNull(testContainerF);
91 final ContainerSchemaNode testSubContainerF = (ContainerSchemaNode) testContainerF.getDataChildByName(
92 QName.create(testModule.getQNameModule(), "test-subcontainer-f"));
93 assertNull(testSubContainerF);
95 final ContainerSchemaNode testContainerG = (ContainerSchemaNode) testModule.getDataChildByName(
96 QName.create(testModule.getQNameModule(), "test-container-g"));
97 assertNotNull(testContainerG);
98 assertEquals(1, testContainerG.getAvailableAugmentations().size());
99 final LeafSchemaNode testLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
100 QName.create(testModule.getQNameModule(), "test-leaf-g"));
101 assertNotNull(testLeafG);
102 final LeafSchemaNode augmentingTestLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
103 QName.create(testModule.getQNameModule(), "augmenting-test-leaf-g"));
104 assertNull(augmentingTestLeafG);
105 final AnyXmlSchemaNode augmentingTestAnyxmlG = (AnyXmlSchemaNode) testContainerG.getDataChildByName(
106 QName.create(testModule.getQNameModule(), "augmenting-test-anyxml-g"));
107 assertNotNull(augmentingTestAnyxmlG);
109 final ContainerSchemaNode testContainerH = (ContainerSchemaNode) testModule.getDataChildByName(
110 QName.create(testModule.getQNameModule(), "test-container-h"));
111 assertNotNull(testContainerH);
112 assertEquals(0, testContainerH.getChildNodes().size());
113 assertEquals(0, testContainerH.getUses().size());
115 final ContainerSchemaNode testContainerI = (ContainerSchemaNode) testModule.getDataChildByName(
116 QName.create(testModule.getQNameModule(), "test-container-i"));
117 assertNotNull(testContainerI);
118 assertEquals(1, testContainerI.getUses().size());
119 ContainerSchemaNode testGroupingSubContainer = (ContainerSchemaNode) testContainerI.getDataChildByName(
120 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
121 assertNotNull(testGroupingSubContainer);
122 final LeafSchemaNode testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
123 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
124 assertNull(testGroupingLeaf);
126 final ContainerSchemaNode testContainerJ = (ContainerSchemaNode) testModule.getDataChildByName(
127 QName.create(testModule.getQNameModule(), "test-container-j"));
128 assertNotNull(testContainerJ);
129 final LeafSchemaNode testLeafJ = (LeafSchemaNode) testContainerJ.getDataChildByName(
130 QName.create(testModule.getQNameModule(), "test-leaf-j"));
131 assertNotNull(testLeafJ);
133 final ContainerSchemaNode testContainerK = (ContainerSchemaNode) testModule.getDataChildByName(
134 QName.create(testModule.getQNameModule(), "test-container-k"));
135 assertNotNull(testContainerK);
136 assertEquals(1, testContainerK.getUses().size());
137 testGroupingSubContainer = (ContainerSchemaNode) testContainerK.getDataChildByName(
138 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
139 assertNotNull(testGroupingSubContainer);
140 assertEquals(1, testGroupingSubContainer.getAvailableAugmentations().size());
141 final LeafSchemaNode augmentingTestGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
142 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf"));
143 assertNotNull(augmentingTestGroupingLeaf);
144 final LeafSchemaNode augmentingTestGroupingLeaf2 = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
145 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf-2"));
146 assertNull(augmentingTestGroupingLeaf2);
150 public void testAllFeaturesSupported() throws ReactorException {
151 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
152 reactor.addSources(FOO_MODULE, BAR_MODULE);
154 final SchemaContext schemaContext = reactor.buildEffective();
155 assertNotNull(schemaContext);
157 final Module testModule = schemaContext.findModuleByName("foo", null);
158 assertNotNull(testModule);
160 assertEquals(11, testModule.getChildNodes().size());
162 final ContainerSchemaNode testContainerA = (ContainerSchemaNode) testModule.getDataChildByName(
163 QName.create(testModule.getQNameModule(), "test-container-a"));
164 assertNotNull(testContainerA);
165 final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
166 QName.create(testModule.getQNameModule(), "test-leaf-a"));
167 assertNotNull(testLeafA);
170 final ContainerSchemaNode testContainerB = (ContainerSchemaNode) testModule.getDataChildByName(
171 QName.create(testModule.getQNameModule(), "test-container-b"));
172 assertNotNull(testContainerB);
173 final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
174 QName.create(testModule.getQNameModule(), "test-leaf-b"));
175 assertNotNull(testLeafB);
177 final ContainerSchemaNode testContainerC = (ContainerSchemaNode) testModule.getDataChildByName(
178 QName.create(testModule.getQNameModule(), "test-container-c"));
179 assertNotNull(testContainerC);
180 final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
181 QName.create(testModule.getQNameModule(), "test-leaf-c"));
182 assertNotNull(testLeafC);
184 final ContainerSchemaNode testContainerD = (ContainerSchemaNode) testModule.getDataChildByName(
185 QName.create(testModule.getQNameModule(), "test-container-d"));
186 assertNotNull(testContainerD);
187 final LeafSchemaNode testLeafD = (LeafSchemaNode) testContainerD.getDataChildByName(
188 QName.create(testModule.getQNameModule(), "test-leaf-d"));
189 assertNotNull(testLeafD);
191 final ContainerSchemaNode testContainerE = (ContainerSchemaNode) testModule.getDataChildByName(
192 QName.create(testModule.getQNameModule(), "test-container-e"));
193 assertNotNull(testContainerE);
194 final ContainerSchemaNode testSubContainerE = (ContainerSchemaNode) testContainerE.getDataChildByName(
195 QName.create(testModule.getQNameModule(), "test-subcontainer-e"));
196 assertNotNull(testSubContainerE);
197 final LeafSchemaNode testLeafE = (LeafSchemaNode) testSubContainerE.getDataChildByName(
198 QName.create(testModule.getQNameModule(), "test-leaf-e"));
199 assertNotNull(testLeafE);
201 final ContainerSchemaNode testContainerF = (ContainerSchemaNode) testModule.getDataChildByName(
202 QName.create(testModule.getQNameModule(), "test-container-f"));
203 assertNotNull(testContainerF);
204 final ContainerSchemaNode testSubContainerF = (ContainerSchemaNode) testContainerF.getDataChildByName(
205 QName.create(testModule.getQNameModule(), "test-subcontainer-f"));
206 assertNotNull(testSubContainerF);
207 final ContainerSchemaNode testSubSubContainerF = (ContainerSchemaNode) testSubContainerF.getDataChildByName(
208 QName.create(testModule.getQNameModule(), "test-subsubcontainer-f"));
209 assertNotNull(testSubSubContainerF);
210 final LeafSchemaNode testLeafF = (LeafSchemaNode) testSubSubContainerF.getDataChildByName(
211 QName.create(testModule.getQNameModule(), "test-leaf-f"));
212 assertNotNull(testLeafF);
214 final ContainerSchemaNode testContainerG = (ContainerSchemaNode) testModule.getDataChildByName(
215 QName.create(testModule.getQNameModule(), "test-container-g"));
216 assertNotNull(testContainerG);
217 assertEquals(2, testContainerG.getAvailableAugmentations().size());
218 final LeafSchemaNode testLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
219 QName.create(testModule.getQNameModule(), "test-leaf-g"));
220 assertNotNull(testLeafG);
221 final LeafSchemaNode augmentingTestLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
222 QName.create(testModule.getQNameModule(), "augmenting-test-leaf-g"));
223 assertNotNull(augmentingTestLeafG);
224 final AnyXmlSchemaNode augmentingTestAnyxmlG = (AnyXmlSchemaNode) testContainerG.getDataChildByName(
225 QName.create(testModule.getQNameModule(), "augmenting-test-anyxml-g"));
226 assertNotNull(augmentingTestAnyxmlG);
228 final ContainerSchemaNode testContainerH = (ContainerSchemaNode) testModule.getDataChildByName(
229 QName.create(testModule.getQNameModule(), "test-container-h"));
230 assertNotNull(testContainerH);
231 assertEquals(1, testContainerH.getUses().size());
232 ContainerSchemaNode testGroupingSubContainer = (ContainerSchemaNode) testContainerH.getDataChildByName(
233 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
234 assertNotNull(testGroupingSubContainer);
235 LeafSchemaNode testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
236 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
237 assertNotNull(testGroupingLeaf);
239 final ContainerSchemaNode testContainerI = (ContainerSchemaNode) testModule.getDataChildByName(
240 QName.create(testModule.getQNameModule(), "test-container-i"));
241 assertNotNull(testContainerI);
242 assertEquals(1, testContainerI.getUses().size());
243 testGroupingSubContainer = (ContainerSchemaNode) testContainerI.getDataChildByName(
244 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
245 assertNotNull(testGroupingSubContainer);
246 testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
247 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
248 assertNotNull(testGroupingLeaf);
250 final ContainerSchemaNode testContainerJ = (ContainerSchemaNode) testModule.getDataChildByName(
251 QName.create(testModule.getQNameModule(), "test-container-j"));
252 assertNotNull(testContainerJ);
253 final LeafSchemaNode testLeafJ = (LeafSchemaNode) testContainerJ.getDataChildByName(
254 QName.create(testModule.getQNameModule(), "test-leaf-j"));
255 assertNotNull(testLeafJ);
257 final ContainerSchemaNode testContainerK = (ContainerSchemaNode) testModule.getDataChildByName(
258 QName.create(testModule.getQNameModule(), "test-container-k"));
259 assertNotNull(testContainerK);
260 assertEquals(1, testContainerK.getUses().size());
261 testGroupingSubContainer = (ContainerSchemaNode) testContainerK.getDataChildByName(
262 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
263 assertNotNull(testGroupingSubContainer);
264 assertEquals(1, testGroupingSubContainer.getAvailableAugmentations().size());
265 final LeafSchemaNode augmentingTestGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
266 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf"));
267 assertNotNull(augmentingTestGroupingLeaf);
268 final LeafSchemaNode augmentingTestGroupingLeaf2 = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
269 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf-2"));
270 assertNotNull(augmentingTestGroupingLeaf2);
271 testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
272 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
273 assertNotNull(testGroupingLeaf);
277 public void testNoFeaturesSupported() throws ReactorException {
278 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
279 Optional.of(ImmutableSet.of()));
280 reactor.addSources(FOO_MODULE, BAR_MODULE);
282 final SchemaContext schemaContext = reactor.buildEffective();
283 assertNotNull(schemaContext);
285 final Module testModule = schemaContext.findModuleByName("foo", null);
286 assertNotNull(testModule);
288 assertEquals(6, testModule.getChildNodes().size());
290 final ContainerSchemaNode testContainerE = (ContainerSchemaNode) testModule.getDataChildByName(
291 QName.create(testModule.getQNameModule(), "test-container-e"));
292 assertNotNull(testContainerE);
293 final ContainerSchemaNode testSubContainerE = (ContainerSchemaNode) testContainerE.getDataChildByName(
294 QName.create(testModule.getQNameModule(), "test-subcontainer-e"));
295 assertNotNull(testSubContainerE);
296 final LeafSchemaNode testLeafE = (LeafSchemaNode) testSubContainerE.getDataChildByName(
297 QName.create(testModule.getQNameModule(), "test-leaf-e"));
298 assertNull(testLeafE);
300 final ContainerSchemaNode testContainerF = (ContainerSchemaNode) testModule.getDataChildByName(
301 QName.create(testModule.getQNameModule(), "test-container-f"));
302 assertNotNull(testContainerF);
303 final ContainerSchemaNode testSubContainerF = (ContainerSchemaNode) testContainerF.getDataChildByName(
304 QName.create(testModule.getQNameModule(), "test-subcontainer-f"));
305 assertNull(testSubContainerF);
307 final ContainerSchemaNode testContainerG = (ContainerSchemaNode) testModule.getDataChildByName(
308 QName.create(testModule.getQNameModule(), "test-container-g"));
309 assertNotNull(testContainerG);
310 assertEquals(1, testContainerG.getAvailableAugmentations().size());
311 final LeafSchemaNode testLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
312 QName.create(testModule.getQNameModule(), "test-leaf-g"));
313 assertNotNull(testLeafG);
314 final LeafSchemaNode augmentingTestLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
315 QName.create(testModule.getQNameModule(), "augmenting-test-leaf-g"));
316 assertNull(augmentingTestLeafG);
317 final AnyXmlSchemaNode augmentingTestAnyxmlG = (AnyXmlSchemaNode) testContainerG.getDataChildByName(
318 QName.create(testModule.getQNameModule(), "augmenting-test-anyxml-g"));
319 assertNotNull(augmentingTestAnyxmlG);
321 final ContainerSchemaNode testContainerH = (ContainerSchemaNode) testModule.getDataChildByName(
322 QName.create(testModule.getQNameModule(), "test-container-h"));
323 assertNotNull(testContainerH);
324 assertEquals(0, testContainerH.getChildNodes().size());
325 assertEquals(0, testContainerH.getUses().size());
327 final ContainerSchemaNode testContainerI = (ContainerSchemaNode) testModule.getDataChildByName(
328 QName.create(testModule.getQNameModule(), "test-container-i"));
329 assertNotNull(testContainerI);
330 assertEquals(1, testContainerI.getUses().size());
331 ContainerSchemaNode testGroupingSubContainer = (ContainerSchemaNode) testContainerI.getDataChildByName(
332 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
333 assertNotNull(testGroupingSubContainer);
334 LeafSchemaNode testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
335 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
336 assertNull(testGroupingLeaf);
338 final ContainerSchemaNode testContainerK = (ContainerSchemaNode) testModule.getDataChildByName(
339 QName.create(testModule.getQNameModule(), "test-container-k"));
340 assertNotNull(testContainerK);
341 assertEquals(1, testContainerK.getUses().size());
342 testGroupingSubContainer = (ContainerSchemaNode) testContainerK.getDataChildByName(
343 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
344 assertNotNull(testGroupingSubContainer);
345 assertEquals(1, testGroupingSubContainer.getAvailableAugmentations().size());
346 final LeafSchemaNode augmentingTestGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
347 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf"));
348 assertNull(augmentingTestGroupingLeaf);
349 final LeafSchemaNode augmentingTestGroupingLeaf2 = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
350 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf-2"));
351 assertNull(augmentingTestGroupingLeaf2);
352 testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
353 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
354 assertNull(testGroupingLeaf);