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.hamcrest.CoreMatchers.anyOf;
12 import static org.hamcrest.CoreMatchers.is;
13 import static org.hamcrest.MatcherAssert.assertThat;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
18 import java.util.Iterator;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
31 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
34 public class IncludedStmtsTest {
36 private static final StatementStreamSource ROOT_MODULE = sourceForResource(
37 "/included-statements-test/root-module.yang");
38 private static final StatementStreamSource CHILD_MODULE = sourceForResource(
39 "/included-statements-test/child-module.yang");
41 private SchemaContext result;
44 public void setup() throws ReactorException {
45 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
46 reactor.addSources(ROOT_MODULE, CHILD_MODULE);
47 result = reactor.buildEffective();
51 public void includedTypedefsTest() {
52 final Module testModule = result.findModules("root-module").iterator().next();
53 assertNotNull(testModule);
55 final Set<TypeDefinition<?>> typedefs = testModule.getTypeDefinitions();
56 assertEquals(2, typedefs.size());
58 final Iterator<TypeDefinition<?>> typedefsIterator = typedefs.iterator();
59 TypeDefinition<?> typedef = typedefsIterator.next();
60 assertThat(typedef.getQName().getLocalName(), anyOf(is("new-string-type"), is("new-int32-type")));
61 assertThat(typedef.getBaseType().getQName().getLocalName(), anyOf(is("string"), is("int32")));
62 typedef = typedefsIterator.next();
63 assertThat(typedef.getQName().getLocalName(), anyOf(is("new-string-type"), is("new-int32-type")));
64 assertThat(typedef.getBaseType().getQName().getLocalName(), anyOf(is("string"), is("int32")));
68 public void includedFeaturesTest() {
69 final Module testModule = result.findModules("root-module").iterator().next();
70 assertNotNull(testModule);
72 final Set<FeatureDefinition> features = testModule.getFeatures();
73 assertEquals(2, features.size());
75 final Iterator<FeatureDefinition> featuresIterator = features.iterator();
76 FeatureDefinition feature = featuresIterator.next();
77 assertThat(feature.getQName().getLocalName(), anyOf(is("new-feature1"), is("new-feature2")));
78 feature = featuresIterator.next();
79 assertThat(feature.getQName().getLocalName(), anyOf(is("new-feature1"), is("new-feature2")));
83 public void includedContainersAndListsTest() {
84 final Module testModule = result.findModules("root-module").iterator().next();
85 assertNotNull(testModule);
87 ContainerSchemaNode cont = (ContainerSchemaNode) testModule.getDataChildByName(
88 QName.create(testModule.getQNameModule(), "parent-container"));
90 cont = (ContainerSchemaNode) cont.getDataChildByName(
91 QName.create(testModule.getQNameModule(), "child-container"));
93 assertEquals(2, cont.getChildNodes().size());
95 LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName(
96 QName.create(testModule.getQNameModule(), "autumn-leaf"));
98 leaf = (LeafSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "winter-snow"));
103 public void submoduleNamespaceTest() {
104 final Module testModule = result.findModules("root-module").iterator().next();
105 assertNotNull(testModule);
107 final Module subModule = testModule.getSubmodules().iterator().next();
108 assertEquals("urn:opendaylight.org/root-module", subModule.getNamespace().toString());