2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.main.parameters;
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;
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;
36 class TestPdpParameters {
37 private static final Coder coder = new StandardCoder();
38 private static final CommonTestData testData = new CommonTestData();
42 PdpParameters params = testData.getPapParameterGroup(1).getPdpParameters();
44 PdpUpdateParameters update = params.getUpdateParameters();
45 assertNotNull(update);
46 assertEquals(1, update.getMaxRetryCount());
48 PdpStateChangeParameters state = params.getStateChangeParameters();
50 assertEquals(5, state.getMaxWaitMs());
52 assertEquals(6000L, params.getHeartBeatMs());
54 assertEquals(20000L, params.getMaxMessageAgeMs());
56 // check default value
57 assertEquals(600000L, new PdpParameters().getMaxMessageAgeMs());
61 void testValidate() throws Exception {
62 String json = testData.getPapParameterGroupAsString(1);
66 ValidationResult result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
67 assertNull(result.getResult());
68 assertTrue(result.isValid());
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('\'', '"'));
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('\'', '"'));
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");
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('\'', '"'));
97 // no state-change params
98 json2 = testData.nullifyField(json, "stateChangeParameters");
99 result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
100 assertFalse(result.isValid());
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('\'', '"'));