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.jaxrs;
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertEntity;
12 import static org.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertError;
14 import com.google.common.io.CharStreams;
15 import java.io.Reader;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.junit.MockitoJUnitRunner;
21 import org.opendaylight.mdsal.dom.api.DOMActionService;
22 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
23 import org.opendaylight.mdsal.dom.api.DOMRpcService;
24 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
25 import org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl;
26 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
27 import org.opendaylight.restconf.api.ApiPath;
28 import org.opendaylight.restconf.nb.rfc8040.legacy.ErrorTags;
29 import org.opendaylight.restconf.server.mdsal.MdsalDatabindProvider;
30 import org.opendaylight.restconf.server.mdsal.MdsalRestconfServer;
31 import org.opendaylight.yangtools.yang.common.ErrorTag;
32 import org.opendaylight.yangtools.yang.common.ErrorType;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
37 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
40 * Unit tests for {@code RestconfSchemaService}.
42 @RunWith(MockitoJUnitRunner.StrictStubs.class)
43 public class RestconfSchemaServiceMountTest {
44 private static final ApiPath MOUNT_POINT = AbstractRestconfTest.apiPath("mount-point-1:cont/yang-ext:mount");
45 private static final ApiPath NULL_MOUNT_POINT = AbstractRestconfTest.apiPath("mount-point-2:cont/yang-ext:mount");
46 private static final ApiPath NOT_EXISTING_MOUNT_POINT =
47 AbstractRestconfTest.apiPath("mount-point-3:cont/yang-ext:mount");
49 // schema context with modules behind mount point
50 private static final EffectiveModelContext SCHEMA_CONTEXT_BEHIND_MOUNT_POINT =
51 YangParserTestUtils.parseYangResourceDirectory("/modules/modules-behind-mount-point");
52 // schema context with mount points
53 private static final EffectiveModelContext SCHEMA_CONTEXT_WITH_MOUNT_POINTS =
54 YangParserTestUtils.parseYangResourceDirectory("/modules/mount-points");
58 private DOMDataBroker dataBroker;
60 private DOMActionService actionService;
62 private DOMRpcService rpcService;
64 private YangTextSource yangSource;
66 private Reader yangReader;
69 private JaxRsRestconf restconf;
72 public void setup() throws Exception {
73 final var mountPointService = new DOMMountPointServiceImpl();
74 // create and register mount points
76 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:1", "2016-01-01", "cont")))
77 .addService(DOMSchemaService.class, new FixedDOMSchemaService(SCHEMA_CONTEXT_BEHIND_MOUNT_POINT))
78 .addService(DOMDataBroker.class, dataBroker)
81 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:2", "2016-01-01", "cont")))
84 restconf = new JaxRsRestconf(new MdsalRestconfServer(
85 new MdsalDatabindProvider(new FixedDOMSchemaService(SCHEMA_CONTEXT_WITH_MOUNT_POINTS)), dataBroker,
86 rpcService, actionService, mountPointService));
90 * Get schema with identifier of existing module behind mount point and check if correct module was found.
93 public void getSchemaMountPointTest() throws Exception {
94 final var reader = assertEntity(Reader.class, 200,
95 ar -> restconf.modulesYangGET(MOUNT_POINT, "module1-behind-mount-point", "2014-02-03", ar));
97 module module1-behind-mount-point {
98 namespace module:1:behind:mount:point;
101 rpc rpc-behind-module1;
103 """, CharStreams.toString(reader));
107 * Get schema with identifier of not-existing module behind mount point. Trying to create
108 * <code>SchemaExportContext</code> with not-existing module behind mount point should result in error.
111 public void getSchemaForNotExistingModuleMountPointTest() {
112 final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "not-existing", "2016-01-01", ar));
113 assertEquals("Source not-existing@2016-01-01 not found", error.getErrorMessage());
114 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
115 assertEquals(ErrorType.APPLICATION, error.getErrorType());
119 * Try to get schema with <code>null</code> <code>SchemaContext</code> behind mount point when using
120 * <code>NULL_MOUNT_POINT</code>. Test is expected to fail with <code>NullPointerException</code>.
123 public void getSchemaNullSchemaContextBehindMountPointTest() {
124 // make test - call service on mount point with null schema context
125 // NULL_MOUNT_POINT contains null schema context
126 final var error = assertError(
127 ar -> restconf.modulesYangGET(NULL_MOUNT_POINT, "module1-behind-mount-point", "2014-02-03", ar));
128 assertEquals("Mount point 'mount-point-2:cont' does not expose DOMSchemaService", error.getErrorMessage());
129 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
130 assertEquals(ErrorTags.RESOURCE_DENIED_TRANSPORT, error.getErrorTag());
134 * Try to get schema with empty (not valid) identifier behind mount point catching
135 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
139 public void getSchemaWithEmptyIdentifierMountPointTest() {
140 final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "", null, ar));
141 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
142 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
143 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
147 * Try to get schema behind mount point with not-parsable identifier catching
148 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
152 public void getSchemaWithNotParsableIdentifierMountPointTest() {
153 final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "01_module", "2016-01-01", ar));
154 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
155 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
156 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
160 * Try to get schema with wrong (not valid) identifier behind mount point catching
161 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
165 * Not valid identifier contains only revision without module name.
168 public void getSchemaWrongIdentifierMountPointTest() {
169 final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "2014-01-01", null, ar));
170 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
171 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
172 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
176 * Try to get schema behind mount point with identifier when does not contain revision catching
177 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
181 public void getSchemaWithoutRevisionMountPointTest() {
182 final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "module", null, ar));
183 assertEquals("Source module not found", error.getErrorMessage());
184 assertEquals(ErrorType.APPLICATION, error.getErrorType());
185 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
189 * Negative test when mount point module is not found in current <code>SchemaContext</code> for mount points.
190 * <code>IllegalArgumentException</code> exception is expected.
193 public void getSchemaContextWithNotExistingMountPointTest() {
194 final var error = assertError(
195 ar -> restconf.modulesYangGET(NOT_EXISTING_MOUNT_POINT, "module1-behind-mount-point", "2014-02-03", ar));
196 assertEquals("Failed to lookup for module with name 'mount-point-3'.", error.getErrorMessage());
197 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
198 assertEquals(ErrorTag.UNKNOWN_ELEMENT, error.getErrorTag());