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.api.query.PrettyPrintParam;
29 import org.opendaylight.restconf.nb.rfc8040.legacy.ErrorTags;
30 import org.opendaylight.restconf.server.mdsal.MdsalDatabindProvider;
31 import org.opendaylight.restconf.server.mdsal.MdsalRestconfServer;
32 import org.opendaylight.yangtools.yang.common.ErrorTag;
33 import org.opendaylight.yangtools.yang.common.ErrorType;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
38 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41 * Unit tests for {@code RestconfSchemaService}.
43 @RunWith(MockitoJUnitRunner.StrictStubs.class)
44 public class RestconfSchemaServiceMountTest {
45 private static final ApiPath MOUNT_POINT = AbstractRestconfTest.apiPath("mount-point-1:cont/yang-ext:mount");
46 private static final ApiPath NULL_MOUNT_POINT = AbstractRestconfTest.apiPath("mount-point-2:cont/yang-ext:mount");
47 private static final ApiPath NOT_EXISTING_MOUNT_POINT =
48 AbstractRestconfTest.apiPath("mount-point-3:cont/yang-ext:mount");
50 // schema context with modules behind mount point
51 private static final EffectiveModelContext SCHEMA_CONTEXT_BEHIND_MOUNT_POINT =
52 YangParserTestUtils.parseYangResourceDirectory("/modules/modules-behind-mount-point");
53 // schema context with mount points
54 private static final EffectiveModelContext SCHEMA_CONTEXT_WITH_MOUNT_POINTS =
55 YangParserTestUtils.parseYangResourceDirectory("/modules/mount-points");
59 private DOMDataBroker dataBroker;
61 private DOMActionService actionService;
63 private DOMRpcService rpcService;
65 private YangTextSource yangSource;
67 private Reader yangReader;
70 private JaxRsRestconf restconf;
73 public void setup() throws Exception {
74 final var mountPointService = new DOMMountPointServiceImpl();
75 // create and register mount points
77 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:1", "2016-01-01", "cont")))
78 .addService(DOMSchemaService.class, new FixedDOMSchemaService(SCHEMA_CONTEXT_BEHIND_MOUNT_POINT))
79 .addService(DOMDataBroker.class, dataBroker)
82 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:2", "2016-01-01", "cont")))
85 restconf = new JaxRsRestconf(
86 new MdsalRestconfServer(new MdsalDatabindProvider(
87 new FixedDOMSchemaService(SCHEMA_CONTEXT_WITH_MOUNT_POINTS)), dataBroker, rpcService, actionService,
89 PrettyPrintParam.FALSE);
93 * Get schema with identifier of existing module behind mount point and check if correct module was found.
96 public void getSchemaMountPointTest() throws Exception {
97 final var reader = assertEntity(Reader.class, 200,
98 ar -> restconf.modulesYangGET(MOUNT_POINT, "module1-behind-mount-point", "2014-02-03", ar));
100 module module1-behind-mount-point {
101 namespace module:1:behind:mount:point;
104 rpc rpc-behind-module1;
106 """, CharStreams.toString(reader));
110 * Get schema with identifier of not-existing module behind mount point. Trying to create
111 * <code>SchemaExportContext</code> with not-existing module behind mount point should result in error.
114 public void getSchemaForNotExistingModuleMountPointTest() {
115 final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "not-existing", "2016-01-01", ar));
116 assertEquals("Source not-existing@2016-01-01 not found", error.getErrorMessage());
117 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
118 assertEquals(ErrorType.APPLICATION, error.getErrorType());
122 * Try to get schema with <code>null</code> <code>SchemaContext</code> behind mount point when using
123 * <code>NULL_MOUNT_POINT</code>. Test is expected to fail with <code>NullPointerException</code>.
126 public void getSchemaNullSchemaContextBehindMountPointTest() {
127 // make test - call service on mount point with null schema context
128 // NULL_MOUNT_POINT contains null schema context
129 final var error = assertError(
130 ar -> restconf.modulesYangGET(NULL_MOUNT_POINT, "module1-behind-mount-point", "2014-02-03", ar));
131 assertEquals("Mount point 'mount-point-2:cont' does not expose DOMSchemaService", error.getErrorMessage());
132 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
133 assertEquals(ErrorTags.RESOURCE_DENIED_TRANSPORT, error.getErrorTag());
137 * Try to get schema with empty (not valid) identifier behind mount point catching
138 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
142 public void getSchemaWithEmptyIdentifierMountPointTest() {
143 final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "", null, ar));
144 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
145 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
146 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
150 * Try to get schema behind mount point with not-parsable identifier catching
151 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
155 public void getSchemaWithNotParsableIdentifierMountPointTest() {
156 final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "01_module", "2016-01-01", ar));
157 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
158 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
159 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
163 * Try to get schema with wrong (not valid) identifier behind mount point catching
164 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
168 * Not valid identifier contains only revision without module name.
171 public void getSchemaWrongIdentifierMountPointTest() {
172 final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "2014-01-01", null, ar));
173 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
174 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
175 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
179 * Try to get schema behind mount point with identifier when does not contain revision catching
180 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
184 public void getSchemaWithoutRevisionMountPointTest() {
185 final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "module", null, ar));
186 assertEquals("Source module not found", error.getErrorMessage());
187 assertEquals(ErrorType.APPLICATION, error.getErrorType());
188 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
192 * Negative test when mount point module is not found in current <code>SchemaContext</code> for mount points.
193 * <code>IllegalArgumentException</code> exception is expected.
196 public void getSchemaContextWithNotExistingMountPointTest() {
197 final var error = assertError(
198 ar -> restconf.modulesYangGET(NOT_EXISTING_MOUNT_POINT, "module1-behind-mount-point", "2014-02-03", ar));
199 assertEquals("Failed to lookup for module with name 'mount-point-3'.", error.getErrorMessage());
200 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
201 assertEquals(ErrorTag.UNKNOWN_ELEMENT, error.getErrorTag());