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.base.services.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 com.google.common.collect.ImmutableClassToInstanceMap;
18 import java.util.HashMap;
19 import org.junit.Before;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.junit.rules.ExpectedException;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
27 import org.opendaylight.controller.md.sal.dom.broker.impl.mount.DOMMountPointServiceImpl;
28 import org.opendaylight.controller.md.sal.dom.broker.spi.mount.SimpleDOMMountPoint;
29 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
30 import org.opendaylight.netconf.md.sal.rest.schema.SchemaExportContext;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
33 import org.opendaylight.restconf.base.services.api.RestconfSchemaService;
34 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
35 import org.opendaylight.restconf.handlers.SchemaContextHandler;
36 import org.opendaylight.restconf.utils.RestconfConstants;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.model.api.Module;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45 * Unit tests for {@code RestconfSchemaService}.
47 public class RestconfSchemaServiceTest {
48 private static final String MOUNT_POINT = "mount-point-1:cont" + "/" + RestconfConstants.MOUNT + "/";
49 private static final String NULL_MOUNT_POINT = "mount-point-2:cont" + "/" + RestconfConstants.MOUNT + "/";
50 private static final String NOT_EXISTING_MOUNT_POINT = "mount-point-3:cont" + "/" + RestconfConstants.MOUNT + "/";
52 private static final String TEST_MODULE = "module1/2014-01-01";
53 private static final String TEST_MODULE_BEHIND_MOUNT_POINT = "module1-behind-mount-point/2014-02-03";
54 private static final String NOT_EXISTING_MODULE = "not-existing/2016-01-01";
56 @Rule public ExpectedException thrown = ExpectedException.none();
59 private RestconfSchemaService schemaService;
63 private SchemaContextHandler mockContextHandler;
65 private DOMMountPointServiceHandler mockMountPointHandler;
67 // schema context with modules
68 private SchemaContext schemaContext;
69 // schema context with modules behind mount point
70 private SchemaContext schemaContextBehindMountPoint;
71 // schema context with mount points
72 private SchemaContext schemaContextWithMountPoints;
74 // mount point with schema context with modules behind mount point
75 private DOMMountPoint mountPoint;
76 // mount point with null schema context
77 private DOMMountPoint mountPointWithNullSchemaContext;
78 // mount point service
79 private DOMMountPointService mountPointService;
82 public void setup() throws Exception {
83 MockitoAnnotations.initMocks(this);
85 this.schemaContext = YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles("/modules"));
86 this.schemaContextBehindMountPoint = YangParserTestUtils
87 .parseYangSources(TestRestconfUtils.loadFiles("/modules/modules-behind-mount-point"));
88 this.schemaContextWithMountPoints =
89 YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles("/modules/mount-points"));
91 // create and register mount points
92 this.mountPoint = SimpleDOMMountPoint.create(
93 YangInstanceIdentifier.of(QName.create("mount:point:1", "2016-01-01", "cont")),
94 ImmutableClassToInstanceMap.copyOf(new HashMap<>()),
95 this.schemaContextBehindMountPoint
98 this.mountPointWithNullSchemaContext = SimpleDOMMountPoint.create(
99 YangInstanceIdentifier.of(QName.create("mount:point:2", "2016-01-01", "cont")),
100 ImmutableClassToInstanceMap.copyOf(new HashMap<>()),
104 this.mountPointService = new DOMMountPointServiceImpl();
105 ((DOMMountPointServiceImpl) this.mountPointService).registerMountPoint(this.mountPoint);
106 ((DOMMountPointServiceImpl) this.mountPointService).registerMountPoint(this.mountPointWithNullSchemaContext);
107 when(this.mockMountPointHandler.get()).thenReturn(this.mountPointService);
109 this.schemaService = new RestconfSchemaServiceImpl(this.mockContextHandler, this.mockMountPointHandler);
113 * Test if service was successfully created.
116 public void schemaServiceImplInitTest() {
117 assertNotNull("Schema service should be initialized and not null", this.schemaService);
121 * Get schema with identifier of existing module and check if correct module was found.
124 public void getSchemaTest() {
125 // prepare conditions - return not-mount point schema context
126 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
129 final SchemaExportContext exportContext = this.schemaService.getSchema(TEST_MODULE);
132 assertNotNull("Export context should not be null", exportContext);
134 final Module module = exportContext.getModule();
135 assertNotNull("Existing module should be found", module);
137 assertEquals("Not expected module name", "module1", module.getName());
138 assertEquals("Not expected module revision", "2014-01-01",
139 SimpleDateFormatUtil.getRevisionFormat().format(module.getRevision()));
140 assertEquals("Not expected module namespace", "module:1", module.getNamespace().toString());
144 * Get schema with identifier of not-existing module. <code>SchemaExportContext</code> is still created, but module
145 * should be set to <code>null</code>.
148 public void getSchemaForNotExistingModuleTest() {
149 // prepare conditions - return not-mount point schema context
150 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
153 final SchemaExportContext exportContext = this.schemaService.getSchema(NOT_EXISTING_MODULE);
156 assertNotNull("Export context should not be null", exportContext);
157 assertNull("Not-existing module should not be found", exportContext.getModule());
161 * Get schema with identifier of existing module behind mount point and check if correct module was found.
164 public void getSchemaMountPointTest() {
165 // prepare conditions - return schema context with mount points
166 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
169 final SchemaExportContext exportContext =
170 this.schemaService.getSchema(MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
173 assertNotNull("Export context should not be null", exportContext);
175 final Module module = exportContext.getModule();
176 assertNotNull("Existing module should be found", module);
178 assertEquals("Not expected module name", "module1-behind-mount-point", module.getName());
179 assertEquals("Not expected module revision", "2014-02-03",
180 SimpleDateFormatUtil.getRevisionFormat().format(module.getRevision()));
181 assertEquals("Not expected module namespace", "module:1:behind:mount:point", module.getNamespace().toString());
185 * Get schema with identifier of not-existing module behind mount point. <code>SchemaExportContext</code> is still
186 * created, but module should be set to <code>null</code>.
189 public void getSchemaForNotExistingModuleMountPointTest() {
190 // prepare conditions - return schema context with mount points
191 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
194 final SchemaExportContext exportContext = this.schemaService.getSchema(MOUNT_POINT + NOT_EXISTING_MODULE);
197 assertNotNull("Export context should not be null", exportContext);
198 assertNull("Not-existing module should not be found", exportContext.getModule());
202 * Try to get schema with <code>null</code> <code>SchemaContext</code> expecting <code>NullPointerException</code>.
205 public void getSchemaWithNullSchemaContextTest() {
206 // prepare conditions - returned schema context is null
207 when(this.mockContextHandler.get()).thenReturn(null);
210 this.thrown.expect(NullPointerException.class);
211 this.schemaService.getSchema(TEST_MODULE);
215 * Try to get schema with <code>null</code> <code>SchemaContext</code> for mount points.
216 * <code>NullPointerException</code> is expected.
219 public void getSchemaWithNullSchemaContextMountPointTest() {
220 // prepare conditions - returned schema context for mount points is null
221 when(this.mockContextHandler.get()).thenReturn(null);
224 this.thrown.expect(NullPointerException.class);
225 this.schemaService.getSchema(MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
229 * Try to get schema with <code>null</code> <code>SchemaContext</code> behind mount point when using
230 * <code>NULL_MOUNT_POINT</code>. Test is expected to fail with <code>NullPointerException</code>.
233 public void getSchemaNullSchemaContextBehindMountPointTest() {
234 // prepare conditions - return correct schema context for mount points (this is not null)
235 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
237 // make test - call service on mount point with null schema context
238 this.thrown.expect(NullPointerException.class);
239 // NULL_MOUNT_POINT contains null schema context
240 this.schemaService.getSchema(NULL_MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
244 * Try to get schema with null identifier expecting <code>NullPointerException</code>. The same processing is for
245 * server and also for mount point.
248 public void getSchemaWithNullIdentifierTest() {
249 // prepare conditions - return correct schema context
250 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
253 this.thrown.expect(NullPointerException.class);
254 this.schemaService.getSchema(null);
258 * Try to get schema with empty (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
259 * type, error tag and error status code are compared to expected values.
262 public void getSchemaWithEmptyIdentifierTest() {
263 // prepare conditions - return correct schema context
264 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
266 // make test and verify
268 this.schemaService.getSchema("");
269 fail("Test should fail due to invalid identifier");
270 } catch (final RestconfDocumentedException e) {
271 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
272 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
273 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
278 * Try to get schema with empty (not valid) identifier behind mount point catching
279 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
283 public void getSchemaWithEmptyIdentifierMountPointTest() {
284 // prepare conditions - return correct schema context with mount points
285 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
287 // make test and verify
289 this.schemaService.getSchema(MOUNT_POINT + "");
290 fail("Test should fail due to invalid identifier");
291 } catch (final RestconfDocumentedException e) {
292 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
293 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
294 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
299 * Try to get schema with not-parsable identifier catching <code>RestconfDocumentedException</code>. Error type,
300 * error tag and error status code are compared to expected values.
303 public void getSchemaWithNotParsableIdentifierTest() {
304 // prepare conditions - return correct schema context without mount points
305 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
307 // make test and verify
309 this.schemaService.getSchema("01_module/2016-01-01");
310 fail("Test should fail due to invalid identifier");
311 } catch (final RestconfDocumentedException e) {
312 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
313 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
314 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
319 * Try to get schema behind mount point with not-parsable identifier catching
320 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
324 public void getSchemaWithNotParsableIdentifierMountPointTest() {
325 // prepare conditions - return correct schema context with mount points
326 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
328 // make test and verify
330 this.schemaService.getSchema(MOUNT_POINT + "01_module/2016-01-01");
331 fail("Test should fail due to invalid identifier");
332 } catch (final RestconfDocumentedException e) {
333 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
334 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
335 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
340 * Try to get schema with wrong (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
341 * type, error tag and error status code are compared to expected values.
344 * Not valid identifier contains only revision without module name.
347 public void getSchemaWrongIdentifierTest() {
348 // prepare conditions - return correct schema context without mount points
349 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
351 // make test and verify
353 this.schemaService.getSchema("2014-01-01");
354 fail("Test should fail due to invalid identifier");
355 } catch (final RestconfDocumentedException e) {
356 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
357 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
358 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
363 * Try to get schema with wrong (not valid) identifier behind mount point catching
364 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
368 * Not valid identifier contains only revision without module name.
371 public void getSchemaWrongIdentifierMountPointTest() {
372 // prepare conditions - return correct schema context with mount points
373 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
375 // make test and verify
377 this.schemaService.getSchema(MOUNT_POINT + "2014-01-01");
378 fail("Test should fail due to invalid identifier");
379 } catch (final RestconfDocumentedException e) {
380 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
381 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
382 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
387 * Try to get schema with identifier which does not contain revision catching
388 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
392 public void getSchemaWithoutRevisionTest() {
393 // prepare conditions - return correct schema context without mount points
394 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
396 // make test and verify
398 this.schemaService.getSchema("module");
399 fail("Test should fail due to invalid identifier");
400 } catch (final RestconfDocumentedException e) {
401 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
402 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
403 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
408 * Try to get schema behind mount point with identifier when does not contain revision catching
409 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
413 public void getSchemaWithoutRevisionMountPointTest() {
414 // prepare conditions - return correct schema context with mount points
415 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
417 // make test and verify
419 this.schemaService.getSchema(MOUNT_POINT + "module");
420 fail("Test should fail due to invalid identifier");
421 } catch (final RestconfDocumentedException e) {
422 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
423 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
424 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
429 * Negative test when mount point module is not found in current <code>SchemaContext</code> for mount points.
430 * <code>IllegalArgumentException</code> exception is expected.
433 public void getSchemaContextWithNotExistingMountPointTest() {
434 // prepare conditions - return schema context with mount points
435 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
438 this.thrown.expect(IllegalArgumentException.class);
439 this.schemaService.getSchema(NOT_EXISTING_MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);