c3d0c1575be70a0b10df08541f5f42ffc8887cf9
[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 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.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     @AfterClass
44     public static void teardown() {
45         result = null;
46     }
47
48     @Test
49     public void includedTypedefsTest() {
50         final Module testModule = result.findModules("root-module").iterator().next();
51         assertNotNull(testModule);
52
53         final Collection<? extends TypeDefinition<?>> typedefs = testModule.getTypeDefinitions();
54         assertEquals(2, typedefs.size());
55
56         final Iterator<? extends TypeDefinition<?>> typedefsIterator = typedefs.iterator();
57         TypeDefinition<?> typedef = typedefsIterator.next();
58         assertThat(typedef.getQName().getLocalName(), anyOf(is("new-string-type"), is("new-int32-type")));
59         assertThat(typedef.getBaseType().getQName().getLocalName(), anyOf(is("string"), is("int32")));
60         typedef = typedefsIterator.next();
61         assertThat(typedef.getQName().getLocalName(), anyOf(is("new-string-type"), is("new-int32-type")));
62         assertThat(typedef.getBaseType().getQName().getLocalName(), anyOf(is("string"), is("int32")));
63     }
64
65     @Test
66     public void includedFeaturesTest() {
67         final Module testModule = result.findModules("root-module").iterator().next();
68         assertNotNull(testModule);
69
70         final Collection<? extends FeatureDefinition> features = testModule.getFeatures();
71         assertEquals(2, features.size());
72
73         final Iterator<? extends FeatureDefinition> featuresIterator = features.iterator();
74         FeatureDefinition feature = featuresIterator.next();
75         assertThat(feature.getQName().getLocalName(), anyOf(is("new-feature1"), is("new-feature2")));
76         feature = featuresIterator.next();
77         assertThat(feature.getQName().getLocalName(), anyOf(is("new-feature1"), is("new-feature2")));
78     }
79
80     @Test
81     public void includedContainersAndListsTest() {
82         final Module testModule = result.findModules("root-module").iterator().next();
83         assertNotNull(testModule);
84
85         ContainerSchemaNode cont = (ContainerSchemaNode) testModule.getDataChildByName(
86             QName.create(testModule.getQNameModule(), "parent-container"));
87         assertNotNull(cont);
88         cont = (ContainerSchemaNode) cont.getDataChildByName(
89             QName.create(testModule.getQNameModule(), "child-container"));
90         assertNotNull(cont);
91         assertEquals(2, cont.getChildNodes().size());
92
93         LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName(
94             QName.create(testModule.getQNameModule(), "autumn-leaf"));
95         assertNotNull(leaf);
96         leaf = (LeafSchemaNode) cont.getDataChildByName(QName.create(testModule.getQNameModule(), "winter-snow"));
97         assertNotNull(leaf);
98     }
99
100     @Test
101     public void submoduleNamespaceTest() {
102         final Module testModule = result.findModules("root-module").iterator().next();
103         assertNotNull(testModule);
104
105         final Module subModule = testModule.getSubmodules().iterator().next();
106         assertEquals("urn:opendaylight.org/root-module", subModule.getNamespace().toString());
107     }
108 }