BUG-865: remove String-based getDataChildByName()
[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
17 import com.google.common.collect.ImmutableList;
18 import java.io.FileNotFoundException;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
25 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.meta.ModelStatement;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
37 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
39 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
40 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
41 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
42 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
43
44 public class AugmentProcessTest {
45
46     private static final YangStatementSourceImpl AUGMENTED = new YangStatementSourceImpl(
47             "/stmt-test/augments/augmented.yang", false);
48     private static final YangStatementSourceImpl ROOT = new YangStatementSourceImpl(
49             "/stmt-test/augments/aug-root.yang", false);
50
51     private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(
52             URI.create("root"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
53     private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule
54             .create(URI.create("aug"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
55
56     private static GroupingDefinition grp2Def;
57
58     private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
59             "aug-parent1");
60     private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
61             "aug-parent2");
62     private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
63             "cont-target");
64
65     private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
66             "cont-added1");
67     private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
68             "cont-added2");
69
70     private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
71     private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
72
73     private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
74             "cont-grp");
75     private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
76             "axml-grp");
77
78     private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
79     private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
80
81     private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
82     private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
83             "grp-cont22");
84     private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
85
86     private static final YangStatementSourceImpl MULTIPLE_AUGMENT = new YangStatementSourceImpl(
87             "/stmt-test/augments/multiple-augment-test.yang",false);
88
89     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_ROOT = new YangStatementSourceImpl(
90             "/stmt-test/augments/multiple-augment-root.yang",false);
91     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_IMPORTED = new YangStatementSourceImpl(
92             "/stmt-test/augments/multiple-augment-imported.yang",false);
93     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_SUBMODULE = new YangStatementSourceImpl(
94             "/stmt-test/augments/multiple-augment-submodule.yang",false);
95
96     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT = new YangStatementSourceImpl(
97             "/stmt-test/augments/multiple-augment-incorrect.yang",false);
98
99     private static final YangStatementSourceImpl MULTIPLE_AUGMENT_INCORRECT2 = new YangStatementSourceImpl(
100             "/stmt-test/augments/multiple-augment-incorrect2.yang",false);
101
102     @Test
103     public void multipleAugmentsAndMultipleModulesTest()
104             throws SourceException, ReactorException {
105         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
106                 .newBuild();
107         addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
108                 MULTIPLE_AUGMENT_SUBMODULE);
109
110         EffectiveSchemaContext result = null;
111         result = reactor.buildEffective();
112         assertNotNull(result);
113     }
114
115     @Test
116     public void multipleAugmentTest() throws SourceException, ReactorException {
117         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
118                 .newBuild();
119         addSources(reactor, MULTIPLE_AUGMENT);
120
121         EffectiveSchemaContext result = null;
122         result = reactor.buildEffective();
123         assertNotNull(result);
124     }
125
126     @Test(expected=SomeModifiersUnresolvedException.class)
127     public void multipleAugmentIncorrectPathTest() throws SourceException,
128             ReactorException {
129         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
130                 .newBuild();
131         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
132
133         EffectiveSchemaContext result = null;
134         result = reactor.buildEffective();
135         assertNull(result);
136     }
137
138     @Test(expected=SomeModifiersUnresolvedException.class)
139     public void multipleAugmentIncorrectPathAndGrpTest()
140             throws SourceException, ReactorException {
141         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
142                 .newBuild();
143         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
144         EffectiveSchemaContext result = null;
145         result = reactor.buildEffective();
146         assertNull(result);
147     }
148
149
150
151     @Test
152     public void readAndParseYangFileTest() throws SourceException,
153             ReactorException {
154
155         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
156                 .newBuild();
157         addSources(reactor, AUGMENTED, ROOT);
158
159         final EffectiveSchemaContext root = reactor.buildEffective();
160         assertNotNull(root);
161
162         final Module augmentedModule = root.findModuleByName("augmented", null);
163         assertNotNull(augmentedModule);
164
165         final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root
166                 .getDataChildByName(augParent1);
167         final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node
168                 .getDataChildByName(augParent2);
169         final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node
170                 .getDataChildByName(contTarget);
171         assertNotNull(targetContNode);
172
173         assertNotNull(targetContNode.getChildNodes());
174         assertEquals(3, targetContNode.getChildNodes().size());
175
176         final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
177                 .getDataChildByName(contAdded1);
178         assertNotNull(contAdded1Node);
179         final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
180                 .getDataChildByName(list1);
181         assertNotNull(list1Node);
182
183         final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
184                 .getDataChildByName(contAdded2);
185         assertNotNull(contAdded2Node);
186         final AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
187                 .getDataChildByName(axml);
188         assertNotNull(axmlNode);
189
190         final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
191                 .getDataChildByName(contGrp);
192         assertNotNull(contGrpNode);
193         final AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
194                 .getDataChildByName(axmlGrp);
195         assertNotNull(axmlGrpNode);
196
197         final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
198                 .getDataChildByName(augCont1);
199         final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
200                 .getDataChildByName(augCont2);
201         assertNotNull(augCont2Node);
202
203         final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
204                 .getDataChildByName(grpCont2);
205         final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
206                 .getDataChildByName(grpCont22);
207         assertNotNull(grpCont22Node);
208
209         final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
210                 .getDataChildByName(grpAdd);
211         assertNotNull(grpAddNode);
212     }
213
214     private static <T extends ModelStatement<?>> T findInStatements(final QName target,
215             final ImmutableList<T> statements) {
216
217         for (final T statement : statements) {
218             if (target
219                     .equals(statement.statementDefinition().getArgumentName())) {
220                 return statement;
221             }
222         }
223
224         return null;
225     }
226
227     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
228             final StatementStreamSource... sources) {
229         for (final StatementStreamSource source : sources) {
230             reactor.addSource(source);
231         }
232     }
233
234     @Test
235     public void caseShortHandAugmentingTest() throws SourceException, FileNotFoundException, ReactorException,
236             URISyntaxException {
237
238         final SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
239
240         assertNotNull(context);
241
242         final String rev = "2013-07-01";
243         final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
244         final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
245
246         final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
247                 "netconf-state"));
248         final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
249                 "datastores"));
250         final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
251                 "datastore"));
252         final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
253                 "locks"));
254         final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
255                 .create(ns, rev, "lock-type"));
256
257         final ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case");
258         assertTrue(leafAugCase.isAugmenting());
259         final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
260         assertFalse(leafAug.isAugmenting());
261     }
262
263 }