e852442f3e4abb2284e7827337b6b08dd2ea5d60
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / parameters / TestPdpRequestParameters.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.parameters;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27
28 import org.junit.Test;
29 import org.onap.policy.common.parameters.GroupValidationResult;
30 import org.onap.policy.common.utils.coder.Coder;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32
33 public class TestPdpRequestParameters {
34     private static final Coder coder = new StandardCoder();
35
36     @Test
37     public void test() throws Exception {
38         PdpRequestParameters params = makeParams(10, 20);
39         assertEquals(10, params.getMaxRetryCount());
40         assertEquals(20, params.getMaxWaitMs());
41     }
42
43     @Test
44     public void testValidate() throws Exception {
45         // valid, zeroes
46         PdpRequestParameters params = makeParams(0, 0);
47         GroupValidationResult result = params.validate();
48         assertNull(result.getResult());
49         assertTrue(result.isValid());
50
51         // valid
52         params = makeParams(100, 110);
53         result = params.validate();
54         assertNull(result.getResult());
55         assertTrue(result.isValid());
56
57         // invalid retry count
58         params = makeParams(-1, 120);
59         result = params.validate();
60         assertFalse(result.isValid());
61         assertTrue(result.getResult().contains(
62                         "field 'maxRetryCount' type 'int' value '-1' INVALID, must be >= 0".replace('\'', '"')));
63
64         // invalid wait time
65         params = makeParams(130, -1);
66         result = params.validate();
67         assertFalse(result.isValid());
68         assertTrue(result.getResult()
69                         .contains("field 'maxWaitMs' type 'long' value '-1' INVALID, must be >= 0".replace('\'', '"')));
70     }
71
72     private PdpRequestParameters makeParams(int maxRetry, long maxWait) throws Exception {
73         String json = "{'name':'abc', 'maxRetryCount':" + maxRetry + ", 'maxWaitMs':" + maxWait + "}";
74         return coder.decode(json.replace('\'', '"'), PdpRequestParameters.class);
75     }
76 }