Bug 2366 - Effective statements impl for new yang parser.
[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
6 import java.net.URI;
7
8 import org.junit.Test;
9 import org.opendaylight.yangtools.yang.common.QName;
10 import org.opendaylight.yangtools.yang.common.QNameModule;
11 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
12 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
13 import org.opendaylight.yangtools.yang.model.api.Module;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
16 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
17 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
18 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
19 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
21
22 public class EffectiveBuildTest {
23
24     private static final YangStatementSourceImpl SIMPLE_MODULE = new YangStatementSourceImpl(
25             "/stmt-test/effective-build/simple-module.yang");
26     private static final QNameModule SIMPLE_MODULE_QNAME = QNameModule.create(URI.create("simple.yang"), null);
27
28     @Test
29     public void effectiveBuildTest() throws SourceException, ReactorException {
30         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
31         addSources(reactor, SIMPLE_MODULE);
32         EffectiveSchemaContext result = reactor.buildEffective();
33
34         assertNotNull(result);
35
36         Module simpleModule = result.findModuleByName("simple-module", null);
37         assertNotNull(simpleModule);
38
39         QName q1 = QName.create(SIMPLE_MODULE_QNAME, "root-container");
40         QName q2 = QName.create(SIMPLE_MODULE_QNAME, "sub-container");
41         QName q3 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container");
42         QName q4 = QName.create(SIMPLE_MODULE_QNAME, "root-container2");
43         QName q5 = QName.create(SIMPLE_MODULE_QNAME, "sub-container2");
44         QName q6 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container2");
45         QName q7 = QName.create(SIMPLE_MODULE_QNAME, "grp");
46
47         ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule.getDataChildByName(q1);
48         assertNotNull(rootCon);
49
50         ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon.getDataChildByName(q2);
51         assertNotNull(subCon);
52
53         ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon.getDataChildByName(q3);
54         assertNotNull(subSubCon);
55
56         ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule.getDataChildByName(q4);
57         assertNotNull(rootCon2);
58
59         ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2.getDataChildByName(q5);
60         assertNotNull(subCon2);
61
62         ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2.getDataChildByName(q6);
63         assertNotNull(subSubCon2);
64
65         GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
66         assertNotNull(grp);
67         assertEquals(q7, grp.getQName());
68
69         ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp.getDataChildByName(q5);
70         assertNotNull(grpSubCon2);
71
72         ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2.getDataChildByName(q6);
73         assertNotNull(grpSubSubCon2);
74
75         assertEquals(SchemaPath.create(true, q1, q2, q3), subSubCon.getPath());
76         assertEquals(SchemaPath.create(true, q4, q5, q6), subSubCon2.getPath());
77         assertEquals(SchemaPath.create(true, q7, q5, q6), grpSubSubCon2.getPath());
78
79     }
80
81     private void log(Throwable e, String indent) {
82         System.out.println(indent + e.getMessage());
83
84         Throwable[] suppressed = e.getSuppressed();
85         for (Throwable throwable : suppressed) {
86             log(throwable, indent + "        ");
87         }
88
89     }
90
91     private void addSources(BuildAction reactor, YangStatementSourceImpl... sources) {
92         for (YangStatementSourceImpl source : sources) {
93             reactor.addSource(source);
94         }
95     }
96
97 }