cd276de589dafd55bd9dab241f392102a567b044
[policy/common.git] /
1 /*
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
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.common.endpoints.event.comm.bus;
22
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;
41
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.LinkedList;
45 import java.util.List;
46 import java.util.Map;
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;
53
54 public class DmaapTopicSourceFactoryTest extends BusTopicTestBase {
55
56     private static final String SERVER = "my-server";
57     private static final String TOPIC2 = "my-topic-2";
58
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";
65
66     private SourceFactory factory;
67
68     /**
69      * Creates the object to be tested.
70      */
71     @Before
72     public void setUp() {
73         super.setUp();
74
75         factory = new SourceFactory();
76     }
77
78     @After
79     public void tearDown() {
80         factory.destroy();
81     }
82
83     @Test
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);
91
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);
100
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);
106
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()));
110     }
111
112     @Test(expected = IllegalArgumentException.class)
113     public void testBuildBusTopicParams_NullTopic() {
114         factory.build(makeBuilder().topic(null).build());
115     }
116
117     @Test(expected = IllegalArgumentException.class)
118     public void testBuildBusTopicParams_EmptyTopic() {
119         factory.build(makeBuilder().topic("").build());
120     }
121
122     @Test
123     public void testBuildProperties() {
124         assertEquals(1, factory.build(makePropBuilder().makeTopic(MY_TOPIC).build()).size());
125
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());
140
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));
149     }
150
151     @Test
152     public void testBuildProperties_Variations() {
153         TopicPropertyBuilder builder = makePropBuilder().makeTopic(MY_TOPIC);
154
155         // null sources
156         Properties props = builder.build();
157         props.remove(PROPERTY_DMAAP_SOURCE_TOPICS);
158         assertTrue(factory.build(props).isEmpty());
159
160         // empty sources
161         props = builder.build();
162         props.setProperty(PROPERTY_DMAAP_SOURCE_TOPICS, "");
163         assertTrue(factory.build(props).isEmpty());
164
165         // null servers
166         assertTrue(factory.build(makePropBuilder().makeTopic(MY_TOPIC)
167                         .removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX).build()).isEmpty());
168
169         // empty servers
170         assertTrue(factory.build(makePropBuilder().makeTopic(MY_TOPIC)
171                         .removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX).build()).isEmpty());
172
173         // check boolean properties that default to true
174         checkDefault(builder, PROPERTY_MANAGED_SUFFIX, BusTopicParams::isManaged);
175
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());
180
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, "",
184             "invalid-timeout");
185         checkDefault(builder, PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX,
186             params -> params.getFetchLimit() == DmaapTopicSource.DEFAULT_LIMIT_FETCH, null, "",
187             "invalid-limit");
188
189         // check "additional" properties
190         expectNullAddProp(builder, PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX,
191                         DmaapTopicSourceFactory.DME2_EP_CONN_TIMEOUT_PROPERTY);
192
193         expectNullAddProp(builder, PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX,
194                         DmaapTopicSourceFactory.DME2_READ_TIMEOUT_PROPERTY);
195
196         expectNullAddProp(builder, PROPERTY_DMAAP_DME2_ROUNDTRIP_TIMEOUT_MS_SUFFIX,
197                         DmaapTopicSourceFactory.DME2_ROUNDTRIP_TIMEOUT_PROPERTY);
198
199         expectNullAddProp(builder, PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX,
200                         DmaapTopicSourceFactory.DME2_ROUTE_OFFER_PROPERTY);
201
202         expectNullAddProp(builder, PROPERTY_DMAAP_DME2_SESSION_STICKINESS_REQUIRED_SUFFIX,
203                         DmaapTopicSourceFactory.DME2_SESSION_STICKINESS_REQUIRED_PROPERTY);
204
205         expectNullAddProp(builder, PROPERTY_DMAAP_DME2_SUB_CONTEXT_PATH_SUFFIX,
206                         DmaapTopicSourceFactory.DME2_SUBCONTEXT_PATH_PROPERTY);
207
208         expectNullAddProp(builder, PROPERTY_DMAAP_DME2_VERSION_SUFFIX, DmaapTopicSourceFactory.DME2_VERSION_PROPERTY);
209     }
210
211     @Test
212     public void testBuildProperties_Multiple() {
213         TopicPropertyBuilder builder =
214                         makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).addTopic(MY_TOPIC).addTopic(MY_TOPIC);
215
216         List<DmaapTopicSource> lst = factory.build(builder.build());
217         assertEquals(4, lst.size());
218
219         int index = 0;
220         DmaapTopicSource source = lst.get(index++);
221         assertTrue(source != lst.get(index++));
222         assertTrue(source == lst.get(index++));
223         assertTrue(source == lst.get(index++));
224     }
225
226     @Test
227     public void testBuildListOfStringStringStringString() {
228         DmaapTopicSource source1 = factory.build(servers, MY_TOPIC, MY_API_KEY, MY_API_SECRET);
229         assertNotNull(source1);
230
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());
242     }
243
244     @Test
245     public void testBuildListOfStringString() {
246         DmaapTopicSource source1 = factory.build(servers, MY_TOPIC);
247         assertNotNull(source1);
248
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());
260
261         DmaapTopicSource source2 = factory.build(servers, TOPIC2);
262         assertNotNull(source2);
263         assertTrue(source1 != source2);
264
265         // duplicate - should be the same as these topics are managed
266         DmaapTopicSource source3 = factory.build(Collections.emptyList(), TOPIC2);
267         assertTrue(source2 == source3);
268     }
269
270     @Test
271     public void testDestroyString_testGet_testInventory() {
272         List<DmaapTopicSource> lst = factory.build(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
273
274         int index = 0;
275         DmaapTopicSource source1 = lst.get(index++);
276         DmaapTopicSource source2 = lst.get(index++);
277
278         assertEquals(2, factory.inventory().size());
279         assertTrue(factory.inventory().contains(source1));
280         assertTrue(factory.inventory().contains(source2));
281
282         source1.start();
283         source2.start();
284
285         assertEquals(source1, factory.get(MY_TOPIC));
286         assertEquals(source2, factory.get(TOPIC2));
287
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));
294
295         // repeat
296         factory.destroy(MY_TOPIC);
297         assertFalse(source1.isAlive());
298         assertTrue(source2.isAlive());
299
300         // with other topic
301         factory.destroy(TOPIC2);
302         assertFalse(source1.isAlive());
303         assertFalse(source2.isAlive());
304         assertEquals(0, factory.inventory().size());
305     }
306
307     @Test(expected = IllegalArgumentException.class)
308     public void testDestroyString_NullTopic() {
309         factory.destroy(null);
310     }
311
312     @Test(expected = IllegalArgumentException.class)
313     public void testDestroyString_EmptyTopic() {
314         factory.destroy("");
315     }
316
317     @Test
318     public void testDestroy() {
319         List<DmaapTopicSource> lst = factory.build(makePropBuilder().makeTopic(MY_TOPIC).makeTopic(TOPIC2).build());
320
321         int index = 0;
322         DmaapTopicSource source1 = lst.get(index++);
323         DmaapTopicSource source2 = lst.get(index++);
324
325         source1.start();
326         source2.start();
327
328         factory.destroy();
329         assertFalse(source1.isAlive());
330         assertFalse(source2.isAlive());
331         assertEquals(0, factory.inventory().size());
332     }
333
334     @Test(expected = IllegalArgumentException.class)
335     public void testGet_NullTopic() {
336         factory.get(null);
337     }
338
339     @Test(expected = IllegalArgumentException.class)
340     public void testGet_EmptyTopic() {
341         factory.get("");
342     }
343
344     @Test(expected = IllegalArgumentException.class)
345     public void testGet_UnknownTopic() {
346         factory.build(makePropBuilder().makeTopic(MY_TOPIC).build());
347         factory.get(TOPIC2);
348     }
349
350     @Test
351     public void testToString() {
352         assertTrue(factory.toString().startsWith("IndexedDmaapTopicSourceFactory ["));
353     }
354
355     private DmaapTopicPropertyBuilder makePropBuilder() {
356         return new DmaapTopicPropertyBuilder(PROPERTY_DMAAP_SOURCE_TOPICS);
357     }
358
359     /**
360      * Verifies that a parameter has the correct default, if the original builder property
361      * is not provided.
362      *
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, ""}
367      */
368     private void checkDefault(TopicPropertyBuilder builder, String builderName,
369                     Function<BusTopicParams, Boolean> getter, Object... values) {
370
371         Object[] values2 = (values.length > 0 ? values : new String[] {null, ""});
372
373         for (Object value : values2) {
374             // always start with a fresh factory
375             factory.destroy();
376             factory = new SourceFactory();
377
378             if (value == null) {
379                 builder.removeTopicProperty(builderName);
380
381             } else {
382                 builder.setTopicProperty(builderName, value.toString());
383             }
384
385             assertEquals(1, factory.build(builder.build()).size());
386             assertTrue(getter.apply(factory.params.get(0)));
387         }
388     }
389
390     /**
391      * Verifies that an "additional" property does not exist, if the original builder
392      * property is not provided.
393      *
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
397      */
398     private void expectNullAddProp(TopicPropertyBuilder builder, String builderName, String addName) {
399         // always start with a fresh factory
400         factory.destroy();
401         factory = new SourceFactory();
402
403         Properties props = builder.build();
404         props.remove(PROPERTY_DMAAP_SOURCE_TOPICS + "." + MY_TOPIC + builderName);
405
406         assertEquals(1, factory.build(props).size());
407         assertFalse(factory.params.get(0).getAdditionalProps().containsKey(addName));
408
409         // repeat, this time using an empty string instead of null
410         factory.destroy();
411         factory = new SourceFactory();
412
413         props.setProperty(PROPERTY_DMAAP_SOURCE_TOPICS + "." + MY_TOPIC + builderName, "");
414
415         assertEquals(1, factory.build(props).size());
416         assertFalse(factory.params.get(0).getAdditionalProps().containsKey(addName));
417     }
418
419     /**
420      * Factory that records the parameters of all of the sources it creates.
421      */
422     private static class SourceFactory extends IndexedDmaapTopicSourceFactory {
423         private List<BusTopicParams> params = new LinkedList<>();
424
425         @Override
426         protected DmaapTopicSource makeSource(BusTopicParams busTopicParams) {
427             params.add(busTopicParams);
428             return super.makeSource(busTopicParams);
429         }
430     }
431 }