Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / AugmentTest.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 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.net.URI;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.SortedMap;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
39
40 public class AugmentTest {
41     private static final QNameModule FOO = QNameModule.create(
42         URI.create("urn:opendaylight.foo"), Revision.of("2013-10-13"));
43     private static final QNameModule BAR = QNameModule.create(
44         URI.create("urn:opendaylight.bar"), Revision.of("2013-10-14"));
45     private static final QNameModule BAZ = QNameModule.create(
46         URI.create("urn:opendaylight.baz"), Revision.of("2013-10-15"));
47
48     private static final QName Q0 = QName.create(BAR, "interfaces");
49     private static final QName Q1 = QName.create(BAR, "ifEntry");
50     private static final QName Q2 = QName.create(BAZ, "augment-holder");
51
52     @Test
53     public void testAugmentParsing() throws Exception {
54         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
55             .toURI());
56         final List<QName> qnames = new ArrayList<>();
57         qnames.add(Q0);
58         qnames.add(Q1);
59         qnames.add(Q2);
60
61         // foo.yang
62         final Module module1 = TestUtils.findModule(context, "foo").get();
63         Set<AugmentationSchemaNode> augmentations = module1.getAugmentations();
64         assertEquals(1, augmentations.size());
65         final AugmentationSchemaNode augment = augmentations.iterator().next();
66         assertNotNull(augment);
67
68         SchemaPath expectedSchemaPath = SchemaPath.create(qnames, true);
69         assertEquals(expectedSchemaPath, augment.getTargetPath());
70
71         final Collection<DataSchemaNode> augmentChildren = augment.getChildNodes();
72         assertEquals(4, augmentChildren.size());
73         for (final DataSchemaNode dsn : augmentChildren) {
74             TestUtils.checkIsAugmenting(dsn, false);
75         }
76
77         final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augment.getDataChildByName(QName.create(
78                 module1.getQNameModule(), "ds0ChannelNumber"));
79         final LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName(QName.create(
80                 module1.getQNameModule(), "interface-id"));
81         final ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName(QName.create(
82                 module1.getQNameModule(), "schemas"));
83         final ChoiceSchemaNode odl = (ChoiceSchemaNode) augment.getDataChildByName(QName.create(
84                 module1.getQNameModule(), "odl"));
85
86         assertNotNull(ds0ChannelNumber);
87         assertNotNull(interfaceId);
88         assertNotNull(schemas);
89         assertNotNull(odl);
90
91         // leaf ds0ChannelNumber
92         QName qname = QName.create(FOO, "ds0ChannelNumber");
93         qnames.add(qname);
94         assertEquals(qname, ds0ChannelNumber.getQName());
95         expectedSchemaPath = SchemaPath.create(qnames, true);
96         assertEquals(expectedSchemaPath, ds0ChannelNumber.getPath());
97         assertFalse(ds0ChannelNumber.isAugmenting());
98         // type of leaf ds0ChannelNumber
99         final List<QName> typePath = Collections.singletonList(BaseTypes.STRING_QNAME);
100         expectedSchemaPath = SchemaPath.create(typePath, true);
101         assertEquals(expectedSchemaPath, ds0ChannelNumber.getType().getPath());
102
103         // leaf interface-id
104         qname = QName.create(FOO, "interface-id");
105         assertEquals(qname, interfaceId.getQName());
106         qnames.set(3, qname);
107         expectedSchemaPath = SchemaPath.create(qnames, true);
108         assertEquals(expectedSchemaPath, interfaceId.getPath());
109         assertFalse(interfaceId.isAugmenting());
110
111         // container schemas
112         qname = QName.create(FOO, "schemas");
113         assertEquals(qname, schemas.getQName());
114         qnames.set(3, qname);
115         expectedSchemaPath = SchemaPath.create(qnames, true);
116         assertEquals(expectedSchemaPath, schemas.getPath());
117         assertFalse(schemas.isAugmenting());
118
119         // choice odl
120         qname = QName.create(FOO, "odl");
121         assertEquals(qname, odl.getQName());
122         qnames.set(3, qname);
123         expectedSchemaPath = SchemaPath.create(qnames, true);
124         assertEquals(expectedSchemaPath, odl.getPath());
125         assertFalse(odl.isAugmenting());
126
127         // baz.yang
128         final Module module3 = TestUtils.findModule(context, "baz").get();
129         augmentations = module3.getAugmentations();
130         assertEquals(3, augmentations.size());
131         AugmentationSchemaNode augment1 = null;
132         AugmentationSchemaNode augment2 = null;
133         AugmentationSchemaNode augment3 = null;
134         for (final AugmentationSchemaNode as : augmentations) {
135             if (!as.getWhenCondition().isPresent()) {
136                 augment3 = as;
137             } else if ("if:ifType='ds0'".equals(as.getWhenCondition().get().toString())) {
138                 augment1 = as;
139             } else if ("if:ifType='ds2'".equals(as.getWhenCondition().get().toString())) {
140                 augment2 = as;
141             }
142         }
143         assertNotNull(augment1);
144         assertNotNull(augment2);
145         assertNotNull(augment3);
146
147         assertEquals(1, augment1.getChildNodes().size());
148         final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment1.getDataChildByName(QName.create(
149                 module3.getQNameModule(), "augment-holder"));
150         assertNotNull(augmentHolder);
151
152         assertEquals(1, augment2.getChildNodes().size());
153         final ContainerSchemaNode augmentHolder2 = (ContainerSchemaNode) augment2.getDataChildByName(QName.create(
154                 module3.getQNameModule(), "augment-holder2"));
155         assertNotNull(augmentHolder2);
156
157         assertEquals(1, augment3.getChildNodes().size());
158         final ChoiceCaseNode pause = (ChoiceCaseNode) augment3.getDataChildByName(QName.create(
159                 module3.getQNameModule(), "pause"));
160         assertNotNull(pause);
161     }
162
163     @Test
164     public void testAugmentResolving() throws Exception {
165         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
166             .toURI());
167         final Module module2 = TestUtils.findModule(context, "bar").get();
168         final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
169                 module2.getQNameModule(), "interfaces"));
170         final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
171                 module2.getQNameModule(), "ifEntry"));
172
173         final List<QName> qnames = new ArrayList<>();
174         qnames.add(Q0);
175         qnames.add(Q1);
176         qnames.add(Q2);
177
178         // baz.yang
179         // augment "/br:interfaces/br:ifEntry" {
180         final ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(BAZ,
181                 "augment-holder"));
182         TestUtils.checkIsAugmenting(augmentHolder, true);
183         assertEquals(Q2, augmentHolder.getQName());
184         assertEquals(SchemaPath.create(qnames, true), augmentHolder.getPath());
185
186         // foo.yang
187         // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
188         final LeafSchemaNode ds0ChannelNumber = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
189                 "ds0ChannelNumber"));
190         final LeafSchemaNode interfaceId = (LeafSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
191                 "interface-id"));
192         final ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO,
193                 "schemas"));
194         final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentHolder.getDataChildByName(QName.create(FOO, "odl"));
195
196         assertNotNull(ds0ChannelNumber);
197         assertNotNull(interfaceId);
198         assertNotNull(schemas);
199         assertNotNull(odl);
200
201         // leaf ds0ChannelNumber
202         QName qname = QName.create(FOO, "ds0ChannelNumber");
203         assertEquals(qname, ds0ChannelNumber.getQName());
204         qnames.add(qname);
205         assertEquals(SchemaPath.create(qnames, true), ds0ChannelNumber.getPath());
206
207         // leaf interface-id
208         qname = QName.create(FOO, "interface-id");
209         assertEquals(qname, interfaceId.getQName());
210         qnames.set(3, qname);
211         assertEquals(SchemaPath.create(qnames, true), interfaceId.getPath());
212
213         // container schemas
214         qname = QName.create(FOO, "schemas");
215         assertEquals(qname, schemas.getQName());
216         qnames.set(3, qname);
217         assertEquals(SchemaPath.create(qnames, true), schemas.getPath());
218
219         // choice odl
220         qname = QName.create(FOO, "odl");
221         assertEquals(qname, odl.getQName());
222         qnames.set(3, qname);
223         assertEquals(SchemaPath.create(qnames, true), odl.getPath());
224     }
225
226     @Test
227     public void testAugmentedChoice() throws Exception {
228         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
229             .toURI());
230         final Module module2 = TestUtils.findModule(context, "bar").get();
231         final ContainerSchemaNode interfaces = (ContainerSchemaNode) module2.getDataChildByName(QName.create(
232                 module2.getQNameModule(), "interfaces"));
233         final ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName(QName.create(
234                 module2.getQNameModule(), "ifEntry"));
235         final ContainerSchemaNode augmentedHolder = (ContainerSchemaNode) ifEntry.getDataChildByName(QName.create(
236                 BAZ, "augment-holder"));
237         TestUtils.checkIsAugmenting(augmentedHolder, true);
238
239         // foo.yang
240         // augment "/br:interfaces/br:ifEntry/bz:augment-holder"
241         final ChoiceSchemaNode odl = (ChoiceSchemaNode) augmentedHolder.getDataChildByName(QName.create(FOO, "odl"));
242         assertNotNull(odl);
243         final SortedMap<QName, ChoiceCaseNode> cases = odl.getCases();
244         assertEquals(4, cases.size());
245
246         ChoiceCaseNode id = null;
247         ChoiceCaseNode node1 = null;
248         ChoiceCaseNode node2 = null;
249         ChoiceCaseNode node3 = null;
250
251         for (final ChoiceCaseNode ccn : cases.values()) {
252             if ("id".equals(ccn.getQName().getLocalName())) {
253                 id = ccn;
254             } else if ("node1".equals(ccn.getQName().getLocalName())) {
255                 node1 = ccn;
256             } else if ("node2".equals(ccn.getQName().getLocalName())) {
257                 node2 = ccn;
258             } else if ("node3".equals(ccn.getQName().getLocalName())) {
259                 node3 = ccn;
260             }
261         }
262
263         assertNotNull(id);
264         assertNotNull(node1);
265         assertNotNull(node2);
266         assertNotNull(node3);
267
268         final List<QName> qnames = new ArrayList<>();
269         qnames.add(Q0);
270         qnames.add(Q1);
271         qnames.add(Q2);
272         qnames.add(QName.create(FOO, "odl"));
273
274         // case id
275         QName qname = QName.create(FOO, "id");
276         assertEquals(qname, id.getQName());
277         qnames.add(qname);
278         assertEquals(SchemaPath.create(qnames, true), id.getPath());
279         final Collection<DataSchemaNode> idChildren = id.getChildNodes();
280         assertEquals(1, idChildren.size());
281
282         // case node1
283         qname = QName.create(FOO, "node1");
284         assertEquals(qname, node1.getQName());
285         qnames.set(4, qname);
286         assertEquals(SchemaPath.create(qnames, true), node1.getPath());
287         final Collection<DataSchemaNode> node1Children = node1.getChildNodes();
288         assertTrue(node1Children.isEmpty());
289
290         // case node2
291         qname = QName.create(FOO, "node2");
292         assertEquals(qname, node2.getQName());
293         qnames.set(4, qname);
294         assertEquals(SchemaPath.create(qnames, true), node2.getPath());
295         final Collection<DataSchemaNode> node2Children = node2.getChildNodes();
296         assertTrue(node2Children.isEmpty());
297
298         // case node3
299         qname = QName.create(FOO, "node3");
300         assertEquals(qname, node3.getQName());
301         qnames.set(4, qname);
302         assertEquals(SchemaPath.create(qnames, true), node3.getPath());
303         final Collection<DataSchemaNode> node3Children = node3.getChildNodes();
304         assertEquals(1, node3Children.size());
305
306         // test cases
307         qnames.clear();
308         qnames.add(Q0);
309         qnames.add(Q1);
310         qnames.add(Q2);
311         qnames.add(QName.create(FOO, "odl"));
312
313         // case id child
314         qnames.add(QName.create(FOO, "id"));
315         qnames.add(QName.create(FOO, "id"));
316         final LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next();
317         assertNotNull(caseIdChild);
318         assertEquals(SchemaPath.create(qnames, true), caseIdChild.getPath());
319
320         // case node3 child
321         qnames.set(4, QName.create(FOO, "node3"));
322         qnames.set(5, QName.create(FOO, "node3"));
323         final ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next();
324         assertNotNull(caseNode3Child);
325         assertEquals(SchemaPath.create(qnames, true), caseNode3Child.getPath());
326     }
327
328     @Test
329     public void testAugmentRpc() throws Exception {
330         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").toURI());
331         final URI NS_BAR = URI.create("urn:opendaylight:bar");
332         final URI NS_FOO = URI.create("urn:opendaylight:foo");
333         final Revision revision = Revision.of("2013-10-11");
334         final Module bar = TestUtils.findModule(context, "bar").get();
335         final Set<RpcDefinition> rpcs = bar.getRpcs();
336         assertEquals(2, rpcs.size());
337
338         RpcDefinition submit = null;
339         for (final RpcDefinition rpc : rpcs) {
340             if ("submit".equals(rpc.getQName().getLocalName())) {
341                 submit = rpc;
342                 break;
343             }
344         }
345         assertNotNull(submit);
346
347         final QName submitQName = QName.create(NS_BAR, revision, "submit");
348         assertEquals(submitQName, submit.getQName());
349         final ContainerSchemaNode input = submit.getInput();
350         final QName inputQName = QName.create(NS_BAR, revision, "input");
351         assertEquals(inputQName, input.getQName());
352         final ChoiceSchemaNode arguments = (ChoiceSchemaNode) input.getDataChildByName(QName.create(NS_BAR, revision,
353                 "arguments"));
354         final QName argumentsQName = QName.create(NS_BAR, revision, "arguments");
355         assertEquals(argumentsQName, arguments.getQName());
356         assertFalse(arguments.isAugmenting());
357         final SortedMap<QName, ChoiceCaseNode> cases = arguments.getCases();
358         assertEquals(3, cases.size());
359
360         ChoiceCaseNode attach = null;
361         ChoiceCaseNode create = null;
362         ChoiceCaseNode destroy = null;
363         for (final ChoiceCaseNode child : cases.values()) {
364             if ("attach".equals(child.getQName().getLocalName())) {
365                 attach = child;
366             } else if ("create".equals(child.getQName().getLocalName())) {
367                 create = child;
368             } else if ("destroy".equals(child.getQName().getLocalName())) {
369                 destroy = child;
370             }
371         }
372         assertNotNull(attach);
373         assertNotNull(create);
374         assertNotNull(destroy);
375
376         assertTrue(attach.isAugmenting());
377         assertTrue(create.isAugmenting());
378         assertTrue(destroy.isAugmenting());
379
380         final QName[] qnames = new QName[4];
381         qnames[0] = submitQName;
382         qnames[1] = inputQName;
383         qnames[2] = argumentsQName;
384
385         // case attach
386         qnames[3] = QName.create(NS_FOO, revision, "attach");
387         assertEquals(qnames[3], attach.getQName());
388         assertEquals(SchemaPath.create(true, qnames), attach.getPath());
389         final Collection<DataSchemaNode> attachChildren = attach.getChildNodes();
390         assertEquals(1, attachChildren.size());
391
392         // case create
393         qnames[3] = QName.create(NS_FOO, revision, "create");
394         assertEquals(qnames[3], create.getQName());
395         assertEquals(SchemaPath.create(true, qnames), create.getPath());
396         final Collection<DataSchemaNode> createChildren = create.getChildNodes();
397         assertEquals(1, createChildren.size());
398
399         // case attach
400         qnames[3] = QName.create(NS_FOO, revision, "destroy");
401         assertEquals(qnames[3], destroy.getQName());
402         assertEquals(SchemaPath.create(true, qnames), destroy.getPath());
403         final Collection<DataSchemaNode> destroyChildren = destroy.getChildNodes();
404         assertEquals(1, destroyChildren.size());
405     }
406
407     @Test
408     public void testAugmentInUsesResolving() throws Exception {
409         final SchemaContext context = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-uses")
410             .toURI());
411         assertEquals(1, context.getModules().size());
412
413         final Module test = context.getModules().iterator().next();
414         final DataNodeContainer links = (DataNodeContainer) test.getDataChildByName(QName.create(test.getQNameModule(),
415                 "links"));
416         final DataNodeContainer link = (DataNodeContainer) links.getDataChildByName(QName.create(test.getQNameModule(),
417                 "link"));
418         final DataNodeContainer nodes = (DataNodeContainer) link.getDataChildByName(QName.create(test.getQNameModule(),
419                 "nodes"));
420         final ContainerSchemaNode node = (ContainerSchemaNode) nodes.getDataChildByName(QName.create(
421                 test.getQNameModule(), "node"));
422         final Set<AugmentationSchemaNode> augments = node.getAvailableAugmentations();
423         assertEquals(1, augments.size());
424         assertEquals(1, node.getChildNodes().size());
425         final LeafSchemaNode id = (LeafSchemaNode) node.getDataChildByName(QName.create(test.getQNameModule(), "id"));
426         assertTrue(id.isAugmenting());
427     }
428
429 }