2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.rfc8040.databind.jaxrs;
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;
19 import java.util.List;
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.ContentParam;
32 import org.opendaylight.restconf.nb.rfc8040.DepthParam;
33 import org.opendaylight.restconf.nb.rfc8040.ReadDataParams;
34 import org.opendaylight.restconf.nb.rfc8040.WithDefaultsParam;
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;
43 @RunWith(MockitoJUnitRunner.StrictStubs.class)
44 public class QueryParamsTest {
46 public InstanceIdentifierContext<ContainerSchemaNode> context;
48 public UriInfo uriInfo;
50 public EffectiveModelContext modelContext;
52 public ContainerSchemaNode containerSchema;
54 public LeafSchemaNode containerChildSchema;
57 * Test when parameter is present at most once.
60 public void getSingleParameterTest() {
61 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
62 parameters.putSingle(ContentParam.uriName(), "all");
63 assertEquals("all", QueryParams.getSingleParameter(parameters, ContentParam.uriName()));
67 * Test when parameter is present more than once.
70 public void getSingleParameterNegativeTest() {
71 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
72 parameters.put(ContentParam.uriName(), List.of("config", "nonconfig", "all"));
74 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
75 () -> QueryParams.getSingleParameter(parameters, ContentParam.uriName()));
76 final List<RestconfError> errors = ex.getErrors();
77 assertEquals(1, errors.size());
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());
85 * Test when all parameters are allowed.
88 public void checkParametersTypesTest() {
89 QueryParams.checkParametersTypes(Set.of("content"),
90 Set.of(ContentParam.uriName(), DepthParam.uriName()));
94 * Test when not allowed parameter type is used.
97 public void checkParametersTypesNegativeTest() {
98 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
99 () -> QueryParams.checkParametersTypes(Set.of("not-allowed-parameter"),
100 Set.of(ContentParam.uriName(), DepthParam.uriName())));
101 final List<RestconfError> errors = ex.getErrors();
102 assertEquals(1, errors.size());
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());
110 * Test of parsing default parameters from URI request.
113 public void parseUriParametersDefaultTest() {
114 // no parameters, default values should be used
115 mockQueryParameters(new MultivaluedHashMap<String, String>());
117 final var parsedParameters = QueryParams.newReadDataParams(uriInfo);
118 assertEquals(ContentParam.ALL, parsedParameters.content());
119 assertNull(parsedParameters.depth());
120 assertNull(parsedParameters.fields());
124 * Testing parsing of with-defaults parameter which value which is not supported.
127 public void parseUriParametersWithDefaultInvalidTest() {
128 // preparation of input data
129 mockQueryParameter("with-defaults", "invalid");
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());
139 * Negative test of parsing request URI parameters when depth parameter has not allowed value.
142 public void parseUriParametersDepthParameterNegativeTest() {
143 // inserted value is not allowed
144 mockQueryParameter("depth", "bounded");
146 RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
147 () -> QueryParams.newReadDataParams(uriInfo));
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());
154 * Negative test of parsing request URI parameters when content parameter has not allowed value.
157 public void parseUriParametersContentParameterNegativeTest() {
158 mockQueryParameter("content", "not-allowed-parameter-value");
160 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
161 () -> QueryParams.newReadDataParams(uriInfo));
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());
168 * Negative test of parsing request URI parameters when depth parameter has not allowed value (more than maximum).
171 public void parseUriParametersDepthMaximalParameterNegativeTest() {
172 // inserted value is too high
173 mockQueryParameter("depth", "65536");
175 RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
176 () -> QueryParams.newReadDataParams(uriInfo));
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());
183 * Negative test of parsing request URI parameters when depth parameter has not allowed value (less than minimum).
186 public void parseUriParametersDepthMinimalParameterNegativeTest() {
187 // inserted value is too low
188 mockQueryParameter("depth", "0");
190 RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
191 () -> QueryParams.newReadDataParams(uriInfo));
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());
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}.
202 public void parseUriParametersWithDefaultAndTaggedTest() {
203 // preparation of input data
204 mockQueryParameter("with-defaults", "report-all-tagged");
206 final var parsedParameters = QueryParams.newReadDataParams(uriInfo);
207 assertNull(parsedParameters.withDefaults());
208 assertTrue(parsedParameters.tagged());
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}.
216 public void parseUriParametersWithDefaultAndReportAllTest() {
217 // preparation of input data
218 mockQueryParameter("with-defaults", "report-all");
220 final var parsedParameters = QueryParams.newReadDataParams(uriInfo);
221 assertNull(parsedParameters.withDefaults());
222 assertFalse(parsedParameters.tagged());
226 * Testing parsing of with-defaults parameter which value doesn't match report-all or report-all-tagged patterns
227 * - non-reporting setting.
230 public void parseUriParametersWithDefaultAndNonTaggedTest() {
231 // preparation of input data
232 mockQueryParameter("with-defaults", "explicit");
234 final var parsedParameters = QueryParams.newReadDataParams(uriInfo);
235 assertSame(WithDefaultsParam.EXPLICIT, parsedParameters.withDefaults());
236 assertFalse(parsedParameters.tagged());
240 * Test of parsing user defined parameters from URI request.
243 public void parseUriParametersUserDefinedTest() {
244 final QName containerChild = QName.create("ns", "container-child");
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);
252 final ReadDataParams parsedParameters = QueryParams.newReadDataParams(uriInfo);
254 assertEquals(ContentParam.CONFIG, parsedParameters.content());
257 final DepthParam depth = parsedParameters.depth();
258 assertNotNull(depth);
259 assertEquals(10, depth.value());
262 assertNotNull(parsedParameters.fields());
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();
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));
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);
284 private void mockQueryParameters(final MultivaluedMap<String, String> parameters) {
285 doReturn(parameters).when(uriInfo).getQueryParameters();