a661b063862de603d7d3cb0408e9fe0798097aa3
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019 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;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
30
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Properties;
34
35 import org.junit.After;
36 import org.junit.Test;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicFactories;
39 import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicPropertyBuilder;
40 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
41 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicPropertyBuilder;
42 import org.onap.policy.common.endpoints.event.comm.bus.UebTopicFactories;
43 import org.onap.policy.common.endpoints.event.comm.bus.UebTopicPropertyBuilder;
44 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
45 import org.onap.policy.common.endpoints.parameters.TopicParameters;
46 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
47 import org.onap.policy.common.utils.gson.GsonTestUtils;
48
49 public class TopicEndpointProxyTest {
50
51     private static final String NOOP_SOURCE_TOPIC = "noop-source";
52     private static final String NOOP_SINK_TOPIC = "noop-sink";
53
54     private static final String UEB_SOURCE_TOPIC = "ueb-source";
55     private static final String UEB_SINK_TOPIC = "ueb-sink";
56
57     private static final String DMAAP_SOURCE_TOPIC = "dmaap-source";
58     private static final String DMAAP_SINK_TOPIC = "dmaap-sink";
59
60     private Properties configuration = new Properties();
61     private TopicParameterGroup group = new TopicParameterGroup();
62
63     /**
64      * Constructor.
65      */
66     public TopicEndpointProxyTest() {
67         group.setTopicSinks(new LinkedList<>());
68         group.setTopicSources(new LinkedList<>());
69
70         NoopTopicPropertyBuilder noopSourceBuilder =
71                 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS)
72                         .makeTopic(NOOP_SOURCE_TOPIC);
73         configuration.putAll(noopSourceBuilder.build());
74         group.getTopicSources().add(noopSourceBuilder.getParams());
75
76         NoopTopicPropertyBuilder noopSinkBuilder =
77                 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS)
78                         .makeTopic(NOOP_SINK_TOPIC);
79         configuration.putAll(noopSinkBuilder.build());
80         group.getTopicSinks().add(noopSinkBuilder.getParams());
81
82         UebTopicPropertyBuilder uebSourceBuilder =
83                 new UebTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS)
84                         .makeTopic(UEB_SOURCE_TOPIC);
85         configuration.putAll(uebSourceBuilder.build());
86         group.getTopicSources().add(uebSourceBuilder.getParams());
87
88         UebTopicPropertyBuilder uebSinkBuilder =
89                 new UebTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS)
90                         .makeTopic(UEB_SINK_TOPIC);
91         configuration.putAll(uebSinkBuilder.build());
92         group.getTopicSinks().add(uebSinkBuilder.getParams());
93
94         DmaapTopicPropertyBuilder dmaapSourceBuilder =
95                 new DmaapTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS)
96                         .makeTopic(DMAAP_SOURCE_TOPIC);
97         configuration.putAll(dmaapSourceBuilder.build());
98         group.getTopicSources().add(dmaapSourceBuilder.getParams());
99
100         DmaapTopicPropertyBuilder dmaapSinkBuilder =
101                 new DmaapTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS)
102                         .makeTopic(DMAAP_SINK_TOPIC);
103         configuration.putAll(dmaapSinkBuilder.build());
104         group.getTopicSinks().add(dmaapSinkBuilder.getParams());
105
106         TopicParameters invalidCommInfraParams =
107                 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS)
108                         .makeTopic(NOOP_SOURCE_TOPIC).getParams();
109         invalidCommInfraParams.setTopicCommInfrastructure(Topic.CommInfrastructure.REST.name());
110         group.getTopicSources().add(invalidCommInfraParams);
111         group.getTopicSinks().add(invalidCommInfraParams);
112     }
113
114     private <T extends Topic> boolean exists(List<T> topics, String topicName) {
115         return topics.stream().map(Topic::getTopic).anyMatch(topicName::equals);
116     }
117
118     private <T extends Topic> boolean allSources(List<T> topics) {
119         return exists(topics, NOOP_SOURCE_TOPIC) && exists(topics, UEB_SOURCE_TOPIC)
120                 && exists(topics, DMAAP_SOURCE_TOPIC);
121     }
122
123     private <T extends Topic> boolean allSinks(List<T> topics) {
124         return exists(topics, NOOP_SINK_TOPIC) && exists(topics, UEB_SINK_TOPIC) && exists(topics, DMAAP_SINK_TOPIC);
125     }
126
127     private <T extends Topic> boolean anySource(List<T> topics) {
128         return exists(topics, NOOP_SOURCE_TOPIC) || exists(topics, UEB_SOURCE_TOPIC)
129                 || exists(topics, DMAAP_SOURCE_TOPIC);
130     }
131
132     private <T extends Topic> boolean anySink(List<T> topics) {
133         return exists(topics, NOOP_SINK_TOPIC) || exists(topics, UEB_SINK_TOPIC) || exists(topics, DMAAP_SINK_TOPIC);
134     }
135
136     /**
137      * Destroys all managed topics.
138      */
139     @After
140     public void tearDown() {
141         NoopTopicFactories.getSinkFactory().destroy();
142         NoopTopicFactories.getSourceFactory().destroy();
143
144         UebTopicFactories.getSinkFactory().destroy();
145         UebTopicFactories.getSourceFactory().destroy();
146
147         DmaapTopicFactories.getSinkFactory().destroy();
148         DmaapTopicFactories.getSourceFactory().destroy();
149     }
150
151     @Test
152     public void testSerialize() {
153         TopicEndpoint manager = new TopicEndpointProxy();
154
155         manager.addTopicSources(configuration);
156         manager.addTopicSinks(configuration);
157
158         assertThatCode(() -> new GsonTestUtils().compareGson(manager, TopicEndpointProxyTest.class))
159                 .doesNotThrowAnyException();
160     }
161
162     @Test
163     public void testAddTopicSourcesListOfTopicParameters() {
164         TopicEndpoint manager = new TopicEndpointProxy();
165
166         List<TopicSource> sources = manager.addTopicSources(group.getTopicSources());
167         assertSame(3, sources.size());
168
169         assertTrue(allSources(sources));
170         assertFalse(anySink(sources));
171     }
172
173     @Test
174     public void testAddTopicSourcesProperties() {
175         TopicEndpoint manager = new TopicEndpointProxy();
176
177         List<TopicSource> sources = manager.addTopicSources(configuration);
178         assertSame(3, sources.size());
179
180         assertTrue(allSources(sources));
181         assertFalse(anySink(sources));
182     }
183
184     @Test
185     public void testAddTopicSinksListOfTopicParameters() {
186         TopicEndpoint manager = new TopicEndpointProxy();
187
188         List<TopicSink> sinks = manager.addTopicSinks(group.getTopicSinks());
189         assertSame(3, sinks.size());
190
191         assertFalse(anySource(sinks));
192         assertTrue(allSinks(sinks));
193     }
194
195     @Test
196     public void testAddTopicSinksProperties() {
197         TopicEndpoint manager = new TopicEndpointProxy();
198
199         List<TopicSink> sinks = manager.addTopicSinks(configuration);
200         assertSame(3, sinks.size());
201
202         assertFalse(anySource(sinks));
203         assertTrue(allSinks(sinks));
204     }
205
206     @Test
207     public void testAddTopicsProperties() {
208         TopicEndpoint manager = new TopicEndpointProxy();
209
210         List<Topic> topics = manager.addTopics(configuration);
211         assertSame(6, topics.size());
212
213         assertTrue(allSources(topics));
214         assertTrue(allSinks(topics));
215     }
216
217     @Test
218     public void testAddTopicsTopicParameterGroup() {
219         TopicEndpoint manager = new TopicEndpointProxy();
220
221         List<Topic> topics = manager.addTopics(group);
222         assertSame(6, topics.size());
223
224         assertTrue(allSources(topics));
225         assertTrue(allSinks(topics));
226     }
227
228     @Test
229     public void testAddTopicsTopicParameterGroupNull() {
230         TopicEndpoint manager = new TopicEndpointProxy();
231
232         List<Topic> topics = manager.addTopics(new TopicParameterGroup());
233         assertEquals(0, topics.size());
234     }
235
236     @Test
237     public void testLockSinks_lockSources_locked() {
238         TopicEndpoint manager = new TopicEndpointProxy();
239         manager.lock();
240         for (Topic topic : manager.addTopics(group)) {
241             assertTrue(topic.isLocked());
242         }
243     }
244
245     @Test
246     public void testLockSinks_lockSources_unlocked() {
247         TopicEndpoint manager = new TopicEndpointProxy();
248         for (Topic topic : manager.addTopics(group)) {
249             assertFalse(topic.isLocked());
250         }
251     }
252
253     @Test
254     public void testGetTopicSources() {
255         TopicEndpoint manager = new TopicEndpointProxy();
256
257         manager.addTopicSources(configuration);
258         manager.addTopicSinks(configuration);
259
260         List<TopicSource> sources = manager.getTopicSources();
261         assertSame(3, sources.size());
262
263         assertTrue(allSources(sources));
264         assertFalse(anySink(sources));
265     }
266
267     @Test
268     public void testGetTopicSinks() {
269         TopicEndpoint manager = new TopicEndpointProxy();
270
271         manager.addTopicSources(configuration);
272         manager.addTopicSinks(configuration);
273
274         List<TopicSink> sinks = manager.getTopicSinks();
275         assertSame(3, sinks.size());
276
277         assertFalse(anySource(sinks));
278         assertTrue(allSinks(sinks));
279     }
280
281     @Test
282     public void testGetUebTopicSources() {
283         TopicEndpoint manager = new TopicEndpointProxy();
284
285         manager.addTopicSources(configuration);
286         assertSame(1, manager.getUebTopicSources().size());
287     }
288
289     @Test
290     public void testGetDmaapTopicSources() {
291         TopicEndpoint manager = new TopicEndpointProxy();
292
293         manager.addTopicSources(configuration);
294         assertSame(1, manager.getDmaapTopicSources().size());
295     }
296
297     @Test
298     public void testGetNoopTopicSources() {
299         TopicEndpoint manager = new TopicEndpointProxy();
300
301         manager.addTopicSources(configuration);
302         assertSame(1, manager.getNoopTopicSources().size());
303     }
304
305     @Test
306     public void testGetUebTopicSinks() {
307         TopicEndpoint manager = new TopicEndpointProxy();
308
309         manager.addTopicSinks(configuration);
310         assertSame(1, manager.getUebTopicSinks().size());
311     }
312
313     @Test
314     public void testGetDmaapTopicSinks() {
315         TopicEndpoint manager = new TopicEndpointProxy();
316
317         manager.addTopicSinks(configuration);
318         assertSame(1, manager.getDmaapTopicSinks().size());
319     }
320
321     @Test
322     public void testGetNoopTopicSinks() {
323         TopicEndpoint manager = new TopicEndpointProxy();
324
325         manager.addTopicSinks(configuration);
326         assertSame(1, manager.getNoopTopicSinks().size());
327     }
328
329     @Test
330     public void testLifecycle() {
331         TopicEndpoint manager = new TopicEndpointProxy();
332
333         assertTrue(manager.start());
334         assertTrue(manager.isAlive());
335
336         assertTrue(manager.stop());
337         assertFalse(manager.isAlive());
338
339         assertTrue(manager.start());
340         assertTrue(manager.isAlive());
341
342         manager.shutdown();
343         assertFalse(manager.isAlive());
344     }
345
346     @Test
347     public void testLock() {
348         TopicEndpoint manager = new TopicEndpointProxy();
349
350         manager.lock();
351         assertTrue(manager.isLocked());
352
353         manager.unlock();
354         assertFalse(manager.isLocked());
355     }
356
357     @Test
358     public void testGetTopicSource() {
359         TopicEndpoint manager = new TopicEndpointProxy();
360         manager.addTopicSources(configuration);
361
362         assertSame(NOOP_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC).getTopic());
363         assertSame(UEB_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.UEB, UEB_SOURCE_TOPIC).getTopic());
364         assertSame(DMAAP_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.DMAAP, DMAAP_SOURCE_TOPIC).getTopic());
365
366         assertThatIllegalStateException()
367                 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SINK_TOPIC));
368         assertThatIllegalStateException()
369                 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.UEB, UEB_SINK_TOPIC));
370         assertThatIllegalStateException()
371                 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.DMAAP, DMAAP_SINK_TOPIC));
372     }
373
374     @Test
375     public void testGetTopicSink() {
376         TopicEndpoint manager = new TopicEndpointProxy();
377         manager.addTopicSinks(configuration);
378
379         assertSame(NOOP_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SINK_TOPIC).getTopic());
380         assertSame(UEB_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.UEB, UEB_SINK_TOPIC).getTopic());
381         assertSame(DMAAP_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.DMAAP, DMAAP_SINK_TOPIC).getTopic());
382
383         assertThatIllegalStateException()
384                 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC));
385         assertThatIllegalStateException()
386                 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.UEB, UEB_SOURCE_TOPIC));
387         assertThatIllegalStateException()
388                 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.DMAAP, DMAAP_SOURCE_TOPIC));
389     }
390
391     @Test
392     public void testGetUebTopicSource() {
393         TopicEndpoint manager = new TopicEndpointProxy();
394         manager.addTopicSources(configuration);
395
396         assertSame(UEB_SOURCE_TOPIC, manager.getUebTopicSource(UEB_SOURCE_TOPIC).getTopic());
397
398         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(NOOP_SOURCE_TOPIC));
399         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(DMAAP_SOURCE_TOPIC));
400
401         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(null));
402         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(""));
403     }
404
405     @Test
406     public void testGetUebTopicSink() {
407         TopicEndpoint manager = new TopicEndpointProxy();
408         manager.addTopicSinks(configuration);
409
410         assertSame(UEB_SINK_TOPIC, manager.getUebTopicSink(UEB_SINK_TOPIC).getTopic());
411
412         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(NOOP_SINK_TOPIC));
413         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(DMAAP_SINK_TOPIC));
414
415         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(null));
416         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(""));
417     }
418
419     @Test
420     public void testGetDmaapTopicSource() {
421         TopicEndpoint manager = new TopicEndpointProxy();
422         manager.addTopicSources(configuration);
423
424         assertSame(DMAAP_SOURCE_TOPIC, manager.getDmaapTopicSource(DMAAP_SOURCE_TOPIC).getTopic());
425
426         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(NOOP_SOURCE_TOPIC));
427         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(UEB_SOURCE_TOPIC));
428
429         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(null));
430         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(""));
431     }
432
433     @Test
434     public void testGetDmaapTopicSink() {
435         TopicEndpoint manager = new TopicEndpointProxy();
436         manager.addTopicSinks(configuration);
437
438         assertSame(DMAAP_SINK_TOPIC, manager.getDmaapTopicSink(DMAAP_SINK_TOPIC).getTopic());
439
440         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(NOOP_SINK_TOPIC));
441         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(UEB_SINK_TOPIC));
442
443         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(null));
444         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(""));
445     }
446
447     @Test
448     public void testGetNoopTopicSource() {
449         TopicEndpoint manager = new TopicEndpointProxy();
450         manager.addTopicSources(configuration);
451
452         assertSame(NOOP_SOURCE_TOPIC, manager.getNoopTopicSource(NOOP_SOURCE_TOPIC).getTopic());
453
454         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(DMAAP_SOURCE_TOPIC));
455         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(UEB_SOURCE_TOPIC));
456
457         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(null));
458         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(""));
459     }
460
461     @Test
462     public void testGetNoopTopicSink() {
463         TopicEndpoint manager = new TopicEndpointProxy();
464         manager.addTopicSinks(configuration);
465
466         assertSame(NOOP_SINK_TOPIC, manager.getNoopTopicSink(NOOP_SINK_TOPIC).getTopic());
467
468         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(DMAAP_SINK_TOPIC));
469         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(UEB_SINK_TOPIC));
470
471         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(null));
472         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(""));
473     }
474 }