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