Bug 2366 - Effective statement implementation
[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 import static org.junit.Assert.assertNull;
6 import java.net.URI;
7 import org.junit.Test;
8 import org.opendaylight.yangtools.yang.common.QName;
9 import org.opendaylight.yangtools.yang.common.QNameModule;
10 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
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.ListSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.Module;
15 import org.opendaylight.yangtools.yang.model.api.meta.ModelStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
17 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
18 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
19 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
23 import com.google.common.collect.ImmutableList;
24
25 public class AugmentProcessTest {
26
27     private static final YangStatementSourceImpl AUGMENTED = new YangStatementSourceImpl(
28             "/stmt-test/augments/augmented.yang");
29     private static final YangStatementSourceImpl ROOT = new YangStatementSourceImpl(
30             "/stmt-test/augments/aug-root.yang");
31
32     private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(
33             URI.create("root"), null);
34     private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule
35             .create(URI.create("aug"), null);
36
37     private static GroupingDefinition grp2Def;
38
39     private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
40             "aug-parent1");
41     private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
42             "aug-parent2");
43     private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
44             "cont-target");
45
46     private final QName contAdded1 = QName.create(AUGMENTED_QNAME_MODULE,
47             "cont-added1");
48     private final QName contAdded2 = QName.create(AUGMENTED_QNAME_MODULE,
49             "cont-added2");
50
51     private final QName list1 = QName.create(AUGMENTED_QNAME_MODULE, "list1");
52     private final QName axml = QName.create(AUGMENTED_QNAME_MODULE, "axml");
53
54     private final QName contGrp = QName.create(AUGMENTED_QNAME_MODULE,
55             "cont-grp");
56     private final QName axmlGrp = QName.create(AUGMENTED_QNAME_MODULE,
57             "axml-grp");
58
59     private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
60     private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
61
62     private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
63     private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
64             "grp-cont22");
65     private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
66
67     private static final YangStatementSourceImpl MULTIPLE_AUGMENT = new YangStatementSourceImpl(
68             "/stmt-test/augments/multiple-augment-test.yang");
69
70     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_ROOT = new YangStatementSourceImpl(
71             "/stmt-test/augments/multiple-augment-root.yang");
72     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_IMPORTED = new YangStatementSourceImpl(
73             "/stmt-test/augments/multiple-augment-imported.yang");
74     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_SUBMODULE = new YangStatementSourceImpl(
75             "/stmt-test/augments/multiple-augment-submodule.yang");
76
77     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT = new YangStatementSourceImpl(
78             "/stmt-test/augments/multiple-augment-incorrect.yang");
79
80     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT2 = new YangStatementSourceImpl(
81             "/stmt-test/augments/multiple-augment-incorrect2.yang");
82
83     @Test
84     public void multipleAugmentsAndMultipleModulesTest() throws SourceException,
85             ReactorException {
86         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
87                 .newBuild();
88         addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED, MULTIPLE_AUGMENT_SUBMODULE);
89
90         EffectiveSchemaContext result = null;
91         try {
92             result = reactor.buildEffective();
93         } catch (Exception e) {
94             log(e, "");
95         }
96         assertNotNull(result);
97     }
98
99     @Test
100     public void multipleAugmentTest() throws SourceException, ReactorException {
101         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
102                 .newBuild();
103         addSources(reactor, MULTIPLE_AUGMENT);
104
105         EffectiveSchemaContext result = null;
106         try {
107             result = reactor.buildEffective();
108         } catch (Exception e) {
109             log(e, "");
110         }
111         assertNotNull(result);
112     }
113
114     @Test
115     public void multipleAugmentIncorrectPathTest() throws SourceException,
116             ReactorException {
117         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
118                 .newBuild();
119         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
120
121         EffectiveSchemaContext result = null;
122         try {
123             result = reactor.buildEffective();
124         } catch (Exception e) {
125             log(e, "");
126         }
127
128         assertNull(result);
129     }
130
131     @Test
132     public void multipleAugmentIncorrectPathAndGrpTest() throws SourceException,
133             ReactorException {
134         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
135                 .newBuild();
136         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
137
138         EffectiveSchemaContext result = null;
139         try {
140             result = reactor.buildEffective();
141         } catch (Exception e) {
142             log(e, "");
143         }
144
145         assertNull(result);
146     }
147
148     private void log(Throwable e, String indent) {
149         System.out.println(indent + e.getMessage());
150
151         Throwable[] suppressed = e.getSuppressed();
152         for (Throwable throwable : suppressed) {
153             log(throwable, indent + "        ");
154         }
155     }
156
157     @Test
158     public void readAndParseYangFileTest() throws SourceException,
159             ReactorException {
160
161         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
162                 .newBuild();
163         addSources(reactor, AUGMENTED, ROOT);
164
165         final EffectiveSchemaContext root = reactor.buildEffective();
166         assertNotNull(root);
167
168         Module augmentedModule = root.findModuleByName("augmented", null);
169         assertNotNull(augmentedModule);
170
171         ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root
172                 .getDataChildByName(augParent1);
173         ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node
174                 .getDataChildByName(augParent2);
175         ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node
176                 .getDataChildByName(contTarget);
177         assertNotNull(targetContNode);
178
179         assertNotNull(targetContNode.getChildNodes());
180         assertEquals(3, targetContNode.getChildNodes().size());
181
182         ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
183                 .getDataChildByName(contAdded1);
184         assertNotNull(contAdded1Node);
185         ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
186                 .getDataChildByName(list1);
187         assertNotNull(list1Node);
188
189         ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
190                 .getDataChildByName(contAdded2);
191         assertNotNull(contAdded2Node);
192         AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
193                 .getDataChildByName(axml);
194         assertNotNull(axmlNode);
195
196         ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
197                 .getDataChildByName(contGrp);
198         assertNotNull(contGrpNode);
199         AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
200                 .getDataChildByName(axmlGrp);
201         assertNotNull(axmlGrpNode);
202
203         ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
204                 .getDataChildByName(augCont1);
205         ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
206                 .getDataChildByName(augCont2);
207         assertNotNull(augCont2Node);
208
209         ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
210                 .getDataChildByName(grpCont2);
211         ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
212                 .getDataChildByName(grpCont22);
213         assertNotNull(grpCont22Node);
214
215         ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
216                 .getDataChildByName(grpAdd);
217         assertNotNull(grpAddNode);
218     }
219
220     private <T extends ModelStatement> T findInStatements(QName target,
221             ImmutableList<T> statements) {
222
223         for (final T statement : statements) {
224             if (target
225                     .equals(statement.statementDefinition().getArgumentName())) {
226                 return statement;
227             }
228         }
229
230         return null;
231     }
232
233     private void addSources(CrossSourceStatementReactor.BuildAction reactor,
234             StatementStreamSource... sources) {
235         for (StatementStreamSource source : sources) {
236             reactor.addSource(source);
237         }
238     }
239 }