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.rest.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;
16 import com.google.common.collect.ImmutableClassToInstanceMap;
17 import java.util.HashMap;
18 import org.junit.Before;
19 import org.junit.Rule;
20 import org.junit.Test;
21 import org.junit.rules.ExpectedException;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
26 import org.opendaylight.controller.md.sal.dom.broker.impl.mount.DOMMountPointServiceImpl;
27 import org.opendaylight.controller.md.sal.dom.broker.spi.mount.SimpleDOMMountPoint;
28 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
29 import org.opendaylight.netconf.md.sal.rest.schema.SchemaExportContext;
30 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
32 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
33 import org.opendaylight.restconf.handlers.SchemaContextHandler;
34 import org.opendaylight.restconf.rest.services.api.RestconfSchemaService;
35 import org.opendaylight.restconf.rest.services.impl.RestconfSchemaServiceImpl;
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;
44 * Unit tests for {@code RestconfSchemaService}
46 public class RestconfSchemaServiceTest {
47 private static final String MOUNT_POINT = "mount-point-1:cont" + "/" + RestconfConstants.MOUNT + "/";
48 private static final String NULL_MOUNT_POINT = "mount-point-2:cont" + "/" + RestconfConstants.MOUNT + "/";
49 private static final String NOT_EXISTING_MOUNT_POINT = "mount-point-3:cont" + "/" + RestconfConstants.MOUNT + "/";
51 private static final String TEST_MODULE = "module1/2014-01-01";
52 private static final String TEST_MODULE_BEHIND_MOUNT_POINT = "module1-behind-mount-point/2014-02-03";
53 private static final String NOT_EXISTING_MODULE = "not-existing/2016-01-01";
55 @Rule public ExpectedException thrown = ExpectedException.none();
58 private RestconfSchemaService schemaService;
62 private SchemaContextHandler mockContextHandler;
64 private DOMMountPointServiceHandler mockMountPointHandler;
66 // schema context with modules
67 private SchemaContext schemaContext;
68 // schema context with modules behind mount point
69 private SchemaContext schemaContextBehindMountPoint;
70 // schema context with mount points
71 private SchemaContext schemaContextWithMountPoints;
73 // mount point with schema context with modules behind mount point
74 private DOMMountPoint mountPoint;
75 // mount point with null schema context
76 private DOMMountPoint mountPointWithNullSchemaContext;
77 // mount point service
78 private DOMMountPointService mountPointService;
81 public void setup() throws Exception {
82 MockitoAnnotations.initMocks(this);
84 this.schemaContext = TestRestconfUtils.loadSchemaContext("/modules");
85 this.schemaContextBehindMountPoint = TestRestconfUtils.loadSchemaContext("/modules/modules-behind-mount-point");
86 this.schemaContextWithMountPoints = TestRestconfUtils.loadSchemaContext("/modules/mount-points");
88 // create and register mount points
89 this.mountPoint = SimpleDOMMountPoint.create(
90 YangInstanceIdentifier.of(QName.create("mount:point:1", "2016-01-01", "cont")),
91 ImmutableClassToInstanceMap.copyOf(new HashMap<>()),
92 this.schemaContextBehindMountPoint
95 this.mountPointWithNullSchemaContext = SimpleDOMMountPoint.create(
96 YangInstanceIdentifier.of(QName.create("mount:point:2", "2016-01-01", "cont")),
97 ImmutableClassToInstanceMap.copyOf(new HashMap<>()),
101 this.mountPointService = new DOMMountPointServiceImpl();
102 ((DOMMountPointServiceImpl) this.mountPointService).registerMountPoint(this.mountPoint);
103 ((DOMMountPointServiceImpl) this.mountPointService).registerMountPoint(this.mountPointWithNullSchemaContext);
104 when(this.mockMountPointHandler.get()).thenReturn(this.mountPointService);
106 this.schemaService = new RestconfSchemaServiceImpl(this.mockContextHandler, this.mockMountPointHandler);
110 * Test if service was successfully created.
113 public void schemaServiceImplInitTest() {
114 assertNotNull("Schema service should be initialized and not null", this.schemaService);
118 * Get schema with identifier of existing module and check if correct module was found.
121 public void getSchemaTest() {
122 // prepare conditions - return not-mount point schema context
123 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
126 final SchemaExportContext exportContext = this.schemaService.getSchema(TEST_MODULE);
129 assertNotNull("Export context should not be null", exportContext);
131 final Module module = exportContext.getModule();
132 assertNotNull("Existing module should be found", module);
134 assertEquals("Not expected module name", "module1", module.getName());
135 assertEquals("Not expected module revision", "2014-01-01",
136 SimpleDateFormatUtil.getRevisionFormat().format(module.getRevision()));
137 assertEquals("Not expected module namespace", "module:1", module.getNamespace().toString());
141 * Get schema with identifier of not-existing module. <code>SchemaExportContext</code> is still created, but module
142 * should be set to <code>null</code>.
145 public void getSchemaForNotExistingModuleTest() {
146 // prepare conditions - return not-mount point schema context
147 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
150 final SchemaExportContext exportContext = this.schemaService.getSchema(NOT_EXISTING_MODULE);
153 assertNotNull("Export context should not be null", exportContext);
154 assertNull("Not-existing module should not be found", exportContext.getModule());
158 * Get schema with identifier of existing module behind mount point and check if correct module was found.
161 public void getSchemaMountPointTest() {
162 // prepare conditions - return schema context with mount points
163 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
166 final SchemaExportContext exportContext = this.schemaService.getSchema(MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
169 assertNotNull("Export context should not be null", exportContext);
171 final Module module = exportContext.getModule();
172 assertNotNull("Existing module should be found", module);
174 assertEquals("Not expected module name", "module1-behind-mount-point", module.getName());
175 assertEquals("Not expected module revision", "2014-02-03",
176 SimpleDateFormatUtil.getRevisionFormat().format(module.getRevision()));
177 assertEquals("Not expected module namespace", "module:1:behind:mount:point", module.getNamespace().toString());
181 * Get schema with identifier of not-existing module behind mount point. <code>SchemaExportContext</code> is still
182 * created, but module should be set to <code>null</code>.
185 public void getSchemaForNotExistingModuleMountPointTest() {
186 // prepare conditions - return schema context with mount points
187 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
190 final SchemaExportContext exportContext = this.schemaService.getSchema(MOUNT_POINT + NOT_EXISTING_MODULE);
193 assertNotNull("Export context should not be null", exportContext);
194 assertNull("Not-existing module should not be found", exportContext.getModule());
198 * Try to get schema with <code>null</code> <code>SchemaContext</code> expecting <code>NullPointerException</code>.
201 public void getSchemaWithNullSchemaContextTest() {
202 // prepare conditions - returned schema context is null
203 when(this.mockContextHandler.get()).thenReturn(null);
206 this.thrown.expect(NullPointerException.class);
207 this.schemaService.getSchema(TEST_MODULE);
211 * Try to get schema with <code>null</code> <code>SchemaContext</code> for mount points.
212 * <code>NullPointerException</code> is expected.
215 public void getSchemaWithNullSchemaContextMountPointTest() {
216 // prepare conditions - returned schema context for mount points is null
217 when(this.mockContextHandler.get()).thenReturn(null);
220 this.thrown.expect(NullPointerException.class);
221 this.schemaService.getSchema(MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
225 * Try to get schema with <code>null</code> <code>SchemaContext</code> behind mount point when using
226 * <code>NULL_MOUNT_POINT</code>. Test is expected to fail with <code>NullPointerException</code>.
229 public void getSchemaNullSchemaContextBehindMountPointTest() {
230 // prepare conditions - return correct schema context for mount points (this is not null)
231 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
233 // make test - call service on mount point with null schema context
234 this.thrown.expect(NullPointerException.class);
235 // NULL_MOUNT_POINT contains null schema context
236 this.schemaService.getSchema(NULL_MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);
240 * Try to get schema with null identifier expecting <code>NullPointerException</code>. The same processing is for
241 * server and also for mount point.
244 public void getSchemaWithNullIdentifierTest() {
245 // prepare conditions - return correct schema context
246 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
249 this.thrown.expect(NullPointerException.class);
250 this.schemaService.getSchema(null);
254 * Try to get schema with empty (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
255 * type, error tag and error status code are compared to expected values.
258 public void getSchemaWithEmptyIdentifierTest() {
259 // prepare conditions - return correct schema context
260 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
262 // make test and verify
264 this.schemaService.getSchema("");
265 fail("Test should fail due to invalid identifier");
266 } catch (final RestconfDocumentedException e) {
267 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
268 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
269 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
274 * Try to get schema with empty (not valid) identifier behind mount point catching
275 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
279 public void getSchemaWithEmptyIdentifierMountPointTest() {
280 // prepare conditions - return correct schema context with mount points
281 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
283 // make test and verify
285 this.schemaService.getSchema(MOUNT_POINT + "");
286 fail("Test should fail due to invalid identifier");
287 } catch (final RestconfDocumentedException e) {
288 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
289 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
290 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
295 * Try to get schema with not-parsable identifier catching <code>RestconfDocumentedException</code>. Error type,
296 * error tag and error status code are compared to expected values.
299 public void getSchemaWithNotParsableIdentifierTest() {
300 // prepare conditions - return correct schema context without mount points
301 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
303 // make test and verify
305 this.schemaService.getSchema("01_module/2016-01-01");
306 fail("Test should fail due to invalid identifier");
307 } catch (final RestconfDocumentedException e) {
308 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
309 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
310 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
315 * Try to get schema behind mount point with not-parsable identifier catching
316 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
320 public void getSchemaWithNotParsableIdentifierMountPointTest() {
321 // prepare conditions - return correct schema context with mount points
322 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
324 // make test and verify
326 this.schemaService.getSchema(MOUNT_POINT + "01_module/2016-01-01");
327 fail("Test should fail due to invalid identifier");
328 } catch (final RestconfDocumentedException e) {
329 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
330 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
331 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
336 * Try to get schema with wrong (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
337 * type, error tag and error status code are compared to expected values.
339 * Not valid identifier contains only revision without module name.
342 public void getSchemaWrongIdentifierTest() {
343 // prepare conditions - return correct schema context without mount points
344 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
346 // make test and verify
348 this.schemaService.getSchema("2014-01-01");
349 fail("Test should fail due to invalid identifier");
350 } catch (final RestconfDocumentedException e) {
351 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
352 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
353 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
358 * Try to get schema with wrong (not valid) identifier behind mount point catching
359 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
362 * Not valid identifier contains only revision without module name.
365 public void getSchemaWrongIdentifierMountPointTest() {
366 // prepare conditions - return correct schema context with mount points
367 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
369 // make test and verify
371 this.schemaService.getSchema(MOUNT_POINT + "2014-01-01");
372 fail("Test should fail due to invalid identifier");
373 } catch (final RestconfDocumentedException e) {
374 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
375 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
376 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
381 * Try to get schema with identifier which does not contain revision catching
382 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
386 public void getSchemaWithoutRevisionTest() {
387 // prepare conditions - return correct schema context without mount points
388 when(this.mockContextHandler.get()).thenReturn(this.schemaContext);
390 // make test and verify
392 this.schemaService.getSchema("module");
393 fail("Test should fail due to invalid identifier");
394 } catch (final RestconfDocumentedException e) {
395 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
396 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
397 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
402 * Try to get schema behind mount point with identifier when does not contain revision catching
403 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
407 public void getSchemaWithoutRevisionMountPointTest() {
408 // prepare conditions - return correct schema context with mount points
409 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
411 // make test and verify
413 this.schemaService.getSchema(MOUNT_POINT + "module");
414 fail("Test should fail due to invalid identifier");
415 } catch (final RestconfDocumentedException e) {
416 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
417 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
418 assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
423 * Negative test when mount point module is not found in current <code>SchemaContext</code> for mount points.
424 * <code>IllegalArgumentException</code> exception is expected.
427 public void getSchemaContextWithNotExistingMountPointTest() {
428 // prepare conditions - return schema context with mount points
429 when(this.mockContextHandler.get()).thenReturn(this.schemaContextWithMountPoints);
432 this.thrown.expect(IllegalArgumentException.class);
433 this.schemaService.getSchema(NOT_EXISTING_MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT);