YANGTOOLS-706: Split out yang-parser-rfc7950
[yangtools.git] / yang / yang-parser-rfc7950 / 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.BeforeClass;
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.rfc7950.reactor.RFC7950Reactors;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31
32 public class IncludedStmtsTest {
33     private static SchemaContext result;
34
35     @BeforeClass
36     public static void setup() throws ReactorException {
37         result = RFC7950Reactors.defaultReactor().newBuild()
38                 .addSource(sourceForResource("/included-statements-test/root-module.yang"))
39                 .addSource(sourceForResource("/included-statements-test/child-module.yang"))
40                 .buildEffective();
41     }
42
43     @Test
44     public void includedTypedefsTest() {
45         final Module testModule = result.findModules("root-module").iterator().next();
46         assertNotNull(testModule);
47
48         final Set<TypeDefinition<?>> typedefs = testModule.getTypeDefinitions();
49         assertEquals(2, typedefs.size());
50
51         final Iterator<TypeDefinition<?>> typedefsIterator = typedefs.iterator();
52         TypeDefinition<?> typedef = typedefsIterator.next();
53         assertThat(typedef.getQName().getLocalName(), anyOf(is("new-string-type"), is("new-int32-type")));
54         assertThat(typedef.getBaseType().getQName().getLocalName(), anyOf(is("string"), is("int32")));
55         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     }
59
60     @Test
61     public void includedFeaturesTest() {
62         final Module testModule = result.findModules("root-module").iterator().next();
63         assertNotNull(testModule);
64
65         final Set<FeatureDefinition> features = testModule.getFeatures();
66         assertEquals(2, features.size());
67
68         final Iterator<FeatureDefinition> featuresIterator = features.iterator();
69         FeatureDefinition feature = featuresIterator.next();
70         assertThat(feature.getQName().getLocalName(), anyOf(is("new-feature1"), is("new-feature2")));
71         feature = featuresIterator.next();
72         assertThat(feature.getQName().getLocalName(), anyOf(is("new-feature1"), is("new-feature2")));
73     }
74
75     @Test
76     public void includedContainersAndListsTest() {
77         final Module testModule = result.findModules("root-module").iterator().next();
78         assertNotNull(testModule);
79
80         ContainerSchemaNode cont = (ContainerSchemaNode) testModule.getDataChildByName(
81             QName.create(testModule.getQNameModule(), "parent-container"));
82         assertNotNull(cont);
83         cont = (ContainerSchemaNode) cont.getDataChildByName(
84             QName.create(testModule.getQNameModule(), "child-container"));
85         assertNotNull(cont);
86         assertEquals(2, cont.getChildNodes().size());
87
88         LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName(
89             QName.create(testModule.getQNameModule(), "autumn-leaf"));
90         assertNotNull(leaf);
91         leaf = (LeafSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "winter-snow"));
92         assertNotNull(leaf);
93     }
94
95     @Test
96     public void submoduleNamespaceTest() {
97         final Module testModule = result.findModules("root-module").iterator().next();
98         assertNotNull(testModule);
99
100         final Module subModule = testModule.getSubmodules().iterator().next();
101         assertEquals("urn:opendaylight.org/root-module", subModule.getNamespace().toString());
102     }
103 }