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