97e3e456d5febe1ac055934b650288964c39320f
[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.assertThrows;
12
13 import java.util.List;
14 import java.util.Set;
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;
23
24 public class QueryParamsTest {
25     /**
26      * Test when parameter is present at most once.
27      */
28     @Test
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()));
33     }
34
35     /**
36      * Test when parameter is present more than once.
37      */
38     @Test
39     public void getSingleParameterNegativeTest() {
40         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
41         parameters.put(ContentParameter.uriName(), List.of("config", "nonconfig", "all"));
42
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());
47
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());
51     }
52
53     /**
54      * Test when all parameters are allowed.
55      */
56     @Test
57     public void checkParametersTypesTest() {
58         QueryParams.checkParametersTypes(Set.of("content"),
59             Set.of(ContentParameter.uriName(), DepthParameter.uriName()));
60     }
61
62     /**
63      * Test when not allowed parameter type is used.
64      */
65     @Test
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());
72
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());
76     }
77 }