Split Restconf implementations (draft02 and RFC) - move
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / restconf / restful / utils / ParametersUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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
9 package org.opendaylight.restconf.restful.utils;
10
11 import static org.junit.Assert.assertEquals;
12
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Sets;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
19 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
20
21 /**
22  * Unit test for {@link ParametersUtil}.
23  */
24 public class ParametersUtilTest {
25
26     /**
27      * Test when all parameters are allowed.
28      */
29     @Test
30     public void checkParametersTypesTest() {
31         ParametersUtil.checkParametersTypes(
32                 RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
33                 Sets.newHashSet("content"),
34                 RestconfDataServiceConstant.ReadData.CONTENT, RestconfDataServiceConstant.ReadData.DEPTH);
35     }
36
37     /**
38      * Test when not allowed parameter type is used.
39      */
40     @Test
41     public void checkParametersTypesNegativeTest() {
42         try {
43             ParametersUtil.checkParametersTypes(
44                     RestconfDataServiceConstant.ReadData.READ_TYPE_TX,
45                     Sets.newHashSet("not-allowed-parameter"),
46                     RestconfDataServiceConstant.ReadData.CONTENT, RestconfDataServiceConstant.ReadData.DEPTH);
47
48             Assert.fail("Test expected to fail due to not allowed parameter used with operation");
49         } catch (final RestconfDocumentedException e) {
50             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
51             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
52             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
53         }
54     }
55
56     /**
57      * Test when parameter is present at most once.
58      */
59     @Test
60     public void checkParameterCountTest() {
61         ParametersUtil.checkParameterCount(Lists.newArrayList("all"), RestconfDataServiceConstant.ReadData.CONTENT);
62     }
63
64     /**
65      * Test when parameter is present more than once.
66      */
67     @Test
68     public void checkParameterCountNegativeTest() {
69         try {
70             ParametersUtil.checkParameterCount(Lists.newArrayList("config", "nonconfig", "all"),
71                     RestconfDataServiceConstant.ReadData.CONTENT);
72
73             Assert.fail("Test expected to fail due to multiple values of the same parameter");
74         } catch (final RestconfDocumentedException e) {
75             assertEquals("Error type is not correct", ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
76             assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
77             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
78         }
79     }
80 }