2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.main.parameters;
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;
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;
35 public class TestPdpParameters {
36 private static final Coder coder = new StandardCoder();
37 private static final CommonTestData testData = new CommonTestData();
40 public void testGetters() {
41 PdpParameters params = testData.getPapParameterGroup(1).getPdpParameters();
43 PdpUpdateParameters update = params.getUpdateParameters();
44 assertNotNull(update);
45 assertEquals(1, update.getMaxRetryCount());
47 PdpStateChangeParameters state = params.getStateChangeParameters();
49 assertEquals(5, state.getMaxWaitMs());
51 assertEquals(6000L, params.getHeartBeatMs());
53 assertEquals(20000L, params.getMaxMessageAgeMs());
55 // check default value
56 assertEquals(600000L, new PdpParameters().getMaxMessageAgeMs());
60 public void testValidate() throws Exception {
61 String json = testData.getPapParameterGroupAsString(1);
65 ValidationResult result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
66 assertNull(result.getResult());
67 assertTrue(result.isValid());
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('\'', '"'));
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('\'', '"'));
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");
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('\'', '"'));
96 // no state-change params
97 json2 = testData.nullifyField(json, "stateChangeParameters");
98 result = coder.decode(json2, PapParameterGroup.class).getPdpParameters().validate();
99 assertFalse(result.isValid());
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('\'', '"'));