BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / DeclaredStatementsTest.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.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
16
17 import java.text.ParseException;
18 import java.util.Collection;
19 import java.util.Date;
20 import java.util.Set;
21 import java.util.function.Predicate;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.Status;
32 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.ArgumentStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.CaseStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
38 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigStatement;
39 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
40 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
41 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultStatement;
42 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
43 import org.opendaylight.yangtools.yang.model.api.stmt.ErrorAppTagStatement;
44 import org.opendaylight.yangtools.yang.model.api.stmt.ErrorMessageStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureStatement;
47 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingStatement;
48 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
49 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
50 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
51 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
52 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
53 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
54 import org.opendaylight.yangtools.yang.model.api.stmt.MustStatement;
55 import org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement;
56 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
57 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceStatement;
58 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
59 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
60 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
61 import org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement;
62 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
63 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
64 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
65 import org.opendaylight.yangtools.yang.model.api.stmt.YangVersionStatement;
66 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
67 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
68 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AnyXmlEffectiveStatementImpl;
69 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
70 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ChoiceEffectiveStatementImpl;
71 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ContainerEffectiveStatementImpl;
72 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ModuleEffectiveStatementImpl;
73 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.SubmoduleEffectiveStatementImpl;
74
75 public class DeclaredStatementsTest {
76
77     @Test
78     public void testDeclaredAnyXml() throws ReactorException {
79         final StatementStreamSource anyxmlStmtModule =
80                 sourceForResource("/declared-statements-test/anyxml-declared-test.yang");
81
82         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(anyxmlStmtModule);
83         assertNotNull(schemaContext);
84
85         final Module testModule = schemaContext.findModules("anyxml-declared-test").iterator().next();
86         assertNotNull(testModule);
87
88         final AnyXmlSchemaNode anyxmlSchemaNode = (AnyXmlSchemaNode) testModule.getDataChildByName(
89                 QName.create(testModule.getQNameModule(), "foobar"));
90         assertNotNull(anyxmlSchemaNode);
91         final AnyxmlStatement anyxmlStatement = ((AnyXmlEffectiveStatementImpl) anyxmlSchemaNode).getDeclared();
92
93         final QName name = anyxmlStatement.getName();
94         assertNotNull(name);
95
96         final WhenStatement whenStatement = anyxmlStatement.getWhenStatement();
97         assertNotNull(whenStatement);
98         final RevisionAwareXPath whenRevisionAwareXPath = whenStatement.getCondition();
99         assertNotNull(whenRevisionAwareXPath);
100         final DescriptionStatement whenStatementDescription = whenStatement.getDescription();
101         assertNotNull(whenStatementDescription);
102         final ReferenceStatement whenStatementReference = whenStatement.getReference();
103         assertNotNull(whenStatementReference);
104
105         final Collection<? extends IfFeatureStatement> ifFeatureStatements = anyxmlStatement.getIfFeatures();
106         assertNotNull(ifFeatureStatements);
107         assertEquals(1, ifFeatureStatements.size());
108         final Predicate<Set<QName>> ifFeaturePredicate = ifFeatureStatements.iterator().next().getIfFeaturePredicate();
109         assertNotNull(ifFeaturePredicate);
110
111         final Collection<? extends MustStatement> mustStatements = anyxmlStatement.getMusts();
112         assertNotNull(mustStatements);
113         assertEquals(1, mustStatements.size());
114         final MustStatement mustStatement = mustStatements.iterator().next();
115         final RevisionAwareXPath mustRevisionAwareXPath = mustStatement.getCondition();
116         assertNotNull(mustRevisionAwareXPath);
117         final ErrorAppTagStatement errorAppTagStatement = mustStatement.getErrorAppTagStatement();
118         assertNotNull(errorAppTagStatement);
119         final ErrorMessageStatement errorMessageStatement = mustStatement.getErrorMessageStatement();
120         assertNotNull(errorMessageStatement);
121         final DescriptionStatement mustStatementDescription = mustStatement.getDescription();
122         assertNotNull(mustStatementDescription);
123         final ReferenceStatement mustStatementReference = mustStatement.getReference();
124         assertNotNull(mustStatementReference);
125
126         final ConfigStatement configStatement = anyxmlStatement.getConfig();
127         assertNotNull(configStatement);
128         assertFalse(configStatement.getValue());
129
130         final StatusStatement statusStatement = anyxmlStatement.getStatus();
131         assertNotNull(statusStatement);
132         final Status status = statusStatement.getValue();
133         assertNotNull(status);
134
135         final DescriptionStatement descriptionStatement = anyxmlStatement.getDescription();
136         assertNotNull(descriptionStatement);
137         assertEquals("anyxml description", descriptionStatement.getText());
138
139         final ReferenceStatement referenceStatement = anyxmlStatement.getReference();
140         assertNotNull(referenceStatement);
141         assertEquals("anyxml reference", referenceStatement.getText());
142
143         final MandatoryStatement mandatoryStatement = anyxmlStatement.getMandatory();
144         assertNotNull(mandatoryStatement);
145     }
146
147     @Test
148     public void testDeclaredChoice() throws ReactorException {
149         final StatementStreamSource choiceStmtModule =
150                 sourceForResource("/declared-statements-test/choice-declared-test.yang");
151
152         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(choiceStmtModule);
153         assertNotNull(schemaContext);
154
155         final Module testModule = schemaContext.findModules("choice-declared-test").iterator().next();
156         assertNotNull(testModule);
157
158         final ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) testModule.getDataChildByName(
159                 QName.create(testModule.getQNameModule(), "test-choice"));
160         assertNotNull(choiceSchemaNode);
161         final ChoiceStatement choiceStatement = ((ChoiceEffectiveStatementImpl) choiceSchemaNode).getDeclared();
162
163         final QName name = choiceStatement.getName();
164         assertNotNull(name);
165
166         final DefaultStatement defaultStatement = choiceStatement.getDefault();
167         assertNotNull(defaultStatement);
168         assertEquals("case-two", defaultStatement.getValue());
169
170         final ConfigStatement configStatement = choiceStatement.getConfig();
171         assertNotNull(configStatement);
172
173         final MandatoryStatement mandatoryStatement = choiceStatement.getMandatory();
174         assertNotNull(mandatoryStatement);
175
176         final Collection<? extends CaseStatement> caseStatements = choiceStatement.getCases();
177         assertNotNull(caseStatements);
178         assertEquals(3, caseStatements.size());
179         final CaseStatement caseStatement = caseStatements.iterator().next();
180         final QName caseStatementName = caseStatement.getName();
181         assertNotNull(caseStatementName);
182         final WhenStatement caseStatementWhen = caseStatement.getWhenStatement();
183         assertNotNull(caseStatementWhen);
184         final Collection<? extends IfFeatureStatement> caseStatementIfFeatures = caseStatement.getIfFeatures();
185         assertNotNull(caseStatementIfFeatures);
186         assertEquals(1, caseStatementIfFeatures.size());
187         final Collection<? extends DataDefinitionStatement> caseStatementDataDefinitions = caseStatement.getDataDefinitions();
188         assertNotNull(caseStatementDataDefinitions);
189         assertEquals(1, caseStatementDataDefinitions.size());
190         final StatusStatement caseStatementStatus = caseStatement.getStatus();
191         assertNotNull(caseStatementStatus);
192         final DescriptionStatement caseStatementDescription = caseStatement.getDescription();
193         assertNotNull(caseStatementDescription);
194         final ReferenceStatement caseStatementReference = caseStatement.getReference();
195         assertNotNull(caseStatementReference);
196
197         final WhenStatement whenStatement = choiceStatement.getWhenStatement();
198         assertNotNull(whenStatement);
199
200         final Collection<? extends IfFeatureStatement> ifFeatureStatements = choiceStatement.getIfFeatures();
201         assertNotNull(ifFeatureStatements);
202         assertEquals(1, ifFeatureStatements.size());
203
204         final StatusStatement statusStatement = choiceStatement.getStatus();
205         assertNotNull(statusStatement);
206
207         final DescriptionStatement descriptionStatement = choiceStatement.getDescription();
208         assertNotNull(descriptionStatement);
209
210         final ReferenceStatement referenceStatement = choiceStatement.getReference();
211         assertNotNull(referenceStatement);
212     }
213
214     @Test
215     public void testDeclaredAugment() throws ReactorException {
216         final StatementStreamSource augmentStmtModule =
217                 sourceForResource("/declared-statements-test/augment-declared-test.yang");
218
219         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(augmentStmtModule);
220         assertNotNull(schemaContext);
221
222         final Module testModule = schemaContext.findModules("augment-declared-test").iterator().next();
223         assertNotNull(testModule);
224
225         final Set<AugmentationSchema> augmentationSchemas = testModule.getAugmentations();
226         assertNotNull(augmentationSchemas);
227         assertEquals(1, augmentationSchemas.size());
228
229         final AugmentationSchema augmentationSchema = augmentationSchemas.iterator().next();
230         final AugmentStatement augmentStatement = ((AugmentEffectiveStatementImpl) augmentationSchema).getDeclared();
231
232         final SchemaNodeIdentifier targetNode = augmentStatement.getTargetNode();
233         assertNotNull(targetNode);
234
235         final Collection<? extends DataDefinitionStatement> augmentStatementDataDefinitions =
236                 augmentStatement.getDataDefinitions();
237         assertNotNull(augmentStatementDataDefinitions);
238         assertEquals(1, augmentStatementDataDefinitions.size());
239     }
240
241     @Test
242     public void testDeclaredModuleAndSubmodule() throws ReactorException {
243         final StatementStreamSource parentModule =
244                 sourceForResource("/declared-statements-test/parent-module-declared-test.yang");
245
246         final StatementStreamSource childModule =
247                 sourceForResource("/declared-statements-test/child-module-declared-test.yang");
248
249         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(parentModule, childModule);
250         assertNotNull(schemaContext);
251
252         final Module testModule = schemaContext.findModules("parent-module-declared-test").iterator().next();
253         assertNotNull(testModule);
254
255         final ModuleStatement moduleStatement = ((ModuleEffectiveStatementImpl) testModule).getDeclared();
256
257         final String moduleStatementName = moduleStatement.getName();
258         assertNotNull(moduleStatementName);
259
260         final YangVersionStatement moduleStatementYangVersion = moduleStatement.getYangVersion();
261         assertNotNull(moduleStatementYangVersion);
262         assertNotNull(moduleStatementYangVersion.getValue());
263
264         final NamespaceStatement moduleStatementNamspace = moduleStatement.getNamespace();
265         assertNotNull(moduleStatementNamspace);
266         assertNotNull(moduleStatementNamspace.getUri());
267
268         final PrefixStatement moduleStatementPrefix= moduleStatement.getPrefix();
269         assertNotNull(moduleStatementPrefix);
270         assertNotNull(moduleStatementPrefix.getValue());
271
272         assertEquals(1, moduleStatement.getIncludes().size());
273         final IncludeStatement includeStatement = moduleStatement.getIncludes().iterator().next();
274         assertEquals("child-module-declared-test", includeStatement.getModule());
275
276         final Set<Module> submodules = testModule.getSubmodules();
277         assertNotNull(submodules);
278         assertEquals(1, submodules.size());
279
280         final Module submodule = submodules.iterator().next();
281         final SubmoduleStatement submoduleStatement = ((SubmoduleEffectiveStatementImpl) submodule).getDeclared();
282
283         final String submoduleStatementName = submoduleStatement.getName();
284         assertNotNull(submoduleStatementName);
285
286         final YangVersionStatement submoduleStatementYangVersion = submoduleStatement.getYangVersion();
287         assertNotNull(submoduleStatementYangVersion);
288
289         final BelongsToStatement belongsToStatement = submoduleStatement.getBelongsTo();
290         assertNotNull(belongsToStatement);
291         assertNotNull(belongsToStatement.getModule());
292         assertNotNull(belongsToStatement.getPrefix());
293     }
294
295     @Test
296     public void testDeclaredModule() throws ReactorException, ParseException {
297         final StatementStreamSource rootModule =
298                 sourceForResource("/declared-statements-test/root-module-declared-test.yang");
299
300         final StatementStreamSource importedModule =
301                 sourceForResource("/declared-statements-test/imported-module-declared-test.yang");
302
303         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(rootModule, importedModule);
304         assertNotNull(schemaContext);
305
306         final Date revision = QName.parseRevision("2016-09-28");
307
308         final Module testModule = schemaContext.findModule("root-module-declared-test", revision).get();
309         assertNotNull(testModule);
310
311         final ModuleStatement moduleStatement = ((ModuleEffectiveStatementImpl) testModule).getDeclared();
312
313         assertEquals(1, moduleStatement.getImports().size());
314         final ImportStatement importStatement = moduleStatement.getImports().iterator().next();
315         assertEquals("imported-module-declared-test", importStatement.getModule());
316         assertEquals("imdt", importStatement.getPrefix().getValue());
317         assertEquals(revision, importStatement.getRevisionDate().getDate());
318
319         assertEquals("test description", moduleStatement.getDescription().getText());
320         assertEquals("test reference", moduleStatement.getReference().getText());
321         assertEquals("test organization", moduleStatement.getOrganization().getText());
322         assertEquals("test contact", moduleStatement.getContact().getText());
323
324         assertEquals(1, moduleStatement.getRevisions().size());
325         final RevisionStatement revisionStatement = moduleStatement.getRevisions().iterator().next();
326         assertEquals(revision, revisionStatement.getDate());
327         assertEquals("test description", revisionStatement.getDescription().getText());
328         assertEquals("test reference", revisionStatement.getReference().getText());
329
330         assertEquals(1, moduleStatement.getExtensions().size());
331         final ExtensionStatement extensionStatement = moduleStatement.getExtensions().iterator().next();
332         assertEquals(Status.CURRENT, extensionStatement.getStatus().getValue());
333         assertEquals("test description", extensionStatement.getDescription().getText());
334         assertEquals("test reference", extensionStatement.getReference().getText());
335         final ArgumentStatement argumentStatement = extensionStatement.getArgument();
336         assertEquals("ext-argument", argumentStatement.getName().getLocalName());
337         assertTrue(argumentStatement.getYinElement().getValue());
338
339         assertEquals(1, moduleStatement.getFeatures().size());
340         final FeatureStatement featureStatement = moduleStatement.getFeatures().iterator().next();
341         assertEquals(Status.CURRENT, featureStatement.getStatus().getValue());
342         assertEquals("test description", featureStatement.getDescription().getText());
343         assertEquals("test reference", featureStatement.getReference().getText());
344         assertEquals("test-feature", featureStatement.getName().getLocalName());
345         assertEquals(1, featureStatement.getIfFeatures().size());
346
347         assertEquals(2, moduleStatement.getIdentities().size());
348         IdentityStatement identityStatement = null;
349         for (final IdentityStatement identity : moduleStatement.getIdentities()) {
350             if (identity.getName().getLocalName().equals("test-id")) {
351                 identityStatement = identity;
352             }
353         }
354
355         assertEquals("test-base-id", identityStatement.getBase().getName().getLocalName());
356         assertEquals(Status.CURRENT, identityStatement.getStatus().getValue());
357         assertEquals("test description", identityStatement.getDescription().getText());
358         assertEquals("test reference", identityStatement.getReference().getText());
359         assertEquals("test-id", identityStatement.getName().getLocalName());
360
361         assertEquals(1, moduleStatement.getTypedefs().size());
362         final TypedefStatement typedefStatement = moduleStatement.getTypedefs().iterator().next();
363         assertEquals(Status.CURRENT, typedefStatement.getStatus().getValue());
364         assertEquals("test description", typedefStatement.getDescription().getText());
365         assertEquals("test reference", typedefStatement.getReference().getText());
366         assertEquals("test-typedef", typedefStatement.getName().getLocalName());
367         assertEquals("int32", typedefStatement.getType().getName());
368         assertEquals("meter", typedefStatement.getUnits().getName());
369     }
370
371     @Test
372     public void testDeclaredContainer() throws ReactorException {
373         final StatementStreamSource containerStmtModule =
374                 sourceForResource("/declared-statements-test/container-declared-test.yang");
375
376         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(containerStmtModule);
377         assertNotNull(schemaContext);
378
379         final Module testModule = schemaContext.findModules("container-declared-test").iterator().next();
380         assertNotNull(testModule);
381
382         final ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) testModule.getDataChildByName(
383                 QName.create(testModule.getQNameModule(), "test-container"));
384         assertNotNull(containerSchemaNode);
385         final ContainerStatement containerStatement = ((ContainerEffectiveStatementImpl) containerSchemaNode).getDeclared();
386
387         final QName name = containerStatement.getName();
388         assertNotNull(name);
389
390         final WhenStatement containerStatementWhen = containerStatement.getWhenStatement();
391         assertNotNull(containerStatementWhen);
392
393         final Collection<? extends IfFeatureStatement> containerStatementIfFeatures = containerStatement.getIfFeatures();
394         assertNotNull(containerStatementIfFeatures);
395         assertEquals(1, containerStatementIfFeatures.size());
396
397         final Collection<? extends MustStatement> containerStatementMusts = containerStatement.getMusts();
398         assertNotNull(containerStatementMusts);
399         assertEquals(1, containerStatementMusts.size());
400
401         final PresenceStatement containerStatementPresence = containerStatement.getPresence();
402         assertNotNull(containerStatementPresence);
403         assertNotNull(containerStatementPresence.getValue());
404
405         final ConfigStatement containerStatementConfig = containerStatement.getConfig();
406         assertNotNull(containerStatementConfig);
407
408         final StatusStatement containerStatementStatus = containerStatement.getStatus();
409         assertNotNull(containerStatementStatus);
410
411         final DescriptionStatement containerStatementDescription = containerStatement.getDescription();
412         assertNotNull(containerStatementDescription);
413
414         final ReferenceStatement containerStatementReference = containerStatement.getReference();
415         assertNotNull(containerStatementReference);
416
417         final Collection<? extends TypedefStatement> containerStatementTypedefs = containerStatement.getTypedefs();
418         assertNotNull(containerStatementTypedefs);
419         assertEquals(1, containerStatementTypedefs.size());
420
421         final Collection<? extends GroupingStatement> containerStatementGroupings = containerStatement.getGroupings();
422         assertNotNull(containerStatementGroupings);
423         assertEquals(1, containerStatementGroupings.size());
424
425         final Collection<? extends DataDefinitionStatement> containerStatementDataDefinitions =
426                 containerStatement.getDataDefinitions();
427
428         assertNotNull(containerStatementDataDefinitions);
429         assertEquals(1, containerStatementDataDefinitions.size());
430     }
431 }