2 * ============LICENSE_START=======================================================
3 * ONAP Policy Engine - Common Modules
4 * ================================================================================
5 * Copyright (C) 2018 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.common.endpoints.event.comm.bus;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX;
28 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX;
29 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX;
30 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX;
31 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX;
32 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX;
33 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX;
34 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_DMAAP_DME2_VERSION_SUFFIX;
35 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS;
36 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX;
37 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX;
38 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX;
39 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX;
40 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.LinkedList;
45 import java.util.List;
47 import java.util.Properties;
48 import java.util.function.Function;
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
54 public class DmaapTopicSourceFactoryTest extends BusTopicTestBase {
56 private static final String SERVER = "my-server";
57 private static final String TOPIC2 = "my-topic-2";
59 private static final String MY_CONN_TIMEOUT = "200";
60 private static final String MY_READ_TIMEOUT = "201";
61 private static final String MY_ROUNDTRIP_TIMEOUT = "202";
62 private static final String MY_STICKINESS = "true";
63 private static final String MY_SUBCONTEXT = "my-subcontext";
64 private static final String MY_DME_VERSION = "my-version";
66 private SourceFactory factory;
69 * Creates the object to be tested.
75 factory = new SourceFactory();
79 public void tearDown() {
84 public void testBuildBusTopicParams() {
85 // two unmanaged topics
86 DmaapTopicSource source = factory.build(makeBuilder().managed(false).build());
87 DmaapTopicSource source2 = factory.build(makeBuilder().managed(false).topic(TOPIC2).build());
88 assertNotNull(source);
89 assertNotNull(source2);
90 assertTrue(source != source2);
92 // duplicate topics, but since they aren't managed, they should be different
93 DmaapTopicSource source3 = factory.build(makeBuilder().managed(false).build());
94 DmaapTopicSource source4 = factory.build(makeBuilder().managed(false).build());
95 assertNotNull(source3);
96 assertNotNull(source4);
97 assertTrue(source != source3);
98 assertTrue(source != source4);
99 assertTrue(source3 != source4);
101 // two managed topics
102 DmaapTopicSource source5 = factory.build(makeBuilder().build());
103 DmaapTopicSource source6 = factory.build(makeBuilder().topic(TOPIC2).build());
104 assertNotNull(source5);
105 assertNotNull(source6);
107 // re-build same managed topics - should get exact same objects
108 assertTrue(source5 == factory.build(BusTopicParams.builder().topic(MY_TOPIC).build()));
109 assertTrue(source6 == factory.build(makeBuilder().topic(TOPIC2).build()));
112 @Test(expected = IllegalArgumentException.class)
113 public void testBuildBusTopicParams_NullTopic() {
114 factory.build(makeBuilder().topic(null).build());
117 @Test(expected = IllegalArgumentException.class)
118 public void testBuildBusTopicParams_EmptyTopic() {
119 factory.build(makeBuilder().topic("").build());
123 public void testBuildProperties() {
124 assertEquals(1, factory.build(makePropBuilder().makeTopic(MY_TOPIC).build()).size());
126 BusTopicParams params = factory.params.get(0);
127 assertEquals(true, params.isManaged());
128 assertEquals(true, params.isUseHttps());
129 assertEquals(true, params.isAllowSelfSignedCerts());
130 assertEquals(MY_API_KEY, params.getApiKey());
131 assertEquals(MY_API_SECRET, params.getApiSecret());
132 assertEquals(MY_ENV, params.getEnvironment());
133 assertEquals(MY_LAT, params.getLatitude());
134 assertEquals(MY_LONG, params.getLongitude());
135 assertEquals(MY_PARTNER, params.getPartner());
136 assertEquals(Arrays.asList(SERVER), params.getServers());
137 assertEquals(MY_TOPIC, params.getTopic());
138 assertEquals(MY_FETCH_LIMIT, params.getFetchLimit());
139 assertEquals(MY_FETCH_TIMEOUT, params.getFetchTimeout());
141 Map<String, String> add = params.getAdditionalProps();
142 assertEquals(MY_CONN_TIMEOUT, add.get(DmaapTopicSourceFactory.DME2_EP_CONN_TIMEOUT_PROPERTY));
143 assertEquals(MY_READ_TIMEOUT, add.get(DmaapTopicSourceFactory.DME2_READ_TIMEOUT_PROPERTY));
144 assertEquals(MY_ROUNDTRIP_TIMEOUT, add.get(DmaapTopicSourceFactory.DME2_ROUNDTRIP_TIMEOUT_PROPERTY));
145 assertEquals(MY_ROUTE, add.get(DmaapTopicSourceFactory.DME2_ROUTE_OFFER_PROPERTY));
146 assertEquals(MY_STICKINESS, add.get(DmaapTopicSourceFactory.DME2_SESSION_STICKINESS_REQUIRED_PROPERTY));
147 assertEquals(MY_SUBCONTEXT, add.get(DmaapTopicSourceFactory.DME2_SUBCONTEXT_PATH_PROPERTY));
148 assertEquals(MY_DME_VERSION, add.get(DmaapTopicSourceFactory.DME2_VERSION_PROPERTY));
152 public void testBuildProperties_Variations() {
153 TopicPropertyBuilder builder = makePropBuilder().makeTopic(MY_TOPIC);
156 Properties props = builder.build();
157 props.remove(PROPERTY_DMAAP_SOURCE_TOPICS);
158 assertTrue(factory.build(props).isEmpty());
161 props = builder.build();
162 props.setProperty(PROPERTY_DMAAP_SOURCE_TOPICS, "");
163 assertTrue(factory.build(props).isEmpty());
166 assertTrue(factory.build(makePropBuilder().makeTopic(MY_TOPIC)
167 .removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX).build()).isEmpty());
170 assertTrue(factory.build(makePropBuilder().makeTopic(MY_TOPIC)
171 .removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX).build()).isEmpty());
173 // check boolean properties that default to true
174 checkDefault(builder, PROPERTY_MANAGED_SUFFIX, BusTopicParams::isManaged);
176 // check boolean properties that default to false
177 checkDefault(builder, PROPERTY_HTTP_HTTPS_SUFFIX, params -> !params.isUseHttps());
178 checkDefault(builder, PROPERTY_ALLOW_SELF_SIGNED_CERTIFICATES_SUFFIX,
179 params -> !params.isAllowSelfSignedCerts());
181 // check other properties having default values
182 checkDefault(builder, PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX,
183 params -> params.getFetchTimeout() == DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH, null, "",
185 checkDefault(builder, PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX,
186 params -> params.getFetchLimit() == DmaapTopicSource.DEFAULT_LIMIT_FETCH, null, "",
189 // check "additional" properties
190 expectNullAddProp(builder, PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX,
191 DmaapTopicSourceFactory.DME2_EP_CONN_TIMEOUT_PROPERTY);
193 expectNullAddProp(builder, PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX,
194 DmaapTopicSourceFactory.DME2_READ_TIMEOUT_PROPERTY);
196 expectNullAddProp(builder, PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX,
197 DmaapTopicSourceFactory.DME2_ROUNDTRIP_TIMEOUT_PROPERTY);
199 expectNullAddProp(builder, PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX,
200 DmaapTopicSourceFactory.DME2_ROUTE_OFFER_PROPERTY);
202 expectNullAddProp(builder, PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX,
203 DmaapTopicSourceFactory.DME2_SESSION_STICKINESS_REQUIRED_PROPERTY);
205 expectNullAddProp(builder, PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX,
206 DmaapTopicSourceFactory.DME2_SUBCONTEXT_PATH_PROPERTY);
208 expectNullAddProp(builder, PROPERTY_DMAAP_DME2_VERSION_SUFFIX, DmaapTopicSourceFactory.DME2_VERSION_PROPERTY);
212 public void testBuildProperties_Multiple() {
213 TopicPropertyBuilder builder =
214 makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).addTopic(MY_TOPIC).addTopic(MY_TOPIC);
216 List<DmaapTopicSource> lst = factory.build(builder.build());
217 assertEquals(4, lst.size());
220 DmaapTopicSource source = lst.get(index++);
221 assertTrue(source != lst.get(index++));
222 assertTrue(source == lst.get(index++));
223 assertTrue(source == lst.get(index++));
227 public void testBuildListOfStringStringStringString() {
228 DmaapTopicSource source1 = factory.build(servers, MY_TOPIC, MY_API_KEY, MY_API_SECRET);
229 assertNotNull(source1);
231 // check parameters that were used
232 BusTopicParams params = factory.params.get(0);
233 assertEquals(servers, params.getServers());
234 assertEquals(MY_TOPIC, params.getTopic());
235 assertEquals(true, params.isManaged());
236 assertEquals(false, params.isUseHttps());
237 assertEquals(false, params.isAllowSelfSignedCerts());
238 assertEquals(MY_API_KEY, params.getApiKey());
239 assertEquals(MY_API_SECRET, params.getApiSecret());
240 assertEquals(DmaapTopicSource.DEFAULT_LIMIT_FETCH, params.getFetchLimit());
241 assertEquals(DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH, params.getFetchTimeout());
245 public void testBuildListOfStringString() {
246 DmaapTopicSource source1 = factory.build(servers, MY_TOPIC);
247 assertNotNull(source1);
249 // check parameters that were used
250 BusTopicParams params = factory.params.get(0);
251 assertEquals(servers, params.getServers());
252 assertEquals(MY_TOPIC, params.getTopic());
253 assertEquals(true, params.isManaged());
254 assertEquals(false, params.isUseHttps());
255 assertEquals(false, params.isAllowSelfSignedCerts());
256 assertEquals(null, params.getApiKey());
257 assertEquals(null, params.getApiSecret());
258 assertEquals(DmaapTopicSource.DEFAULT_LIMIT_FETCH, params.getFetchLimit());
259 assertEquals(DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH, params.getFetchTimeout());
261 DmaapTopicSource source2 = factory.build(servers, TOPIC2);
262 assertNotNull(source2);
263 assertTrue(source1 != source2);
265 // duplicate - should be the same as these topics are managed
266 DmaapTopicSource source3 = factory.build(Collections.emptyList(), TOPIC2);
267 assertTrue(source2 == source3);
271 public void testDestroyString_testGet_testInventory() {
272 List<DmaapTopicSource> lst = factory.build(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
275 DmaapTopicSource source1 = lst.get(index++);
276 DmaapTopicSource source2 = lst.get(index++);
278 assertEquals(2, factory.inventory().size());
279 assertTrue(factory.inventory().contains(source1));
280 assertTrue(factory.inventory().contains(source2));
285 assertEquals(source1, factory.get(MY_TOPIC));
286 assertEquals(source2, factory.get(TOPIC2));
288 factory.destroy(MY_TOPIC);
289 assertFalse(source1.isAlive());
290 assertTrue(source2.isAlive());
291 assertEquals(source2, factory.get(TOPIC2));
292 assertEquals(1, factory.inventory().size());
293 assertTrue(factory.inventory().contains(source2));
296 factory.destroy(MY_TOPIC);
297 assertFalse(source1.isAlive());
298 assertTrue(source2.isAlive());
301 factory.destroy(TOPIC2);
302 assertFalse(source1.isAlive());
303 assertFalse(source2.isAlive());
304 assertEquals(0, factory.inventory().size());
307 @Test(expected = IllegalArgumentException.class)
308 public void testDestroyString_NullTopic() {
309 factory.destroy(null);
312 @Test(expected = IllegalArgumentException.class)
313 public void testDestroyString_EmptyTopic() {
318 public void testDestroy() {
319 List<DmaapTopicSource> lst = factory.build(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
322 DmaapTopicSource source1 = lst.get(index++);
323 DmaapTopicSource source2 = lst.get(index++);
329 assertFalse(source1.isAlive());
330 assertFalse(source2.isAlive());
331 assertEquals(0, factory.inventory().size());
334 @Test(expected = IllegalArgumentException.class)
335 public void testGet_NullTopic() {
339 @Test(expected = IllegalArgumentException.class)
340 public void testGet_EmptyTopic() {
344 @Test(expected = IllegalArgumentException.class)
345 public void testGet_UnknownTopic() {
346 factory.build(makePropBuilder().makeTopic(MY_TOPIC).build());
351 public void testToString() {
352 assertTrue(factory.toString().startsWith("IndexedDmaapTopicSourceFactory ["));
355 private DmaapTopicPropertyBuilder makePropBuilder() {
356 return new DmaapTopicPropertyBuilder(PROPERTY_DMAAP_SOURCE_TOPICS);
360 * Verifies that a parameter has the correct default, if the original builder property
363 * @param builder used to build a set of properties
364 * @param builderName name of the builder property
365 * @param getter function to get the property from a set of parameters
366 * @param values possible values to try, defaults to {null, ""}
368 private void checkDefault(TopicPropertyBuilder builder, String builderName,
369 Function<BusTopicParams, Boolean> getter, Object... values) {
371 Object[] values2 = (values.length > 0 ? values : new String[] {null, ""});
373 for (Object value : values2) {
374 // always start with a fresh factory
376 factory = new SourceFactory();
379 builder.removeTopicProperty(builderName);
382 builder.setTopicProperty(builderName, value.toString());
385 assertEquals(1, factory.build(builder.build()).size());
386 assertTrue(getter.apply(factory.params.get(0)));
391 * Verifies that an "additional" property does not exist, if the original builder
392 * property is not provided.
394 * @param builder used to build a set of properties
395 * @param builderName name of the builder property
396 * @param addName name of the "additional" property
398 private void expectNullAddProp(TopicPropertyBuilder builder, String builderName, String addName) {
399 // always start with a fresh factory
401 factory = new SourceFactory();
403 Properties props = builder.build();
404 props.remove(PROPERTY_DMAAP_SOURCE_TOPICS + "." + MY_TOPIC + builderName);
406 assertEquals(1, factory.build(props).size());
407 assertFalse(factory.params.get(0).getAdditionalProps().containsKey(addName));
409 // repeat, this time using an empty string instead of null
411 factory = new SourceFactory();
413 props.setProperty(PROPERTY_DMAAP_SOURCE_TOPICS + "." + MY_TOPIC + builderName, "");
415 assertEquals(1, factory.build(props).size());
416 assertFalse(factory.params.get(0).getAdditionalProps().containsKey(addName));
420 * Factory that records the parameters of all of the sources it creates.
422 private static class SourceFactory extends IndexedDmaapTopicSourceFactory {
423 private List<BusTopicParams> params = new LinkedList<>();
426 protected DmaapTopicSource makeSource(BusTopicParams busTopicParams) {
427 params.add(busTopicParams);
428 return super.makeSource(busTopicParams);