Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / IncludedStmtsTest.java
1 /*
2  * Copyright (c) 2015 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.test;
10
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
17 import java.util.Iterator;
18 import java.util.Set;
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;
30
31 public class IncludedStmtsTest {
32
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);
37
38     @Test
39     public void includedTypedefsTest() throws ReactorException {
40         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
41         StmtTestUtils.addSources(reactor, ROOT_MODULE, CHILD_MODULE);
42
43         EffectiveSchemaContext result = reactor.buildEffective();
44         assertNotNull(result);
45
46         Module testModule = result.findModuleByName("root-module", null);
47         assertNotNull(testModule);
48
49         Set<TypeDefinition<?>> typedefs = testModule.getTypeDefinitions();
50         assertEquals(2, typedefs.size());
51
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")));
59     }
60
61     @Test
62     public void includedFeaturesTest() throws ReactorException {
63         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
64         StmtTestUtils.addSources(reactor, ROOT_MODULE, CHILD_MODULE);
65
66         EffectiveSchemaContext result = reactor.buildEffective();
67         assertNotNull(result);
68
69         Module testModule = result.findModuleByName("root-module", null);
70         assertNotNull(testModule);
71
72         Set<FeatureDefinition> features = testModule.getFeatures();
73         assertEquals(2, features.size());
74
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")));
80     }
81
82     @Test
83     public void includedContainersAndListsTest() throws ReactorException {
84         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
85         StmtTestUtils.addSources(reactor, ROOT_MODULE, CHILD_MODULE);
86
87         EffectiveSchemaContext result = reactor.buildEffective();
88         assertNotNull(result);
89
90         Module testModule = result.findModuleByName("root-module", null);
91         assertNotNull(testModule);
92
93         ContainerSchemaNode cont = (ContainerSchemaNode) testModule.getDataChildByName("parent-container");
94         assertNotNull(cont);
95         cont = (ContainerSchemaNode) cont.getDataChildByName("child-container");
96         assertNotNull(cont);
97         assertEquals(2, cont.getChildNodes().size());
98
99         LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName("autumn-leaf");
100         assertNotNull(leaf);
101         leaf = (LeafSchemaNode) cont.getDataChildByName("winter-snow");
102         assertNotNull(leaf);
103     }
104
105     @Test
106     public void submoduleNamespaceTest() throws ReactorException {
107         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
108         StmtTestUtils.addSources(reactor, ROOT_MODULE, CHILD_MODULE);
109
110         EffectiveSchemaContext result = reactor.buildEffective();
111         assertNotNull(result);
112
113         Module testModule = result.findModuleByName("root-module", null);
114         assertNotNull(testModule);
115
116         Module subModule = testModule.getSubmodules().iterator().next();
117         assertEquals("urn:opendaylight.org/root-module", subModule.getNamespace().toString());
118     }
119 }