2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.services.simple.impl;
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;
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;
41 * Unit tests for {@code RestconfSchemaService}.
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 + "/";
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";
52 @Rule public ExpectedException thrown = ExpectedException.none();
55 private RestconfSchemaService schemaService;
59 private SchemaContextHandler mockContextHandler;
61 private DOMYangTextSourceProvider sourceProvider;
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;
70 // mount point service
71 private DOMMountPointService mountPointService;
74 public void setup() throws Exception {
75 MockitoAnnotations.initMocks(this);
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"));
83 this.mountPointService = new DOMMountPointServiceImpl();
84 // create and register mount points
86 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:1", "2016-01-01", "cont")))
87 .addInitialSchemaContext(schemaContextBehindMountPoint)
90 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:2", "2016-01-01", "cont")))
93 this.schemaService = new RestconfSchemaServiceImpl(this.mockContextHandler,
94 DOMMountPointServiceHandler.newInstance(mountPointService), sourceProvider);
98 * Test if service was successfully created.
101 public void schemaServiceImplInitTest() {
102 assertNotNull("Schema service should be initialized and not null", this.schemaService);
106 * Get schema with identifier of existing module and check if correct module was found.
109 public void getSchemaTest() {
110 // prepare conditions - return not-mount point schema context
111 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
114 final SchemaExportContext exportContext = this.schemaService.getSchema(TEST_MODULE);
117 assertNotNull("Export context should not be null", exportContext);
119 final Module module = exportContext.getModule();
120 assertNotNull("Existing module should be found", module);
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());
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>.
132 public void getSchemaForNotExistingModuleTest() {
133 // prepare conditions - return not-mount point schema context
134 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
137 final SchemaExportContext exportContext = this.schemaService.getSchema(NOT_EXISTING_MODULE);
140 assertNotNull("Export context should not be null", exportContext);
141 assertNull("Not-existing module should not be found", exportContext.getModule());
145 * Get schema with identifier of existing module behind mount point and check if correct module was found.
148 public void getSchemaMountPointTest() {
149 // prepare conditions - return schema context with mount points
150 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
153 final SchemaExportContext exportContext =
154 this.schemaService.getSchema(MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
157 assertNotNull("Export context should not be null", exportContext);
159 final Module module = exportContext.getModule();
160 assertNotNull("Existing module should be found", module);
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());
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>.
172 public void getSchemaForNotExistingModuleMountPointTest() {
173 // prepare conditions - return schema context with mount points
174 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
177 final SchemaExportContext exportContext = this.schemaService.getSchema(MOUNT_POINT + NOT_EXISTING_MODULE);
180 assertNotNull("Export context should not be null", exportContext);
181 assertNull("Not-existing module should not be found", exportContext.getModule());
185 * Try to get schema with <code>null</code> <code>SchemaContext</code> expecting <code>NullPointerException</code>.
188 public void getSchemaWithNullSchemaContextTest() {
189 // prepare conditions - returned schema context is null
190 when(this.mockContextHandler.get()).thenReturn(null);
193 this.thrown.expect(NullPointerException.class);
194 this.schemaService.getSchema(TEST_MODULE);
198 * Try to get schema with <code>null</code> <code>SchemaContext</code> for mount points.
199 * <code>NullPointerException</code> is expected.
202 public void getSchemaWithNullSchemaContextMountPointTest() {
203 // prepare conditions - returned schema context for mount points is null
204 when(this.mockContextHandler.get()).thenReturn(null);
207 this.thrown.expect(NullPointerException.class);
208 this.schemaService.getSchema(MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
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>.
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);
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);
227 * Try to get schema with null identifier expecting <code>NullPointerException</code>. The same processing is for
228 * server and also for mount point.
231 public void getSchemaWithNullIdentifierTest() {
232 // prepare conditions - return correct schema context
233 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
236 this.thrown.expect(NullPointerException.class);
237 this.schemaService.getSchema(null);
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.
245 public void getSchemaWithEmptyIdentifierTest() {
246 // prepare conditions - return correct schema context
247 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
249 // make test and verify
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());
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
266 public void getSchemaWithEmptyIdentifierMountPointTest() {
267 // prepare conditions - return correct schema context with mount points
268 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
270 // make test and verify
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());
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.
286 public void getSchemaWithNotParsableIdentifierTest() {
287 // prepare conditions - return correct schema context without mount points
288 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
290 // make test and verify
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());
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
307 public void getSchemaWithNotParsableIdentifierMountPointTest() {
308 // prepare conditions - return correct schema context with mount points
309 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
311 // make test and verify
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());
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.
327 * Not valid identifier contains only revision without module name.
330 public void getSchemaWrongIdentifierTest() {
331 // prepare conditions - return correct schema context without mount points
332 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
334 // make test and verify
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());
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
351 * Not valid identifier contains only revision without module name.
354 public void getSchemaWrongIdentifierMountPointTest() {
355 // prepare conditions - return correct schema context with mount points
356 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
358 // make test and verify
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());
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
375 public void getSchemaWithoutRevisionTest() {
376 // prepare conditions - return correct schema context without mount points
377 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
379 // make test and verify
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());
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
396 public void getSchemaWithoutRevisionMountPointTest() {
397 // prepare conditions - return correct schema context with mount points
398 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
400 // make test and verify
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());
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.
416 public void getSchemaContextWithNotExistingMountPointTest() {
417 // prepare conditions - return schema context with mount points
418 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
421 this.thrown.expect(RestconfDocumentedException.class);
422 this.schemaService.getSchema(NOT_EXISTING_MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);