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.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;
17 import java.text.ParseException;
18 import java.util.Collection;
20 import java.util.function.Predicate;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
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.Submodule;
33 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.ArgumentStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
38 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
39 import org.opendaylight.yangtools.yang.model.api.stmt.CaseStatement;
40 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
41 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
42 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigStatement;
43 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
44 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultStatement;
47 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
48 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
49 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureStatement;
50 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingStatement;
51 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
52 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
53 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
54 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
55 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
56 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
57 import org.opendaylight.yangtools.yang.model.api.stmt.MustStatement;
58 import org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement;
59 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
60 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceStatement;
61 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
62 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
63 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
64 import org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement;
65 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
66 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
67 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
68 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
69 import org.opendaylight.yangtools.yang.model.api.stmt.YangVersionStatement;
70 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
71 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
73 public class DeclaredStatementsTest {
76 public void testDeclaredAnyXml() throws ReactorException {
77 final StatementStreamSource anyxmlStmtModule =
78 sourceForResource("/declared-statements-test/anyxml-declared-test.yang");
80 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(anyxmlStmtModule);
81 assertNotNull(schemaContext);
83 final Module testModule = schemaContext.findModules("anyxml-declared-test").iterator().next();
84 assertNotNull(testModule);
86 final AnyxmlSchemaNode anyxmlSchemaNode = (AnyxmlSchemaNode) testModule.getDataChildByName(
87 QName.create(testModule.getQNameModule(), "foobar"));
88 assertNotNull(anyxmlSchemaNode);
89 final AnyxmlStatement anyxmlStatement = ((AnyxmlEffectiveStatement) anyxmlSchemaNode).getDeclared();
91 final QName name = anyxmlStatement.getName();
94 final WhenStatement whenStatement = anyxmlStatement.getWhenStatement().get();
95 final RevisionAwareXPath whenRevisionAwareXPath = whenStatement.getCondition();
96 assertNotNull(whenRevisionAwareXPath);
97 final DescriptionStatement whenStatementDescription = whenStatement.getDescription().get();
98 assertTrue(whenStatement.getReference().isPresent());
100 final Collection<? extends IfFeatureStatement> ifFeatureStatements = anyxmlStatement.getIfFeatures();
101 assertNotNull(ifFeatureStatements);
102 assertEquals(1, ifFeatureStatements.size());
103 final Predicate<Set<QName>> ifFeaturePredicate = ifFeatureStatements.iterator().next().getIfFeaturePredicate();
104 assertNotNull(ifFeaturePredicate);
106 final Collection<? extends MustStatement> mustStatements = anyxmlStatement.getMustStatements();
107 assertNotNull(mustStatements);
108 assertEquals(1, mustStatements.size());
109 final MustStatement mustStatement = mustStatements.iterator().next();
110 final RevisionAwareXPath mustRevisionAwareXPath = mustStatement.getCondition();
111 assertNotNull(mustRevisionAwareXPath);
112 assertTrue(mustStatement.getErrorAppTagStatement().isPresent());
113 assertTrue(mustStatement.getErrorMessageStatement().isPresent());
114 assertTrue(mustStatement.getDescription().isPresent());
115 assertTrue(mustStatement.getReference().isPresent());
117 final ConfigStatement configStatement = anyxmlStatement.getConfig().get();
118 assertFalse(configStatement.getValue());
120 final StatusStatement statusStatement = anyxmlStatement.getStatus().get();
121 final Status status = statusStatement.getValue();
122 assertNotNull(status);
124 final DescriptionStatement descriptionStatement = anyxmlStatement.getDescription().get();
125 assertEquals("anyxml description", descriptionStatement.getText());
127 final ReferenceStatement referenceStatement = anyxmlStatement.getReference().get();
128 assertEquals("anyxml reference", referenceStatement.getText());
130 assertTrue(anyxmlStatement.getMandatory().isPresent());
134 public void testDeclaredChoice() throws ReactorException {
135 final StatementStreamSource choiceStmtModule =
136 sourceForResource("/declared-statements-test/choice-declared-test.yang");
138 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(choiceStmtModule);
139 assertNotNull(schemaContext);
141 final Module testModule = schemaContext.findModules("choice-declared-test").iterator().next();
142 assertNotNull(testModule);
144 final ChoiceSchemaNode choiceSchemaNode = (ChoiceSchemaNode) testModule.getDataChildByName(
145 QName.create(testModule.getQNameModule(), "test-choice"));
146 assertNotNull(choiceSchemaNode);
147 final ChoiceStatement choiceStatement = ((ChoiceEffectiveStatement) choiceSchemaNode).getDeclared();
149 final QName name = choiceStatement.getName();
152 final DefaultStatement defaultStatement = choiceStatement.getDefault().get();
153 assertEquals("case-two", defaultStatement.getValue());
155 assertTrue(choiceStatement.getConfig().isPresent());
156 assertTrue(choiceStatement.getMandatory().isPresent());
158 final Collection<? extends CaseStatement> caseStatements = choiceStatement.getCases();
159 assertNotNull(caseStatements);
160 assertEquals(3, caseStatements.size());
161 final CaseStatement caseStatement = caseStatements.iterator().next();
162 final QName caseStatementName = caseStatement.getName();
163 assertNotNull(caseStatementName);
164 final WhenStatement caseStatementWhen = caseStatement.getWhenStatement().get();
165 final Collection<? extends IfFeatureStatement> caseStatementIfFeatures = caseStatement.getIfFeatures();
166 assertNotNull(caseStatementIfFeatures);
167 assertEquals(1, caseStatementIfFeatures.size());
168 final Collection<? extends DataDefinitionStatement> caseStatementDataDefinitions =
169 caseStatement.getDataDefinitions();
170 assertNotNull(caseStatementDataDefinitions);
171 assertEquals(1, caseStatementDataDefinitions.size());
172 assertTrue(caseStatement.getStatus().isPresent());
173 assertTrue(caseStatement.getDescription().isPresent());
174 assertTrue(caseStatement.getReference().isPresent());
176 final WhenStatement whenStatement = choiceStatement.getWhenStatement().get();
178 final Collection<? extends IfFeatureStatement> ifFeatureStatements = choiceStatement.getIfFeatures();
179 assertNotNull(ifFeatureStatements);
180 assertEquals(1, ifFeatureStatements.size());
182 assertTrue(choiceStatement.getStatus().isPresent());
183 assertTrue(choiceStatement.getDescription().isPresent());
184 assertTrue(choiceStatement.getReference().isPresent());
188 public void testDeclaredAugment() throws ReactorException {
189 final StatementStreamSource augmentStmtModule =
190 sourceForResource("/declared-statements-test/augment-declared-test.yang");
192 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(augmentStmtModule);
193 assertNotNull(schemaContext);
195 final Module testModule = schemaContext.findModules("augment-declared-test").iterator().next();
196 assertNotNull(testModule);
198 final Collection<? extends AugmentationSchemaNode> augmentationSchemas = testModule.getAugmentations();
199 assertNotNull(augmentationSchemas);
200 assertEquals(1, augmentationSchemas.size());
202 final AugmentationSchemaNode augmentationSchema = augmentationSchemas.iterator().next();
203 final AugmentStatement augmentStatement = ((AugmentEffectiveStatement) augmentationSchema).getDeclared();
205 final SchemaNodeIdentifier targetNode = augmentStatement.getTargetNode();
206 assertNotNull(targetNode);
208 final Collection<? extends DataDefinitionStatement> augmentStatementDataDefinitions =
209 augmentStatement.getDataDefinitions();
210 assertNotNull(augmentStatementDataDefinitions);
211 assertEquals(1, augmentStatementDataDefinitions.size());
215 public void testDeclaredModuleAndSubmodule() throws ReactorException {
216 final StatementStreamSource parentModule =
217 sourceForResource("/declared-statements-test/parent-module-declared-test.yang");
219 final StatementStreamSource childModule =
220 sourceForResource("/declared-statements-test/child-module-declared-test.yang");
222 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(parentModule, childModule);
223 assertNotNull(schemaContext);
225 final Module testModule = schemaContext.findModules("parent-module-declared-test").iterator().next();
226 assertNotNull(testModule);
228 final ModuleStatement moduleStatement = ((ModuleEffectiveStatement) testModule).getDeclared();
230 final String moduleStatementName = moduleStatement.getName();
231 assertNotNull(moduleStatementName);
233 final YangVersionStatement moduleStatementYangVersion = moduleStatement.getYangVersion();
234 assertNotNull(moduleStatementYangVersion);
235 assertNotNull(moduleStatementYangVersion.getValue());
237 final NamespaceStatement moduleStatementNamspace = moduleStatement.getNamespace();
238 assertNotNull(moduleStatementNamspace);
239 assertNotNull(moduleStatementNamspace.getUri());
241 final PrefixStatement moduleStatementPrefix = moduleStatement.getPrefix();
242 assertNotNull(moduleStatementPrefix);
243 assertNotNull(moduleStatementPrefix.getValue());
245 assertEquals(1, moduleStatement.getIncludes().size());
246 final IncludeStatement includeStatement = moduleStatement.getIncludes().iterator().next();
247 assertEquals("child-module-declared-test", includeStatement.getModule());
249 final Collection<? extends Submodule> submodules = testModule.getSubmodules();
250 assertNotNull(submodules);
251 assertEquals(1, submodules.size());
253 final Submodule submodule = submodules.iterator().next();
254 final SubmoduleStatement submoduleStatement = ((SubmoduleEffectiveStatement) submodule).getDeclared();
256 final String submoduleStatementName = submoduleStatement.getName();
257 assertNotNull(submoduleStatementName);
259 final YangVersionStatement submoduleStatementYangVersion = submoduleStatement.getYangVersion();
260 assertNotNull(submoduleStatementYangVersion);
262 final BelongsToStatement belongsToStatement = submoduleStatement.getBelongsTo();
263 assertNotNull(belongsToStatement);
264 assertNotNull(belongsToStatement.getModule());
265 assertNotNull(belongsToStatement.getPrefix());
269 public void testDeclaredModule() throws ReactorException, ParseException {
270 final StatementStreamSource rootModule =
271 sourceForResource("/declared-statements-test/root-module-declared-test.yang");
273 final StatementStreamSource importedModule =
274 sourceForResource("/declared-statements-test/imported-module-declared-test.yang");
276 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(rootModule, importedModule);
277 assertNotNull(schemaContext);
279 final Revision revision = Revision.of("2016-09-28");
280 final Module testModule = schemaContext.findModule("root-module-declared-test", revision).get();
281 assertNotNull(testModule);
283 final ModuleStatement moduleStatement = ((ModuleEffectiveStatement) testModule).getDeclared();
285 assertEquals(1, moduleStatement.getImports().size());
286 final ImportStatement importStatement = moduleStatement.getImports().iterator().next();
287 assertEquals("imported-module-declared-test", importStatement.getModule());
288 assertEquals("imdt", importStatement.getPrefix().getValue());
289 assertEquals(revision, importStatement.getRevisionDate().getDate());
291 assertEquals("test description", moduleStatement.getDescription().get().getText());
292 assertEquals("test reference", moduleStatement.getReference().get().getText());
293 assertEquals("test organization", moduleStatement.getOrganization().get().getText());
294 assertEquals("test contact", moduleStatement.getContact().get().getText());
296 assertEquals(1, moduleStatement.getRevisions().size());
297 final RevisionStatement revisionStatement = moduleStatement.getRevisions().iterator().next();
298 assertEquals(revision, revisionStatement.getDate());
299 assertEquals("test description", revisionStatement.getDescription().get().getText());
300 assertEquals("test reference", revisionStatement.getReference().get().getText());
302 assertEquals(1, moduleStatement.getExtensions().size());
303 final ExtensionStatement extensionStatement = moduleStatement.getExtensions().iterator().next();
304 assertEquals(Status.CURRENT, extensionStatement.getStatus().get().getValue());
305 assertEquals("test description", extensionStatement.getDescription().get().getText());
306 assertEquals("test reference", extensionStatement.getReference().get().getText());
307 final ArgumentStatement argumentStatement = extensionStatement.getArgument();
308 assertEquals("ext-argument", argumentStatement.getName().getLocalName());
309 assertTrue(argumentStatement.getYinElement().getValue());
311 assertEquals(2, moduleStatement.getFeatures().size());
312 final FeatureStatement featureStatement = moduleStatement.getFeatures().iterator().next();
313 assertEquals(Status.CURRENT, featureStatement.getStatus().get().getValue());
314 assertEquals("test description", featureStatement.getDescription().get().getText());
315 assertEquals("test reference", featureStatement.getReference().get().getText());
316 assertEquals("test-feature", featureStatement.getName().getLocalName());
317 assertEquals(1, featureStatement.getIfFeatures().size());
319 assertEquals(2, moduleStatement.getIdentities().size());
320 IdentityStatement identityStatement = null;
321 for (final IdentityStatement identity : moduleStatement.getIdentities()) {
322 if (identity.getName().getLocalName().equals("test-id")) {
323 identityStatement = identity;
327 assertEquals("test-base-id", identityStatement.getBases().iterator().next().getName().getLocalName());
328 assertEquals(Status.CURRENT, identityStatement.getStatus().get().getValue());
329 assertEquals("test description", identityStatement.getDescription().get().getText());
330 assertEquals("test reference", identityStatement.getReference().get().getText());
331 assertEquals("test-id", identityStatement.getName().getLocalName());
333 assertEquals(1, moduleStatement.getTypedefs().size());
334 final TypedefStatement typedefStatement = moduleStatement.getTypedefs().iterator().next();
335 assertEquals(Status.CURRENT, typedefStatement.getStatus().get().getValue());
336 assertEquals("test description", typedefStatement.getDescription().get().getText());
337 assertEquals("test reference", typedefStatement.getReference().get().getText());
338 assertEquals("test-typedef", typedefStatement.getName().getLocalName());
339 assertEquals("int32", typedefStatement.getType().getName());
340 assertEquals("meter", typedefStatement.getUnits().get().getName());
344 public void testDeclaredContainer() throws ReactorException {
345 final StatementStreamSource containerStmtModule =
346 sourceForResource("/declared-statements-test/container-declared-test.yang");
348 final SchemaContext schemaContext = StmtTestUtils.parseYangSources(containerStmtModule);
349 assertNotNull(schemaContext);
351 final Module testModule = schemaContext.findModules("container-declared-test").iterator().next();
352 assertNotNull(testModule);
354 final ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) testModule.getDataChildByName(
355 QName.create(testModule.getQNameModule(), "test-container"));
356 assertNotNull(containerSchemaNode);
357 final ContainerStatement containerStatement =
358 ((ContainerEffectiveStatement) containerSchemaNode).getDeclared();
360 final QName name = containerStatement.getName();
363 final WhenStatement containerStatementWhen = containerStatement.getWhenStatement().get();
365 final Collection<? extends IfFeatureStatement> containerStatementIfFeatures =
366 containerStatement.getIfFeatures();
367 assertNotNull(containerStatementIfFeatures);
368 assertEquals(1, containerStatementIfFeatures.size());
370 final Collection<? extends MustStatement> containerStatementMusts = containerStatement.getMustStatements();
371 assertNotNull(containerStatementMusts);
372 assertEquals(1, containerStatementMusts.size());
374 final PresenceStatement containerStatementPresence = containerStatement.getPresence();
375 assertNotNull(containerStatementPresence);
376 assertNotNull(containerStatementPresence.getValue());
378 assertTrue(containerStatement.getConfig().isPresent());
379 assertTrue(containerStatement.getStatus().isPresent());
380 assertTrue(containerStatement.getDescription().isPresent());
381 assertTrue(containerStatement.getReference().isPresent());
383 final Collection<? extends TypedefStatement> containerStatementTypedefs = containerStatement.getTypedefs();
384 assertNotNull(containerStatementTypedefs);
385 assertEquals(1, containerStatementTypedefs.size());
387 final Collection<? extends GroupingStatement> containerStatementGroupings = containerStatement.getGroupings();
388 assertNotNull(containerStatementGroupings);
389 assertEquals(1, containerStatementGroupings.size());
391 final Collection<? extends DataDefinitionStatement> containerStatementDataDefinitions =
392 containerStatement.getDataDefinitions();
394 assertNotNull(containerStatementDataDefinitions);
395 assertEquals(1, containerStatementDataDefinitions.size());