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.Reader;
20 import java.util.List;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.mdsal.dom.api.DOMActionService;
27 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
28 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
29 import org.opendaylight.mdsal.dom.api.DOMRpcService;
30 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
31 import org.opendaylight.mdsal.dom.api.DOMSchemaService.YangTextSourceExtension;
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 SCHEMA_CONTEXT =
48 YangParserTestUtils.parseYangResourceDirectory("/modules");
51 private DOMSchemaService schemaService;
53 private YangTextSourceExtension sourceProvider;
55 private DOMDataBroker dataBroker;
57 private DOMActionService actionService;
59 private DOMRpcService rpcService;
61 private DOMMountPointService mountPointService;
63 private YangTextSource yangSource;
65 private Reader yangReader;
68 private JaxRsRestconf restconf;
71 public void setup() throws Exception {
72 doReturn(SCHEMA_CONTEXT).when(schemaService).getGlobalContext();
73 doCallRealMethod().when(schemaService).extension(any());
74 doReturn(List.of(sourceProvider)).when(schemaService).supportedExtensions();
76 restconf = new JaxRsRestconf(new MdsalRestconfServer(schemaService, dataBroker, rpcService, actionService,
81 * Get schema with identifier of existing module and check if correct module was found.
84 public void getSchemaTest() throws Exception {
85 doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
86 .getYangTexttSource(new SourceIdentifier("module1", Revision.of("2014-01-01")));
87 doReturn(yangReader).when(yangSource).openStream();
89 assertSame(yangReader, assertEntity(Reader.class, 200,
90 ar -> restconf.modulesYangGET("module1", "2014-01-01", ar)));
94 * Get schema with identifier of not-existing module. Trying to create <code>SchemaExportContext</code> with
95 * not-existing module should result in error.
98 public void getSchemaForNotExistingModuleTest() {
99 final var error = assertError(ar -> restconf.modulesYinGET("not-existing", "2016-01-01", ar));
100 assertEquals("Source not-existing@2016-01-01 not found", error.getErrorMessage());
101 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
102 assertEquals(ErrorType.APPLICATION, error.getErrorType());
106 * Try to get schema with empty (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
107 * type, error tag and error status code are compared to expected values.
110 public void getSchemaWithEmptyIdentifierTest() {
111 final var error = assertError(ar -> restconf.modulesYangGET("", null, ar));
112 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
113 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
114 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
118 * Try to get schema with not-parsable identifier catching <code>RestconfDocumentedException</code>. Error type,
119 * error tag and error status code are compared to expected values.
122 public void getSchemaWithNotParsableIdentifierTest() {
123 final var error = assertError(ar -> restconf.modulesYangGET("01_module", "2016-01-01", ar));
124 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
125 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
126 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
130 * Try to get schema with wrong (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
131 * type, error tag and error status code are compared to expected values.
134 * Not valid identifier contains only revision without module name.
137 public void getSchemaWrongIdentifierTest() {
138 final var error = assertError(ar -> restconf.modulesYangGET("2014-01-01", null, ar));
139 assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
140 assertEquals(ErrorType.PROTOCOL, error.getErrorType());
141 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
145 * Try to get schema with identifier which does not contain revision catching
146 * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
150 public void getSchemaWithoutRevisionTest() {
151 final var error = assertError(ar -> restconf.modulesYinGET("module", null, ar));
152 assertEquals("Source module not found", error.getErrorMessage());
153 assertEquals(ErrorType.APPLICATION, error.getErrorType());
154 assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());