1 package org.opendaylight.yangtools.yang.stmt.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
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;
25 public class AugmentProcessTest {
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");
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);
37 private static GroupingDefinition grp2Def;
39 private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
41 private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
43 private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
46 private final QName contAdded1 = QName.create(AUGMENTED_QNAME_MODULE,
48 private final QName contAdded2 = QName.create(AUGMENTED_QNAME_MODULE,
51 private final QName list1 = QName.create(AUGMENTED_QNAME_MODULE, "list1");
52 private final QName axml = QName.create(AUGMENTED_QNAME_MODULE, "axml");
54 private final QName contGrp = QName.create(AUGMENTED_QNAME_MODULE,
56 private final QName axmlGrp = QName.create(AUGMENTED_QNAME_MODULE,
59 private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
60 private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
62 private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
63 private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
65 private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
67 private static final YangStatementSourceImpl MULTIPLE_AUGMENT = new YangStatementSourceImpl(
68 "/stmt-test/augments/multiple-augment-test.yang");
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");
77 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT = new YangStatementSourceImpl(
78 "/stmt-test/augments/multiple-augment-incorrect.yang");
80 private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT2 = new YangStatementSourceImpl(
81 "/stmt-test/augments/multiple-augment-incorrect2.yang");
84 public void multipleAugmentsAndMultipleModulesTest() throws SourceException,
86 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
88 addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED, MULTIPLE_AUGMENT_SUBMODULE);
90 EffectiveSchemaContext result = null;
92 result = reactor.buildEffective();
93 } catch (Exception e) {
96 assertNotNull(result);
100 public void multipleAugmentTest() throws SourceException, ReactorException {
101 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
103 addSources(reactor, MULTIPLE_AUGMENT);
105 EffectiveSchemaContext result = null;
107 result = reactor.buildEffective();
108 } catch (Exception e) {
111 assertNotNull(result);
115 public void multipleAugmentIncorrectPathTest() throws SourceException,
117 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
119 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
121 EffectiveSchemaContext result = null;
123 result = reactor.buildEffective();
124 } catch (Exception e) {
132 public void multipleAugmentIncorrectPathAndGrpTest() throws SourceException,
134 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
136 addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
138 EffectiveSchemaContext result = null;
140 result = reactor.buildEffective();
141 } catch (Exception e) {
148 private void log(Throwable e, String indent) {
149 System.out.println(indent + e.getMessage());
151 Throwable[] suppressed = e.getSuppressed();
152 for (Throwable throwable : suppressed) {
153 log(throwable, indent + " ");
158 public void readAndParseYangFileTest() throws SourceException,
161 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
163 addSources(reactor, AUGMENTED, ROOT);
165 final EffectiveSchemaContext root = reactor.buildEffective();
168 Module augmentedModule = root.findModuleByName("augmented", null);
169 assertNotNull(augmentedModule);
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);
179 assertNotNull(targetContNode.getChildNodes());
180 assertEquals(3, targetContNode.getChildNodes().size());
182 ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
183 .getDataChildByName(contAdded1);
184 assertNotNull(contAdded1Node);
185 ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
186 .getDataChildByName(list1);
187 assertNotNull(list1Node);
189 ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
190 .getDataChildByName(contAdded2);
191 assertNotNull(contAdded2Node);
192 AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
193 .getDataChildByName(axml);
194 assertNotNull(axmlNode);
196 ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
197 .getDataChildByName(contGrp);
198 assertNotNull(contGrpNode);
199 AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
200 .getDataChildByName(axmlGrp);
201 assertNotNull(axmlGrpNode);
203 ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
204 .getDataChildByName(augCont1);
205 ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
206 .getDataChildByName(augCont2);
207 assertNotNull(augCont2Node);
209 ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
210 .getDataChildByName(grpCont2);
211 ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
212 .getDataChildByName(grpCont22);
213 assertNotNull(grpCont22Node);
215 ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
216 .getDataChildByName(grpAdd);
217 assertNotNull(grpAddNode);
220 private <T extends ModelStatement> T findInStatements(QName target,
221 ImmutableList<T> statements) {
223 for (final T statement : statements) {
225 .equals(statement.statementDefinition().getArgumentName())) {
233 private void addSources(CrossSourceStatementReactor.BuildAction reactor,
234 StatementStreamSource... sources) {
235 for (StatementStreamSource source : sources) {
236 reactor.addSource(source);