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