Build with Mockito 2.1
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / rfc7950 / repo / 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.parser.rfc7950.repo;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doReturn;
15
16 import java.io.IOException;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.util.Collections;
20 import java.util.Optional;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.common.Revision;
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.GroupingDefinition;
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.NotificationDefinition;
36 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
37 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
42 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
43 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
44 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
46 import org.opendaylight.yangtools.yang.stmt.TestUtils;
47
48 public class SchemaContextUtilTest {
49     @Mock
50     private SchemaContext mockSchemaContext;
51     @Mock
52     private Module mockModule;
53
54     @Test
55     public void testFindDummyData() {
56         MockitoAnnotations.initMocks(this);
57         doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
58         doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
59         doReturn(URI.create("dummy")).when(mockModule).getNamespace();
60         doReturn(Optional.empty()).when(mockModule).getRevision();
61
62         final QName qName = QName.create("dummy", "TestQName");
63         final SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);
64         assertEquals("Should be null. Module TestQName not found", null,
65                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
66
67         final RevisionAwareXPath xPath = new RevisionAwareXPathImpl("/bookstore/book/title", true);
68         assertEquals("Should be null. Module bookstore not found", null,
69                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
70
71         final SchemaNode schemaNode = BaseTypes.int32Type();
72         final RevisionAwareXPath xPathRelative = new RevisionAwareXPathImpl("../prefix", false);
73         assertEquals("Should be null, Module prefix not found", null,
74                 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
75                         xPathRelative));
76
77         assertEquals("Should be null. Module TestQName not found", null,
78                 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qName)));
79
80         assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
81     }
82
83     @Test
84     public void findNodeInSchemaContextTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
85             ReactorException {
86
87         final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
88
89         final Module myModule = context.findModule(new URI("uri:my-module"),Revision.of("2014-10-07")).get();
90
91         SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
92                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
93                 "my-leaf-in-container"));
94
95         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
96                 QName.create(myModule.getQNameModule(), "my-leaf-in-container"));
97         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
98
99         assertNotNull(testNode);
100         assertNotNull(foundNode);
101         assertEquals(testNode, foundNode);
102
103         RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
104         testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-input-leaf"));
105
106         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
107                 QName.create(myModule.getQNameModule(), "input"),
108                 QName.create(myModule.getQNameModule(), "my-input-leaf"));
109
110         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
111
112         assertNotNull(testNode);
113         assertNotNull(foundNode);
114         assertEquals(testNode, foundNode);
115
116         rpc = getRpcByName(myModule, "my-rpc");
117         testNode = rpc.getOutput().getDataChildByName(QName.create(myModule.getQNameModule(), "my-output-leaf"));
118
119         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
120                 QName.create(myModule.getQNameModule(), "output"),
121                 QName.create(myModule.getQNameModule(), "my-output-leaf"));
122
123         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
124
125         assertNotNull(testNode);
126         assertNotNull(foundNode);
127         assertEquals(testNode, foundNode);
128
129         final NotificationDefinition notification = myModule.getNotifications().iterator().next();
130         testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "my-notification-leaf"));
131
132         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
133                 QName.create(myModule.getQNameModule(), "my-notification-leaf"));
134
135         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
136
137         assertNotNull(testNode);
138         assertNotNull(foundNode);
139         assertEquals(testNode, foundNode);
140
141         final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
142         testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
143                 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
144                 "my-leaf-in-grouping"));
145
146         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
147                 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
148                 QName.create(myModule.getQNameModule(), "my-leaf-in-grouping"));
149
150         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
151
152         assertNotNull(testNode);
153         assertNotNull(foundNode);
154         assertEquals(testNode, foundNode);
155
156         testNode = ((ChoiceSchemaNode) myModule
157                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
158                 .findCaseNodes("one").iterator().next()
159                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
160
161         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
162                 QName.create(myModule.getQNameModule(), "one"),
163                 QName.create(myModule.getQNameModule(), "my-choice-leaf-one"));
164         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
165
166         assertNotNull(testNode);
167         assertNotNull(foundNode);
168         assertEquals(testNode, foundNode);
169
170         ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
171                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
172                 "my-list"));
173
174         testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
175
176         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
177                 QName.create(myModule.getQNameModule(), "my-list"),
178                 QName.create(myModule.getQNameModule(), "my-leaf-in-list"));
179         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
180
181         assertNotNull(testNode);
182         assertNotNull(foundNode);
183         assertEquals(testNode, foundNode);
184
185         listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
186                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
187                 "my-list"));
188
189         testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
190
191         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
192                 QName.create(myModule.getQNameModule(), "my-list"),
193                 QName.create(myModule.getQNameModule(), "my-leaf-list-in-list"));
194         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
195
196         assertNotNull(testNode);
197         assertNotNull(foundNode);
198         assertEquals(testNode, foundNode);
199
200     }
201
202     @Test
203     public void findNodeInSchemaContextTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
204             ReactorException {
205
206         final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
207
208         final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
209
210         SchemaNode testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
211                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
212                 "my-leaf-not-in-container"));
213
214         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
215                 QName.create(myModule.getQNameModule(), "my-leaf-not-in-container"));
216         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
217
218         assertNull(testNode);
219         assertNull(foundNode);
220
221         final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
222         testNode = rpc.getInput().getDataChildByName(QName.create(myModule.getQNameModule(), "no-input-leaf"));
223
224         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
225                 QName.create(myModule.getQNameModule(), "input"),
226                 QName.create(myModule.getQNameModule(), "no-input-leaf"));
227
228         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
229
230         assertNull(testNode);
231         assertNull(foundNode);
232
233         final NotificationDefinition notification = myModule.getNotifications().iterator().next();
234         testNode = notification.getDataChildByName(QName.create(myModule.getQNameModule(), "no-notification-leaf"));
235
236         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
237                 QName.create(myModule.getQNameModule(), "no-notification-leaf"));
238
239         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
240
241         assertNull(testNode);
242         assertNull(foundNode);
243
244         final GroupingDefinition grouping = getGroupingByName(myModule, "my-grouping");
245         testNode = ((ContainerSchemaNode) grouping.getDataChildByName(QName.create(myModule.getQNameModule(),
246                 "my-container-in-grouping"))).getDataChildByName(QName.create(myModule.getQNameModule(),
247                 "no-leaf-in-grouping"));
248
249         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
250                 QName.create(myModule.getQNameModule(), "my-container-in-grouping"),
251                 QName.create(myModule.getQNameModule(), "no-leaf-in-grouping"));
252
253         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
254
255         assertNull(testNode);
256         assertNull(foundNode);
257
258         testNode = ((ChoiceSchemaNode) myModule
259                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice")))
260                 .findCaseNodes("one").iterator().next()
261                 .getDataChildByName(QName.create(myModule.getQNameModule(), "no-choice-leaf"));
262
263         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
264                 QName.create(myModule.getQNameModule(), "one"),
265                 QName.create(myModule.getQNameModule(), "no-choice-leaf"));
266         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
267
268         assertNull(testNode);
269         assertNull(foundNode);
270
271         ListSchemaNode listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
272                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
273                 "my-list"));
274
275         testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
276
277         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
278                 QName.create(myModule.getQNameModule(), "my-list"),
279                 QName.create(myModule.getQNameModule(), "no-leaf-in-list"));
280         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
281
282         assertNull(testNode);
283         assertNull(foundNode);
284
285         listNode = (ListSchemaNode) ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(
286                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
287                 "my-list"));
288
289         testNode = listNode.getDataChildByName(QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
290
291         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
292                 QName.create(myModule.getQNameModule(), "my-list"),
293                 QName.create(myModule.getQNameModule(), "no-leaf-list-in-list"));
294         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
295
296         assertNull(testNode);
297         assertNull(foundNode);
298
299     }
300
301     @Test
302     public void findNodeInSchemaContextTest3() throws URISyntaxException, IOException, YangSyntaxErrorException,
303             ReactorException {
304
305         final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
306
307         final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
308
309         SchemaNode testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-container"));
310
311         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"));
312         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
313
314         assertNotNull(testNode);
315         assertNotNull(foundNode);
316         assertEquals(testNode, foundNode);
317
318         testNode = getRpcByName(myModule, "my-rpc");
319
320         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
321         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
322
323         assertNotNull(testNode);
324         assertNotNull(foundNode);
325         assertEquals(testNode, foundNode);
326
327         testNode = myModule.getNotifications().iterator().next();
328
329         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"));
330         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
331
332         assertNotNull(testNode);
333         assertNotNull(foundNode);
334         assertEquals(testNode, foundNode);
335
336         testNode = getGroupingByName(myModule, "my-grouping");
337
338         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"));
339         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
340
341         assertNotNull(testNode);
342         assertNotNull(foundNode);
343         assertEquals(testNode, foundNode);
344
345         testNode = myModule.getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
346
347         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"));
348         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
349
350         assertNotNull(testNode);
351         assertNotNull(foundNode);
352         assertEquals(testNode, foundNode);
353
354         testNode = ((ContainerSchemaNode) myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
355                 "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(), "my-list"));
356
357         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
358                 QName.create(myModule.getQNameModule(), "my-list"));
359         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
360
361         assertNotNull(testNode);
362         assertNotNull(foundNode);
363         assertEquals(testNode, foundNode);
364
365     }
366
367     @Test
368     public void findParentModuleTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
369             ReactorException {
370
371         final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
372
373         final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
374
375         final DataSchemaNode node = myModule.getDataChildByName(QName.create(myModule.getQNameModule(),
376             "my-container"));
377
378         final Module foundModule = SchemaContextUtil.findParentModule(context, node);
379
380         assertEquals(myModule, foundModule);
381     }
382
383     @Test(expected = IllegalArgumentException.class)
384     public void findParentModuleIllegalArgumentTest() {
385
386         final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
387         SchemaContextUtil.findParentModule(mockContext, null);
388
389     }
390
391     @Test(expected = IllegalArgumentException.class)
392     public void findParentModuleIllegalArgumentTest2() {
393
394         final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
395         SchemaContextUtil.findParentModule(null, mockSchemaNode);
396
397     }
398
399     @Test(expected = IllegalStateException.class)
400     public void findParentModuleIllegalStateTest() {
401
402         final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
403         final SchemaNode mockSchemaNode = Mockito.mock(SchemaNode.class);
404         Mockito.when(mockSchemaNode.getPath()).thenReturn(null);
405         SchemaContextUtil.findParentModule(mockContext, mockSchemaNode);
406
407     }
408
409     @Test(expected = IllegalArgumentException.class)
410     public void findDataSchemaNodeIllegalArgumentTest() {
411
412         final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
413         SchemaContextUtil.findDataSchemaNode(mockContext, null);
414
415     }
416
417     @Test(expected = IllegalArgumentException.class)
418     public void findDataSchemaNodeIllegalArgumentTest2() {
419
420         final SchemaPath mockSchemaPath = Mockito.mock(SchemaPath.class);
421         SchemaContextUtil.findDataSchemaNode(null, mockSchemaPath);
422
423     }
424
425     @Test
426     public void findDataSchemaNodeTest() throws URISyntaxException, IOException, YangSyntaxErrorException,
427             ReactorException {
428         final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
429         final Module module = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
430         final Module importedModule = context.findModule(new URI("uri:imported-module"),
431             Revision.of("2014-10-07")).get();
432
433         final SchemaNode testNode = ((ContainerSchemaNode) importedModule.getDataChildByName(QName.create(
434                 importedModule.getQNameModule(), "my-imported-container"))).getDataChildByName(QName.create(
435                 importedModule.getQNameModule(), "my-imported-leaf"));
436
437         final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("imp:my-imported-container/imp:my-imported-leaf",
438                 true);
439
440         final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
441
442         assertNotNull(foundNode);
443         assertNotNull(testNode);
444         assertEquals(testNode, foundNode);
445     }
446
447     @Test
448     public void findDataSchemaNodeTest2() throws URISyntaxException, IOException, YangSyntaxErrorException,
449             ReactorException {
450         // findDataSchemaNode(final SchemaContext context, final Module module,
451         // final RevisionAwareXPath nonCondXPath) {
452
453         final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
454         final Module module = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
455
456         final GroupingDefinition grouping = getGroupingByName(module, "my-grouping");
457         final SchemaNode testNode = grouping.getDataChildByName(QName.create(module.getQNameModule(),
458                 "my-leaf-in-gouping2"));
459
460         final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
461
462         final SchemaNode foundNode = SchemaContextUtil.findDataSchemaNode(context, module, xpath);
463
464         assertNotNull(foundNode);
465         assertNotNull(testNode);
466         assertEquals(testNode, foundNode);
467
468     }
469
470     @Test(expected = IllegalArgumentException.class)
471     public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
472
473         final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
474         final Module module = Mockito.mock(Module.class);
475
476         SchemaContextUtil.findDataSchemaNode(mockContext, module, null);
477
478     }
479
480     @Test(expected = IllegalArgumentException.class)
481     public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
482
483         final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
484         final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
485
486         SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath);
487
488     }
489
490     @Test(expected = IllegalArgumentException.class)
491     public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
492
493         final Module module = Mockito.mock(Module.class);
494         final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
495
496         SchemaContextUtil.findDataSchemaNode(null, module, xpath);
497
498     }
499
500     @Test(expected = IllegalArgumentException.class)
501     public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
502
503         final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
504         final Module module = Mockito.mock(Module.class);
505         final RevisionAwareXPath xpath = new RevisionAwareXPathImpl(
506                 "my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", true);
507
508         SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath);
509
510     }
511
512     @Test
513     public void findDataSchemaNodeFromXPathNullTest() {
514
515         final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
516         final Module module = Mockito.mock(Module.class);
517         final RevisionAwareXPath xpath = Mockito.mock(RevisionAwareXPath.class);
518
519         Mockito.when(xpath.toString()).thenReturn(null);
520         assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
521
522     }
523
524     @Test
525     public void findDataSchemaNodeFromXPathNullTest2() {
526
527         final SchemaContext mockContext = Mockito.mock(SchemaContext.class);
528         final Module module = Mockito.mock(Module.class);
529         final RevisionAwareXPath xpath = new RevisionAwareXPathImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
530
531         assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
532
533     }
534
535     @Test
536     public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
537             YangSyntaxErrorException, ReactorException {
538
539         final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
540         final Module myModule = context.findModule(URI.create("uri:my-module"), Revision.of("2014-10-07")).get();
541
542         // find grouping in container
543         DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
544                 myModule.getQNameModule(), "my-container"));
545         SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
546
547         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
548                 QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
549         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
550
551         assertNotNull(testNode);
552         assertNotNull(foundNode);
553         assertEquals(testNode, foundNode);
554
555         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
556                 "my-leaf-in-grouping-in-container"));
557         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
558
559         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
560
561         assertNotNull(testNode);
562         assertNotNull(foundNode);
563         assertEquals(testNode, foundNode);
564
565         // find grouping in list
566         dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
567                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
568                 "my-list"));
569         testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
570
571         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
572                 QName.create(myModule.getQNameModule(), "my-list"),
573                 QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
574         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
575
576         assertNotNull(testNode);
577         assertNotNull(foundNode);
578         assertEquals(testNode, foundNode);
579
580         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
581                 "my-leaf-in-grouping-in-list"));
582         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
583
584         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
585
586         assertNotNull(testNode);
587         assertNotNull(foundNode);
588         assertEquals(testNode, foundNode);
589
590         // find grouping in grouping
591         dataContainer = getGroupingByName(myModule, "my-grouping");
592         testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
593
594         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
595                 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
596         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
597
598         assertNotNull(testNode);
599         assertNotNull(foundNode);
600         assertEquals(testNode, foundNode);
601
602         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
603                 "my-leaf-in-grouping-in-grouping"));
604         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
605
606         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
607
608         assertNotNull(testNode);
609         assertNotNull(foundNode);
610         assertEquals(testNode, foundNode);
611
612         // find grouping in rpc
613         final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
614         for (final GroupingDefinition grouping : rpc.getGroupings()) {
615             if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
616                 testNode = grouping;
617             }
618         }
619
620         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
621                 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
622         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
623
624         assertNotNull(testNode);
625         assertNotNull(foundNode);
626         assertEquals(testNode, foundNode);
627
628         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
629                 "my-leaf-in-grouping-in-rpc"));
630         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
631
632         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
633
634         assertNotNull(testNode);
635         assertNotNull(foundNode);
636         assertEquals(testNode, foundNode);
637
638         // find grouping in output
639         dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
640         testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
641
642         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
643                 QName.create(myModule.getQNameModule(), "output"),
644                 QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
645         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
646
647         assertNotNull(testNode);
648         assertNotNull(foundNode);
649         assertEquals(testNode, foundNode);
650
651         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
652                 "my-leaf-in-grouping-in-output"));
653         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
654
655         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
656
657         assertNotNull(testNode);
658         assertNotNull(foundNode);
659         assertEquals(testNode, foundNode);
660
661         // find grouping in input
662         dataContainer = getRpcByName(myModule, "my-rpc").getInput();
663         testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
664
665         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
666                 QName.create(myModule.getQNameModule(), "input"),
667                 QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
668         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
669
670         assertNotNull(testNode);
671         assertNotNull(foundNode);
672         assertEquals(testNode, foundNode);
673
674         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
675                 "my-leaf-in-grouping-in-input"));
676         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
677
678         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
679
680         assertNotNull(testNode);
681         assertNotNull(foundNode);
682         assertEquals(testNode, foundNode);
683
684         // find grouping in notification
685         dataContainer = getNotificationByName(myModule, "my-notification");
686         testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
687
688         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
689                 QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
690         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
691
692         assertNotNull(testNode);
693         assertNotNull(foundNode);
694         assertEquals(testNode, foundNode);
695
696         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
697                 "my-leaf-in-grouping-in-notification"));
698         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
699
700         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
701
702         assertNotNull(testNode);
703         assertNotNull(foundNode);
704         assertEquals(testNode, foundNode);
705
706         // find grouping in case
707         dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
708             QName.create(myModule.getQNameModule(), "my-choice")))
709                 .findCaseNodes("one").iterator().next()
710                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case"));
711         testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
712
713         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
714                 QName.create(myModule.getQNameModule(), "one"),
715                 QName.create(myModule.getQNameModule(), "my-container-in-case"),
716                 QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
717         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
718
719         assertNotNull(testNode);
720         assertNotNull(foundNode);
721         assertEquals(testNode, foundNode);
722
723         testNode = ((GroupingDefinition) testNode).getDataChildByName(QName.create(myModule.getQNameModule(),
724                 "my-leaf-in-grouping-in-case"));
725         path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
726
727         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
728
729         assertNotNull(testNode);
730         assertNotNull(foundNode);
731         assertEquals(testNode, foundNode);
732
733     }
734
735     @Test
736     public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
737             YangSyntaxErrorException, ReactorException {
738
739         final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
740
741         final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
742
743         // find grouping in container
744         DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName(QName.create(
745                 myModule.getQNameModule(), "my-container"));
746         SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
747
748         SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
749                 QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
750         SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
751
752         assertNull(testNode);
753         assertNull(foundNode);
754
755         // find grouping in list
756         dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName(QName.create(
757                 myModule.getQNameModule(), "my-container"))).getDataChildByName(QName.create(myModule.getQNameModule(),
758                 "my-list"));
759         testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
760
761         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
762                 QName.create(myModule.getQNameModule(), "my-list"),
763                 QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
764         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
765
766         assertNull(testNode);
767         assertNull(foundNode);
768
769         // find grouping in grouping
770         dataContainer = getGroupingByName(myModule, "my-grouping");
771         testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
772
773         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
774                 QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
775         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
776
777         assertNull(testNode);
778         assertNull(foundNode);
779
780         // find grouping in rpc
781         final RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
782         for (final GroupingDefinition grouping : rpc.getGroupings()) {
783             if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
784                 testNode = grouping;
785             }
786         }
787
788         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
789                 QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
790         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
791
792         assertNull(testNode);
793         assertNull(foundNode);
794
795         // find grouping in output
796         dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
797         testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
798
799         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
800                 QName.create(myModule.getQNameModule(), "output"),
801                 QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
802         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
803
804         assertNull(testNode);
805         assertNull(foundNode);
806
807         // find grouping in input
808         dataContainer = getRpcByName(myModule, "my-rpc").getInput();
809         testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
810
811         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
812                 QName.create(myModule.getQNameModule(), "input"),
813                 QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
814         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
815
816         assertNull(testNode);
817         assertNull(foundNode);
818
819         // find grouping in notification
820         dataContainer = getNotificationByName(myModule, "my-notification");
821         testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
822
823         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
824                 QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
825         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
826
827         assertNull(testNode);
828         assertNull(foundNode);
829
830         // find grouping in case
831         dataContainer = (DataNodeContainer) ((ChoiceSchemaNode) myModule.getDataChildByName(
832             QName.create(myModule.getQNameModule(), "my-choice")))
833                 .findCaseNodes("one").iterator().next()
834                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-container-in-case"));
835         testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
836
837         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
838                 QName.create(myModule.getQNameModule(), "one"),
839                 QName.create(myModule.getQNameModule(), "my-container-in-case"),
840                 QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
841         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
842
843         assertNull(testNode);
844         assertNull(foundNode);
845
846     }
847
848     private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final String name) {
849         for (final GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
850             if (grouping.getQName().getLocalName().equals(name)) {
851                 return grouping;
852             }
853         }
854         return null;
855     }
856
857     private static RpcDefinition getRpcByName(final Module module, final String name) {
858         for (final RpcDefinition rpc : module.getRpcs()) {
859             if (rpc.getQName().getLocalName().equals(name)) {
860                 return rpc;
861             }
862         }
863         return null;
864     }
865
866     private static NotificationDefinition getNotificationByName(final Module module, final String name) {
867         for (final NotificationDefinition notification : module.getNotifications()) {
868             if (notification.getQName().getLocalName().equals(name)) {
869                 return notification;
870             }
871         }
872         return null;
873     }
874
875     @Test
876     public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
877             YangSyntaxErrorException, ReactorException {
878
879         final SchemaContext context = TestUtils.parseYangSources("/schema-context-util-test");
880
881         final Module myModule = context.findModule(new URI("uri:my-module"), Revision.of("2014-10-07")).get();
882         final ChoiceSchemaNode choice = (ChoiceSchemaNode) getRpcByName(myModule, "my-name").getInput()
883                 .getDataChildByName(QName.create(myModule.getQNameModule(), "my-choice"));
884         final SchemaNode testNode = choice.findCaseNodes("case-two").iterator().next()
885                 .getDataChildByName(QName.create(myModule.getQNameModule(), "two"));
886
887         final SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
888                 QName.create(myModule.getQNameModule(), "input"), QName.create(myModule.getQNameModule(), "my-choice"),
889                 QName.create(myModule.getQNameModule(), "case-two"), QName.create(myModule.getQNameModule(), "two"));
890         final SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
891
892         assertNotNull(testNode);
893         assertNotNull(foundNode);
894         assertEquals(testNode, foundNode);
895     }
896 }