Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / utils / parser / ParserIdentifierTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.restconf.nb.rfc8040.utils.parser;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.base.Optional;
18 import com.google.common.collect.ImmutableClassToInstanceMap;
19 import com.google.common.collect.Maps;
20 import java.util.Map.Entry;
21 import org.junit.Before;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
28 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
29 import org.opendaylight.controller.md.sal.dom.broker.impl.mount.DOMMountPointServiceImpl;
30 import org.opendaylight.controller.md.sal.dom.broker.spi.mount.SimpleDOMMountPoint;
31 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
32 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
33 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
34 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
35 import org.opendaylight.restconf.common.errors.RestconfError;
36 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
37 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
38 import org.opendaylight.restconf.common.schema.SchemaExportContext;
39 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
40 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.Revision;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.model.api.Module;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47
48 /**
49  * Unit tests for {@link ParserIdentifier}.
50  */
51 public class ParserIdentifierTest {
52     // mount point identifier
53     private static final String MOUNT_POINT_IDENT =
54             "mount-point:mount-container/point-number" + "/" + RestconfConstants.MOUNT;
55
56     // invalid mount point identifier
57     private static final String INVALID_MOUNT_POINT_IDENT =
58             "mount-point:point-number" + "/" + RestconfConstants.MOUNT;
59
60     // test identifier + expected result
61     private static final String TEST_IDENT =
62             "parser-identifier:cont1/cont2/listTest/list-in-grouping=name/leaf-A.B";
63
64     private static final String TEST_IDENT_RESULT =
65             "/(parser:identifier?revision=2016-06-02)cont1/cont2/listTest/listTest/list-in-grouping/"
66             + "list-in-grouping[{(parser:identifier?revision=2016-06-02)name=name}]/leaf-A.B";
67
68     // test identifier with nodes defined in other modules using augmentation + expected result
69     private static final String TEST_IDENT_OTHERS =
70             "parser-identifier-included:list-1=name,2016-06-02/parser-identifier:augment-leaf";
71
72     private static final String TEST_IDENT_OTHERS_RESULT =
73             "/(parser:identifier:included?revision=2016-06-02)list-1/list-1"
74             + "[{(parser:identifier:included?revision=2016-06-02)name=name, "
75             + "(parser:identifier:included?revision=2016-06-02)revision=2016-06-02}]"
76             + "/AugmentationIdentifier{childNames=[(parser:identifier?revision=2016-06-02)augment-leaf]}/"
77             + "(parser:identifier?revision=2016-06-02)augment-leaf";
78
79     // invalid test identifier
80     private static final String INVALID_TEST_IDENT =
81             "parser-identifier:cont2/listTest/list-in-grouping=name/leaf-A.B";
82
83     // schema context with test modules
84     private SchemaContext schemaContext;
85     // contains the same modules but it is different object (it can be compared with equals)
86     private SchemaContext schemaContextOnMountPoint;
87
88     private static final String TEST_MODULE_NAME = "test-module";
89     private static final String TEST_MODULE_REVISION = "2016-06-02";
90     private static final String TEST_MODULE_NAMESPACE = "test:module";
91
92     private static final String INVOKE_RPC = "invoke-rpc-module:rpc-test";
93
94     // mount point and mount point service
95     private DOMMountPoint mountPoint;
96     private DOMMountPointService mountPointService;
97
98     // mock mount point and mount point service
99     @Mock
100     private DOMMountPoint mockMountPoint;
101     @Mock
102     private DOMMountPointService mockMountPointService;
103     @Mock
104     private DOMSchemaService domSchemaService;
105     @Mock
106     private DOMYangTextSourceProvider sourceProvider;
107
108     @Rule
109     public final ExpectedException thrown = ExpectedException.none();
110
111     @Before
112     public void setup() throws Exception {
113         MockitoAnnotations.initMocks(this);
114         this.schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/parser-identifier"));
115         this.schemaContextOnMountPoint =
116                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/parser-identifier"));
117
118         // create and register mount point
119         this.mountPoint = SimpleDOMMountPoint.create(
120                 YangInstanceIdentifier.builder()
121                         .node(QName.create("mount:point", "2016-06-02", "mount-container"))
122                         .node(QName.create("mount:point", "2016-06-02", "point-number"))
123                         .build(),
124                 ImmutableClassToInstanceMap.copyOf(Maps.newHashMap()),
125                 this.schemaContextOnMountPoint
126         );
127
128         this.mountPointService = new DOMMountPointServiceImpl();
129         ((DOMMountPointServiceImpl) this.mountPointService).registerMountPoint(this.mountPoint);
130
131         // register mount point with null schema context
132         when(this.mockMountPoint.getSchemaContext()).thenReturn(null);
133         when(this.mockMountPointService.getMountPoint(YangInstanceIdentifier.EMPTY))
134                 .thenReturn(Optional.of(this.mockMountPoint));
135     }
136
137     /**
138      * {@link ParserIdentifier#toInstanceIdentifier(String, SchemaContext)} tests.
139      */
140
141     /**
142      * Positive test of creating <code>InstanceIdentifierContext</code> from identifier when all nodes are defined
143      * in one module.
144      */
145     @Test
146     public void toInstanceIdentifierTest() {
147         final InstanceIdentifierContext<?> context = ParserIdentifier.toInstanceIdentifier(
148                 TEST_IDENT, this.schemaContext, Optional.absent());
149
150         assertEquals("Returned not expected identifier",
151                 TEST_IDENT_RESULT, context .getInstanceIdentifier().toString());
152     }
153
154     /**
155      * Positive test of creating <code>InstanceIdentifierContext</code> from identifier when nodes are defined in
156      * multiple modules.
157      */
158     @Test
159     public void toInstanceIdentifierOtherModulesTest() {
160         final InstanceIdentifierContext<?> context = ParserIdentifier.toInstanceIdentifier(
161                 TEST_IDENT_OTHERS, this.schemaContext, Optional.absent());
162
163         assertEquals("Returned not expected identifier",
164                 TEST_IDENT_OTHERS_RESULT, context.getInstanceIdentifier().toString());
165     }
166
167     /**
168      * Positive test of creating <code>InstanceIdentifierContext</code> from identifier containing
169      * {@link RestconfConstants#MOUNT}.
170      */
171     @Test
172     public void toInstanceIdentifierMountPointTest() {
173         final InstanceIdentifierContext<?> context = ParserIdentifier.toInstanceIdentifier(
174                 MOUNT_POINT_IDENT + "/" + TEST_IDENT, this.schemaContext, Optional.of(this.mountPointService));
175
176         assertEquals("Returned not expected identifier",
177                 TEST_IDENT_RESULT.toString(), context.getInstanceIdentifier().toString());
178
179         assertEquals("Mount point not found",
180                 this.mountPoint, context.getMountPoint());
181
182         assertEquals("Schema context from mount point expected",
183                 this.schemaContextOnMountPoint, context.getSchemaContext());
184     }
185
186     /**
187      * Test of creating <code>InstanceIdentifierContext</code> when identifier is <code>null</code>.
188      * <code>{@link YangInstanceIdentifier#EMPTY}</code> should be returned.
189      */
190     @Test
191     public void toInstanceIdentifierNullIdentifierTest() {
192         final InstanceIdentifierContext<?> context = ParserIdentifier.toInstanceIdentifier(
193                 null, this.schemaContext, Optional.absent());
194         assertEquals("Returned not expected identifier",
195                 YangInstanceIdentifier.EMPTY, context.getInstanceIdentifier());
196     }
197
198     /**
199      * Negative test of creating <code>InstanceIdentifierContext</code> when <code>SchemaContext</code> is
200      * <code>null</code>. Test fails expecting <code>NullPointerException</code>.
201      */
202     @Test
203     public void toInstanceIdentifierNullSchemaContextNegativeTest() {
204         this.thrown.expect(NullPointerException.class);
205         ParserIdentifier.toInstanceIdentifier(TEST_IDENT, null, Optional.absent());
206     }
207
208     /**
209      * Api path can be empty. <code>YangInstanceIdentifier.EMPTY</code> is expected to be returned.
210      */
211     @Test
212     public void toInstanceIdentifierEmptyIdentifierTest() {
213         final InstanceIdentifierContext<?> context = ParserIdentifier.toInstanceIdentifier(
214                 "", this.schemaContext, Optional.absent());
215         assertEquals("Returned not expected identifier",
216                 YangInstanceIdentifier.EMPTY, context.getInstanceIdentifier());
217     }
218
219     /**
220      * Negative test with invalid test identifier. Test should fail with <code>IllegalArgumentException</code>.
221      */
222     @Test
223     public void toInstanceIdentifierInvalidIdentifierNegativeTest() {
224         this.thrown.expect(IllegalArgumentException.class);
225         ParserIdentifier.toInstanceIdentifier(INVALID_TEST_IDENT, this.schemaContext, Optional.absent());
226     }
227
228     /**
229      * Negative test when identifier contains {@link RestconfConstants#MOUNT} but identifier part is not valid. Test
230      * should fail with <code>IllegalArgumentException</code>.
231      */
232     @Test
233     public void toInstanceIdentifierMountPointInvalidIdentifierNegativeTest() {
234         this.thrown.expect(IllegalArgumentException.class);
235         ParserIdentifier.toInstanceIdentifier(
236                 INVALID_MOUNT_POINT_IDENT, this.schemaContext, Optional.of(this.mountPointService));
237     }
238
239     /**
240      * Negative test when <code>DOMMountPoint</code> cannot be found. Test is expected to fail with
241      * <code>RestconfDocumentedException</code> error type, error tag and error status code are
242      * compared to expected values.
243      */
244     @Test
245     public void toInstanceIdentifierMissingMountPointNegativeTest() {
246         try {
247             ParserIdentifier.toInstanceIdentifier(
248                     "" + "/" + RestconfConstants.MOUNT, this.schemaContext, Optional.of(this.mountPointService));
249             fail("Test should fail due to missing mount point");
250         } catch (final RestconfDocumentedException e) {
251             assertEquals("Not expected error type",
252                     RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
253             assertEquals("Not expected error tag",
254                     ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
255             assertEquals("Not expected error status code",
256                     404, e.getErrors().get(0).getErrorTag().getStatusCode());
257         }
258     }
259
260     /**
261      * Negative test when <code>{@link DOMMountPointService}</code> is absent. Test is expected to fail with
262      * <code>RestconfDocumentedException</code> error type, error tag and error status code are
263      * compared to expected values.
264      */
265     @Test
266     public void toInstanceIdentifierMissingMountPointServiceNegativeTest() {
267         try {
268             ParserIdentifier.toInstanceIdentifier(RestconfConstants.MOUNT, this.schemaContext, Optional.absent());
269             fail("Test should fail due to absent mount point service");
270         } catch (final RestconfDocumentedException e) {
271             assertEquals("Not expected error type",
272                     ErrorType.APPLICATION, e.getErrors().get(0).getErrorType());
273             assertEquals("Not expected error tag",
274                     ErrorTag.OPERATION_FAILED, e.getErrors().get(0).getErrorTag());
275             assertEquals("Not expected error status code",
276                     500, e.getErrors().get(0).getErrorTag().getStatusCode());
277         }
278     }
279
280     /**
281      * {@link ParserIdentifier#makeQNameFromIdentifier(String)} tests.
282      */
283
284     /**
285      * Positive test of making <code>QName</code> from identifier and compare values from returned <code>QName</code>
286      * to expected values.
287      */
288     @Test
289     public void makeQNameFromIdentifierTest() {
290         final Entry<String, Revision> qName = ParserIdentifier.makeQNameFromIdentifier(
291             TEST_MODULE_NAME + "/" + TEST_MODULE_REVISION);
292
293         assertNotNull("QName should be created", qName);
294         assertEquals("Returned not expected module name", TEST_MODULE_NAME, qName.getKey());
295         assertEquals("Returned not expected module revision", Revision.of(TEST_MODULE_REVISION), qName.getValue());
296     }
297
298     /**
299      * Negative test when supplied identifier is in invalid format and then revision is not parsable.
300      * <code>RestconfDocumentedException</code> is expected and error type, error tag and error status code are
301      * compared to expected values.
302      */
303     @Test
304     public void makeQNameFromIdentifierInvalidIdentifierNegativeTest() {
305         try {
306             ParserIdentifier.makeQNameFromIdentifier(TEST_MODULE_REVISION + "/" + TEST_MODULE_NAME);
307             fail("Test should fail due to invalid identifier format");
308         } catch (final RestconfDocumentedException e) {
309             assertEquals("Not expected error type",
310                     RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
311             assertEquals("Not expected error tag",
312                     RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
313             assertEquals("Not expected error status code",
314                     400, e.getErrors().get(0).getErrorTag().getStatusCode());
315         }
316     }
317
318     /**
319      * Negative test when supplied identifier is too short (contains only module name).
320      * <code>RestconfDocumentedException</code> is expected and error type, error tag and error status code are
321      * compared to expected values.
322      */
323     @Test
324     public void makeQNameFromIdentifierTooShortIdentifierNegativeTest() {
325         try {
326             ParserIdentifier.makeQNameFromIdentifier(TEST_MODULE_NAME);
327             fail("Test should fail due to too short identifier format");
328         } catch (final RestconfDocumentedException e) {
329             assertEquals("Not expected error type",
330                     RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
331             assertEquals("Not expected error tag",
332                     RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
333             assertEquals("Not expected error status code",
334                     400, e.getErrors().get(0).getErrorTag().getStatusCode());
335         }
336     }
337
338     /**
339      * Positive test of making <code>QName</code> from identifier for module behind mount point. Value from returned
340      * <code>QName</code> are compared to expected values.
341      */
342     @Test
343     public void makeQNameFromIdentifierMountTest() {
344         final Entry<String, Revision> qName = ParserIdentifier.makeQNameFromIdentifier(
345                 MOUNT_POINT_IDENT
346                 + "/"
347                 + TEST_MODULE_NAME
348                 + "/"
349                 + TEST_MODULE_REVISION);
350
351         assertNotNull("QName should be created", qName);
352         assertEquals("Returned not expected module name", TEST_MODULE_NAME, qName.getKey());
353         assertEquals("Returned not expected module revision", Revision.of(TEST_MODULE_REVISION), qName.getValue());
354     }
355
356     /**
357      * Negative test when supplied identifier for module behind mount point is in invalid format and then revision is
358      * not parsable. <code>RestconfDocumentedException</code> is expected and error type, error tag and error status
359      * code are compared to expected values.
360      */
361     @Test
362     public void makeQNameFromIdentifierMountPointInvalidIdentifierNegativeTest() {
363         try {
364             ParserIdentifier.makeQNameFromIdentifier(
365                     MOUNT_POINT_IDENT
366                     + "/"
367                     + TEST_MODULE_REVISION
368                     + "/"
369                     + TEST_MODULE_NAME);
370
371             fail("Test should fail due to invalid identifier format");
372         } catch (final RestconfDocumentedException e) {
373             assertEquals("Not expected error type",
374                     RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
375             assertEquals("Not expected error tag",
376                     RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
377             assertEquals("Not expected error status code",
378                     400, e.getErrors().get(0).getErrorTag().getStatusCode());
379         }
380     }
381
382     /**
383      * Negative test when supplied identifier for module behind mount point is too short (contains only module name).
384      * <code>RestconfDocumentedException</code> is expected and error type, error tag and error status code
385      * are compared to expected values.
386      */
387     @Test
388     public void makeQNameFromIdentifierMountPointTooShortIdentifierNegativeTest() {
389         try {
390             ParserIdentifier.makeQNameFromIdentifier(
391                     MOUNT_POINT_IDENT
392                     + "/"
393                     + TEST_MODULE_NAME);
394
395             fail("Test should fail due to too short identifier format");
396         } catch (final RestconfDocumentedException e) {
397             assertEquals("Not expected error type",
398                     RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
399             assertEquals("Not expected error tag",
400                     RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
401             assertEquals("Not expected error status code",
402                     400, e.getErrors().get(0).getErrorTag().getStatusCode());
403         }
404     }
405
406     /**
407      * Negative test trying to make <code>QName</code> from <code>null</code> identifier. Test is expected to fail with
408      * <code>NullPointerException</code>.
409      */
410     @Test
411     public void makeQNameFromIdentifierNullIdentifierNegativeTest() {
412         this.thrown.expect(NullPointerException.class);
413         ParserIdentifier.makeQNameFromIdentifier(null);
414     }
415
416     /**
417      * Negative test trying to make <code>QName</code> from empty identifier. Test is expected to fail with
418      * <code>RestconfDocumentedException</code>. Error type, error tag and error status code is compared to expected
419      * values.
420      */
421     @Test
422     public void makeQNameFromIdentifierEmptyIdentifierNegativeTest() {
423         try {
424             ParserIdentifier.makeQNameFromIdentifier("");
425             fail("Test should fail due to empty identifier");
426         } catch (final RestconfDocumentedException e) {
427             assertEquals("Not expected error type",
428                     RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
429             assertEquals("Not expected error tag",
430                     RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
431             assertEquals("Not expected error status code",
432                     400, e.getErrors().get(0).getErrorTag().getStatusCode());
433         }
434     }
435
436     /**
437      * Negative test with identifier containing double slash. Between // there is one empty string which will be
438      * incorrectly considered to be module revision. Test is expected to fail with
439      * <code>RestconfDocumentedException</code> and error type, error tag and error status code are compared to
440      * expected values.
441      */
442     @Test
443     public void makeQNameFromIdentifierDoubleSlashNegativeTest() {
444         try {
445             ParserIdentifier.makeQNameFromIdentifier(TEST_MODULE_NAME + "//" + TEST_MODULE_REVISION);
446             fail("Test should fail due to identifier containing double slash");
447         } catch (final RestconfDocumentedException e) {
448             assertEquals("Not expected error type",
449                     RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
450             assertEquals("Not expected error tag",
451                     RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
452             assertEquals("Not expected error status code",
453                     400, e.getErrors().get(0).getErrorTag().getStatusCode());
454         }
455     }
456
457     /**
458      * {@link ParserIdentifier#toSchemaExportContextFromIdentifier(SchemaContext, String, DOMMountPointService)} tests.
459      */
460
461     /**
462      * Positive test of getting <code>SchemaExportContext</code>. Expected module name, revision and namespace are
463      * verified.
464      */
465     @Test
466     public void toSchemaExportContextFromIdentifierTest() {
467         final SchemaExportContext exportContext = ParserIdentifier.toSchemaExportContextFromIdentifier(
468                 this.schemaContext, TEST_MODULE_NAME + "/" + TEST_MODULE_REVISION, null, sourceProvider);
469
470         assertNotNull("Export context should be parsed", exportContext);
471
472         final Module module = exportContext.getModule();
473         assertNotNull("Export context should contains test module", module);
474
475         assertEquals("Returned not expected module name",
476                 TEST_MODULE_NAME, module.getName());
477         assertEquals("Returned not expected module revision",
478                 Revision.ofNullable(TEST_MODULE_REVISION), module.getRevision());
479         assertEquals("Returned not expected module namespace",
480                 TEST_MODULE_NAMESPACE, module.getNamespace().toString());
481     }
482
483     /**
484      * Test of getting <code>SchemaExportContext</code> when desired module is not found.
485      * <code>SchemaExportContext</code> should be created but module should be set to <code>null</code>.
486      */
487     @Test
488     public void toSchemaExportContextFromIdentifierNotFoundTest() {
489         final SchemaExportContext exportContext = ParserIdentifier.toSchemaExportContextFromIdentifier(
490                 this.schemaContext,
491                 "not-existing-module" + "/" + "2016-01-01",
492                 null, sourceProvider);
493
494         assertNotNull("Export context should be parsed", exportContext);
495         assertNull("Not-existing module should be null", exportContext.getModule());
496     }
497
498     /**
499      * Negative test trying to get <code>SchemaExportContext</code> with invalid identifier. Test is expected to fail
500      * with <code>RestconfDocumentedException</code> error type, error tag and error status code are compared to
501      * expected values.
502      */
503     @Test
504     public void toSchemaExportContextFromIdentifierInvalidIdentifierNegativeTest() {
505         try {
506             ParserIdentifier.toSchemaExportContextFromIdentifier(
507                     this.schemaContext, TEST_MODULE_REVISION + "/" + TEST_MODULE_NAME, null, sourceProvider);
508             fail("Test should fail due to invalid identifier supplied");
509         } catch (final RestconfDocumentedException e) {
510             assertEquals("Not expected error type",
511                     RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
512             assertEquals("Not expected error tag",
513                     RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
514             assertEquals("Not expected error status code",
515                     400, e.getErrors().get(0).getErrorTag().getStatusCode());
516         }
517     }
518
519     /**
520      * Positive test of getting <code>SchemaExportContext</code> for module behind mount point.
521      * Expected module name, revision and namespace are verified.
522      */
523     @Test
524     public void toSchemaExportContextFromIdentifierMountPointTest() {
525         final SchemaExportContext exportContext = ParserIdentifier.toSchemaExportContextFromIdentifier(
526                 this.schemaContext,
527                 MOUNT_POINT_IDENT + "/" + TEST_MODULE_NAME + "/" + TEST_MODULE_REVISION,
528                 this.mountPointService, sourceProvider);
529
530         final Module module = exportContext.getModule();
531         assertNotNull("Export context should contains test module", module);
532
533         assertEquals("Returned not expected module name",
534                 TEST_MODULE_NAME, module.getName());
535         assertEquals("Returned not expected module revision",
536                 Revision.ofNullable(TEST_MODULE_REVISION), module.getRevision());
537         assertEquals("Returned not expected module namespace",
538                 TEST_MODULE_NAMESPACE, module.getNamespace().toString());
539     }
540
541     /**
542      * Negative test of getting <code>SchemaExportContext</code> when desired module is not found behind mount point.
543      * <code>SchemaExportContext</code> should be still created but module should be set to <code>null</code>.
544      */
545     @Test
546     public void toSchemaExportContextFromIdentifierMountPointNotFoundTest() {
547         final SchemaExportContext exportContext = ParserIdentifier.toSchemaExportContextFromIdentifier(
548                 this.schemaContext,
549                 MOUNT_POINT_IDENT + "/" + "not-existing-module" + "/" + "2016-01-01",
550                 this.mountPointService, sourceProvider);
551
552         assertNotNull("Export context should be parsed", exportContext);
553         assertNull("Not-existing module should be null", exportContext.getModule());
554     }
555
556     /**
557      * Negative test trying to get <code>SchemaExportContext</code> behind mount point with invalid identifier. Test is
558      * expected to fail with <code>RestconfDocumentedException</code> error type, error tag and error status code are
559      * compared to expected values.
560      */
561     @Test
562     public void toSchemaExportContextFromIdentifierMountPointInvalidIdentifierNegativeTest() {
563         try {
564             ParserIdentifier.toSchemaExportContextFromIdentifier(
565                     this.schemaContext,
566                     MOUNT_POINT_IDENT + "/" + TEST_MODULE_REVISION + "/" + TEST_MODULE_NAME,
567                     this.mountPointService, sourceProvider);
568
569             fail("Test should fail due to invalid identifier supplied");
570         } catch (final RestconfDocumentedException e) {
571             assertEquals("Not expected error type",
572                     RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
573             assertEquals("Not expected error tag",
574                     RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
575             assertEquals("Not expected error status code",
576                     400, e.getErrors().get(0).getErrorTag().getStatusCode());
577         }
578     }
579
580     /**
581      * Negative test of getting <code>SchemaExportContext</code> when supplied identifier is null.
582      * <code>NullPointerException</code> is expected. <code>DOMMountPointService</code> is not used.
583      */
584     @Test
585     public void toSchemaExportContextFromIdentifierNullIdentifierNegativeTest() {
586         this.thrown.expect(NullPointerException.class);
587         ParserIdentifier.toSchemaExportContextFromIdentifier(this.schemaContext, null, null, sourceProvider);
588     }
589
590     /**
591      * Negative test of of getting <code>SchemaExportContext</code> when supplied <code>SchemaContext</code> is
592      * <code>null</code>. Test is expected to fail with <code>NullPointerException</code>.
593      */
594     @Test
595     public void toSchemaExportContextFromIdentifierNullSchemaContextNegativeTest() {
596         this.thrown.expect(NullPointerException.class);
597         ParserIdentifier.toSchemaExportContextFromIdentifier(null, TEST_MODULE_NAME + "/" + TEST_MODULE_REVISION, null,
598                 sourceProvider);
599     }
600
601     /**
602      * Negative test of of getting <code>SchemaExportContext</code> when supplied <code>SchemaContext</code> is
603      * <code>null</code> and identifier specifies module behind mount point. Test is expected to fail with
604      * <code>NullPointerException</code>.
605      */
606     @Test
607     public void toSchemaExportContextFromIdentifierMountPointNullSchemaContextNegativeTest() {
608         this.thrown.expect(NullPointerException.class);
609         ParserIdentifier.toSchemaExportContextFromIdentifier(
610                 null,
611                 MOUNT_POINT_IDENT
612                 + "/"
613                 + TEST_MODULE_NAME
614                 + "/"
615                 + TEST_MODULE_REVISION,
616                 this.mountPointService,
617                 sourceProvider);
618     }
619
620     /**
621      * Negative test of of getting <code>SchemaExportContext</code> when supplied <code>DOMMountPointService</code>
622      * is <code>null</code> and identifier defines module behind mount point. Test is expected to fail with
623      * <code>NullPointerException</code>.
624      */
625     @Test
626     public void toSchemaExportContextFromIdentifierNullMountPointServiceNegativeTest() {
627         this.thrown.expect(NullPointerException.class);
628         ParserIdentifier.toSchemaExportContextFromIdentifier(
629                 this.schemaContext,
630                 MOUNT_POINT_IDENT
631                 + "/"
632                 + TEST_MODULE_NAME
633                 + "/"
634                 + TEST_MODULE_REVISION,
635                 null,
636                 sourceProvider);
637     }
638
639     /**
640      * Negative test of of getting <code>SchemaExportContext</code> when <code>SchemaContext</code> behind mount
641      * point is <code>null</code>. Test is expected to fail with <code>NullPointerException</code>.
642      */
643     @Test
644     public void toSchemaExportContextFromIdentifierNullSchemaContextBehindMountPointNegativeTest() {
645         this.thrown.expect(NullPointerException.class);
646         ParserIdentifier.toSchemaExportContextFromIdentifier(
647                 this.schemaContext,
648                 "/"
649                 + RestconfConstants.MOUNT
650                 + "/"
651                 + TEST_MODULE_NAME
652                 + "/"
653                 + TEST_MODULE_REVISION,
654                 this.mockMountPointService,
655                 sourceProvider);
656     }
657
658     /**
659      * Test invoke RPC.
660      *
661      * <p>
662      * Verify if RPC schema node was found.
663      */
664     @Test
665     public void invokeRpcTest() {
666         final InstanceIdentifierContext<?> result = ParserIdentifier.toInstanceIdentifier(
667                 INVOKE_RPC, this.schemaContext, Optional.absent());
668
669         // RPC schema node
670         final QName rpcQName = result.getSchemaNode().getQName();
671         assertEquals("invoke:rpc:module", rpcQName.getModule().getNamespace().toString());
672         assertEquals("rpc-test", rpcQName.getLocalName());
673
674         // other fields
675         assertEquals(IdentifierCodec.deserialize(INVOKE_RPC, schemaContext), result.getInstanceIdentifier());
676         assertEquals(null, result.getMountPoint());
677         assertEquals(this.schemaContext, result.getSchemaContext());
678     }
679
680     /**
681      * Test invoke RPC on mount point.
682      *
683      * <p>
684      * Verify if RPC schema node was found.
685      */
686     @Test
687     public void invokeRpcOnMountPointTest() {
688         final InstanceIdentifierContext<?> result = ParserIdentifier.toInstanceIdentifier(
689                 MOUNT_POINT_IDENT + "/" + INVOKE_RPC, this.schemaContext, Optional.of(this.mountPointService));
690
691         // RPC schema node
692         final QName rpcQName = result.getSchemaNode().getQName();
693         assertEquals("invoke:rpc:module", rpcQName.getModule().getNamespace().toString());
694         assertEquals("rpc-test", rpcQName.getLocalName());
695
696         // other fields
697         assertEquals(IdentifierCodec.deserialize(INVOKE_RPC, schemaContext), result.getInstanceIdentifier());
698         assertEquals(this.mountPoint, result.getMountPoint());
699         assertEquals(this.schemaContextOnMountPoint, result.getSchemaContext());
700     }
701 }