Update StmtTestUtils
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / IfFeatureResolutionTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.stmt;
10
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;
15
16 import com.google.common.collect.ImmutableSet;
17 import java.util.Set;
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;
29
30 public class IfFeatureResolutionTest {
31
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");
34
35     @Test
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"));
42
43         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(supportedFeatures);
44         reactor.addSources(FOO_MODULE, BAR_MODULE);
45
46         final SchemaContext schemaContext = reactor.buildEffective();
47         assertNotNull(schemaContext);
48
49         final Module testModule = schemaContext.findModuleByName("foo", null);
50         assertNotNull(testModule);
51
52         assertEquals(9, testModule.getChildNodes().size());
53
54         final ContainerSchemaNode testContainerA = (ContainerSchemaNode) testModule.getDataChildByName(
55                 QName.create(testModule.getQNameModule(), "test-container-a"));
56         assertNull(testContainerA);
57
58         final ContainerSchemaNode testContainerB = (ContainerSchemaNode) testModule.getDataChildByName(
59                 QName.create(testModule.getQNameModule(), "test-container-b"));
60         assertNotNull(testContainerB);
61         final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
62                 QName.create(testModule.getQNameModule(), "test-leaf-b"));
63         assertNotNull(testLeafB);
64
65         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) testModule.getDataChildByName(
66                 QName.create(testModule.getQNameModule(), "test-container-c"));
67         assertNotNull(testContainerC);
68         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
69                 QName.create(testModule.getQNameModule(), "test-leaf-c"));
70         assertNotNull(testLeafC);
71
72         final ContainerSchemaNode testContainerD = (ContainerSchemaNode) testModule.getDataChildByName(
73                 QName.create(testModule.getQNameModule(), "test-container-d"));
74         assertNull(testContainerD);
75
76         final ContainerSchemaNode testContainerE = (ContainerSchemaNode) testModule.getDataChildByName(
77                 QName.create(testModule.getQNameModule(), "test-container-e"));
78         assertNotNull(testContainerE);
79         final ContainerSchemaNode testSubContainerE = (ContainerSchemaNode) testContainerE.getDataChildByName(
80                 QName.create(testModule.getQNameModule(), "test-subcontainer-e"));
81         assertNotNull(testSubContainerE);
82         final LeafSchemaNode testLeafE = (LeafSchemaNode) testSubContainerE.getDataChildByName(
83                 QName.create(testModule.getQNameModule(), "test-leaf-e"));
84         assertNull(testLeafE);
85
86         final ContainerSchemaNode testContainerF = (ContainerSchemaNode) testModule.getDataChildByName(
87                 QName.create(testModule.getQNameModule(), "test-container-f"));
88         assertNotNull(testContainerF);
89         final ContainerSchemaNode testSubContainerF = (ContainerSchemaNode) testContainerF.getDataChildByName(
90                 QName.create(testModule.getQNameModule(), "test-subcontainer-f"));
91         assertNull(testSubContainerF);
92
93         final ContainerSchemaNode testContainerG = (ContainerSchemaNode) testModule.getDataChildByName(
94                 QName.create(testModule.getQNameModule(), "test-container-g"));
95         assertNotNull(testContainerG);
96         assertEquals(1, testContainerG.getAvailableAugmentations().size());
97         final LeafSchemaNode testLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
98                 QName.create(testModule.getQNameModule(), "test-leaf-g"));
99         assertNotNull(testLeafG);
100         final LeafSchemaNode augmentingTestLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
101                 QName.create(testModule.getQNameModule(), "augmenting-test-leaf-g"));
102         assertNull(augmentingTestLeafG);
103         final AnyXmlSchemaNode augmentingTestAnyxmlG = (AnyXmlSchemaNode) testContainerG.getDataChildByName(
104                 QName.create(testModule.getQNameModule(), "augmenting-test-anyxml-g"));
105         assertNotNull(augmentingTestAnyxmlG);
106
107         final ContainerSchemaNode testContainerH = (ContainerSchemaNode) testModule.getDataChildByName(
108                 QName.create(testModule.getQNameModule(), "test-container-h"));
109         assertNotNull(testContainerH);
110         assertEquals(0, testContainerH.getChildNodes().size());
111         assertEquals(0, testContainerH.getUses().size());
112
113         final ContainerSchemaNode testContainerI = (ContainerSchemaNode) testModule.getDataChildByName(
114                 QName.create(testModule.getQNameModule(), "test-container-i"));
115         assertNotNull(testContainerI);
116         assertEquals(1, testContainerI.getUses().size());
117         ContainerSchemaNode testGroupingSubContainer = (ContainerSchemaNode) testContainerI.getDataChildByName(
118                 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
119         assertNotNull(testGroupingSubContainer);
120         final LeafSchemaNode testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
121                 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
122         assertNull(testGroupingLeaf);
123
124         final ContainerSchemaNode testContainerJ = (ContainerSchemaNode) testModule.getDataChildByName(
125                 QName.create(testModule.getQNameModule(), "test-container-j"));
126         assertNotNull(testContainerJ);
127         final LeafSchemaNode testLeafJ = (LeafSchemaNode) testContainerJ.getDataChildByName(
128                 QName.create(testModule.getQNameModule(), "test-leaf-j"));
129         assertNotNull(testLeafJ);
130
131         final ContainerSchemaNode testContainerK = (ContainerSchemaNode) testModule.getDataChildByName(
132                 QName.create(testModule.getQNameModule(), "test-container-k"));
133         assertNotNull(testContainerK);
134         assertEquals(1, testContainerK.getUses().size());
135         testGroupingSubContainer = (ContainerSchemaNode) testContainerK.getDataChildByName(
136                 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
137         assertNotNull(testGroupingSubContainer);
138         assertEquals(1, testGroupingSubContainer.getAvailableAugmentations().size());
139         final LeafSchemaNode augmentingTestGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
140                 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf"));
141         assertNotNull(augmentingTestGroupingLeaf);
142         final LeafSchemaNode augmentingTestGroupingLeaf2 = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
143                 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf-2"));
144         assertNull(augmentingTestGroupingLeaf2);
145     }
146
147     @Test
148     public void testAllFeaturesSupported() throws ReactorException {
149         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
150         reactor.addSources(FOO_MODULE, BAR_MODULE);
151
152         final SchemaContext schemaContext = reactor.buildEffective();
153         assertNotNull(schemaContext);
154
155         final Module testModule = schemaContext.findModuleByName("foo", null);
156         assertNotNull(testModule);
157
158         assertEquals(11, testModule.getChildNodes().size());
159
160         final ContainerSchemaNode testContainerA = (ContainerSchemaNode) testModule.getDataChildByName(
161                 QName.create(testModule.getQNameModule(), "test-container-a"));
162         assertNotNull(testContainerA);
163         final LeafSchemaNode testLeafA = (LeafSchemaNode) testContainerA.getDataChildByName(
164                 QName.create(testModule.getQNameModule(), "test-leaf-a"));
165         assertNotNull(testLeafA);
166
167
168         final ContainerSchemaNode testContainerB = (ContainerSchemaNode) testModule.getDataChildByName(
169                 QName.create(testModule.getQNameModule(), "test-container-b"));
170         assertNotNull(testContainerB);
171         final LeafSchemaNode testLeafB = (LeafSchemaNode) testContainerB.getDataChildByName(
172                 QName.create(testModule.getQNameModule(), "test-leaf-b"));
173         assertNotNull(testLeafB);
174
175         final ContainerSchemaNode testContainerC = (ContainerSchemaNode) testModule.getDataChildByName(
176                 QName.create(testModule.getQNameModule(), "test-container-c"));
177         assertNotNull(testContainerC);
178         final LeafSchemaNode testLeafC = (LeafSchemaNode) testContainerC.getDataChildByName(
179                 QName.create(testModule.getQNameModule(), "test-leaf-c"));
180         assertNotNull(testLeafC);
181
182         final ContainerSchemaNode testContainerD = (ContainerSchemaNode) testModule.getDataChildByName(
183                 QName.create(testModule.getQNameModule(), "test-container-d"));
184         assertNotNull(testContainerD);
185         final LeafSchemaNode testLeafD = (LeafSchemaNode) testContainerD.getDataChildByName(
186                 QName.create(testModule.getQNameModule(), "test-leaf-d"));
187         assertNotNull(testLeafD);
188
189         final ContainerSchemaNode testContainerE = (ContainerSchemaNode) testModule.getDataChildByName(
190                 QName.create(testModule.getQNameModule(), "test-container-e"));
191         assertNotNull(testContainerE);
192         final ContainerSchemaNode testSubContainerE = (ContainerSchemaNode) testContainerE.getDataChildByName(
193                 QName.create(testModule.getQNameModule(), "test-subcontainer-e"));
194         assertNotNull(testSubContainerE);
195         final LeafSchemaNode testLeafE = (LeafSchemaNode) testSubContainerE.getDataChildByName(
196                 QName.create(testModule.getQNameModule(), "test-leaf-e"));
197         assertNotNull(testLeafE);
198
199         final ContainerSchemaNode testContainerF = (ContainerSchemaNode) testModule.getDataChildByName(
200                 QName.create(testModule.getQNameModule(), "test-container-f"));
201         assertNotNull(testContainerF);
202         final ContainerSchemaNode testSubContainerF = (ContainerSchemaNode) testContainerF.getDataChildByName(
203                 QName.create(testModule.getQNameModule(), "test-subcontainer-f"));
204         assertNotNull(testSubContainerF);
205         final ContainerSchemaNode testSubSubContainerF = (ContainerSchemaNode) testSubContainerF.getDataChildByName(
206                 QName.create(testModule.getQNameModule(), "test-subsubcontainer-f"));
207         assertNotNull(testSubSubContainerF);
208         final LeafSchemaNode testLeafF = (LeafSchemaNode) testSubSubContainerF.getDataChildByName(
209                 QName.create(testModule.getQNameModule(), "test-leaf-f"));
210         assertNotNull(testLeafF);
211
212         final ContainerSchemaNode testContainerG = (ContainerSchemaNode) testModule.getDataChildByName(
213                 QName.create(testModule.getQNameModule(), "test-container-g"));
214         assertNotNull(testContainerG);
215         assertEquals(2, testContainerG.getAvailableAugmentations().size());
216         final LeafSchemaNode testLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
217                 QName.create(testModule.getQNameModule(), "test-leaf-g"));
218         assertNotNull(testLeafG);
219         final LeafSchemaNode augmentingTestLeafG = (LeafSchemaNode) testContainerG.getDataChildByName(
220                 QName.create(testModule.getQNameModule(), "augmenting-test-leaf-g"));
221         assertNotNull(augmentingTestLeafG);
222         final AnyXmlSchemaNode augmentingTestAnyxmlG = (AnyXmlSchemaNode) testContainerG.getDataChildByName(
223                 QName.create(testModule.getQNameModule(), "augmenting-test-anyxml-g"));
224         assertNotNull(augmentingTestAnyxmlG);
225
226         final ContainerSchemaNode testContainerH = (ContainerSchemaNode) testModule.getDataChildByName(
227                 QName.create(testModule.getQNameModule(), "test-container-h"));
228         assertNotNull(testContainerH);
229         assertEquals(1, testContainerH.getUses().size());
230         ContainerSchemaNode testGroupingSubContainer = (ContainerSchemaNode) testContainerH.getDataChildByName(
231                 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
232         assertNotNull(testGroupingSubContainer);
233         LeafSchemaNode testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
234                 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
235         assertNotNull(testGroupingLeaf);
236
237         final ContainerSchemaNode testContainerI = (ContainerSchemaNode) testModule.getDataChildByName(
238                 QName.create(testModule.getQNameModule(), "test-container-i"));
239         assertNotNull(testContainerI);
240         assertEquals(1, testContainerI.getUses().size());
241         testGroupingSubContainer = (ContainerSchemaNode) testContainerI.getDataChildByName(
242                 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
243         assertNotNull(testGroupingSubContainer);
244         testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
245                 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
246         assertNotNull(testGroupingLeaf);
247
248         final ContainerSchemaNode testContainerJ = (ContainerSchemaNode) testModule.getDataChildByName(
249                 QName.create(testModule.getQNameModule(), "test-container-j"));
250         assertNotNull(testContainerJ);
251         final LeafSchemaNode testLeafJ = (LeafSchemaNode) testContainerJ.getDataChildByName(
252                 QName.create(testModule.getQNameModule(), "test-leaf-j"));
253         assertNotNull(testLeafJ);
254
255         final ContainerSchemaNode testContainerK = (ContainerSchemaNode) testModule.getDataChildByName(
256                 QName.create(testModule.getQNameModule(), "test-container-k"));
257         assertNotNull(testContainerK);
258         assertEquals(1, testContainerK.getUses().size());
259         testGroupingSubContainer = (ContainerSchemaNode) testContainerK.getDataChildByName(
260                 QName.create(testModule.getQNameModule(), "test-grouping-subcontainer"));
261         assertNotNull(testGroupingSubContainer);
262         assertEquals(1, testGroupingSubContainer.getAvailableAugmentations().size());
263         final LeafSchemaNode augmentingTestGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
264                 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf"));
265         assertNotNull(augmentingTestGroupingLeaf);
266         final LeafSchemaNode augmentingTestGroupingLeaf2 = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
267                 QName.create(testModule.getQNameModule(), "augmenting-test-grouping-leaf-2"));
268         assertNotNull(augmentingTestGroupingLeaf2);
269         testGroupingLeaf = (LeafSchemaNode) testGroupingSubContainer.getDataChildByName(
270                 QName.create(testModule.getQNameModule(), "test-grouping-leaf"));
271         assertNotNull(testGroupingLeaf);
272     }
273
274     @Test
275     public void testNoFeaturesSupported() throws ReactorException {
276         final Set<QName> supportedFeatures = ImmutableSet.of();
277
278         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(supportedFeatures);
279         reactor.addSources(FOO_MODULE, BAR_MODULE);
280
281         final SchemaContext schemaContext = reactor.buildEffective();
282         assertNotNull(schemaContext);
283
284         final Module testModule = schemaContext.findModuleByName("foo", null);
285         assertNotNull(testModule);
286
287         assertEquals(6, testModule.getChildNodes().size());
288
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);
298
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);
305
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);
319
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());
325
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);
336
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);
354     }
355 }