Bump upstreams
[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.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;
40
41 /**
42  * Unit tests for {@code RestconfSchemaService}.
43  */
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");
49
50     @Mock
51     private DOMSchemaService schemaService;
52     @Mock
53     private YangTextSourceExtension sourceProvider;
54     @Mock
55     private DOMDataBroker dataBroker;
56     @Mock
57     private DOMActionService actionService;
58     @Mock
59     private DOMRpcService rpcService;
60     @Mock
61     private DOMMountPointService mountPointService;
62     @Mock
63     private YangTextSource yangSource;
64     @Mock
65     private Reader yangReader;
66
67     // service under test
68     private JaxRsRestconf restconf;
69
70     @Before
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();
75
76         restconf = new JaxRsRestconf(new MdsalRestconfServer(schemaService, dataBroker, rpcService, actionService,
77             mountPointService));
78     }
79
80     /**
81      * Get schema with identifier of existing module and check if correct module was found.
82      */
83     @Test
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();
88
89         assertSame(yangReader, assertEntity(Reader.class, 200,
90             ar -> restconf.modulesYangGET("module1", "2014-01-01", ar)));
91     }
92
93     /**
94      * Get schema with identifier of not-existing module. Trying to create <code>SchemaExportContext</code> with
95      * not-existing module should result in error.
96      */
97     @Test
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());
103     }
104
105     /**
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.
108      */
109     @Test
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());
115     }
116
117     /**
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.
120      */
121     @Test
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());
127     }
128
129     /**
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.
132      *
133      * <p>
134      * Not valid identifier contains only revision without module name.
135      */
136     @Test
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());
142     }
143
144     /**
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
147      * values.
148      */
149     @Test
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());
155     }
156 }