2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018, 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 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.drools.pooling;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.onap.policy.drools.pooling.PoolingProperties.ACTIVE_HEARTBEAT_MS;
27 import static org.onap.policy.drools.pooling.PoolingProperties.FEATURE_ENABLED;
28 import static org.onap.policy.drools.pooling.PoolingProperties.IDENTIFICATION_MS;
29 import static org.onap.policy.drools.pooling.PoolingProperties.INTER_HEARTBEAT_MS;
30 import static org.onap.policy.drools.pooling.PoolingProperties.OFFLINE_AGE_MS;
31 import static org.onap.policy.drools.pooling.PoolingProperties.OFFLINE_LIMIT;
32 import static org.onap.policy.drools.pooling.PoolingProperties.OFFLINE_PUB_WAIT_MS;
33 import static org.onap.policy.drools.pooling.PoolingProperties.POOLING_TOPIC;
34 import static org.onap.policy.drools.pooling.PoolingProperties.PREFIX;
35 import static org.onap.policy.drools.pooling.PoolingProperties.REACTIVATE_MS;
36 import static org.onap.policy.drools.pooling.PoolingProperties.START_HEARTBEAT_MS;
38 import java.util.Properties;
39 import java.util.function.Function;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.onap.policy.common.utils.properties.exception.PropertyException;
44 public class PoolingPropertiesTest {
46 private static final String CONTROLLER = "a.controller";
48 private static final String STD_POOLING_TOPIC = "my.topic";
49 public static final boolean STD_FEATURE_ENABLED = true;
50 public static final int STD_OFFLINE_LIMIT = 10;
51 public static final long STD_OFFLINE_AGE_MS = 1000L;
52 public static final long STD_OFFLINE_PUB_WAIT_MS = 2000L;
53 public static final long STD_START_HEARTBEAT_MS = 3000L;
54 public static final long STD_REACTIVATE_MS = 4000L;
55 public static final long STD_IDENTIFICATION_MS = 5000L;
56 public static final long STD_ACTIVE_HEARTBEAT_MS = 7000L;
57 public static final long STD_INTER_HEARTBEAT_MS = 8000L;
59 private Properties plain;
60 private PoolingProperties pooling;
65 * @throws Exception throws an exception
68 public void setUp() throws Exception {
69 plain = makeProperties();
71 pooling = new PoolingProperties(CONTROLLER, plain);
75 void testPoolingProperties() {
76 // ensure no exceptions
77 assertThatCode(() -> new PoolingProperties(CONTROLLER, plain)).doesNotThrowAnyException();
81 void testGetSource() {
82 assertEquals(plain, pooling.getSource());
86 void testGetPoolingTopic() {
87 assertEquals(STD_POOLING_TOPIC, pooling.getPoolingTopic());
91 void testGetOfflineLimit() throws PropertyException {
92 doTest(OFFLINE_LIMIT, STD_OFFLINE_LIMIT, 1000, xxx -> pooling.getOfflineLimit());
96 void testGetOfflineAgeMs() throws PropertyException {
97 doTest(OFFLINE_AGE_MS, STD_OFFLINE_AGE_MS, 60000L, xxx -> pooling.getOfflineAgeMs());
101 void testGetOfflinePubWaitMs() throws PropertyException {
102 doTest(OFFLINE_PUB_WAIT_MS, STD_OFFLINE_PUB_WAIT_MS, 3000L, xxx -> pooling.getOfflinePubWaitMs());
106 void testGetStartHeartbeatMs() throws PropertyException {
107 doTest(START_HEARTBEAT_MS, STD_START_HEARTBEAT_MS, 100000L, xxx -> pooling.getStartHeartbeatMs());
111 void testGetReactivateMs() throws PropertyException {
112 doTest(REACTIVATE_MS, STD_REACTIVATE_MS, 50000L, xxx -> pooling.getReactivateMs());
116 void testGetIdentificationMs() throws PropertyException {
117 doTest(IDENTIFICATION_MS, STD_IDENTIFICATION_MS, 50000L, xxx -> pooling.getIdentificationMs());
121 void testGetActiveHeartbeatMs() throws PropertyException {
122 doTest(ACTIVE_HEARTBEAT_MS, STD_ACTIVE_HEARTBEAT_MS, 50000L, xxx -> pooling.getActiveHeartbeatMs());
126 void testGetInterHeartbeatMs() throws PropertyException {
127 doTest(INTER_HEARTBEAT_MS, STD_INTER_HEARTBEAT_MS, 15000L, xxx -> pooling.getInterHeartbeatMs());
131 * Tests a particular property. Verifies that the correct value is returned if the
132 * specialized property has a value or the property has no value. Also verifies that
133 * the property name can be generalized.
135 * @param propnm name of the property of interest
136 * @param specValue expected specialized value
137 * @param dfltValue expected default value
138 * @param func function to get the field
139 * @throws PropertyException if an error occurs
141 private <T> void doTest(String propnm, T specValue, T dfltValue, Function<Void, T> func) throws PropertyException {
143 * With specialized property
145 pooling = new PoolingProperties(CONTROLLER, plain);
146 assertEquals(specValue, func.apply(null), "special " + propnm);
149 * Without the property - should use the default value.
151 plain.remove(specialize(propnm));
152 plain.remove(propnm);
153 pooling = new PoolingProperties(CONTROLLER, plain);
154 assertEquals(dfltValue, func.apply(null), "default " + propnm);
158 * Makes a set of properties, where all the properties are specialized for the
161 * @return a new property set
163 private Properties makeProperties() {
164 Properties props = new Properties();
166 props.setProperty(specialize(POOLING_TOPIC), STD_POOLING_TOPIC);
167 props.setProperty(specialize(FEATURE_ENABLED), "" + STD_FEATURE_ENABLED);
168 props.setProperty(specialize(OFFLINE_LIMIT), "" + STD_OFFLINE_LIMIT);
169 props.setProperty(specialize(OFFLINE_AGE_MS), "" + STD_OFFLINE_AGE_MS);
170 props.setProperty(specialize(OFFLINE_PUB_WAIT_MS), "" + STD_OFFLINE_PUB_WAIT_MS);
171 props.setProperty(specialize(START_HEARTBEAT_MS), "" + STD_START_HEARTBEAT_MS);
172 props.setProperty(specialize(REACTIVATE_MS), "" + STD_REACTIVATE_MS);
173 props.setProperty(specialize(IDENTIFICATION_MS), "" + STD_IDENTIFICATION_MS);
174 props.setProperty(specialize(ACTIVE_HEARTBEAT_MS), "" + STD_ACTIVE_HEARTBEAT_MS);
175 props.setProperty(specialize(INTER_HEARTBEAT_MS), "" + STD_INTER_HEARTBEAT_MS);
181 * Embeds a specializer within a property name, after the prefix.
183 * @param propnm property name into which it should be embedded
184 * @return the property name, with the specializer embedded within it
186 private String specialize(String propnm) {
187 String suffix = propnm.substring(PREFIX.length());
188 return PREFIX + PoolingPropertiesTest.CONTROLLER + "." + suffix;