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