Fixed wrong exception types
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / services / simple / impl / RestconfSchemaServiceTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040.services.simple.impl;
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.junit.Assert.fail;
14 import static org.mockito.Mockito.when;
15
16 import org.junit.Before;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.ExpectedException;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
23 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
24 import org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl;
25 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
26 import org.opendaylight.restconf.common.errors.RestconfError;
27 import org.opendaylight.restconf.common.schema.SchemaExportContext;
28 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
29 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
30 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
31 import org.opendaylight.restconf.nb.rfc8040.services.simple.api.RestconfSchemaService;
32 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.common.Revision;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39
40 /**
41  * Unit tests for {@code RestconfSchemaService}.
42  */
43 public class RestconfSchemaServiceTest {
44     private static final String MOUNT_POINT = "mount-point-1:cont" + "/" + RestconfConstants.MOUNT + "/";
45     private static final String NULL_MOUNT_POINT = "mount-point-2:cont" + "/" + RestconfConstants.MOUNT + "/";
46     private static final String NOT_EXISTING_MOUNT_POINT = "mount-point-3:cont" + "/" + RestconfConstants.MOUNT + "/";
47
48     private static final String TEST_MODULE = "module1/2014-01-01";
49     private static final String TEST_MODULE_BEHIND_MOUNT_POINT = "module1-behind-mount-point/2014-02-03";
50     private static final String NOT_EXISTING_MODULE = "not-existing/2016-01-01";
51
52     @Rule public ExpectedException thrown = ExpectedException.none();
53
54     // service under test
55     private RestconfSchemaService schemaService;
56
57     // handlers
58     @Mock
59     private SchemaContextHandler mockContextHandler;
60     @Mock
61     private DOMYangTextSourceProvider sourceProvider;
62
63     // schema context with modules
64     private SchemaContext schemaContext;
65     // schema context with modules behind mount point
66     private SchemaContext schemaContextBehindMountPoint;
67     // schema context with mount points
68     private SchemaContext schemaContextWithMountPoints;
69
70     // mount point service
71     private DOMMountPointService mountPointService;
72
73     @Before
74     public void setup() throws Exception {
75         MockitoAnnotations.initMocks(this);
76
77         this.schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/modules"));
78         this.schemaContextBehindMountPoint = YangParserTestUtils
79                 .parseYangFiles(TestRestconfUtils.loadFiles("/modules/modules-behind-mount-point"));
80         this.schemaContextWithMountPoints =
81                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/modules/mount-points"));
82
83         this.mountPointService = new DOMMountPointServiceImpl();
84         // create and register mount points
85         mountPointService
86                 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:1", "2016-01-01", "cont")))
87                 .addInitialSchemaContext(schemaContextBehindMountPoint)
88                 .register();
89         mountPointService
90                 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:2", "2016-01-01", "cont")))
91                 .register();
92
93         this.schemaService = new RestconfSchemaServiceImpl(this.mockContextHandler,
94                 DOMMountPointServiceHandler.newInstance(mountPointService), sourceProvider);
95     }
96
97     /**
98      * Test if service was successfully created.
99      */
100     @Test
101     public void schemaServiceImplInitTest() {
102         assertNotNull("Schema service should be initialized and not null", this.schemaService);
103     }
104
105     /**
106      * Get schema with identifier of existing module and check if correct module was found.
107      */
108     @Test
109     public void getSchemaTest() {
110         // prepare conditions - return not-mount point schema context
111         when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
112
113         // make test
114         final SchemaExportContext exportContext = this.schemaService.getSchema(TEST_MODULE);
115
116         // verify
117         assertNotNull("Export context should not be null", exportContext);
118
119         final Module module = exportContext.getModule();
120         assertNotNull("Existing module should be found", module);
121
122         assertEquals("Not expected module name", "module1", module.getName());
123         assertEquals("Not expected module revision", Revision.ofNullable("2014-01-01"), module.getRevision());
124         assertEquals("Not expected module namespace", "module:1", module.getNamespace().toString());
125     }
126
127     /**
128      * Get schema with identifier of not-existing module. <code>SchemaExportContext</code> is still created, but module
129      * should be set to <code>null</code>.
130      */
131     @Test
132     public void getSchemaForNotExistingModuleTest() {
133         // prepare conditions - return not-mount point schema context
134         when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
135
136         // make test
137         final SchemaExportContext exportContext = this.schemaService.getSchema(NOT_EXISTING_MODULE);
138
139         // verify
140         assertNotNull("Export context should not be null", exportContext);
141         assertNull("Not-existing module should not be found", exportContext.getModule());
142     }
143
144     /**
145      * Get schema with identifier of existing module behind mount point and check if correct module was found.
146      */
147     @Test
148     public void getSchemaMountPointTest() {
149         // prepare conditions - return schema context with mount points
150         when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
151
152         // make test
153         final SchemaExportContext exportContext =
154                 this.schemaService.getSchema(MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
155
156         // verify
157         assertNotNull("Export context should not be null", exportContext);
158
159         final Module module = exportContext.getModule();
160         assertNotNull("Existing module should be found", module);
161
162         assertEquals("Not expected module name", "module1-behind-mount-point", module.getName());
163         assertEquals("Not expected module revision", Revision.ofNullable("2014-02-03"), module.getRevision());
164         assertEquals("Not expected module namespace", "module:1:behind:mount:point", module.getNamespace().toString());
165     }
166
167     /**
168      * Get schema with identifier of not-existing module behind mount point. <code>SchemaExportContext</code> is still
169      * created, but module should be set to <code>null</code>.
170      */
171     @Test
172     public void getSchemaForNotExistingModuleMountPointTest() {
173         // prepare conditions - return schema context with mount points
174         when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
175
176         // make test
177         final SchemaExportContext exportContext = this.schemaService.getSchema(MOUNT_POINT + NOT_EXISTING_MODULE);
178
179         // verify
180         assertNotNull("Export context should not be null", exportContext);
181         assertNull("Not-existing module should not be found", exportContext.getModule());
182     }
183
184     /**
185      * Try to get schema with <code>null</code> <code>SchemaContext</code> expecting <code>NullPointerException</code>.
186      */
187     @Test
188     public void getSchemaWithNullSchemaContextTest() {
189         // prepare conditions - returned schema context is null
190         when(this.mockContextHandler.get()).thenReturn(null);
191
192         // make test
193         this.thrown.expect(NullPointerException.class);
194         this.schemaService.getSchema(TEST_MODULE);
195     }
196
197     /**
198      * Try to get schema with <code>null</code> <code>SchemaContext</code> for mount points.
199      * <code>NullPointerException</code> is expected.
200      */
201     @Test
202     public void getSchemaWithNullSchemaContextMountPointTest() {
203         // prepare conditions - returned schema context for mount points is null
204         when(this.mockContextHandler.get()).thenReturn(null);
205
206         // make test
207         this.thrown.expect(NullPointerException.class);
208         this.schemaService.getSchema(MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
209     }
210
211     /**
212      * Try to get schema with <code>null</code> <code>SchemaContext</code> behind mount point when using
213      * <code>NULL_MOUNT_POINT</code>. Test is expected to fail with <code>NullPointerException</code>.
214      */
215     @Test
216     public void getSchemaNullSchemaContextBehindMountPointTest() {
217         // prepare conditions - return correct schema context for mount points (this is not null)
218         when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
219
220         // make test - call service on mount point with null schema context
221         this.thrown.expect(NullPointerException.class);
222         // NULL_MOUNT_POINT contains null schema context
223         this.schemaService.getSchema(NULL_MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
224     }
225
226     /**
227      * Try to get schema with null identifier expecting <code>NullPointerException</code>. The same processing is for
228      * server and also for mount point.
229      */
230     @Test
231     public void getSchemaWithNullIdentifierTest() {
232         // prepare conditions - return correct schema context
233         when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
234
235         // make test
236         this.thrown.expect(NullPointerException.class);
237         this.schemaService.getSchema(null);
238     }
239
240     /**
241      * Try to get schema with empty (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
242      * type, error tag and error status code are compared to expected values.
243      */
244     @Test
245     public void getSchemaWithEmptyIdentifierTest() {
246         // prepare conditions - return correct schema context
247         when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
248
249         // make test and verify
250         try {
251             this.schemaService.getSchema("");
252             fail("Test should fail due to invalid identifier");
253         } catch (final RestconfDocumentedException e) {
254             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
255             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
256             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
257         }
258     }
259
260     /**
261      * Try to get schema with empty (not valid) identifier behind mount point catching
262      * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
263      * values.
264      */
265     @Test
266     public void getSchemaWithEmptyIdentifierMountPointTest() {
267         // prepare conditions - return correct schema context with mount points
268         when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
269
270         // make test and verify
271         try {
272             this.schemaService.getSchema(MOUNT_POINT + "");
273             fail("Test should fail due to invalid identifier");
274         } catch (final RestconfDocumentedException e) {
275             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
276             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
277             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
278         }
279     }
280
281     /**
282      * Try to get schema with not-parsable identifier catching <code>RestconfDocumentedException</code>. Error type,
283      * error tag and error status code are compared to expected values.
284      */
285     @Test
286     public void getSchemaWithNotParsableIdentifierTest() {
287         // prepare conditions - return correct schema context without mount points
288         when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
289
290         // make test and verify
291         try {
292             this.schemaService.getSchema("01_module/2016-01-01");
293             fail("Test should fail due to invalid identifier");
294         } catch (final RestconfDocumentedException e) {
295             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
296             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
297             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
298         }
299     }
300
301     /**
302      * Try to get schema behind mount point with not-parsable identifier catching
303      * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
304      * values.
305      */
306     @Test
307     public void getSchemaWithNotParsableIdentifierMountPointTest() {
308         // prepare conditions - return correct schema context with mount points
309         when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
310
311         // make test and verify
312         try {
313             this.schemaService.getSchema(MOUNT_POINT + "01_module/2016-01-01");
314             fail("Test should fail due to invalid identifier");
315         } catch (final RestconfDocumentedException e) {
316             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
317             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
318             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
319         }
320     }
321
322     /**
323      * Try to get schema with wrong (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
324      * type, error tag and error status code are compared to expected values.
325      *
326      * <p>
327      * Not valid identifier contains only revision without module name.
328      */
329     @Test
330     public void getSchemaWrongIdentifierTest() {
331         // prepare conditions - return correct schema context without mount points
332         when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
333
334         // make test and verify
335         try {
336             this.schemaService.getSchema("2014-01-01");
337             fail("Test should fail due to invalid identifier");
338         } catch (final RestconfDocumentedException e) {
339             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
340             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
341             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
342         }
343     }
344
345     /**
346      * Try to get schema with wrong (not valid) identifier behind mount point catching
347      * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
348      * values.
349      *
350      * <p>
351      * Not valid identifier contains only revision without module name.
352      */
353     @Test
354     public void getSchemaWrongIdentifierMountPointTest() {
355         // prepare conditions - return correct schema context with mount points
356         when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
357
358         // make test and verify
359         try {
360             this.schemaService.getSchema(MOUNT_POINT + "2014-01-01");
361             fail("Test should fail due to invalid identifier");
362         } catch (final RestconfDocumentedException e) {
363             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
364             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
365             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
366         }
367     }
368
369     /**
370      * Try to get schema with identifier which does not contain revision catching
371      * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
372      * values.
373      */
374     @Test
375     public void getSchemaWithoutRevisionTest() {
376         // prepare conditions - return correct schema context without mount points
377         when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
378
379         // make test and verify
380         try {
381             this.schemaService.getSchema("module");
382             fail("Test should fail due to invalid identifier");
383         } catch (final RestconfDocumentedException e) {
384             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
385             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
386             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
387         }
388     }
389
390     /**
391      * Try to get schema behind mount point with identifier when does not contain revision catching
392      * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
393      * values.
394      */
395     @Test
396     public void getSchemaWithoutRevisionMountPointTest() {
397         // prepare conditions - return correct schema context with mount points
398         when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
399
400         // make test and verify
401         try {
402             this.schemaService.getSchema(MOUNT_POINT + "module");
403             fail("Test should fail due to invalid identifier");
404         } catch (final RestconfDocumentedException e) {
405             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
406             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
407             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
408         }
409     }
410
411     /**
412      * Negative test when mount point module is not found in current <code>SchemaContext</code> for mount points.
413      * <code>IllegalArgumentException</code> exception is expected.
414      */
415     @Test
416     public void getSchemaContextWithNotExistingMountPointTest() {
417         // prepare conditions - return schema context with mount points
418         when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
419
420         // make test
421         this.thrown.expect(RestconfDocumentedException.class);
422         this.schemaService.getSchema(NOT_EXISTING_MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
423     }
424 }