Promote QueryParams as WriteDataParams
[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 javax.ws.rs.core.MultivaluedHashMap;
15 import org.junit.Test;
16 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
17 import org.opendaylight.restconf.common.errors.RestconfError;
18 import org.opendaylight.restconf.nb.rfc8040.ContentParameter;
19 import org.opendaylight.yangtools.yang.common.ErrorTag;
20 import org.opendaylight.yangtools.yang.common.ErrorType;
21
22 public class QueryParamsTest {
23     /**
24      * Test when parameter is present at most once.
25      */
26     @Test
27     public void getSingleParameterTest() {
28         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
29         parameters.putSingle(ContentParameter.uriName(), "all");
30         assertEquals("all", QueryParams.getSingleParameter(parameters, ContentParameter.uriName()));
31     }
32
33     /**
34      * Test when parameter is present more than once.
35      */
36     @Test
37     public void getSingleParameterNegativeTest() {
38         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
39         parameters.put(ContentParameter.uriName(), List.of("config", "nonconfig", "all"));
40
41         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
42             () -> QueryParams.getSingleParameter(parameters, ContentParameter.uriName()));
43         final List<RestconfError> errors = ex.getErrors();
44         assertEquals(1, errors.size());
45
46         final RestconfError error = errors.get(0);
47         assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
48         assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());
49     }
50 }