Fix get{Min,Max}Elements() usage
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / GroupingTest.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.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.IOException;
17 import java.net.URI;
18 import java.text.ParseException;
19 import java.util.Collection;
20 import java.util.Map;
21 import java.util.Optional;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
26 import org.opendaylight.yangtools.yang.common.Revision;
27 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
34 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.UsesNode;
45 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
46 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
47 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
49
50 public class GroupingTest {
51     private SchemaContext ctx;
52     private Module foo;
53     private Module baz;
54
55     @Before
56     public void init() throws Exception {
57         ctx = TestUtils.loadModules(getClass().getResource("/model").toURI());
58         foo = TestUtils.findModule(ctx, "foo").get();
59         baz = TestUtils.findModule(ctx, "baz").get();
60         assertEquals(3, ctx.getModules().size());
61     }
62
63     @Test
64     public void testRefine() {
65         final Module testModule = TestUtils.findModule(ctx, "foo").get();
66         final ContainerSchemaNode peer = (ContainerSchemaNode) testModule.getDataChildByName(QName.create(
67                 testModule.getQNameModule(), "peer"));
68         final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
69                 testModule.getQNameModule(), "destination"));
70
71         final Collection<? extends UsesNode> usesNodes = destination.getUses();
72         assertEquals(1, usesNodes.size());
73         final UsesNode usesNode = usesNodes.iterator().next();
74         final Map<Descendant, SchemaNode> refines = usesNode.getRefines();
75         assertEquals(4, refines.size());
76
77         LeafSchemaNode refineLeaf = null;
78         ContainerSchemaNode refineContainer = null;
79         ListSchemaNode refineList = null;
80         LeafSchemaNode refineInnerLeaf = null;
81         for (final Map.Entry<Descendant, SchemaNode> entry : refines.entrySet()) {
82             final SchemaNode value = entry.getValue();
83             if ("address".equals(value.getQName().getLocalName())) {
84                 refineLeaf = (LeafSchemaNode) destination.getDataChildByName(value.getQName());
85             } else if ("port".equals(value.getQName().getLocalName())) {
86                 refineContainer = (ContainerSchemaNode) destination.getDataChildByName(value.getQName());
87             } else if ("addresses".equals(value.getQName().getLocalName())) {
88                 refineList = (ListSchemaNode) destination.getDataChildByName(value.getQName());
89             }
90         }
91
92         assertNotNull(refineList);
93         for (final Map.Entry<Descendant, SchemaNode> entry : refines.entrySet()) {
94             final SchemaNode value = entry.getValue();
95             if ("id".equals(value.getQName().getLocalName())) {
96                 refineInnerLeaf = (LeafSchemaNode) refineList.getDataChildByName(value.getQName());
97             }
98         }
99
100         // leaf address
101         assertNotNull(refineLeaf);
102         assertEquals(Optional.of("IP address of target node"), refineLeaf.getDescription());
103         assertEquals(Optional.of("address reference added by refine"), refineLeaf.getReference());
104         assertFalse(refineLeaf.isConfiguration());
105         assertFalse(refineLeaf.isMandatory());
106         final Collection<? extends MustDefinition> leafMustConstraints = refineLeaf.getMustConstraints();
107         assertEquals(1, leafMustConstraints.size());
108         final MustDefinition leafMust = leafMustConstraints.iterator().next();
109         assertEquals("ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)", leafMust.getXpath().toString());
110         assertEquals(1, refineLeaf.getUnknownSchemaNodes().size());
111
112         // container port
113         assertNotNull(refineContainer);
114         final Collection<? extends MustDefinition> mustConstraints = refineContainer.getMustConstraints();
115         assertTrue(mustConstraints.isEmpty());
116         assertEquals(Optional.of("description of port defined by refine"), refineContainer.getDescription());
117         assertEquals(Optional.of("port reference added by refine"), refineContainer.getReference());
118         assertFalse(refineContainer.isConfiguration());
119         assertTrue(refineContainer.isPresenceContainer());
120
121         // list addresses
122         assertEquals(Optional.of("description of addresses defined by refine"), refineList.getDescription());
123         assertEquals(Optional.of("addresses reference added by refine"), refineList.getReference());
124         assertFalse(refineList.isConfiguration());
125
126         final ElementCountConstraint constraint = refineList.getElementCountConstraint().get();
127         assertEquals((Object) 2, constraint.getMinElements());
128         assertNull(constraint.getMaxElements());
129
130         // leaf id
131         assertNotNull(refineInnerLeaf);
132         assertEquals(Optional.of("id of address"), refineInnerLeaf.getDescription());
133     }
134
135     @Test
136     public void testGrouping() {
137         final Module testModule = TestUtils.findModule(ctx, "baz").get();
138         final Collection<? extends GroupingDefinition> groupings = testModule.getGroupings();
139         assertEquals(1, groupings.size());
140         final GroupingDefinition grouping = groupings.iterator().next();
141         final Collection<? extends DataSchemaNode> children = grouping.getChildNodes();
142         assertEquals(5, children.size());
143     }
144
145     @Test
146     public void testUses() {
147         // suffix _u = added by uses
148         // suffix _g = defined in grouping
149
150
151         // get grouping
152         final Collection<? extends GroupingDefinition> groupings = baz.getGroupings();
153         assertEquals(1, groupings.size());
154         final GroupingDefinition grouping = groupings.iterator().next();
155
156         // get node containing uses
157         final ContainerSchemaNode peer = (ContainerSchemaNode) foo.getDataChildByName(QName.create(
158                 foo.getQNameModule(), "peer"));
159         final ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName(QName.create(
160                 foo.getQNameModule(), "destination"));
161
162         // check uses
163         final Collection<? extends UsesNode> uses = destination.getUses();
164         assertEquals(1, uses.size());
165
166         // check uses process
167         final AnyxmlSchemaNode data_u = (AnyxmlSchemaNode) destination.getDataChildByName(QName.create(
168                 foo.getQNameModule(), "data"));
169         assertNotNull(data_u);
170         assertTrue(data_u.isAddedByUses());
171
172         final AnyxmlSchemaNode data_g = (AnyxmlSchemaNode) grouping.getDataChildByName(QName.create(
173                 baz.getQNameModule(), "data"));
174         assertNotNull(data_g);
175         assertFalse(data_g.isAddedByUses());
176         assertFalse(data_u.equals(data_g));
177         assertEquals(data_g, SchemaNodeUtils.getRootOriginalIfPossible(data_u));
178
179         final ChoiceSchemaNode how_u = (ChoiceSchemaNode) destination.getDataChildByName(QName.create(
180                 foo.getQNameModule(), "how"));
181         assertNotNull(how_u);
182         TestUtils.checkIsAddedByUses(how_u, true);
183         assertEquals(2, how_u.getCases().size());
184
185         final ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName(QName.create(
186                 baz.getQNameModule(), "how"));
187         assertNotNull(how_g);
188         TestUtils.checkIsAddedByUses(how_g, false);
189         assertEquals(2, how_g.getCases().size());
190         assertFalse(how_u.equals(how_g));
191         assertEquals(how_g, SchemaNodeUtils.getRootOriginalIfPossible(how_u));
192
193         final LeafSchemaNode address_u = (LeafSchemaNode) destination.getDataChildByName(QName.create(
194                 foo.getQNameModule(), "address"));
195         assertNotNull(address_u);
196         assertEquals(Optional.of("1.2.3.4"), address_u.getType().getDefaultValue());
197         assertEquals(Optional.of("IP address of target node"), address_u.getDescription());
198         assertEquals(Optional.of("address reference added by refine"), address_u.getReference());
199         assertFalse(address_u.isConfiguration());
200         assertTrue(address_u.isAddedByUses());
201         assertFalse(address_u.isMandatory());
202
203         final LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName(QName.create(
204                 baz.getQNameModule(), "address"));
205         assertNotNull(address_g);
206         assertFalse(address_g.isAddedByUses());
207         assertEquals(Optional.empty(), address_g.getType().getDefaultValue());
208         assertEquals(Optional.of("Target IP address"), address_g.getDescription());
209         assertFalse(address_g.getReference().isPresent());
210         assertTrue(address_g.isConfiguration());
211         assertFalse(address_u.equals(address_g));
212         assertTrue(address_g.isMandatory());
213         assertEquals(address_g, SchemaNodeUtils.getRootOriginalIfPossible(address_u));
214
215         final ContainerSchemaNode port_u = (ContainerSchemaNode) destination.getDataChildByName(QName.create(
216                 foo.getQNameModule(), "port"));
217         assertNotNull(port_u);
218         TestUtils.checkIsAddedByUses(port_u, true);
219
220         final ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName(QName.create(
221                 baz.getQNameModule(), "port"));
222         assertNotNull(port_g);
223         TestUtils.checkIsAddedByUses(port_g, false);
224         assertFalse(port_u.equals(port_g));
225         assertEquals(port_g, SchemaNodeUtils.getRootOriginalIfPossible(port_u));
226
227         final ListSchemaNode addresses_u = (ListSchemaNode) destination.getDataChildByName(QName.create(
228                 foo.getQNameModule(), "addresses"));
229         assertNotNull(addresses_u);
230         TestUtils.checkIsAddedByUses(addresses_u, true);
231
232         final ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName(QName.create(
233                 baz.getQNameModule(), "addresses"));
234         assertNotNull(addresses_g);
235         TestUtils.checkIsAddedByUses(addresses_g, false);
236         assertFalse(addresses_u.equals(addresses_g));
237         assertEquals(addresses_g, SchemaNodeUtils.getRootOriginalIfPossible(addresses_u));
238
239         // grouping defined by 'uses'
240         final Collection<? extends GroupingDefinition> groupings_u = destination.getGroupings();
241         assertEquals(0, groupings_u.size());
242
243         // grouping defined in 'grouping' node
244         final Collection<? extends GroupingDefinition> groupings_g = grouping.getGroupings();
245         assertEquals(1, groupings_g.size());
246         final GroupingDefinition grouping_g = groupings_g.iterator().next();
247         TestUtils.checkIsAddedByUses(grouping_g, false);
248
249         final Collection<? extends UnknownSchemaNode> nodes_u = destination.getUnknownSchemaNodes();
250         assertEquals(1, nodes_u.size());
251         final UnknownSchemaNode node_u = nodes_u.iterator().next();
252         assertTrue(node_u.isAddedByUses());
253
254         final Collection<? extends UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
255         assertEquals(1, nodes_g.size());
256         final UnknownSchemaNode node_g = nodes_g.iterator().next();
257         assertFalse(node_g.isAddedByUses());
258         assertFalse(node_u.equals(node_g));
259     }
260
261     @Test
262     public void testUsesUnderModule() {
263         // suffix _u = added by uses
264         // suffix _g = defined in grouping
265
266         // get grouping
267         final Collection<? extends GroupingDefinition> groupings = baz.getGroupings();
268         assertEquals(1, groupings.size());
269         final GroupingDefinition grouping = groupings.iterator().next();
270
271         // check uses
272         final Collection<? extends UsesNode> uses = foo.getUses();
273         assertEquals(1, uses.size());
274
275         // check uses process
276         final AnyxmlSchemaNode data_u = (AnyxmlSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
277                 "data"));
278         assertNotNull(data_u);
279         assertTrue(data_u.isAddedByUses());
280
281         final AnyxmlSchemaNode data_g = (AnyxmlSchemaNode) grouping.getDataChildByName(QName.create(
282             baz.getQNameModule(), "data"));
283         assertNotNull(data_g);
284         assertFalse(data_g.isAddedByUses());
285         assertFalse(data_u.equals(data_g));
286         assertEquals(data_g, SchemaNodeUtils.getRootOriginalIfPossible(data_u));
287
288         final ChoiceSchemaNode how_u = (ChoiceSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
289                 "how"));
290         assertNotNull(how_u);
291         TestUtils.checkIsAddedByUses(how_u, true);
292         assertFalse(how_u.isAugmenting());
293         final Collection<? extends CaseSchemaNode> cases_u = how_u.getCases();
294         assertEquals(2, cases_u.size());
295         final CaseSchemaNode interval = how_u.findCaseNodes("interval").iterator().next();
296         assertFalse(interval.isAugmenting());
297         final LeafSchemaNode name = (LeafSchemaNode) interval.getDataChildByName(QName.create(foo.getQNameModule(),
298                 "name"));
299         assertTrue(name.isAugmenting());
300         final LeafSchemaNode intervalLeaf = (LeafSchemaNode) interval.getDataChildByName(QName.create(
301                 foo.getQNameModule(), "interval"));
302         assertFalse(intervalLeaf.isAugmenting());
303
304         final ChoiceSchemaNode how_g = (ChoiceSchemaNode) grouping.getDataChildByName(QName.create(
305                 baz.getQNameModule(), "how"));
306         assertNotNull(how_g);
307         TestUtils.checkIsAddedByUses(how_g, false);
308         assertFalse(how_u.equals(how_g));
309         assertEquals(how_g, SchemaNodeUtils.getRootOriginalIfPossible(how_u));
310
311         final LeafSchemaNode address_u = (LeafSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
312                 "address"));
313         assertNotNull(address_u);
314         assertEquals(Optional.empty(), address_u.getType().getDefaultValue());
315         assertEquals(Optional.of("Target IP address"), address_u.getDescription());
316         assertFalse(address_u.getReference().isPresent());
317         assertTrue(address_u.isConfiguration());
318         assertTrue(address_u.isAddedByUses());
319
320         final LeafSchemaNode address_g = (LeafSchemaNode) grouping.getDataChildByName(QName.create(
321                 baz.getQNameModule(), "address"));
322         assertNotNull(address_g);
323         assertFalse(address_g.isAddedByUses());
324         assertEquals(Optional.empty(), address_g.getType().getDefaultValue());
325         assertEquals(Optional.of("Target IP address"), address_g.getDescription());
326         assertFalse(address_g.getReference().isPresent());
327         assertTrue(address_g.isConfiguration());
328         assertFalse(address_u.equals(address_g));
329         assertEquals(address_g, SchemaNodeUtils.getRootOriginalIfPossible(address_u));
330
331         final ContainerSchemaNode port_u = (ContainerSchemaNode) foo.getDataChildByName(QName.create(
332                 foo.getQNameModule(), "port"));
333         assertNotNull(port_u);
334         TestUtils.checkIsAddedByUses(port_u, true);
335
336         final ContainerSchemaNode port_g = (ContainerSchemaNode) grouping.getDataChildByName(QName.create(
337                 baz.getQNameModule(), "port"));
338         assertNotNull(port_g);
339         TestUtils.checkIsAddedByUses(port_g, false);
340         assertFalse(port_u.equals(port_g));
341         assertEquals(port_g, SchemaNodeUtils.getRootOriginalIfPossible(port_u));
342
343         final ListSchemaNode addresses_u = (ListSchemaNode) foo.getDataChildByName(QName.create(foo.getQNameModule(),
344                 "addresses"));
345         assertNotNull(addresses_u);
346         TestUtils.checkIsAddedByUses(addresses_u, true);
347
348         final ListSchemaNode addresses_g = (ListSchemaNode) grouping.getDataChildByName(QName.create(
349                 baz.getQNameModule(), "addresses"));
350         assertNotNull(addresses_g);
351         TestUtils.checkIsAddedByUses(addresses_g, false);
352         assertFalse(addresses_u.equals(addresses_g));
353         assertEquals(addresses_g, SchemaNodeUtils.getRootOriginalIfPossible(addresses_u));
354
355         // grouping defined by 'uses'
356         final Collection<? extends GroupingDefinition> groupings_u = foo.getGroupings();
357         assertEquals(0, groupings_u.size());
358
359         // grouping defined in 'grouping' node
360         final Collection<? extends GroupingDefinition> groupings_g = grouping.getGroupings();
361         assertEquals(1, groupings_g.size());
362         final GroupingDefinition grouping_g = groupings_g.iterator().next();
363         TestUtils.checkIsAddedByUses(grouping_g, false);
364
365         final Collection<? extends UnknownSchemaNode> nodes_u = foo.getUnknownSchemaNodes();
366         assertEquals(1, nodes_u.size());
367         final UnknownSchemaNode node_u = nodes_u.iterator().next();
368         assertTrue(node_u.isAddedByUses());
369
370         final Collection<? extends UnknownSchemaNode> nodes_g = grouping.getUnknownSchemaNodes();
371         assertEquals(1, nodes_g.size());
372         final UnknownSchemaNode node_g = nodes_g.iterator().next();
373         assertFalse(node_g.isAddedByUses());
374         assertFalse(node_u.equals(node_g));
375
376         final UsesNode un = uses.iterator().next();
377         final Collection<? extends AugmentationSchemaNode> usesAugments = un.getAugmentations();
378         assertEquals(1, usesAugments.size());
379         final AugmentationSchemaNode augment = usesAugments.iterator().next();
380         assertEquals(Optional.of("inner augment"), augment.getDescription());
381         final Collection<? extends DataSchemaNode> children = augment.getChildNodes();
382         assertEquals(1, children.size());
383         final DataSchemaNode leaf = children.iterator().next();
384         assertTrue(leaf instanceof LeafSchemaNode);
385         assertEquals("name", leaf.getQName().getLocalName());
386     }
387
388     @Test
389     public void testCascadeUses() throws ReactorException, ParseException, IOException, YangSyntaxErrorException {
390         ctx = TestUtils.loadModuleResources(getClass(), "/grouping-test/cascade-uses.yang");
391         assertEquals(1, ctx.getModules().size());
392
393         final Module testModule = TestUtils.findModule(ctx, "cascade-uses").get();
394         final Collection<? extends GroupingDefinition> groupings = testModule.getGroupings();
395
396         GroupingDefinition gu = null;
397         GroupingDefinition gv = null;
398         GroupingDefinition gx = null;
399         GroupingDefinition gy = null;
400         GroupingDefinition gz = null;
401         GroupingDefinition gzz = null;
402         for (final GroupingDefinition gd : groupings) {
403             final String name = gd.getQName().getLocalName();
404             switch (name) {
405                 case "grouping-U":
406                     gu = gd;
407                     break;
408                 case "grouping-V":
409                     gv = gd;
410                     break;
411                 case "grouping-X":
412                     gx = gd;
413                     break;
414                 case "grouping-Y":
415                     gy = gd;
416                     break;
417                 case "grouping-Z":
418                     gz = gd;
419                     break;
420                 case "grouping-ZZ":
421                     gzz = gd;
422                     break;
423                 default:
424                     break;
425             }
426         }
427         assertNotNull(gu);
428         assertNotNull(gv);
429         assertNotNull(gx);
430         assertNotNull(gy);
431         assertNotNull(gz);
432         assertNotNull(gzz);
433
434         final QNameModule expectedModule = QNameModule.create(URI.create("urn:grouping:cascade-uses"),
435             Revision.of("2013-07-18"));
436
437         // grouping-U
438         Collection<? extends DataSchemaNode> childNodes = gu.getChildNodes();
439         assertEquals(7, childNodes.size());
440
441         final LeafSchemaNode leafGroupingU = (LeafSchemaNode) gu.getDataChildByName(QName.create(
442                 testModule.getQNameModule(), "leaf-grouping-U"));
443         assertNotNull(leafGroupingU);
444         assertFalse(leafGroupingU.isAddedByUses());
445         assertFalse(SchemaNodeUtils.getOriginalIfPossible(leafGroupingU).isPresent());
446
447         for (final DataSchemaNode childNode : childNodes) {
448             if (!childNode.getQName().equals(leafGroupingU.getQName())) {
449                 TestUtils.checkIsAddedByUses(childNode, true);
450             }
451         }
452
453         // grouping-V
454         childNodes = gv.getChildNodes();
455         assertEquals(4, childNodes.size());
456         LeafSchemaNode leafGroupingV = null;
457         ContainerSchemaNode containerGroupingV = null;
458         for (final DataSchemaNode childNode : childNodes) {
459             if ("leaf-grouping-V".equals(childNode.getQName().getLocalName())) {
460                 leafGroupingV = (LeafSchemaNode) childNode;
461             } else if ("container-grouping-V".equals(childNode.getQName().getLocalName())) {
462                 containerGroupingV = (ContainerSchemaNode) childNode;
463             } else {
464                 TestUtils.checkIsAddedByUses(childNode, true);
465             }
466         }
467         assertNotNull(leafGroupingV);
468         assertFalse(leafGroupingV.isAddedByUses());
469
470         // grouping-V/container-grouping-V
471         assertNotNull(containerGroupingV);
472         assertFalse(containerGroupingV.isAddedByUses());
473         SchemaPath expectedPath = TestUtils.createPath(true, expectedModule, "grouping-V", "container-grouping-V");
474         assertEquals(expectedPath, containerGroupingV.getPath());
475         childNodes = containerGroupingV.getChildNodes();
476         assertEquals(2, childNodes.size());
477         for (final DataSchemaNode childNode : childNodes) {
478             TestUtils.checkIsAddedByUses(childNode, true);
479         }
480
481         // grouping-V/container-grouping-V/leaf-grouping-X
482         final LeafSchemaNode leafXinContainerV = (LeafSchemaNode) containerGroupingV.getDataChildByName(QName.create(
483                 testModule.getQNameModule(), "leaf-grouping-X"));
484         assertNotNull(leafXinContainerV);
485         expectedPath = TestUtils.createPath(true, expectedModule, "grouping-V", "container-grouping-V",
486             "leaf-grouping-X");
487         assertEquals(expectedPath, leafXinContainerV.getPath());
488         // grouping-V/container-grouping-V/leaf-grouping-Y
489         final LeafSchemaNode leafYinContainerV = (LeafSchemaNode) containerGroupingV.getDataChildByName(QName.create(
490                 testModule.getQNameModule(), "leaf-grouping-Y"));
491         assertNotNull(leafYinContainerV);
492         expectedPath = TestUtils.createPath(true, expectedModule, "grouping-V", "container-grouping-V",
493             "leaf-grouping-Y");
494         assertEquals(expectedPath, leafYinContainerV.getPath());
495
496         // grouping-X
497         childNodes = gx.getChildNodes();
498         assertEquals(2, childNodes.size());
499
500         // grouping-X/leaf-grouping-X
501         final LeafSchemaNode leafXinGX = (LeafSchemaNode) gx.getDataChildByName(QName.create(
502                 testModule.getQNameModule(), "leaf-grouping-X"));
503         assertNotNull(leafXinGX);
504         assertFalse(leafXinGX.isAddedByUses());
505         expectedPath = TestUtils.createPath(true, expectedModule, "grouping-X", "leaf-grouping-X");
506         assertEquals(expectedPath, leafXinGX.getPath());
507
508         // grouping-X/leaf-grouping-Y
509         final LeafSchemaNode leafYinGX = (LeafSchemaNode) gx.getDataChildByName(QName.create(
510                 testModule.getQNameModule(), "leaf-grouping-Y"));
511         assertNotNull(leafYinGX);
512         assertTrue(leafYinGX.isAddedByUses());
513         expectedPath = TestUtils.createPath(true, expectedModule, "grouping-X", "leaf-grouping-Y");
514         assertEquals(expectedPath, leafYinGX.getPath());
515
516         // grouping-Y
517         childNodes = gy.getChildNodes();
518         assertEquals(1, childNodes.size());
519
520         // grouping-Y/leaf-grouping-Y
521         final LeafSchemaNode leafYinGY = (LeafSchemaNode) gy.getDataChildByName(QName.create(
522                 testModule.getQNameModule(), "leaf-grouping-Y"));
523         assertNotNull(leafYinGY);
524         assertFalse(leafYinGY.isAddedByUses());
525         expectedPath = TestUtils.createPath(true, expectedModule, "grouping-Y", "leaf-grouping-Y");
526         assertEquals(expectedPath, leafYinGY.getPath());
527
528         // grouping-Z
529         childNodes = gz.getChildNodes();
530         assertEquals(1, childNodes.size());
531
532         // grouping-Z/leaf-grouping-Z
533         final LeafSchemaNode leafZinGZ = (LeafSchemaNode) gz.getDataChildByName(QName.create(
534                 testModule.getQNameModule(), "leaf-grouping-Z"));
535         assertNotNull(leafZinGZ);
536         assertFalse(leafZinGZ.isAddedByUses());
537         expectedPath = TestUtils.createPath(true, expectedModule, "grouping-Z", "leaf-grouping-Z");
538         assertEquals(expectedPath, leafZinGZ.getPath());
539
540         // grouping-ZZ
541         childNodes = gzz.getChildNodes();
542         assertEquals(1, childNodes.size());
543
544         // grouping-ZZ/leaf-grouping-ZZ
545         final LeafSchemaNode leafZZinGZZ = (LeafSchemaNode) gzz.getDataChildByName(QName.create(
546                 testModule.getQNameModule(), "leaf-grouping-ZZ"));
547         assertNotNull(leafZZinGZZ);
548         assertFalse(leafZZinGZZ.isAddedByUses());
549         expectedPath = TestUtils.createPath(true, expectedModule, "grouping-ZZ", "leaf-grouping-ZZ");
550         assertEquals(expectedPath, leafZZinGZZ.getPath());
551
552         // TEST getOriginal from grouping-U
553         assertEquals(
554                 gv.getDataChildByName(QName.create(testModule.getQNameModule(), "leaf-grouping-V")),
555                 SchemaNodeUtils.getRootOriginalIfPossible(gu.getDataChildByName(QName.create(
556                         testModule.getQNameModule(), "leaf-grouping-V"))));
557         containerGroupingV = (ContainerSchemaNode) gu.getDataChildByName(QName.create(testModule.getQNameModule(),
558                 "container-grouping-V"));
559         assertEquals(gv.getDataChildByName(QName.create(testModule.getQNameModule(), "container-grouping-V")),
560                 SchemaNodeUtils.getRootOriginalIfPossible(containerGroupingV));
561         assertEquals(
562                 gx.getDataChildByName(QName.create(testModule.getQNameModule(), "leaf-grouping-X")),
563                 SchemaNodeUtils.getRootOriginalIfPossible(containerGroupingV.getDataChildByName(QName.create(
564                         testModule.getQNameModule(), "leaf-grouping-X"))));
565         assertEquals(
566                 gy.getDataChildByName(QName.create(testModule.getQNameModule(), "leaf-grouping-Y")),
567                 SchemaNodeUtils.getRootOriginalIfPossible(containerGroupingV.getDataChildByName(QName.create(
568                         testModule.getQNameModule(), "leaf-grouping-Y"))));
569
570         assertEquals(
571                 gz.getDataChildByName(QName.create(testModule.getQNameModule(), "leaf-grouping-Z")),
572                 SchemaNodeUtils.getRootOriginalIfPossible(gu.getDataChildByName(QName.create(
573                         testModule.getQNameModule(), "leaf-grouping-Z"))));
574         assertEquals(
575                 gzz.getDataChildByName(QName.create(testModule.getQNameModule(), "leaf-grouping-ZZ")),
576                 SchemaNodeUtils.getRootOriginalIfPossible(gu.getDataChildByName(QName.create(
577                         testModule.getQNameModule(), "leaf-grouping-ZZ"))));
578
579         // TEST getOriginal from grouping-V
580         assertEquals(
581                 gz.getDataChildByName(QName.create(testModule.getQNameModule(), "leaf-grouping-Z")),
582                 SchemaNodeUtils.getRootOriginalIfPossible(gv.getDataChildByName(QName.create(
583                         testModule.getQNameModule(), "leaf-grouping-Z"))));
584         assertEquals(
585                 gzz.getDataChildByName(QName.create(testModule.getQNameModule(), "leaf-grouping-ZZ")),
586                 SchemaNodeUtils.getRootOriginalIfPossible(gv.getDataChildByName(QName.create(
587                         testModule.getQNameModule(), "leaf-grouping-ZZ"))));
588
589         // TEST getOriginal from grouping-X
590         assertEquals(
591                 gy.getDataChildByName(QName.create(testModule.getQNameModule(), "leaf-grouping-Y")),
592                 SchemaNodeUtils.getRootOriginalIfPossible(gx.getDataChildByName(QName.create(
593                         testModule.getQNameModule(), "leaf-grouping-Y"))));
594     }
595
596     @Test
597     public void testAddedByUsesLeafTypeQName() throws Exception {
598         final SchemaContext loadModules = TestUtils.loadModules(getClass().getResource("/added-by-uses-leaf-test")
599                 .toURI());
600         assertEquals(2, loadModules.getModules().size());
601         foo = TestUtils.findModule(loadModules, "foo").get();
602         final Module imp = TestUtils.findModule(loadModules, "import-module").get();
603
604         final LeafSchemaNode leaf = (LeafSchemaNode) ((ContainerSchemaNode) foo.getDataChildByName(QName.create(
605                 foo.getQNameModule(), "my-container")))
606                 .getDataChildByName(QName.create(foo.getQNameModule(), "my-leaf"));
607
608         TypeDefinition<?> impType = null;
609         for (final TypeDefinition<?> typeDefinition : imp.getTypeDefinitions()) {
610             if (typeDefinition.getQName().getLocalName().equals("imp-type")) {
611                 impType = typeDefinition;
612                 break;
613             }
614         }
615
616         assertNotNull(impType);
617         assertEquals(leaf.getType().getQName(), impType.getQName());
618     }
619 }