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.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doCallRealMethod;
14 import static org.mockito.Mockito.doReturn;
15 import static org.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertEntity;
16 import static org.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertError;
18 import com.google.common.util.concurrent.Futures;
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.opendaylight.mdsal.dom.api.DOMActionService;
28 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
29 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
30 import org.opendaylight.mdsal.dom.api.DOMRpcService;
31 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
32 import org.opendaylight.mdsal.dom.api.DOMSchemaService.YangTextSourceExtension;
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 SCHEMA_CONTEXT =
49 YangParserTestUtils.parseYangResourceDirectory("/modules");
52 private DOMSchemaService schemaService;
54 private YangTextSourceExtension sourceProvider;
56 private DOMDataBroker dataBroker;
58 private DOMActionService actionService;
60 private DOMRpcService rpcService;
62 private DOMMountPointService mountPointService;
64 private YangTextSource yangSource;
66 private Reader yangReader;
69 private JaxRsRestconf restconf;
72 public void setup() throws Exception {
73 doReturn(SCHEMA_CONTEXT).when(schemaService).getGlobalContext();
74 doCallRealMethod().when(schemaService).extension(any());
75 doReturn(List.of(sourceProvider)).when(schemaService).supportedExtensions();
77 restconf = new JaxRsRestconf(new MdsalRestconfServer(schemaService, dataBroker, rpcService, actionService,
82 * Get schema with identifier of existing module and check if correct module was found.
85 public void getSchemaTest() throws Exception {
86 doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
87 .getYangTexttSource(new SourceIdentifier("module1", Revision.of("2014-01-01")));
88 doReturn(yangReader).when(yangSource).openStream();
90 assertSame(yangReader, assertEntity(Reader.class, 200,
91 ar -> restconf.modulesYangGET("module1", "2014-01-01", ar)));
95 * Get schema with identifier of not-existing module. Trying to create <code>SchemaExportContext</code> with
96 * not-existing module should result in error.
99 public void getSchemaForNotExistingModuleTest() {
100 final var error = assertError(ar -> restconf.modulesYinGET("not-existing", "2016-01-01", ar));
101 assertEquals("Source not-existing@2016-01-01 not found", error.getErrorMessage());
102 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
103 assertEquals(ErrorType.APPLICATION, error.getErrorType());
107 * Try to get schema with empty (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
108 * type, error tag and error status code are compared to expected values.
111 public void getSchemaWithEmptyIdentifierTest() {
112 final var error = assertError(ar -> restconf.modulesYangGET("", null, ar));
113 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
114 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
115 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
119 * Try to get schema with not-parsable identifier catching <code>RestconfDocumentedException</code>. Error type,
120 * error tag and error status code are compared to expected values.
123 public void getSchemaWithNotParsableIdentifierTest() {
124 final var error = assertError(ar -> restconf.modulesYangGET("01_module", "2016-01-01", ar));
125 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
126 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
127 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
131 * Try to get schema with wrong (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
132 * type, error tag and error status code are compared to expected values.
135 * Not valid identifier contains only revision without module name.
138 public void getSchemaWrongIdentifierTest() {
139 final var error = assertError(ar -> restconf.modulesYangGET("2014-01-01", null, ar));
140 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
141 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
142 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
146 * Try to get schema with identifier which does not contain revision catching and check if the correct module
150 public void getSchemaWithoutRevisionTest() throws IOException {
151 doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
152 .getYangTexttSource(new SourceIdentifier("module-without-revision", (Revision) null));
153 doReturn(yangReader).when(yangSource).openStream();
154 assertSame(yangReader, assertEntity(Reader.class, 200,
155 ar -> restconf.modulesYangGET("module-without-revision", null, ar)));