BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / IncludedStmtsTest.java
1 /*
2  * Copyright (c) 2016 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;
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 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
17
18 import java.util.Iterator;
19 import java.util.Set;
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;
33
34 public class IncludedStmtsTest {
35
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");
40
41     private SchemaContext result;
42
43     @Before
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();
48     }
49
50     @Test
51     public void includedTypedefsTest() {
52         final Module testModule = result.findModules("root-module").iterator().next();
53         assertNotNull(testModule);
54
55         final Set<TypeDefinition<?>> typedefs = testModule.getTypeDefinitions();
56         assertEquals(2, typedefs.size());
57
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")));
65     }
66
67     @Test
68     public void includedFeaturesTest() {
69         final Module testModule = result.findModules("root-module").iterator().next();
70         assertNotNull(testModule);
71
72         final Set<FeatureDefinition> features = testModule.getFeatures();
73         assertEquals(2, features.size());
74
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")));
80     }
81
82     @Test
83     public void includedContainersAndListsTest() {
84         final Module testModule = result.findModules("root-module").iterator().next();
85         assertNotNull(testModule);
86
87         ContainerSchemaNode cont = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(testModule.getQNameModule(), "parent-container"));
88         assertNotNull(cont);
89         cont = (ContainerSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "child-container"));
90         assertNotNull(cont);
91         assertEquals(2, cont.getChildNodes().size());
92
93         LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "autumn-leaf"));
94         assertNotNull(leaf);
95         leaf = (LeafSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "winter-snow"));
96         assertNotNull(leaf);
97     }
98
99     @Test
100     public void submoduleNamespaceTest() {
101         final Module testModule = result.findModules("root-module").iterator().next();
102         assertNotNull(testModule);
103
104         final Module subModule = testModule.getSubmodules().iterator().next();
105         assertEquals("urn:opendaylight.org/root-module", subModule.getNamespace().toString());
106     }
107 }