Cleanup yang-parser-impl
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / effective / build / test / EffectiveBuildTest.java
1 package org.opendaylight.yangtools.yang.stmt.effective.build.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import java.io.FileNotFoundException;
7 import java.net.URI;
8 import java.net.URISyntaxException;
9 import java.util.Collection;
10 import java.util.Set;
11 import org.junit.Test;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
15 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
28
29 public class EffectiveBuildTest {
30
31     private static final YangStatementSourceImpl SIMPLE_MODULE = new YangStatementSourceImpl(
32             "/stmt-test/effective-build/simple-module.yang", false);
33     private static final QNameModule SIMPLE_MODULE_QNAME = QNameModule.create(
34             URI.create("simple.yang"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
35     private static final YangStatementSourceImpl YANG_EXT = new YangStatementSourceImpl(
36             "/stmt-test/extensions/yang-ext.yang", false);
37
38     @Test
39     public void effectiveBuildTest() throws SourceException, ReactorException {
40         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
41         addSources(reactor, SIMPLE_MODULE);
42         EffectiveSchemaContext result = reactor.buildEffective();
43
44         assertNotNull(result);
45
46         Module simpleModule = result.findModuleByName("simple-module", null);
47         assertNotNull(simpleModule);
48
49         QName q1 = QName.create(SIMPLE_MODULE_QNAME, "root-container");
50         QName q2 = QName.create(SIMPLE_MODULE_QNAME, "sub-container");
51         QName q3 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container");
52         QName q4 = QName.create(SIMPLE_MODULE_QNAME, "root-container2");
53         QName q5 = QName.create(SIMPLE_MODULE_QNAME, "sub-container2");
54         QName q6 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container2");
55         QName q7 = QName.create(SIMPLE_MODULE_QNAME, "grp");
56
57         ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule
58                 .getDataChildByName(q1);
59         assertNotNull(rootCon);
60
61         ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon
62                 .getDataChildByName(q2);
63         assertNotNull(subCon);
64
65         ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon
66                 .getDataChildByName(q3);
67         assertNotNull(subSubCon);
68
69         ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule
70                 .getDataChildByName(q4);
71         assertNotNull(rootCon2);
72
73         ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2
74                 .getDataChildByName(q5);
75         assertNotNull(subCon2);
76
77         ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2
78                 .getDataChildByName(q6);
79         assertNotNull(subSubCon2);
80
81         GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
82         assertNotNull(grp);
83         assertEquals(q7, grp.getQName());
84
85         ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp
86                 .getDataChildByName(q5);
87         assertNotNull(grpSubCon2);
88
89         ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2
90                 .getDataChildByName(q6);
91         assertNotNull(grpSubSubCon2);
92
93         assertEquals(SchemaPath.create(true, q1, q2, q3), subSubCon.getPath());
94         assertEquals(SchemaPath.create(true, q4, q5, q6), subSubCon2.getPath());
95         assertEquals(SchemaPath.create(true, q7, q5, q6),
96                 grpSubSubCon2.getPath());
97
98     }
99
100     @Test
101     public void extensionsTest() throws SourceException, ReactorException {
102         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
103         addSources(reactor, YANG_EXT);
104         EffectiveSchemaContext result = reactor.buildEffective();
105         assertNotNull(result);
106
107         Set<GroupingDefinition> groupings = result.getGroupings();
108         assertEquals(1, groupings.size());
109
110         GroupingDefinition grp = groupings.iterator().next();
111
112         Collection<DataSchemaNode> childNodes = grp.getChildNodes();
113         assertEquals(1, childNodes.size());
114         DataSchemaNode child = childNodes.iterator().next();
115
116         assertTrue(child instanceof LeafSchemaNode);
117         LeafSchemaNode leaf = (LeafSchemaNode) child;
118
119         assertNotNull(leaf.getType());
120     }
121
122     @Test
123     public void mockTest() throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
124         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
125         reactor.addSource(YANG_EXT);
126
127         SchemaContext result = reactor.buildEffective();
128         assertNotNull(result);
129     }
130
131     private static void addSources(final BuildAction reactor, final YangStatementSourceImpl... sources) {
132         for (YangStatementSourceImpl source : sources) {
133             reactor.addSource(source);
134         }
135     }
136 }