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