Java 17 / Spring 6 / Spring Boot 3 Upgrade
[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  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.parameters;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.common.parameters.ValidationResult;
33 import org.onap.policy.common.utils.coder.Coder;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35
36 class TestPdpParameters {
37     private static final Coder coder = new StandardCoder();
38     private static final CommonTestData testData = new CommonTestData();
39
40     @Test
41     void testGetters() {
42         PdpParameters params = testData.getPapParameterGroup(1).getPdpParameters();
43
44         PdpUpdateParameters update = params.getUpdateParameters();
45         assertNotNull(update);
46         assertEquals(1, update.getMaxRetryCount());
47
48         PdpStateChangeParameters state = params.getStateChangeParameters();
49         assertNotNull(state);
50         assertEquals(5, state.getMaxWaitMs());
51
52         assertEquals(6000L, params.getHeartBeatMs());
53
54         assertEquals(20000L, params.getMaxMessageAgeMs());
55
56         // check default value
57         assertEquals(600000L, new PdpParameters().getMaxMessageAgeMs());
58     }
59
60     @Test
61     void testValidate() throws Exception {
62         String json = testData.getPapParameterGroupAsString(1);
63
64         // valid
65         String json2 = json;
66         ValidationResult result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
67         assertNull(result.getResult());
68         assertTrue(result.isValid());
69
70         // invalid heart beat
71         json2 = json.replaceFirst(": 6", ": 0");
72         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
73         assertFalse(result.isValid());
74         assertThat(result.getResult()).contains(
75                         "'heartBeatMs' value '0' INVALID, is below the minimum value: 1".replace('\'', '"'));
76
77         // invalid max message age
78         json2 = json.replaceFirst(": 20000", ": 0");
79         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
80         assertFalse(result.isValid());
81         assertThat(result.getResult()).contains(
82                         "'maxMessageAgeMs' value '0' INVALID, is below the minimum value: 1".replace('\'', '"'));
83
84         // no update params
85         json2 = testData.nullifyField(json, "updateParameters");
86         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
87         assertFalse(result.isValid());
88         assertThat(result.getResult()).contains("\"updateParameters\"", "is null");
89
90         // invalid update params
91         json2 = json.replaceFirst(": 2", ": -2");
92         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
93         assertFalse(result.isValid());
94         assertThat(result.getResult()).contains("\"PdpUpdateParameters\"",
95                         "'maxWaitMs' value '-2' INVALID, is below the minimum value: 0".replace('\'', '"'));
96
97         // no state-change params
98         json2 = testData.nullifyField(json, "stateChangeParameters");
99         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
100         assertFalse(result.isValid());
101
102         // invalid state-change params
103         json2 = json.replaceFirst(": 5", ": -5");
104         result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
105         assertFalse(result.isValid());
106         assertThat(result.getResult()).contains("\"PdpStateChangeParameters\"",
107                         "'maxWaitMs' value '-5' INVALID, is below the minimum value: 0".replace('\'', '"'));
108     }
109
110 }