Drools support for kafka topics
[policy/drools-pdp.git] / feature-pooling-dmaap / 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  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.pooling;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25 import static org.onap.policy.drools.pooling.PoolingProperties.ACTIVE_HEARTBEAT_MS;
26 import static org.onap.policy.drools.pooling.PoolingProperties.FEATURE_ENABLED;
27 import static org.onap.policy.drools.pooling.PoolingProperties.IDENTIFICATION_MS;
28 import static org.onap.policy.drools.pooling.PoolingProperties.INTER_HEARTBEAT_MS;
29 import static org.onap.policy.drools.pooling.PoolingProperties.OFFLINE_AGE_MS;
30 import static org.onap.policy.drools.pooling.PoolingProperties.OFFLINE_LIMIT;
31 import static org.onap.policy.drools.pooling.PoolingProperties.OFFLINE_PUB_WAIT_MS;
32 import static org.onap.policy.drools.pooling.PoolingProperties.POOLING_TOPIC;
33 import static org.onap.policy.drools.pooling.PoolingProperties.PREFIX;
34 import static org.onap.policy.drools.pooling.PoolingProperties.REACTIVATE_MS;
35 import static org.onap.policy.drools.pooling.PoolingProperties.START_HEARTBEAT_MS;
36
37 import java.util.Properties;
38 import java.util.function.Function;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.common.utils.properties.exception.PropertyException;
42
43 public class PoolingPropertiesTest {
44
45     private static final String CONTROLLER = "a.controller";
46
47     private static final String STD_POOLING_TOPIC = "my.topic";
48     public static final boolean STD_FEATURE_ENABLED = true;
49     public static final int STD_OFFLINE_LIMIT = 10;
50     public static final long STD_OFFLINE_AGE_MS = 1000L;
51     public static final long STD_OFFLINE_PUB_WAIT_MS = 2000L;
52     public static final long STD_START_HEARTBEAT_MS = 3000L;
53     public static final long STD_REACTIVATE_MS = 4000L;
54     public static final long STD_IDENTIFICATION_MS = 5000L;
55     public static final long STD_LEADER_MS = 6000L;
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     @Before
68     public void setUp() throws Exception {
69         plain = makeProperties();
70
71         pooling = new PoolingProperties(CONTROLLER, plain);
72     }
73
74     @Test
75     public void testPoolingProperties() throws PropertyException {
76         // ensure no exceptions
77         assertThatCode(() -> new PoolingProperties(CONTROLLER, plain)).doesNotThrowAnyException();
78     }
79
80     @Test
81     public void testGetSource() {
82         assertEquals(plain, pooling.getSource());
83     }
84
85     @Test
86     public void testGetPoolingTopic() {
87         assertEquals(STD_POOLING_TOPIC, pooling.getPoolingTopic());
88     }
89
90     @Test
91     public void testGetOfflineLimit() throws PropertyException {
92         doTest(OFFLINE_LIMIT, STD_OFFLINE_LIMIT, 1000, xxx -> pooling.getOfflineLimit());
93     }
94
95     @Test
96     public void testGetOfflineAgeMs() throws PropertyException {
97         doTest(OFFLINE_AGE_MS, STD_OFFLINE_AGE_MS, 60000L, xxx -> pooling.getOfflineAgeMs());
98     }
99
100     @Test
101     public void testGetOfflinePubWaitMs() throws PropertyException {
102         doTest(OFFLINE_PUB_WAIT_MS, STD_OFFLINE_PUB_WAIT_MS, 3000L, xxx -> pooling.getOfflinePubWaitMs());
103     }
104
105     @Test
106     public void testGetStartHeartbeatMs() throws PropertyException {
107         doTest(START_HEARTBEAT_MS, STD_START_HEARTBEAT_MS, 100000L, xxx -> pooling.getStartHeartbeatMs());
108     }
109
110     @Test
111     public void testGetReactivateMs() throws PropertyException {
112         doTest(REACTIVATE_MS, STD_REACTIVATE_MS, 50000L, xxx -> pooling.getReactivateMs());
113     }
114
115     @Test
116     public void testGetIdentificationMs() throws PropertyException {
117         doTest(IDENTIFICATION_MS, STD_IDENTIFICATION_MS, 50000L, xxx -> pooling.getIdentificationMs());
118     }
119
120     @Test
121     public void testGetActiveHeartbeatMs() throws PropertyException {
122         doTest(ACTIVE_HEARTBEAT_MS, STD_ACTIVE_HEARTBEAT_MS, 50000L, xxx -> pooling.getActiveHeartbeatMs());
123     }
124
125     @Test
126     public 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("special " + propnm, specValue, func.apply(null));
147
148         /*
149          * Without the property - should use the default value.
150          */
151         plain.remove(specialize(propnm, CONTROLLER));
152         plain.remove(propnm);
153         pooling = new PoolingProperties(CONTROLLER, plain);
154         assertEquals("default " + propnm, dfltValue, func.apply(null));
155     }
156
157     /**
158      * Makes a set of properties, where all of 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, CONTROLLER), STD_POOLING_TOPIC);
167         props.setProperty(specialize(FEATURE_ENABLED, CONTROLLER), "" + STD_FEATURE_ENABLED);
168         props.setProperty(specialize(OFFLINE_LIMIT, CONTROLLER), "" + STD_OFFLINE_LIMIT);
169         props.setProperty(specialize(OFFLINE_AGE_MS, CONTROLLER), "" + STD_OFFLINE_AGE_MS);
170         props.setProperty(specialize(OFFLINE_PUB_WAIT_MS, CONTROLLER), "" + STD_OFFLINE_PUB_WAIT_MS);
171         props.setProperty(specialize(START_HEARTBEAT_MS, CONTROLLER), "" + STD_START_HEARTBEAT_MS);
172         props.setProperty(specialize(REACTIVATE_MS, CONTROLLER), "" + STD_REACTIVATE_MS);
173         props.setProperty(specialize(IDENTIFICATION_MS, CONTROLLER), "" + STD_IDENTIFICATION_MS);
174         props.setProperty(specialize(ACTIVE_HEARTBEAT_MS, CONTROLLER), "" + STD_ACTIVE_HEARTBEAT_MS);
175         props.setProperty(specialize(INTER_HEARTBEAT_MS, CONTROLLER), "" + 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      * @param spec specializer to be embedded
185      * @return the property name, with the specializer embedded within it
186      */
187     private String specialize(String propnm, String spec) {
188         String suffix = propnm.substring(PREFIX.length());
189         return PREFIX + spec + "." + suffix;
190     }
191 }