19192dad16ea9f09893e944163ebae0802d1c94a
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / parameters / TestPdpParameters.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.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import org.junit.Test;
31 import org.onap.policy.common.parameters.ValidationResult;
32 import org.onap.policy.common.utils.coder.Coder;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34
35 public class TestPdpParameters {
36     private static final Coder coder = new StandardCoder();
37     private static final CommonTestData testData = new CommonTestData();
38
39     @Test
40     public void testGetters() {
41         PdpParameters params = testData.getPapParameterGroup(1).getPdpParameters();
42
43         PdpUpdateParameters update = params.getUpdateParameters();
44         assertNotNull(update);
45         assertEquals(1, update.getMaxRetryCount());
46
47         PdpStateChangeParameters state = params.getStateChangeParameters();
48         assertNotNull(state);
49         assertEquals(5, state.getMaxWaitMs());
50
51         assertEquals(6000L, params.getHeartBeatMs());
52
53         assertEquals(20000L, params.getMaxMessageAgeMs());
54
55         // check default value
56         assertEquals(600000L, new PdpParameters().getMaxMessageAgeMs());
57     }
58
59     @Test
60     public void testValidate() throws Exception {
61         String json = testData.getPapParameterGroupAsString(1);
62
63         // valid
64         String json2 = json;
65         ValidationResult result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
66         assertNull(result.getResult());
67         assertTrue(result.isValid());
68
69         // invalid heart beat
70         json2 = json.replaceFirst(": 6", ": 0");
71         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
72         assertFalse(result.isValid());
73         assertThat(result.getResult()).contains(
74                         "'heartBeatMs' value '0' INVALID, is below the minimum value: 1".replace('\'', '"'));
75
76         // invalid max message age
77         json2 = json.replaceFirst(": 20000", ": 0");
78         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
79         assertFalse(result.isValid());
80         assertThat(result.getResult()).contains(
81                         "'maxMessageAgeMs' value '0' INVALID, is below the minimum value: 1".replace('\'', '"'));
82
83         // no update params
84         json2 = testData.nullifyField(json, "updateParameters");
85         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
86         assertFalse(result.isValid());
87         assertThat(result.getResult()).contains("\"updateParameters\"", "is null");
88
89         // invalid update params
90         json2 = json.replaceFirst(": 2", ": -2");
91         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
92         assertFalse(result.isValid());
93         assertThat(result.getResult()).contains("\"PdpUpdateParameters\"",
94                         "'maxWaitMs' value '-2' INVALID, is below the minimum value: 0".replace('\'', '"'));
95
96         // no state-change params
97         json2 = testData.nullifyField(json, "stateChangeParameters");
98         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
99         assertFalse(result.isValid());
100
101         // invalid state-change params
102         json2 = json.replaceFirst(": 5", ": -5");
103         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
104         assertFalse(result.isValid());
105         assertThat(result.getResult()).contains("\"PdpStateChangeParameters\"",
106                         "'maxWaitMs' value '-5' INVALID, is below the minimum value: 0".replace('\'', '"'));
107     }
108
109 }