Update StmtTestUtils
[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.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;
32
33 public class IncludedStmtsTest {
34
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");
39
40     @Test
41     public void includedTypedefsTest() throws ReactorException {
42         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
43         reactor.addSources(ROOT_MODULE, CHILD_MODULE);
44
45         final EffectiveSchemaContext result = reactor.buildEffective();
46         assertNotNull(result);
47
48         final Module testModule = result.findModuleByName("root-module", null);
49         assertNotNull(testModule);
50
51         final Set<TypeDefinition<?>> typedefs = testModule.getTypeDefinitions();
52         assertEquals(2, typedefs.size());
53
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")));
61     }
62
63     @Test
64     public void includedFeaturesTest() throws ReactorException {
65         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
66         reactor.addSources(ROOT_MODULE, CHILD_MODULE);
67
68         final EffectiveSchemaContext result = reactor.buildEffective();
69         assertNotNull(result);
70
71         final Module testModule = result.findModuleByName("root-module", null);
72         assertNotNull(testModule);
73
74         final Set<FeatureDefinition> features = testModule.getFeatures();
75         assertEquals(2, features.size());
76
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")));
82     }
83
84     @Test
85     public void includedContainersAndListsTest() throws ReactorException {
86         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
87         reactor.addSources(ROOT_MODULE, CHILD_MODULE);
88
89         final EffectiveSchemaContext result = reactor.buildEffective();
90         assertNotNull(result);
91
92         final Module testModule = result.findModuleByName("root-module", null);
93         assertNotNull(testModule);
94
95         ContainerSchemaNode cont = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(testModule.getQNameModule(), "parent-container"));
96         assertNotNull(cont);
97         cont = (ContainerSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "child-container"));
98         assertNotNull(cont);
99         assertEquals(2, cont.getChildNodes().size());
100
101         LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "autumn-leaf"));
102         assertNotNull(leaf);
103         leaf = (LeafSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "winter-snow"));
104         assertNotNull(leaf);
105     }
106
107     @Test
108     public void submoduleNamespaceTest() throws ReactorException {
109         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
110         reactor.addSources(ROOT_MODULE, CHILD_MODULE);
111
112         final EffectiveSchemaContext result = reactor.buildEffective();
113         assertNotNull(result);
114
115         final Module testModule = result.findModuleByName("root-module", null);
116         assertNotNull(testModule);
117
118         final Module subModule = testModule.getSubmodules().iterator().next();
119         assertEquals("urn:opendaylight.org/root-module", subModule.getNamespace().toString());
120     }
121 }