Rework body formatting wiring
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / jaxrs / RestconfSchemaServiceMountTest.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.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertEntity;
12 import static org.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertError;
13
14 import com.google.common.io.CharStreams;
15 import java.io.Reader;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.junit.MockitoJUnitRunner;
21 import org.opendaylight.mdsal.dom.api.DOMActionService;
22 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
23 import org.opendaylight.mdsal.dom.api.DOMRpcService;
24 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
25 import org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl;
26 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
27 import org.opendaylight.restconf.api.ApiPath;
28 import org.opendaylight.restconf.api.query.PrettyPrintParam;
29 import org.opendaylight.restconf.nb.rfc8040.legacy.ErrorTags;
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.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
38 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39
40 /**
41  * Unit tests for {@code RestconfSchemaService}.
42  */
43 @RunWith(MockitoJUnitRunner.StrictStubs.class)
44 public class RestconfSchemaServiceMountTest {
45     private static final ApiPath MOUNT_POINT = AbstractRestconfTest.apiPath("mount-point-1:cont/yang-ext:mount");
46     private static final ApiPath NULL_MOUNT_POINT = AbstractRestconfTest.apiPath("mount-point-2:cont/yang-ext:mount");
47     private static final ApiPath NOT_EXISTING_MOUNT_POINT =
48         AbstractRestconfTest.apiPath("mount-point-3:cont/yang-ext:mount");
49
50     // schema context with modules behind mount point
51     private static final EffectiveModelContext SCHEMA_CONTEXT_BEHIND_MOUNT_POINT =
52         YangParserTestUtils.parseYangResourceDirectory("/modules/modules-behind-mount-point");
53     // schema context with mount points
54     private static final EffectiveModelContext SCHEMA_CONTEXT_WITH_MOUNT_POINTS =
55         YangParserTestUtils.parseYangResourceDirectory("/modules/mount-points");
56
57     // handlers
58     @Mock
59     private DOMDataBroker dataBroker;
60     @Mock
61     private DOMActionService actionService;
62     @Mock
63     private DOMRpcService rpcService;
64     @Mock
65     private YangTextSource yangSource;
66     @Mock
67     private Reader yangReader;
68
69     // service under test
70     private JaxRsRestconf restconf;
71
72     @Before
73     public void setup() throws Exception {
74         final var mountPointService = new DOMMountPointServiceImpl();
75         // create and register mount points
76         mountPointService
77                 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:1", "2016-01-01", "cont")))
78                 .addService(DOMSchemaService.class, new FixedDOMSchemaService(SCHEMA_CONTEXT_BEHIND_MOUNT_POINT))
79                 .addService(DOMDataBroker.class, dataBroker)
80                 .register();
81         mountPointService
82                 .createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:2", "2016-01-01", "cont")))
83                 .register();
84
85         restconf = new JaxRsRestconf(
86             new MdsalRestconfServer(new MdsalDatabindProvider(
87                 new FixedDOMSchemaService(SCHEMA_CONTEXT_WITH_MOUNT_POINTS)), dataBroker, rpcService, actionService,
88                 mountPointService),
89             PrettyPrintParam.FALSE);
90     }
91
92     /**
93      * Get schema with identifier of existing module behind mount point and check if correct module was found.
94      */
95     @Test
96     public void getSchemaMountPointTest() throws Exception {
97         final var reader = assertEntity(Reader.class, 200,
98             ar -> restconf.modulesYangGET(MOUNT_POINT, "module1-behind-mount-point", "2014-02-03", ar));
99         assertEquals("""
100             module module1-behind-mount-point {
101               namespace module:1:behind:mount:point;
102               prefix mod1bemopo;
103               revision 2014-02-03;
104               rpc rpc-behind-module1;
105             }
106             """, CharStreams.toString(reader));
107     }
108
109     /**
110      * Get schema with identifier of not-existing module behind mount point. Trying to create
111      * <code>SchemaExportContext</code> with not-existing module behind mount point should result in error.
112      */
113     @Test
114     public void getSchemaForNotExistingModuleMountPointTest() {
115         final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "not-existing", "2016-01-01", ar));
116         assertEquals("Source not-existing@2016-01-01 not found", error.getErrorMessage());
117         assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
118         assertEquals(ErrorType.APPLICATION, error.getErrorType());
119     }
120
121     /**
122      * Try to get schema with <code>null</code> <code>SchemaContext</code> behind mount point when using
123      * <code>NULL_MOUNT_POINT</code>. Test is expected to fail with <code>NullPointerException</code>.
124      */
125     @Test
126     public void getSchemaNullSchemaContextBehindMountPointTest() {
127         // make test - call service on mount point with null schema context
128         // NULL_MOUNT_POINT contains null schema context
129         final var error = assertError(
130             ar -> restconf.modulesYangGET(NULL_MOUNT_POINT, "module1-behind-mount-point", "2014-02-03", ar));
131         assertEquals("Mount point 'mount-point-2:cont' does not expose DOMSchemaService", error.getErrorMessage());
132         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
133         assertEquals(ErrorTags.RESOURCE_DENIED_TRANSPORT, error.getErrorTag());
134     }
135
136     /**
137      * Try to get schema with empty (not valid) identifier behind mount point catching
138      * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
139      * values.
140      */
141     @Test
142     public void getSchemaWithEmptyIdentifierMountPointTest() {
143         final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "", null, ar));
144         assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
145         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
146         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
147     }
148
149     /**
150      * Try to get schema behind mount point with not-parsable identifier catching
151      * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
152      * values.
153      */
154     @Test
155     public void getSchemaWithNotParsableIdentifierMountPointTest() {
156         final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "01_module", "2016-01-01", ar));
157         assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
158         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
159         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
160     }
161
162     /**
163      * Try to get schema with wrong (not valid) identifier behind mount point catching
164      * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
165      * values.
166      *
167      * <p>
168      * Not valid identifier contains only revision without module name.
169      */
170     @Test
171     public void getSchemaWrongIdentifierMountPointTest() {
172         final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "2014-01-01", null, ar));
173         assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
174         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
175         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
176     }
177
178     /**
179      * Try to get schema behind mount point with identifier when does not contain revision catching
180      * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
181      * values.
182      */
183     @Test
184     public void getSchemaWithoutRevisionMountPointTest() {
185         final var error = assertError(ar -> restconf.modulesYangGET(MOUNT_POINT, "module", null, ar));
186         assertEquals("Source module not found", error.getErrorMessage());
187         assertEquals(ErrorType.APPLICATION, error.getErrorType());
188         assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
189     }
190
191     /**
192      * Negative test when mount point module is not found in current <code>SchemaContext</code> for mount points.
193      * <code>IllegalArgumentException</code> exception is expected.
194      */
195     @Test
196     public void getSchemaContextWithNotExistingMountPointTest() {
197         final var error = assertError(
198             ar -> restconf.modulesYangGET(NOT_EXISTING_MOUNT_POINT, "module1-behind-mount-point", "2014-02-03", ar));
199         assertEquals("Failed to lookup for module with name 'mount-point-3'.", error.getErrorMessage());
200         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
201         assertEquals(ErrorTag.UNKNOWN_ELEMENT, error.getErrorTag());
202     }
203 }