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.assertThrows;
13 import java.util.List;
15 import javax.ws.rs.core.MultivaluedHashMap;
16 import org.junit.Test;
17 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.restconf.common.errors.RestconfError;
19 import org.opendaylight.restconf.nb.rfc8040.ContentParameter;
20 import org.opendaylight.restconf.nb.rfc8040.DepthParameter;
21 import org.opendaylight.yangtools.yang.common.ErrorTag;
22 import org.opendaylight.yangtools.yang.common.ErrorType;
24 public class QueryParamsTest {
26 * Test when parameter is present at most once.
29 public void getSingleParameterTest() {
30 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
31 parameters.putSingle(ContentParameter.uriName(), "all");
32 assertEquals("all", QueryParams.getSingleParameter(parameters, ContentParameter.uriName()));
36 * Test when parameter is present more than once.
39 public void getSingleParameterNegativeTest() {
40 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
41 parameters.put(ContentParameter.uriName(), List.of("config", "nonconfig", "all"));
43 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
44 () -> QueryParams.getSingleParameter(parameters, ContentParameter.uriName()));
45 final List<RestconfError> errors = ex.getErrors();
46 assertEquals(1, errors.size());
48 final RestconfError error = errors.get(0);
49 assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
50 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());
54 * Test when all parameters are allowed.
57 public void checkParametersTypesTest() {
58 QueryParams.checkParametersTypes(Set.of("content"),
59 Set.of(ContentParameter.uriName(), DepthParameter.uriName()));
63 * Test when not allowed parameter type is used.
66 public void checkParametersTypesNegativeTest() {
67 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
68 () -> QueryParams.checkParametersTypes(Set.of("not-allowed-parameter"),
69 Set.of(ContentParameter.uriName(), DepthParameter.uriName())));
70 final List<RestconfError> errors = ex.getErrors();
71 assertEquals(1, errors.size());
73 final RestconfError error = errors.get(0);
74 assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
75 assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());