Merge "Bug 2363, Bug 2205. Beta version of LeafRefContext tree computation"
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / AugmentProcessTest.java
1 package org.opendaylight.yangtools.yang.stmt.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.AnyXmlSchemaNode;
12 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
14 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.Module;
16 import org.opendaylight.yangtools.yang.model.api.meta.ModelStatement;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
18 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
19 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
20 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
24
25 import com.google.common.collect.ImmutableList;
26
27 public class AugmentProcessTest {
28
29     private static final YangStatementSourceImpl AUGMENTED = new YangStatementSourceImpl(
30             "/stmt-test/effective-build/augmented.yang");
31     private static final YangStatementSourceImpl ROOT = new YangStatementSourceImpl(
32             "/stmt-test/effective-build/aug-root.yang");
33
34     private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(URI.create("root"), null);
35     private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule.create(URI.create("aug"), null);
36
37     private static GroupingDefinition grp2Def;
38
39     private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE, "aug-parent1");
40     private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE, "aug-parent2");
41     private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE, "cont-target");
42
43     private final QName contAdded1 = QName.create(AUGMENTED_QNAME_MODULE, "cont-added1");
44     private final QName contAdded2 = QName.create(AUGMENTED_QNAME_MODULE, "cont-added2");
45
46     private final QName list1 = QName.create(AUGMENTED_QNAME_MODULE, "list1");
47     private final QName axml = QName.create(AUGMENTED_QNAME_MODULE, "axml");
48
49     private final QName contGrp = QName.create(AUGMENTED_QNAME_MODULE, "cont-grp");
50     private final QName axmlGrp = QName.create(AUGMENTED_QNAME_MODULE, "axml-grp");
51
52     private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
53     private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
54
55     private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
56     private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE, "grp-cont22");
57     private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
58
59     @Test
60     public void readAndParseYangFileTest() throws SourceException, ReactorException {
61
62         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
63         addSources(reactor, AUGMENTED, ROOT);
64
65         final EffectiveSchemaContext root = reactor.buildEffective();
66         assertNotNull(root);
67
68         Module augmentedModule = root.findModuleByName("augmented", null);
69         assertNotNull(augmentedModule);
70
71         ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root.getDataChildByName(augParent1);
72         ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node.getDataChildByName(augParent2);
73         ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node.getDataChildByName(contTarget);
74         assertNotNull(targetContNode);
75
76         assertNotNull(targetContNode.getChildNodes());
77         assertEquals(3, targetContNode.getChildNodes().size());
78
79         ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded1);
80         assertNotNull(contAdded1Node);
81         ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node.getDataChildByName(list1);
82         assertNotNull(list1Node);
83
84         ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded2);
85         assertNotNull(contAdded2Node);
86         AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node.getDataChildByName(axml);
87         assertNotNull(axmlNode);
88
89         ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode.getDataChildByName(contGrp);
90         assertNotNull(contGrpNode);
91         AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode.getDataChildByName(axmlGrp);
92         assertNotNull(axmlGrpNode);
93
94         ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root.getDataChildByName(augCont1);
95         ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node.getDataChildByName(augCont2);
96         assertNotNull(augCont2Node);
97
98         ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node.getDataChildByName(grpCont2);
99         ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node.getDataChildByName(grpCont22);
100         assertNotNull(grpCont22Node);
101
102         ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node.getDataChildByName(grpAdd);
103         assertNotNull(grpAddNode);
104     }
105
106     private <T extends ModelStatement> T findInStatements(QName target, ImmutableList<T> statements) {
107
108         for (final T statement : statements) {
109             if (target.equals(statement.statementDefinition().getArgumentName())) {
110                 return statement;
111             }
112         }
113
114         return null;
115     }
116
117     private void addSources(CrossSourceStatementReactor.BuildAction reactor, StatementStreamSource... sources) {
118         for (StatementStreamSource source : sources) {
119             reactor.addSource(source);
120         }
121     }
122 }