Test revision requirement in schema service
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / jaxrs / RestconfSchemaServiceTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.jaxrs;
9
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;
17
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;
41
42 /**
43  * Unit tests for {@code RestconfSchemaService}.
44  */
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");
50
51     @Mock
52     private DOMSchemaService schemaService;
53     @Mock
54     private YangTextSourceExtension sourceProvider;
55     @Mock
56     private DOMDataBroker dataBroker;
57     @Mock
58     private DOMActionService actionService;
59     @Mock
60     private DOMRpcService rpcService;
61     @Mock
62     private DOMMountPointService mountPointService;
63     @Mock
64     private YangTextSource yangSource;
65     @Mock
66     private Reader yangReader;
67
68     // service under test
69     private JaxRsRestconf restconf;
70
71     @Before
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();
76
77         restconf = new JaxRsRestconf(new MdsalRestconfServer(schemaService, dataBroker, rpcService, actionService,
78             mountPointService));
79     }
80
81     /**
82      * Get schema with identifier of existing module and check if correct module was found.
83      */
84     @Test
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();
89
90         assertSame(yangReader, assertEntity(Reader.class, 200,
91             ar -> restconf.modulesYangGET("module1", "2014-01-01", ar)));
92     }
93
94     /**
95      * Get schema with identifier of not-existing module. Trying to create <code>SchemaExportContext</code> with
96      * not-existing module should result in error.
97      */
98     @Test
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());
104     }
105
106     /**
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.
109      */
110     @Test
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());
116     }
117
118     /**
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.
121      */
122     @Test
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());
128     }
129
130     /**
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.
133      *
134      * <p>
135      * Not valid identifier contains only revision without module name.
136      */
137     @Test
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());
143     }
144
145     /**
146      * Try to get schema with identifier which does not contain revision catching and check if the correct module
147      * was found.
148      */
149     @Test
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)));
156     }
157 }