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
9 package org.opendaylight.restconf.nb.rfc8040.services.simple.impl;
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;
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;
42 * Unit tests for {@code RestconfSchemaService}.
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 + "/";
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";
53 @Rule public ExpectedException thrown = ExpectedException.none();
56 private RestconfSchemaService schemaService;
60 private SchemaContextHandler mockContextHandler;
62 private DOMYangTextSourceProvider sourceProvider;
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;
71 // mount point service
72 private DOMMountPointService mountPointService;
75 public void setup() throws Exception {
76 MockitoAnnotations.initMocks(this);
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"));
84 this.mountPointService = new DOMMountPointServiceImpl();
85 // create and register mount points
87 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:1", "2016-01-01", "cont")))
88 .addInitialSchemaContext(schemaContextBehindMountPoint)
91 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:2", "2016-01-01", "cont")))
94 this.schemaService = new RestconfSchemaServiceImpl(this.mockContextHandler,
95 DOMMountPointServiceHandler.newInstance(mountPointService), sourceProvider);
99 * Test if service was successfully created.
102 public void schemaServiceImplInitTest() {
103 assertNotNull("Schema service should be initialized and not null", this.schemaService);
107 * Get schema with identifier of existing module and check if correct module was found.
110 public void getSchemaTest() {
111 // prepare conditions - return not-mount point schema context
112 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
115 final SchemaExportContext exportContext = this.schemaService.getSchema(TEST_MODULE);
118 assertNotNull("Export context should not be null", exportContext);
120 final Module module = exportContext.getModule();
121 assertNotNull("Existing module should be found", module);
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());
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>.
133 public void getSchemaForNotExistingModuleTest() {
134 // prepare conditions - return not-mount point schema context
135 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
138 final SchemaExportContext exportContext = this.schemaService.getSchema(NOT_EXISTING_MODULE);
141 assertNotNull("Export context should not be null", exportContext);
142 assertNull("Not-existing module should not be found", exportContext.getModule());
146 * Get schema with identifier of existing module behind mount point and check if correct module was found.
149 public void getSchemaMountPointTest() {
150 // prepare conditions - return schema context with mount points
151 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
154 final SchemaExportContext exportContext =
155 this.schemaService.getSchema(MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
158 assertNotNull("Export context should not be null", exportContext);
160 final Module module = exportContext.getModule();
161 assertNotNull("Existing module should be found", module);
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());
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>.
173 public void getSchemaForNotExistingModuleMountPointTest() {
174 // prepare conditions - return schema context with mount points
175 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
178 final SchemaExportContext exportContext = this.schemaService.getSchema(MOUNT_POINT + NOT_EXISTING_MODULE);
181 assertNotNull("Export context should not be null", exportContext);
182 assertNull("Not-existing module should not be found", exportContext.getModule());
186 * Try to get schema with <code>null</code> <code>SchemaContext</code> expecting <code>NullPointerException</code>.
189 public void getSchemaWithNullSchemaContextTest() {
190 // prepare conditions - returned schema context is null
191 when(this.mockContextHandler.get()).thenReturn(null);
194 this.thrown.expect(NullPointerException.class);
195 this.schemaService.getSchema(TEST_MODULE);
199 * Try to get schema with <code>null</code> <code>SchemaContext</code> for mount points.
200 * <code>NullPointerException</code> is expected.
203 public void getSchemaWithNullSchemaContextMountPointTest() {
204 // prepare conditions - returned schema context for mount points is null
205 when(this.mockContextHandler.get()).thenReturn(null);
208 this.thrown.expect(NullPointerException.class);
209 this.schemaService.getSchema(MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
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>.
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);
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);
228 * Try to get schema with null identifier expecting <code>NullPointerException</code>. The same processing is for
229 * server and also for mount point.
232 public void getSchemaWithNullIdentifierTest() {
233 // prepare conditions - return correct schema context
234 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
237 this.thrown.expect(NullPointerException.class);
238 this.schemaService.getSchema(null);
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.
246 public void getSchemaWithEmptyIdentifierTest() {
247 // prepare conditions - return correct schema context
248 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
250 // make test and verify
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());
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
267 public void getSchemaWithEmptyIdentifierMountPointTest() {
268 // prepare conditions - return correct schema context with mount points
269 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
271 // make test and verify
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());
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.
287 public void getSchemaWithNotParsableIdentifierTest() {
288 // prepare conditions - return correct schema context without mount points
289 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
291 // make test and verify
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());
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
308 public void getSchemaWithNotParsableIdentifierMountPointTest() {
309 // prepare conditions - return correct schema context with mount points
310 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
312 // make test and verify
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());
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.
328 * Not valid identifier contains only revision without module name.
331 public void getSchemaWrongIdentifierTest() {
332 // prepare conditions - return correct schema context without mount points
333 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
335 // make test and verify
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());
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
352 * Not valid identifier contains only revision without module name.
355 public void getSchemaWrongIdentifierMountPointTest() {
356 // prepare conditions - return correct schema context with mount points
357 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
359 // make test and verify
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());
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
376 public void getSchemaWithoutRevisionTest() {
377 // prepare conditions - return correct schema context without mount points
378 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
380 // make test and verify
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());
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
397 public void getSchemaWithoutRevisionMountPointTest() {
398 // prepare conditions - return correct schema context with mount points
399 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
401 // make test and verify
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());
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.
417 public void getSchemaContextWithNotExistingMountPointTest() {
418 // prepare conditions - return schema context with mount points
419 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
422 this.thrown.expect(IllegalArgumentException.class);
423 this.schemaService.getSchema(NOT_EXISTING_MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);