Move SchemaContextUtilTest
[yangtools.git] / yang / yang-model-util-ut / src / test / java / org / opendaylight / yangtools / yang / model / util / ut / SchemaContextUtilTest.java
1 /*
2  * Copyright (c) 2014 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.model.util.ut;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13
14 import java.net.URI;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.Revision;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
26 import org.opendaylight.yangtools.yang.model.api.PathExpression;
27 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.model.util.PathExpressionImpl;
32 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
33 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
34
35 public class SchemaContextUtilTest {
36     @Test
37     public void findNodeInSchemaContextTest() {
38         final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory("/schema-context-util");
39
40         final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
41
42         SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
43                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
44                 "my-leaf-in-container"));
45
46         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
47                 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
48         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
49
50         assertNotNull(testNode);
51         assertNotNull(foundNode);
52         assertEquals(testNode, foundNode);
53
54         RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
55         testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-input-leaf"));
56
57         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
58                 QName.create(myModule.getQNameModule(), "input"),
59                 QName.create(myModule.getQNameModule(), "my-input-leaf"));
60
61         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
62
63         assertNotNull(testNode);
64         assertNotNull(foundNode);
65         assertEquals(testNode, foundNode);
66
67         rpc = getRpcByName(myModule, "my-rpc");
68         testNode = rpc.getOutput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-output-leaf"));
69
70         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
71                 QName.create(myModule.getQNameModule(), "output"),
72                 QName.create(myModule.getQNameModule(), "my-output-leaf"));
73
74         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
75
76         assertNotNull(testNode);
77         assertNotNull(foundNode);
78         assertEquals(testNode, foundNode);
79
80         final NotificationDefinition notification = myModule.getNotifications().iterator().next();
81         testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "my-notification-leaf"));
82
83         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
84                 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
85
86         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
87
88         assertNotNull(testNode);
89         assertNotNull(foundNode);
90         assertEquals(testNode, foundNode);
91
92         final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
93         testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
94                 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
95                 "my-leaf-in-grouping"));
96
97         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
98                 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
99                 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
100
101         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
102
103         assertNotNull(testNode);
104         assertNotNull(foundNode);
105         assertEquals(testNode, foundNode);
106
107         testNode = ((ChoiceSchemaNode) myModule
108                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
109                 .findCaseNodes("one").iterator().next()
110                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
111
112         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
113                 QName.create(myModule.getQNameModule(), "one"),
114                 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
115         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
116
117         assertNotNull(testNode);
118         assertNotNull(foundNode);
119         assertEquals(testNode, foundNode);
120
121         ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
122                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
123                 "my-list"));
124
125         testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
126
127         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
128                 QName.create(myModule.getQNameModule(), "my-list"),
129                 QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
130         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
131
132         assertNotNull(testNode);
133         assertNotNull(foundNode);
134         assertEquals(testNode, foundNode);
135
136         listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
137                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
138                 "my-list"));
139
140         testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
141
142         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
143                 QName.create(myModule.getQNameModule(), "my-list"),
144                 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
145         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
146
147         assertNotNull(testNode);
148         assertNotNull(foundNode);
149         assertEquals(testNode, foundNode);
150
151     }
152
153     @Test
154     public void findNodeInSchemaContextTest2() {
155
156         final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory("/schema-context-util");
157
158         final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
159
160         SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
161                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
162                 "my-leaf-not-in-container"));
163
164         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
165                 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
166         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
167
168         assertNull(testNode);
169         assertNull(foundNode);
170
171         final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
172         testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "no-input-leaf"));
173
174         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
175                 QName.create(myModule.getQNameModule(), "input"),
176                 QName.create(myModule.getQNameModule(), "no-input-leaf"));
177
178         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
179
180         assertNull(testNode);
181         assertNull(foundNode);
182
183         final NotificationDefinition notification = myModule.getNotifications().iterator().next();
184         testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "no-notification-leaf"));
185
186         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
187                 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
188
189         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
190
191         assertNull(testNode);
192         assertNull(foundNode);
193
194         final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
195         testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
196                 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
197                 "no-leaf-in-grouping"));
198
199         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
200                 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
201                 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
202
203         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
204
205         assertNull(testNode);
206         assertNull(foundNode);
207
208         testNode = ((ChoiceSchemaNode) myModule
209                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
210                 .findCaseNodes("one").iterator().next()
211                 .getDataChildByName(QName.create(myModule.getQNameModule(), "no-choice-leaf"));
212
213         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
214                 QName.create(myModule.getQNameModule(), "one"),
215                 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
216         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
217
218         assertNull(testNode);
219         assertNull(foundNode);
220
221         ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
222                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
223                 "my-list"));
224
225         testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
226
227         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
228                 QName.create(myModule.getQNameModule(), "my-list"),
229                 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
230         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
231
232         assertNull(testNode);
233         assertNull(foundNode);
234
235         listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
236                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
237                 "my-list"));
238
239         testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
240
241         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
242                 QName.create(myModule.getQNameModule(), "my-list"),
243                 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
244         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
245
246         assertNull(testNode);
247         assertNull(foundNode);
248
249     }
250
251     @Test
252     public void findNodeInSchemaContextTest3() {
253
254         final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory("/schema-context-util");
255
256         final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
257
258         SchemaNode testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-container"));
259
260         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
261         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
262
263         assertNotNull(testNode);
264         assertNotNull(foundNode);
265         assertEquals(testNode, foundNode);
266
267         testNode = getRpcByName(myModule, "my-rpc");
268
269         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
270         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
271
272         assertNotNull(testNode);
273         assertNotNull(foundNode);
274         assertEquals(testNode, foundNode);
275
276         testNode = myModule.getNotifications().iterator().next();
277
278         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
279         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
280
281         assertNotNull(testNode);
282         assertNotNull(foundNode);
283         assertEquals(testNode, foundNode);
284
285         testNode = getGroupingByName(myModule, "my-grouping");
286
287         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
288         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
289
290         assertNotNull(testNode);
291         assertNotNull(foundNode);
292         assertEquals(testNode, foundNode);
293
294         testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
295
296         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
297         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
298
299         assertNotNull(testNode);
300         assertNotNull(foundNode);
301         assertEquals(testNode, foundNode);
302
303         testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
304                 "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), "my-list"));
305
306         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
307                 QName.create(myModule.getQNameModule(), "my-list"));
308         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
309
310         assertNotNull(testNode);
311         assertNotNull(foundNode);
312         assertEquals(testNode, foundNode);
313
314     }
315
316     @Test
317     public void findParentModuleTest() {
318
319         final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory("/schema-context-util");
320
321         final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
322
323         final DataSchemaNode node = myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
324             "my-container"));
325
326         final Module foundModule = SchemaContextUtil.findParentModule(context, node);
327
328         assertEquals(myModule, foundModule);
329     }
330
331     @Test
332     public void findDataSchemaNodeTest() {
333         final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory("/schema-context-util");
334         final Module module = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
335         final Module importedModule = context.findModule(URI.create("uri:imported-module"),
336             Revision.of("2014-10-07")).get();
337
338         final SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName(QName.create(
339                 importedModule.getQNameModule(), "my-imported-container"))).getDataChildByName(QName.create(
340                 importedModule.getQNameModule(), "my-imported-leaf"));
341
342         final PathExpression xpath = new PathExpressionImpl("imp:my-imported-container/imp:my-imported-leaf", true);
343
344         final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
345
346         assertNotNull(foundNode);
347         assertNotNull(testNode);
348         assertEquals(testNode, foundNode);
349     }
350
351     @Test
352     public void findDataSchemaNodeTest2() {
353         final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory("/schema-context-util");
354         final Module module = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
355
356         final GroupingDefinition grouping = getGroupingByName(module, "my-grouping");
357         final SchemaNode testNode = grouping.getDataChildByName(QName.create(module.getQNameModule(),
358                 "my-leaf-in-gouping2"));
359
360         final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
361
362         final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
363
364         assertNotNull(foundNode);
365         assertNotNull(testNode);
366         assertEquals(testNode, foundNode);
367
368     }
369
370     @Test
371     public void findNodeInSchemaContextGroupingsTest() {
372         final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory("/schema-context-util");
373         final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
374
375         // find grouping in container
376         DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
377                 myModule.getQNameModule(), "my-container"));
378         SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
379
380         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
381                 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
382         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
383
384         assertNotNull(testNode);
385         assertNotNull(foundNode);
386         assertEquals(testNode, foundNode);
387
388         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
389                 "my-leaf-in-grouping-in-container"));
390         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
391
392         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
393
394         assertNotNull(testNode);
395         assertNotNull(foundNode);
396         assertEquals(testNode, foundNode);
397
398         // find grouping in list
399         dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
400                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
401                 "my-list"));
402         testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
403
404         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
405                 QName.create(myModule.getQNameModule(), "my-list"),
406                 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
407         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
408
409         assertNotNull(testNode);
410         assertNotNull(foundNode);
411         assertEquals(testNode, foundNode);
412
413         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
414                 "my-leaf-in-grouping-in-list"));
415         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
416
417         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
418
419         assertNotNull(testNode);
420         assertNotNull(foundNode);
421         assertEquals(testNode, foundNode);
422
423         // find grouping in grouping
424         dataContainer = getGroupingByName(myModule, "my-grouping");
425         testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
426
427         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
428                 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
429         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
430
431         assertNotNull(testNode);
432         assertNotNull(foundNode);
433         assertEquals(testNode, foundNode);
434
435         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
436                 "my-leaf-in-grouping-in-grouping"));
437         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
438
439         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
440
441         assertNotNull(testNode);
442         assertNotNull(foundNode);
443         assertEquals(testNode, foundNode);
444
445         // find grouping in rpc
446         final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
447         for (final GroupingDefinition grouping : rpc.getGroupings()) {
448             if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
449                 testNode = grouping;
450             }
451         }
452
453         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
454                 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
455         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
456
457         assertNotNull(testNode);
458         assertNotNull(foundNode);
459         assertEquals(testNode, foundNode);
460
461         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
462                 "my-leaf-in-grouping-in-rpc"));
463         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
464
465         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
466
467         assertNotNull(testNode);
468         assertNotNull(foundNode);
469         assertEquals(testNode, foundNode);
470
471         // find grouping in output
472         dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
473         testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
474
475         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
476                 QName.create(myModule.getQNameModule(), "output"),
477                 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
478         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
479
480         assertNotNull(testNode);
481         assertNotNull(foundNode);
482         assertEquals(testNode, foundNode);
483
484         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
485                 "my-leaf-in-grouping-in-output"));
486         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
487
488         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
489
490         assertNotNull(testNode);
491         assertNotNull(foundNode);
492         assertEquals(testNode, foundNode);
493
494         // find grouping in input
495         dataContainer = getRpcByName(myModule, "my-rpc").getInput();
496         testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
497
498         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
499                 QName.create(myModule.getQNameModule(), "input"),
500                 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
501         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
502
503         assertNotNull(testNode);
504         assertNotNull(foundNode);
505         assertEquals(testNode, foundNode);
506
507         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
508                 "my-leaf-in-grouping-in-input"));
509         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
510
511         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
512
513         assertNotNull(testNode);
514         assertNotNull(foundNode);
515         assertEquals(testNode, foundNode);
516
517         // find grouping in notification
518         dataContainer = getNotificationByName(myModule, "my-notification");
519         testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
520
521         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
522                 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
523         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
524
525         assertNotNull(testNode);
526         assertNotNull(foundNode);
527         assertEquals(testNode, foundNode);
528
529         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
530                 "my-leaf-in-grouping-in-notification"));
531         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
532
533         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
534
535         assertNotNull(testNode);
536         assertNotNull(foundNode);
537         assertEquals(testNode, foundNode);
538
539         // find grouping in case
540         dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
541             QName.create(myModule.getQNameModule(), "my-choice")))
542                 .findCaseNodes("one").iterator().next()
543                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case"));
544         testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
545
546         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
547                 QName.create(myModule.getQNameModule(), "one"),
548                 QName.create(myModule.getQNameModule(), "my-container-in-case"),
549                 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
550         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
551
552         assertNotNull(testNode);
553         assertNotNull(foundNode);
554         assertEquals(testNode, foundNode);
555
556         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
557                 "my-leaf-in-grouping-in-case"));
558         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
559
560         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
561
562         assertNotNull(testNode);
563         assertNotNull(foundNode);
564         assertEquals(testNode, foundNode);
565     }
566
567     @Test
568     public void findNodeInSchemaContextGroupingsTest2() {
569
570         final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory("/schema-context-util");
571         final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
572
573         // find grouping in container
574         DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
575                 myModule.getQNameModule(), "my-container"));
576         SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
577
578         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
579                 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
580         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
581
582         assertNull(testNode);
583         assertNull(foundNode);
584
585         // find grouping in list
586         dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
587                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
588                 "my-list"));
589         testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
590
591         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
592                 QName.create(myModule.getQNameModule(), "my-list"),
593                 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
594         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
595
596         assertNull(testNode);
597         assertNull(foundNode);
598
599         // find grouping in grouping
600         dataContainer = getGroupingByName(myModule, "my-grouping");
601         testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
602
603         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
604                 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
605         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
606
607         assertNull(testNode);
608         assertNull(foundNode);
609
610         // find grouping in rpc
611         final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
612         for (final GroupingDefinition grouping : rpc.getGroupings()) {
613             if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
614                 testNode = grouping;
615             }
616         }
617
618         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
619                 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
620         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
621
622         assertNull(testNode);
623         assertNull(foundNode);
624
625         // find grouping in output
626         dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
627         testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
628
629         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
630                 QName.create(myModule.getQNameModule(), "output"),
631                 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
632         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
633
634         assertNull(testNode);
635         assertNull(foundNode);
636
637         // find grouping in input
638         dataContainer = getRpcByName(myModule, "my-rpc").getInput();
639         testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
640
641         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
642                 QName.create(myModule.getQNameModule(), "input"),
643                 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
644         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
645
646         assertNull(testNode);
647         assertNull(foundNode);
648
649         // find grouping in notification
650         dataContainer = getNotificationByName(myModule, "my-notification");
651         testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
652
653         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
654                 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
655         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
656
657         assertNull(testNode);
658         assertNull(foundNode);
659
660         // find grouping in case
661         dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
662             QName.create(myModule.getQNameModule(), "my-choice")))
663                 .findCaseNodes("one").iterator().next()
664                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case"));
665         testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
666
667         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
668                 QName.create(myModule.getQNameModule(), "one"),
669                 QName.create(myModule.getQNameModule(), "my-container-in-case"),
670                 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
671         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
672
673         assertNull(testNode);
674         assertNull(foundNode);
675     }
676
677     private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
678         for (final GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
679             if (grouping.getQName().getLocalName().equals(name)) {
680                 return grouping;
681             }
682         }
683         return null;
684     }
685
686     private static RpcDefinition getRpcByName(final Module module, final String name) {
687         for (final RpcDefinition rpc : module.getRpcs()) {
688             if (rpc.getQName().getLocalName().equals(name)) {
689                 return rpc;
690             }
691         }
692         return null;
693     }
694
695     private static NotificationDefinition getNotificationByName(final Module module, final String name) {
696         for (final NotificationDefinition notification : module.getNotifications()) {
697             if (notification.getQName().getLocalName().equals(name)) {
698                 return notification;
699             }
700         }
701         return null;
702     }
703
704     @Test
705     public void findNodeInSchemaContextTheSameNameOfSiblingsTest() {
706         final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory("/schema-context-util");
707
708         final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
709         final ChoiceSchemaNode choice = (ChoiceSchemaNode) getRpcByName(myModule, "my-name").getInput()
710                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
711         final SchemaNode testNode = choice.findCaseNodes("case-two").iterator().next()
712                 .getDataChildByName(QName.create(myModule.getQNameModule(), "two"));
713
714         final SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
715                 QName.create(myModule.getQNameModule(), "input"), QName.create(myModule.getQNameModule(), "my-choice"),
716                 QName.create(myModule.getQNameModule(), "case-two"), QName.create(myModule.getQNameModule(), "two"));
717         final SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
718
719         assertNotNull(testNode);
720         assertNotNull(foundNode);
721         assertEquals(testNode, foundNode);
722     }
723 }