1 package org.opendaylight.yangtools.yang.stmt.effective.build.test;
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;
8 import java.net.URISyntaxException;
9 import java.util.Collection;
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;
29 public class EffectiveBuildTest {
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);
39 public void effectiveBuildTest() throws SourceException, ReactorException {
40 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
41 addSources(reactor, SIMPLE_MODULE);
42 EffectiveSchemaContext result = reactor.buildEffective();
44 assertNotNull(result);
46 Module simpleModule = result.findModuleByName("simple-module", null);
47 assertNotNull(simpleModule);
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");
57 ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule
58 .getDataChildByName(q1);
59 assertNotNull(rootCon);
61 ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon
62 .getDataChildByName(q2);
63 assertNotNull(subCon);
65 ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon
66 .getDataChildByName(q3);
67 assertNotNull(subSubCon);
69 ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule
70 .getDataChildByName(q4);
71 assertNotNull(rootCon2);
73 ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2
74 .getDataChildByName(q5);
75 assertNotNull(subCon2);
77 ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2
78 .getDataChildByName(q6);
79 assertNotNull(subSubCon2);
81 GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
83 assertEquals(q7, grp.getQName());
85 ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp
86 .getDataChildByName(q5);
87 assertNotNull(grpSubCon2);
89 ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2
90 .getDataChildByName(q6);
91 assertNotNull(grpSubSubCon2);
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());
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);
107 Set<GroupingDefinition> groupings = result.getGroupings();
108 assertEquals(1, groupings.size());
110 GroupingDefinition grp = groupings.iterator().next();
112 Collection<DataSchemaNode> childNodes = grp.getChildNodes();
113 assertEquals(1, childNodes.size());
114 DataSchemaNode child = childNodes.iterator().next();
116 assertTrue(child instanceof LeafSchemaNode);
117 LeafSchemaNode leaf = (LeafSchemaNode) child;
119 assertNotNull(leaf.getType());
123 public void mockTest() throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
124 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
125 reactor.addSource(YANG_EXT);
127 SchemaContext result = reactor.buildEffective();
128 assertNotNull(result);
131 private static void addSources(final BuildAction reactor, final YangStatementSourceImpl... sources) {
132 for (YangStatementSourceImpl source : sources) {
133 reactor.addSource(source);