810bc448a3fc2cb09808d4b6bd57374525d52a7f
[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, 2021 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.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28
29 import org.junit.Test;
30 import org.onap.policy.common.parameters.ValidationResult;
31 import org.onap.policy.common.utils.coder.Coder;
32 import org.onap.policy.common.utils.coder.StandardCoder;
33
34 public class TestPdpRequestParameters {
35     private static final Coder coder = new StandardCoder();
36
37     @Test
38     public void test() throws Exception {
39         PdpRequestParameters params = makeParams(10, 20);
40         assertEquals(10, params.getMaxRetryCount());
41         assertEquals(20, params.getMaxWaitMs());
42     }
43
44     @Test
45     public void testValidate() throws Exception {
46         // valid, zeroes
47         PdpRequestParameters params = makeParams(0, 0);
48         ValidationResult result = params.validate();
49         assertNull(result.getResult());
50         assertTrue(result.isValid());
51
52         // valid
53         params = makeParams(100, 110);
54         result = params.validate();
55         assertNull(result.getResult());
56         assertTrue(result.isValid());
57
58         // invalid retry count
59         params = makeParams(-1, 120);
60         result = params.validate();
61         assertFalse(result.isValid());
62         assertThat(result.getResult()).contains(
63                         "'maxRetryCount' value '-1' INVALID, is below the minimum value: 0".replace('\'', '"'));
64
65         // invalid wait time
66         params = makeParams(130, -1);
67         result = params.validate();
68         assertFalse(result.isValid());
69         assertThat(result.getResult()).contains(
70                         "'maxWaitMs' value '-1' INVALID, is below the minimum value: 0".replace('\'', '"'));
71     }
72
73     private PdpRequestParameters makeParams(int maxRetry, long maxWait) throws Exception {
74         String json = "{'name':'abc', 'maxRetryCount':" + maxRetry + ", 'maxWaitMs':" + maxWait + "}";
75         return coder.decode(json.replace('\'', '"'), PdpRequestParameters.class);
76     }
77 }