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