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