Separate out ReadDataParams
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / databind / jaxrs / QueryParamsTest.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.rfc8040.databind.jaxrs;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.assertThrows;
16 import static org.junit.Assert.assertTrue;
17 import static org.mockito.Mockito.doReturn;
18
19 import java.util.List;
20 import java.util.Set;
21 import javax.ws.rs.core.MultivaluedHashMap;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.core.UriInfo;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.restconf.common.errors.RestconfError;
31 import org.opendaylight.restconf.nb.rfc8040.ContentParameter;
32 import org.opendaylight.restconf.nb.rfc8040.DepthParameter;
33 import org.opendaylight.restconf.nb.rfc8040.ReadDataParams;
34 import org.opendaylight.restconf.nb.rfc8040.WithDefaultsParameter;
35 import org.opendaylight.restconf.nb.rfc8040.legacy.QueryParameters;
36 import org.opendaylight.yangtools.yang.common.ErrorTag;
37 import org.opendaylight.yangtools.yang.common.ErrorType;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
41 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
42
43 @RunWith(MockitoJUnitRunner.StrictStubs.class)
44 public class QueryParamsTest {
45     @Mock
46     public InstanceIdentifierContext<ContainerSchemaNode> context;
47     @Mock
48     public UriInfo uriInfo;
49     @Mock
50     public EffectiveModelContext modelContext;
51     @Mock
52     public ContainerSchemaNode containerSchema;
53     @Mock
54     public LeafSchemaNode containerChildSchema;
55
56     /**
57      * Test when parameter is present at most once.
58      */
59     @Test
60     public void getSingleParameterTest() {
61         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
62         parameters.putSingle(ContentParameter.uriName(), "all");
63         assertEquals("all", QueryParams.getSingleParameter(parameters, ContentParameter.uriName()));
64     }
65
66     /**
67      * Test when parameter is present more than once.
68      */
69     @Test
70     public void getSingleParameterNegativeTest() {
71         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
72         parameters.put(ContentParameter.uriName(), List.of("config", "nonconfig", "all"));
73
74         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
75             () -> QueryParams.getSingleParameter(parameters, ContentParameter.uriName()));
76         final List<RestconfError> errors = ex.getErrors();
77         assertEquals(1, errors.size());
78
79         final RestconfError error = errors.get(0);
80         assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
81         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());
82     }
83
84     /**
85      * Test when all parameters are allowed.
86      */
87     @Test
88     public void checkParametersTypesTest() {
89         QueryParams.checkParametersTypes(Set.of("content"),
90             Set.of(ContentParameter.uriName(), DepthParameter.uriName()));
91     }
92
93     /**
94      * Test when not allowed parameter type is used.
95      */
96     @Test
97     public void checkParametersTypesNegativeTest() {
98         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
99             () -> QueryParams.checkParametersTypes(Set.of("not-allowed-parameter"),
100                 Set.of(ContentParameter.uriName(), DepthParameter.uriName())));
101         final List<RestconfError> errors = ex.getErrors();
102         assertEquals(1, errors.size());
103
104         final RestconfError error = errors.get(0);
105         assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
106         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());
107     }
108
109     /**
110      * Test of parsing default parameters from URI request.
111      */
112     @Test
113     public void parseUriParametersDefaultTest() {
114         // no parameters, default values should be used
115         mockQueryParameters(new MultivaluedHashMap<String, String>());
116
117         final var parsedParameters = QueryParams.newReadDataParams(uriInfo);
118         assertEquals(ContentParameter.ALL, parsedParameters.content());
119         assertNull(parsedParameters.depth());
120         assertNull(parsedParameters.fields());
121     }
122
123     /**
124      * Testing parsing of with-defaults parameter which value which is not supported.
125      */
126     @Test
127     public void parseUriParametersWithDefaultInvalidTest() {
128         // preparation of input data
129         mockQueryParameter("with-defaults", "invalid");
130
131         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
132             () -> QueryParams.newReadDataParams(uriInfo));
133         final List<RestconfError> errors = ex.getErrors();
134         assertEquals(1, errors.size());
135         assertEquals(ErrorTag.INVALID_VALUE, errors.get(0).getErrorTag());
136     }
137
138     /**
139      * Negative test of parsing request URI parameters when depth parameter has not allowed value.
140      */
141     @Test
142     public void parseUriParametersDepthParameterNegativeTest() {
143         // inserted value is not allowed
144         mockQueryParameter("depth", "bounded");
145
146         RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
147             () -> QueryParams.newReadDataParams(uriInfo));
148         // Bad request
149         assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
150         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
151     }
152
153     /**
154      * Negative test of parsing request URI parameters when content parameter has not allowed value.
155      */
156     @Test
157     public void parseUriParametersContentParameterNegativeTest() {
158         mockQueryParameter("content", "not-allowed-parameter-value");
159
160         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
161             () -> QueryParams.newReadDataParams(uriInfo));
162         // Bad request
163         assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
164         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
165     }
166
167     /**
168      * Negative test of parsing request URI parameters when depth parameter has not allowed value (more than maximum).
169      */
170     @Test
171     public void parseUriParametersDepthMaximalParameterNegativeTest() {
172         // inserted value is too high
173         mockQueryParameter("depth", "65536");
174
175         RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
176             () -> QueryParams.newReadDataParams(uriInfo));
177         // Bad request
178         assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
179         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
180     }
181
182     /**
183      * Negative test of parsing request URI parameters when depth parameter has not allowed value (less than minimum).
184      */
185     @Test
186     public void parseUriParametersDepthMinimalParameterNegativeTest() {
187         // inserted value is too low
188         mockQueryParameter("depth", "0");
189
190         RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
191             () -> QueryParams.newReadDataParams(uriInfo));
192         // Bad request
193         assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
194         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
195     }
196
197     /**
198      * Testing parsing of with-defaults parameter which value matches 'report-all-tagged' setting - default value should
199      * be set to {@code null} and tagged flag should be set to {@code true}.
200      */
201     @Test
202     public void parseUriParametersWithDefaultAndTaggedTest() {
203         // preparation of input data
204         mockQueryParameter("with-defaults", "report-all-tagged");
205
206         final var parsedParameters = QueryParams.newReadDataParams(uriInfo);
207         assertNull(parsedParameters.withDefaults());
208         assertTrue(parsedParameters.tagged());
209     }
210
211     /**
212      * Testing parsing of with-defaults parameter which value matches 'report-all' setting - default value should
213      * be set to {@code null} and tagged flag should be set to {@code false}.
214      */
215     @Test
216     public void parseUriParametersWithDefaultAndReportAllTest() {
217         // preparation of input data
218         mockQueryParameter("with-defaults", "report-all");
219
220         final var parsedParameters = QueryParams.newReadDataParams(uriInfo);
221         assertNull(parsedParameters.withDefaults());
222         assertFalse(parsedParameters.tagged());
223     }
224
225     /**
226      * Testing parsing of with-defaults parameter which value doesn't match report-all or report-all-tagged patterns
227      * - non-reporting setting.
228      */
229     @Test
230     public void parseUriParametersWithDefaultAndNonTaggedTest() {
231         // preparation of input data
232         mockQueryParameter("with-defaults", "explicit");
233
234         final var parsedParameters = QueryParams.newReadDataParams(uriInfo);
235         assertSame(WithDefaultsParameter.EXPLICIT, parsedParameters.withDefaults());
236         assertFalse(parsedParameters.tagged());
237     }
238
239     /**
240      * Test of parsing user defined parameters from URI request.
241      */
242     @Test
243     public void parseUriParametersUserDefinedTest() {
244         final QName containerChild = QName.create("ns", "container-child");
245
246         final MultivaluedMap<String, String> parameters = new MultivaluedHashMap<>();
247         parameters.putSingle("content", "config");
248         parameters.putSingle("depth", "10");
249         parameters.putSingle("fields", "container-child");
250         mockQueryParameters(parameters);
251
252         final ReadDataParams parsedParameters = QueryParams.newReadDataParams(uriInfo);
253         // content
254         assertEquals(ContentParameter.CONFIG, parsedParameters.content());
255
256         // depth
257         final DepthParameter depth = parsedParameters.depth();
258         assertNotNull(depth);
259         assertEquals(10, depth.value());
260
261         // fields
262         assertNotNull(parsedParameters.fields());
263
264         // fields for write filtering
265         doReturn(QName.create(containerChild, "container")).when(containerSchema).getQName();
266         doReturn(containerChildSchema).when(containerSchema).dataChildByName(containerChild);
267         doReturn(containerChild).when(containerChildSchema).getQName();
268         doReturn(modelContext).when(context).getSchemaContext();
269         doReturn(containerSchema).when(context).getSchemaNode();
270
271         final QueryParameters queryParameters = QueryParams.newQueryParameters(parsedParameters, context);
272         final List<Set<QName>> fields = queryParameters.fields();
273         assertNotNull(fields);
274         assertEquals(1, fields.size());
275         assertEquals(Set.of(containerChild), fields.get(0));
276     }
277
278     private void mockQueryParameter(final String name, final String value) {
279         final MultivaluedMap<String, String> parameters = new MultivaluedHashMap<>();
280         parameters.putSingle(name, value);
281         mockQueryParameters(parameters);
282     }
283
284     private void mockQueryParameters(final MultivaluedMap<String, String> parameters) {
285         doReturn(parameters).when(uriInfo).getQueryParameters();
286     }
287 }