Populate parser/ hierarchy
[yangtools.git] / parser / 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 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.hamcrest.CoreMatchers.anyOf;
11 import static org.hamcrest.CoreMatchers.is;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
16
17 import java.util.Collection;
18 import java.util.Iterator;
19 import org.junit.AfterClass;
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.Submodule;
29 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32
33 public class IncludedStmtsTest {
34     private static SchemaContext result;
35
36     @BeforeClass
37     public static void setup() throws ReactorException {
38         result = RFC7950Reactors.defaultReactor().newBuild()
39                 .addSource(sourceForResource("/included-statements-test/root-module.yang"))
40                 .addSource(sourceForResource("/included-statements-test/child-module.yang"))
41                 .buildEffective();
42     }
43
44     @AfterClass
45     public static void teardown() {
46         result = null;
47     }
48
49     @Test
50     public void includedTypedefsTest() {
51         final Module testModule = result.findModules("root-module").iterator().next();
52         assertNotNull(testModule);
53
54         final Collection<? extends TypeDefinition<?>> typedefs = testModule.getTypeDefinitions();
55         assertEquals(2, typedefs.size());
56
57         final Iterator<? extends TypeDefinition<?>> typedefsIterator = typedefs.iterator();
58         TypeDefinition<?> 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         typedef = typedefsIterator.next();
62         assertThat(typedef.getQName().getLocalName(), anyOf(is("new-string-type"), is("new-int32-type")));
63         assertThat(typedef.getBaseType().getQName().getLocalName(), anyOf(is("string"), is("int32")));
64     }
65
66     @Test
67     public void includedFeaturesTest() {
68         final Module testModule = result.findModules("root-module").iterator().next();
69         assertNotNull(testModule);
70
71         final Collection<? extends FeatureDefinition> features = testModule.getFeatures();
72         assertEquals(2, features.size());
73
74         final Iterator<? extends FeatureDefinition> featuresIterator = features.iterator();
75         FeatureDefinition feature = featuresIterator.next();
76         assertThat(feature.getQName().getLocalName(), anyOf(is("new-feature1"), is("new-feature2")));
77         feature = featuresIterator.next();
78         assertThat(feature.getQName().getLocalName(), anyOf(is("new-feature1"), is("new-feature2")));
79     }
80
81     @Test
82     public void includedContainersAndListsTest() {
83         final Module testModule = result.findModules("root-module").iterator().next();
84         assertNotNull(testModule);
85
86         ContainerSchemaNode cont = (ContainerSchemaNode) testModule.getDataChildByName(
87             QName.create(testModule.getQNameModule(), "parent-container"));
88         assertNotNull(cont);
89         cont = (ContainerSchemaNode) cont.getDataChildByName(
90             QName.create(testModule.getQNameModule(), "child-container"));
91         assertNotNull(cont);
92         assertEquals(2, cont.getChildNodes().size());
93
94         LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName(
95             QName.create(testModule.getQNameModule(), "autumn-leaf"));
96         assertNotNull(leaf);
97         leaf = (LeafSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "winter-snow"));
98         assertNotNull(leaf);
99     }
100
101     @Test
102     public void submoduleNamespaceTest() {
103         final Module testModule = result.findModules("root-module").iterator().next();
104         assertNotNull(testModule);
105
106         final Submodule subModule = testModule.getSubmodules().iterator().next();
107         assertEquals("urn:opendaylight.org/root-module", subModule.getNamespace().toString());
108     }
109 }