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.server.mdsal.MdsalDatabindProvider;
32 import org.opendaylight.restconf.server.mdsal.MdsalRestconfServer;
33 import org.opendaylight.yangtools.yang.common.ErrorTag;
34 import org.opendaylight.yangtools.yang.common.ErrorType;
35 import org.opendaylight.yangtools.yang.common.Revision;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
38 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42 * Unit tests for {@code RestconfSchemaService}.
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class RestconfSchemaServiceTest {
46 // schema context with modules
47 private static final EffectiveModelContext MODEL_CONTEXT =
48 YangParserTestUtils.parseYangResourceDirectory("/modules");
51 private YangTextSourceExtension sourceProvider;
53 private DOMDataBroker dataBroker;
55 private DOMActionService actionService;
57 private DOMRpcService rpcService;
59 private DOMMountPointService mountPointService;
61 private YangTextSource yangSource;
63 private Reader yangReader;
66 private JaxRsRestconf restconf;
69 public void setup() throws Exception {
70 restconf = new JaxRsRestconf(
71 new MdsalRestconfServer(new MdsalDatabindProvider(
72 new FixedDOMSchemaService(() -> MODEL_CONTEXT, sourceProvider)), dataBroker, rpcService, actionService,
74 PrettyPrintParam.FALSE);
78 * Get schema with identifier of existing module and check if correct module was found.
81 public void getSchemaTest() throws Exception {
82 doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
83 .getYangTexttSource(new SourceIdentifier("module1", Revision.of("2014-01-01")));
84 doReturn(yangReader).when(yangSource).openStream();
86 assertSame(yangReader, assertEntity(Reader.class, 200,
87 ar -> restconf.modulesYangGET("module1", "2014-01-01", ar)));
91 * Get schema with identifier of not-existing module. Trying to create <code>SchemaExportContext</code> with
92 * not-existing module should result in error.
95 public void getSchemaForNotExistingModuleTest() {
96 final var error = assertError(ar -> restconf.modulesYinGET("not-existing", "2016-01-01", ar));
97 assertEquals("Source not-existing@2016-01-01 not found", error.getErrorMessage());
98 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
99 assertEquals(ErrorType.APPLICATION, error.getErrorType());
103 * Try to get schema with empty (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
104 * type, error tag and error status code are compared to expected values.
107 public void getSchemaWithEmptyIdentifierTest() {
108 final var error = assertError(ar -> restconf.modulesYangGET("", null, ar));
109 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
110 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
111 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
115 * Try to get schema with not-parsable identifier catching <code>RestconfDocumentedException</code>. Error type,
116 * error tag and error status code are compared to expected values.
119 public void getSchemaWithNotParsableIdentifierTest() {
120 final var error = assertError(ar -> restconf.modulesYangGET("01_module", "2016-01-01", ar));
121 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
122 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
123 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
127 * Try to get schema with wrong (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
128 * type, error tag and error status code are compared to expected values.
131 * Not valid identifier contains only revision without module name.
134 public void getSchemaWrongIdentifierTest() {
135 final var error = assertError(ar -> restconf.modulesYangGET("2014-01-01", null, ar));
136 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
137 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
138 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
142 * Try to get schema with identifier which does not contain revision catching and check if the correct module
146 public void getSchemaWithoutRevisionTest() throws IOException {
147 doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
148 .getYangTexttSource(new SourceIdentifier("module-without-revision", (Revision) null));
149 doReturn(yangReader).when(yangSource).openStream();
150 assertSame(yangReader, assertEntity(Reader.class, 200,
151 ar -> restconf.modulesYangGET("module-without-revision", null, ar)));