2 * Copyright (c) 2015 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.test;
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;
17 import java.util.Iterator;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
31 public class IncludedStmtsTest {
33 private static final YangStatementSourceImpl ROOT_MODULE = new YangStatementSourceImpl
34 ("/included-statements-test/root-module.yang", false);
35 private static final YangStatementSourceImpl CHILD_MODULE = new YangStatementSourceImpl
36 ("/included-statements-test/child-module.yang", false);
39 public void includedTypedefsTest() throws ReactorException {
40 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
41 StmtTestUtils.addSources(reactor, ROOT_MODULE, CHILD_MODULE);
43 EffectiveSchemaContext result = reactor.buildEffective();
44 assertNotNull(result);
46 Module testModule = result.findModuleByName("root-module", null);
47 assertNotNull(testModule);
49 Set<TypeDefinition<?>> typedefs = testModule.getTypeDefinitions();
50 assertEquals(2, typedefs.size());
52 Iterator<TypeDefinition<?>> typedefsIterator = typedefs.iterator();
53 TypeDefinition<?> typedef = typedefsIterator.next();
54 assertThat(typedef.getQName().getLocalName(), anyOf(is("new-string-type"), is("new-int32-type")));
55 assertThat(typedef.getBaseType().getQName().getLocalName(), anyOf(is("string"), is("int32")));
56 typedef = typedefsIterator.next();
57 assertThat(typedef.getQName().getLocalName(), anyOf(is("new-string-type"), is("new-int32-type")));
58 assertThat(typedef.getBaseType().getQName().getLocalName(), anyOf(is("string"), is("int32")));
62 public void includedFeaturesTest() throws ReactorException {
63 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
64 StmtTestUtils.addSources(reactor, ROOT_MODULE, CHILD_MODULE);
66 EffectiveSchemaContext result = reactor.buildEffective();
67 assertNotNull(result);
69 Module testModule = result.findModuleByName("root-module", null);
70 assertNotNull(testModule);
72 Set<FeatureDefinition> features = testModule.getFeatures();
73 assertEquals(2, features.size());
75 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() throws ReactorException {
84 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
85 StmtTestUtils.addSources(reactor, ROOT_MODULE, CHILD_MODULE);
87 EffectiveSchemaContext result = reactor.buildEffective();
88 assertNotNull(result);
90 Module testModule = result.findModuleByName("root-module", null);
91 assertNotNull(testModule);
93 ContainerSchemaNode cont = (ContainerSchemaNode) testModule.getDataChildByName("parent-container");
95 cont = (ContainerSchemaNode) cont.getDataChildByName("child-container");
97 assertEquals(2, cont.getChildNodes().size());
99 LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName("autumn-leaf");
101 leaf = (LeafSchemaNode) cont.getDataChildByName("winter-snow");
106 public void submoduleNamespaceTest() throws ReactorException {
107 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
108 StmtTestUtils.addSources(reactor, ROOT_MODULE, CHILD_MODULE);
110 EffectiveSchemaContext result = reactor.buildEffective();
111 assertNotNull(result);
113 Module testModule = result.findModuleByName("root-module", null);
114 assertNotNull(testModule);
116 Module subModule = testModule.getSubmodules().iterator().next();
117 assertEquals("urn:opendaylight.org/root-module", subModule.getNamespace().toString());