Drools support for kafka topics
[policy/drools-pdp.git] / feature-pooling-messages / src / test / java / org / onap / policy / drools / pooling / PoolingPropertiesTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.drools.pooling;
23
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;
37
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;
43
44 public class PoolingPropertiesTest {
45
46     private static final String CONTROLLER = "a.controller";
47
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;
58
59     private Properties plain;
60     private PoolingProperties pooling;
61
62     /**
63      * Setup.
64      *
65      * @throws Exception throws an exception
66      */
67     @BeforeEach
68     public void setUp() throws Exception {
69         plain = makeProperties();
70
71         pooling = new PoolingProperties(CONTROLLER, plain);
72     }
73
74     @Test
75     void testPoolingProperties() {
76         // ensure no exceptions
77         assertThatCode(() -> new PoolingProperties(CONTROLLER, plain)).doesNotThrowAnyException();
78     }
79
80     @Test
81     void testGetSource() {
82         assertEquals(plain, pooling.getSource());
83     }
84
85     @Test
86     void testGetPoolingTopic() {
87         assertEquals(STD_POOLING_TOPIC, pooling.getPoolingTopic());
88     }
89
90     @Test
91     void testGetOfflineLimit() throws PropertyException {
92         doTest(OFFLINE_LIMIT, STD_OFFLINE_LIMIT, 1000, xxx -> pooling.getOfflineLimit());
93     }
94
95     @Test
96     void testGetOfflineAgeMs() throws PropertyException {
97         doTest(OFFLINE_AGE_MS, STD_OFFLINE_AGE_MS, 60000L, xxx -> pooling.getOfflineAgeMs());
98     }
99
100     @Test
101     void testGetOfflinePubWaitMs() throws PropertyException {
102         doTest(OFFLINE_PUB_WAIT_MS, STD_OFFLINE_PUB_WAIT_MS, 3000L, xxx -> pooling.getOfflinePubWaitMs());
103     }
104
105     @Test
106     void testGetStartHeartbeatMs() throws PropertyException {
107         doTest(START_HEARTBEAT_MS, STD_START_HEARTBEAT_MS, 100000L, xxx -> pooling.getStartHeartbeatMs());
108     }
109
110     @Test
111     void testGetReactivateMs() throws PropertyException {
112         doTest(REACTIVATE_MS, STD_REACTIVATE_MS, 50000L, xxx -> pooling.getReactivateMs());
113     }
114
115     @Test
116     void testGetIdentificationMs() throws PropertyException {
117         doTest(IDENTIFICATION_MS, STD_IDENTIFICATION_MS, 50000L, xxx -> pooling.getIdentificationMs());
118     }
119
120     @Test
121     void testGetActiveHeartbeatMs() throws PropertyException {
122         doTest(ACTIVE_HEARTBEAT_MS, STD_ACTIVE_HEARTBEAT_MS, 50000L, xxx -> pooling.getActiveHeartbeatMs());
123     }
124
125     @Test
126     void testGetInterHeartbeatMs() throws PropertyException {
127         doTest(INTER_HEARTBEAT_MS, STD_INTER_HEARTBEAT_MS, 15000L, xxx -> pooling.getInterHeartbeatMs());
128     }
129
130     /**
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.
134      *
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
140      */
141     private <T> void doTest(String propnm, T specValue, T dfltValue, Function<Void, T> func) throws PropertyException {
142         /*
143          * With specialized property
144          */
145         pooling = new PoolingProperties(CONTROLLER, plain);
146         assertEquals(specValue, func.apply(null), "special " + propnm);
147
148         /*
149          * Without the property - should use the default value.
150          */
151         plain.remove(specialize(propnm));
152         plain.remove(propnm);
153         pooling = new PoolingProperties(CONTROLLER, plain);
154         assertEquals(dfltValue, func.apply(null), "default " + propnm);
155     }
156
157     /**
158      * Makes a set of properties, where all the properties are specialized for the
159      * controller.
160      *
161      * @return a new property set
162      */
163     private Properties makeProperties() {
164         Properties props = new Properties();
165
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);
176
177         return props;
178     }
179
180     /**
181      * Embeds a specializer within a property name, after the prefix.
182      *
183      * @param propnm property name into which it should be embedded
184      * @return the property name, with the specializer embedded within it
185      */
186     private String specialize(String propnm) {
187         String suffix = propnm.substring(PREFIX.length());
188         return PREFIX + PoolingPropertiesTest.CONTROLLER + "." + suffix;
189     }
190 }