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.junit.Assert.assertSame;
12 import static org.mockito.Mockito.doReturn;
13 import static org.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertEntity;
14 import static org.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertError;
16 import com.google.common.util.concurrent.Futures;
17 import java.io.IOException;
18 import java.io.Reader;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.MockitoJUnitRunner;
24 import org.opendaylight.mdsal.dom.api.DOMActionService;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
27 import org.opendaylight.mdsal.dom.api.DOMRpcService;
28 import org.opendaylight.mdsal.dom.api.DOMSchemaService.YangTextSourceExtension;
29 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
30 import org.opendaylight.restconf.api.query.PrettyPrintParam;
31 import org.opendaylight.restconf.nb.rfc8040.ErrorTagMapping;
32 import org.opendaylight.restconf.server.mdsal.MdsalDatabindProvider;
33 import org.opendaylight.restconf.server.mdsal.MdsalRestconfServer;
34 import org.opendaylight.yangtools.yang.common.ErrorTag;
35 import org.opendaylight.yangtools.yang.common.ErrorType;
36 import org.opendaylight.yangtools.yang.common.Revision;
37 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
38 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
39 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43 * Unit tests for {@code RestconfSchemaService}.
45 @RunWith(MockitoJUnitRunner.StrictStubs.class)
46 public class RestconfSchemaServiceTest {
47 // schema context with modules
48 private static final EffectiveModelContext MODEL_CONTEXT =
49 YangParserTestUtils.parseYangResourceDirectory("/modules");
52 private YangTextSourceExtension sourceProvider;
54 private DOMDataBroker dataBroker;
56 private DOMActionService actionService;
58 private DOMRpcService rpcService;
60 private DOMMountPointService mountPointService;
62 private YangTextSource yangSource;
64 private Reader yangReader;
67 private JaxRsRestconf restconf;
70 public void setup() throws Exception {
71 restconf = new JaxRsRestconf(
72 new MdsalRestconfServer(new MdsalDatabindProvider(
73 new FixedDOMSchemaService(() -> MODEL_CONTEXT, sourceProvider)), dataBroker, rpcService, actionService,
75 ErrorTagMapping.RFC8040, PrettyPrintParam.FALSE);
79 * Get schema with identifier of existing module and check if correct module was found.
82 public void getSchemaTest() throws Exception {
83 doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
84 .getYangTexttSource(new SourceIdentifier("module1", Revision.of("2014-01-01")));
85 doReturn(yangReader).when(yangSource).openStream();
87 assertSame(yangReader, assertEntity(Reader.class, 200,
88 ar -> restconf.modulesYangGET("module1", "2014-01-01", ar)));
92 * Get schema with identifier of not-existing module. Trying to create <code>SchemaExportContext</code> with
93 * not-existing module should result in error.
96 public void getSchemaForNotExistingModuleTest() {
97 final var error = assertError(ar -> restconf.modulesYinGET("not-existing", "2016-01-01", ar));
98 assertEquals("Source not-existing@2016-01-01 not found", error.getErrorMessage());
99 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
100 assertEquals(ErrorType.APPLICATION, error.getErrorType());
104 * Try to get schema with empty (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
105 * type, error tag and error status code are compared to expected values.
108 public void getSchemaWithEmptyIdentifierTest() {
109 final var error = assertError(ar -> restconf.modulesYangGET("", null, ar));
110 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
111 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
112 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
116 * Try to get schema with not-parsable identifier catching <code>RestconfDocumentedException</code>. Error type,
117 * error tag and error status code are compared to expected values.
120 public void getSchemaWithNotParsableIdentifierTest() {
121 final var error = assertError(ar -> restconf.modulesYangGET("01_module", "2016-01-01", ar));
122 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
123 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
124 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
128 * Try to get schema with wrong (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
129 * type, error tag and error status code are compared to expected values.
132 * Not valid identifier contains only revision without module name.
135 public void getSchemaWrongIdentifierTest() {
136 final var error = assertError(ar -> restconf.modulesYangGET("2014-01-01", null, ar));
137 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
138 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
139 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
143 * Try to get schema with identifier which does not contain revision catching and check if the correct module
147 public void getSchemaWithoutRevisionTest() throws IOException {
148 doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
149 .getYangTexttSource(new SourceIdentifier("module-without-revision", (Revision) null));
150 doReturn(yangReader).when(yangSource).openStream();
151 assertSame(yangReader, assertEntity(Reader.class, 200,
152 ar -> restconf.modulesYangGET("module-without-revision", null, ar)));