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.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.Revision;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
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 RestconfSchemaServiceTest {
45 // schema context with modules
46 private static final EffectiveModelContext MODEL_CONTEXT =
47 YangParserTestUtils.parseYangResourceDirectory("/modules");
50 private YangTextSourceExtension sourceProvider;
52 private DOMDataBroker dataBroker;
54 private DOMActionService actionService;
56 private DOMRpcService rpcService;
58 private DOMMountPointService mountPointService;
60 private YangTextSource yangSource;
62 private Reader yangReader;
65 private JaxRsRestconf restconf;
68 public void setup() throws Exception {
69 restconf = new JaxRsRestconf(new MdsalRestconfServer(
70 new MdsalDatabindProvider(new FixedDOMSchemaService(() -> MODEL_CONTEXT, sourceProvider)), dataBroker,
71 rpcService, actionService, mountPointService));
75 * Get schema with identifier of existing module and check if correct module was found.
78 public void getSchemaTest() throws Exception {
79 doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
80 .getYangTexttSource(new SourceIdentifier("module1", Revision.of("2014-01-01")));
81 doReturn(yangReader).when(yangSource).openStream();
83 assertSame(yangReader, assertEntity(Reader.class, 200,
84 ar -> restconf.modulesYangGET("module1", "2014-01-01", ar)));
88 * Get schema with identifier of not-existing module. Trying to create <code>SchemaExportContext</code> with
89 * not-existing module should result in error.
92 public void getSchemaForNotExistingModuleTest() {
93 final var error = assertError(ar -> restconf.modulesYinGET("not-existing", "2016-01-01", ar));
94 assertEquals("Source not-existing@2016-01-01 not found", error.getErrorMessage());
95 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
96 assertEquals(ErrorType.APPLICATION, error.getErrorType());
100 * Try to get schema with empty (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
101 * type, error tag and error status code are compared to expected values.
104 public void getSchemaWithEmptyIdentifierTest() {
105 final var error = assertError(ar -> restconf.modulesYangGET("", null, ar));
106 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
107 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
108 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
112 * Try to get schema with not-parsable identifier catching <code>RestconfDocumentedException</code>. Error type,
113 * error tag and error status code are compared to expected values.
116 public void getSchemaWithNotParsableIdentifierTest() {
117 final var error = assertError(ar -> restconf.modulesYangGET("01_module", "2016-01-01", ar));
118 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
119 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
120 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
124 * Try to get schema with wrong (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
125 * type, error tag and error status code are compared to expected values.
128 * Not valid identifier contains only revision without module name.
131 public void getSchemaWrongIdentifierTest() {
132 final var error = assertError(ar -> restconf.modulesYangGET("2014-01-01", null, ar));
133 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
134 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
135 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
139 * Try to get schema with identifier which does not contain revision catching and check if the correct module
143 public void getSchemaWithoutRevisionTest() throws IOException {
144 doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
145 .getYangTexttSource(new SourceIdentifier("module-without-revision", (Revision) null));
146 doReturn(yangReader).when(yangSource).openStream();
147 assertSame(yangReader, assertEntity(Reader.class, 200,
148 ar -> restconf.modulesYangGET("module-without-revision", null, ar)));