Update StmtTestUtils
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / AugmentProcessTest.java
1 /*
2  * Copyright (c) 2016 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;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
17
18 import java.net.URI;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
23 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
37 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
38
39 public class AugmentProcessTest {
40
41     private static final StatementStreamSource AUGMENTED = sourceForResource("/stmt-test/augments/augmented.yang");
42     private static final StatementStreamSource ROOT = sourceForResource("/stmt-test/augments/aug-root.yang");
43
44     private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(
45             URI.create("root"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
46     private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule
47             .create(URI.create("aug"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
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 StatementStreamSource MULTIPLE_AUGMENT = sourceForResource(
78             "/stmt-test/augments/multiple-augment-test.yang");
79
80     private static final StatementStreamSource MULTIPLE_AUGMENT_ROOT = sourceForResource(
81             "/stmt-test/augments/multiple-augment-root.yang");
82     private static final StatementStreamSource MULTIPLE_AUGMENT_IMPORTED = sourceForResource(
83             "/stmt-test/augments/multiple-augment-imported.yang");
84     private static final StatementStreamSource MULTIPLE_AUGMENT_SUBMODULE = sourceForResource(
85             "/stmt-test/augments/multiple-augment-submodule.yang");
86
87     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT = sourceForResource(
88             "/stmt-test/augments/multiple-augment-incorrect.yang");
89
90     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT2 = sourceForResource(
91             "/stmt-test/augments/multiple-augment-incorrect2.yang");
92
93     @Test
94     public void multipleAugmentsAndMultipleModulesTest() throws SourceException, ReactorException {
95         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
96                 .newBuild();
97         addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
98                 MULTIPLE_AUGMENT_SUBMODULE);
99
100         EffectiveSchemaContext result = null;
101         result = reactor.buildEffective();
102         assertNotNull(result);
103     }
104
105     @Test
106     public void multipleAugmentTest() throws SourceException, ReactorException {
107         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
108                 .newBuild();
109         addSources(reactor, MULTIPLE_AUGMENT);
110
111         EffectiveSchemaContext result = null;
112         result = reactor.buildEffective();
113         assertNotNull(result);
114     }
115
116     @Test(expected = SomeModifiersUnresolvedException.class)
117     public void multipleAugmentIncorrectPathTest() throws SourceException, ReactorException {
118         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
119                 .newBuild();
120         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
121
122         EffectiveSchemaContext result = null;
123         result = reactor.buildEffective();
124         assertNull(result);
125     }
126
127     @Test(expected = SomeModifiersUnresolvedException.class)
128     public void multipleAugmentIncorrectPathAndGrpTest() throws SourceException, ReactorException {
129         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
130                 .newBuild();
131         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
132         EffectiveSchemaContext result = null;
133         result = reactor.buildEffective();
134         assertNull(result);
135     }
136
137     @Test
138     public void readAndParseYangFileTest() throws SourceException, ReactorException {
139         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
140                 .newBuild();
141         addSources(reactor, AUGMENTED, ROOT);
142
143         final EffectiveSchemaContext root = reactor.buildEffective();
144         assertNotNull(root);
145
146         final Module augmentedModule = root.findModuleByName("augmented", null);
147         assertNotNull(augmentedModule);
148
149         final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root
150                 .getDataChildByName(augParent1);
151         final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node
152                 .getDataChildByName(augParent2);
153         final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node
154                 .getDataChildByName(contTarget);
155         assertNotNull(targetContNode);
156
157         assertNotNull(targetContNode.getChildNodes());
158         assertEquals(3, targetContNode.getChildNodes().size());
159
160         final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
161                 .getDataChildByName(contAdded1);
162         assertNotNull(contAdded1Node);
163         final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
164                 .getDataChildByName(list1);
165         assertNotNull(list1Node);
166
167         final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
168                 .getDataChildByName(contAdded2);
169         assertNotNull(contAdded2Node);
170         final AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
171                 .getDataChildByName(axml);
172         assertNotNull(axmlNode);
173
174         final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
175                 .getDataChildByName(contGrp);
176         assertNotNull(contGrpNode);
177         final AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
178                 .getDataChildByName(axmlGrp);
179         assertNotNull(axmlGrpNode);
180
181         final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
182                 .getDataChildByName(augCont1);
183         final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
184                 .getDataChildByName(augCont2);
185         assertNotNull(augCont2Node);
186
187         final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
188                 .getDataChildByName(grpCont2);
189         final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
190                 .getDataChildByName(grpCont22);
191         assertNotNull(grpCont22Node);
192
193         final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
194                 .getDataChildByName(grpAdd);
195         assertNotNull(grpAddNode);
196     }
197
198     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
199             final StatementStreamSource... sources) {
200         for (final StatementStreamSource source : sources) {
201             reactor.addSource(source);
202         }
203     }
204
205     @Test
206     public void caseShortHandAugmentingTest() throws Exception {
207         final SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
208
209         assertNotNull(context);
210
211         final String rev = "2013-07-01";
212         final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
213         final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
214
215         final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
216                 "netconf-state"));
217         final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
218                 "datastores"));
219         final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
220                 "datastore"));
221         final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
222                 "locks"));
223         final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
224                 .create(ns, rev, "lock-type"));
225
226         final ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case");
227         assertTrue(leafAugCase.isAugmenting());
228         final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
229         assertFalse(leafAug.isAugmenting());
230     }
231
232 }