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.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
33 public class IncludedStmtsTest {
35 private static final StatementStreamSource ROOT_MODULE = sourceForResource(
36 "/included-statements-test/root-module.yang");
37 private static final StatementStreamSource CHILD_MODULE = sourceForResource(
38 "/included-statements-test/child-module.yang");
41 public void includedTypedefsTest() throws ReactorException {
42 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
43 reactor.addSources(ROOT_MODULE, CHILD_MODULE);
45 final EffectiveSchemaContext result = reactor.buildEffective();
46 assertNotNull(result);
48 final Module testModule = result.findModuleByName("root-module", null);
49 assertNotNull(testModule);
51 final Set<TypeDefinition<?>> typedefs = testModule.getTypeDefinitions();
52 assertEquals(2, typedefs.size());
54 final Iterator<TypeDefinition<?>> typedefsIterator = typedefs.iterator();
55 TypeDefinition<?> typedef = typedefsIterator.next();
56 assertThat(typedef.getQName().getLocalName(), anyOf(is("new-string-type"), is("new-int32-type")));
57 assertThat(typedef.getBaseType().getQName().getLocalName(), anyOf(is("string"), is("int32")));
58 typedef = typedefsIterator.next();
59 assertThat(typedef.getQName().getLocalName(), anyOf(is("new-string-type"), is("new-int32-type")));
60 assertThat(typedef.getBaseType().getQName().getLocalName(), anyOf(is("string"), is("int32")));
64 public void includedFeaturesTest() throws ReactorException {
65 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
66 reactor.addSources(ROOT_MODULE, CHILD_MODULE);
68 final EffectiveSchemaContext result = reactor.buildEffective();
69 assertNotNull(result);
71 final Module testModule = result.findModuleByName("root-module", null);
72 assertNotNull(testModule);
74 final Set<FeatureDefinition> features = testModule.getFeatures();
75 assertEquals(2, features.size());
77 final Iterator<FeatureDefinition> featuresIterator = features.iterator();
78 FeatureDefinition feature = featuresIterator.next();
79 assertThat(feature.getQName().getLocalName(), anyOf(is("new-feature1"), is("new-feature2")));
80 feature = featuresIterator.next();
81 assertThat(feature.getQName().getLocalName(), anyOf(is("new-feature1"), is("new-feature2")));
85 public void includedContainersAndListsTest() throws ReactorException {
86 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
87 reactor.addSources(ROOT_MODULE, CHILD_MODULE);
89 final EffectiveSchemaContext result = reactor.buildEffective();
90 assertNotNull(result);
92 final Module testModule = result.findModuleByName("root-module", null);
93 assertNotNull(testModule);
95 ContainerSchemaNode cont = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(testModule.getQNameModule(), "parent-container"));
97 cont = (ContainerSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "child-container"));
99 assertEquals(2, cont.getChildNodes().size());
101 LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "autumn-leaf"));
103 leaf = (LeafSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "winter-snow"));
108 public void submoduleNamespaceTest() throws ReactorException {
109 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
110 reactor.addSources(ROOT_MODULE, CHILD_MODULE);
112 final EffectiveSchemaContext result = reactor.buildEffective();
113 assertNotNull(result);
115 final Module testModule = result.findModuleByName("root-module", null);
116 assertNotNull(testModule);
118 final Module subModule = testModule.getSubmodules().iterator().next();
119 assertEquals("urn:opendaylight.org/root-module", subModule.getNamespace().toString());