2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.common.endpoints.event.comm;
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;
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;
47 public class TopicEndpointProxyTest {
49 private static final String NOOP_SOURCE_TOPIC = "noop-source";
50 private static final String NOOP_SINK_TOPIC = "noop-sink";
52 private static final String UEB_SOURCE_TOPIC = "ueb-source";
53 private static final String UEB_SINK_TOPIC = "ueb-sink";
55 private static final String DMAAP_SOURCE_TOPIC = "dmaap-source";
56 private static final String DMAAP_SINK_TOPIC = "dmaap-sink";
58 private Properties configuration = new Properties();
59 private TopicParameterGroup group = new TopicParameterGroup();
64 public TopicEndpointProxyTest() {
65 group.setTopicSinks(new LinkedList<>());
66 group.setTopicSources(new LinkedList<>());
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());
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());
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());
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());
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());
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());
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);
112 private <T extends Topic> boolean exists(List<T> topics, String topicName) {
113 return topics.stream().map(Topic::getTopic).anyMatch(topicName::equals);
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);
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);
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);
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);
141 * Destroys all managed topics.
144 public void tearDown() {
145 NoopTopicFactories.getSinkFactory().destroy();
146 NoopTopicFactories.getSourceFactory().destroy();
148 UebTopicFactories.getSinkFactory().destroy();
149 UebTopicFactories.getSourceFactory().destroy();
151 DmaapTopicFactories.getSinkFactory().destroy();
152 DmaapTopicFactories.getSourceFactory().destroy();
156 public void testSerialize() {
157 TopicEndpoint manager = new TopicEndpointProxy();
159 manager.addTopicSources(configuration);
160 manager.addTopicSinks(configuration);
162 new GsonTestUtils().compareGson(manager, TopicEndpointProxyTest.class);
166 public void addTopicSourcesListOfTopicParameters() {
167 TopicEndpoint manager = new TopicEndpointProxy();
169 List<TopicSource> sources = manager.addTopicSources(group.getTopicSources());
170 assertSame(3, sources.size());
172 assertTrue(allSources(sources));
173 assertFalse(anySink(sources));
177 public void addTopicSourcesProperties() {
178 TopicEndpoint manager = new TopicEndpointProxy();
180 List<TopicSource> sources = manager.addTopicSources(configuration);
181 assertSame(3, sources.size());
183 assertTrue(allSources(sources));
184 assertFalse(anySink(sources));
188 public void addTopicSinksListOfTopicParameters() {
189 TopicEndpoint manager = new TopicEndpointProxy();
191 List<TopicSink> sinks = manager.addTopicSinks(group.getTopicSinks());
192 assertSame(3, sinks.size());
194 assertFalse(anySource(sinks));
195 assertTrue(allSinks(sinks));
199 public void addTopicSinksProperties() {
200 TopicEndpoint manager = new TopicEndpointProxy();
202 List<TopicSink> sinks = manager.addTopicSinks(configuration);
203 assertSame(3, sinks.size());
205 assertFalse(anySource(sinks));
206 assertTrue(allSinks(sinks));
210 public void addTopicsProperties() {
211 TopicEndpoint manager = new TopicEndpointProxy();
213 List<Topic> topics = manager.addTopics(configuration);
214 assertSame(6, topics.size());
216 assertTrue(allSources(topics));
217 assertTrue(allSinks(topics));
221 public void addTopicsTopicParameterGroup() {
222 TopicEndpoint manager = new TopicEndpointProxy();
224 List<Topic> topics = manager.addTopics(group);
225 assertSame(6, topics.size());
227 assertTrue(allSources(topics));
228 assertTrue(allSinks(topics));
232 public void addTopicsTopicParameterGroupNull() {
233 TopicEndpoint manager = new TopicEndpointProxy();
235 List<Topic> topics = manager.addTopics(new TopicParameterGroup());
236 assertEquals(0, topics.size());
240 public void lockSinks_lockSources_locked() {
241 TopicEndpoint manager = new TopicEndpointProxy();
243 for (Topic topic : manager.addTopics(group)) {
244 assertTrue(topic.isLocked());
249 public void lockSinks_lockSources_unlocked() {
250 TopicEndpoint manager = new TopicEndpointProxy();
251 for (Topic topic : manager.addTopics(group)) {
252 assertFalse(topic.isLocked());
257 public void getTopicSources() {
258 TopicEndpoint manager = new TopicEndpointProxy();
260 manager.addTopicSources(configuration);
261 manager.addTopicSinks(configuration);
263 List<TopicSource> sources = manager.getTopicSources();
264 assertSame(3, sources.size());
266 assertTrue(allSources(sources));
267 assertFalse(anySink(sources));
271 public void getTopicSinks() {
272 TopicEndpoint manager = new TopicEndpointProxy();
274 manager.addTopicSources(configuration);
275 manager.addTopicSinks(configuration);
277 List<TopicSink> sinks = manager.getTopicSinks();
278 assertSame(3, sinks.size());
280 assertFalse(anySource(sinks));
281 assertTrue(allSinks(sinks));
285 public void getUebTopicSources() {
286 TopicEndpoint manager = new TopicEndpointProxy();
288 manager.addTopicSources(configuration);
289 assertSame(1, manager.getUebTopicSources().size());
293 public void getDmaapTopicSources() {
294 TopicEndpoint manager = new TopicEndpointProxy();
296 manager.addTopicSources(configuration);
297 assertSame(1, manager.getDmaapTopicSources().size());
301 public void getNoopTopicSources() {
302 TopicEndpoint manager = new TopicEndpointProxy();
304 manager.addTopicSources(configuration);
305 assertSame(1, manager.getNoopTopicSources().size());
309 public void getUebTopicSinks() {
310 TopicEndpoint manager = new TopicEndpointProxy();
312 manager.addTopicSinks(configuration);
313 assertSame(1, manager.getUebTopicSinks().size());
317 public void getDmaapTopicSinks() {
318 TopicEndpoint manager = new TopicEndpointProxy();
320 manager.addTopicSinks(configuration);
321 assertSame(1, manager.getDmaapTopicSinks().size());
325 public void getNoopTopicSinks() {
326 TopicEndpoint manager = new TopicEndpointProxy();
328 manager.addTopicSinks(configuration);
329 assertSame(1, manager.getNoopTopicSinks().size());
333 public void lifecycle() {
334 TopicEndpoint manager = new TopicEndpointProxy();
336 assertTrue(manager.start());
337 assertTrue(manager.isAlive());
339 assertTrue(manager.stop());
340 assertFalse(manager.isAlive());
342 assertTrue(manager.start());
343 assertTrue(manager.isAlive());
346 assertFalse(manager.isAlive());
351 TopicEndpoint manager = new TopicEndpointProxy();
354 assertTrue(manager.isLocked());
357 assertFalse(manager.isLocked());
361 public void getTopicSource() {
362 TopicEndpoint manager = new TopicEndpointProxy();
363 manager.addTopicSources(configuration);
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());
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));
378 public void getTopicSink() {
379 TopicEndpoint manager = new TopicEndpointProxy();
380 manager.addTopicSinks(configuration);
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());
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));
395 public void getUebTopicSource() {
396 TopicEndpoint manager = new TopicEndpointProxy();
397 manager.addTopicSources(configuration);
399 assertSame(UEB_SOURCE_TOPIC, manager.getUebTopicSource(UEB_SOURCE_TOPIC).getTopic());
401 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(NOOP_SOURCE_TOPIC));
402 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(DMAAP_SOURCE_TOPIC));
404 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(null));
405 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(""));
409 public void getUebTopicSink() {
410 TopicEndpoint manager = new TopicEndpointProxy();
411 manager.addTopicSinks(configuration);
413 assertSame(UEB_SINK_TOPIC, manager.getUebTopicSink(UEB_SINK_TOPIC).getTopic());
415 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(NOOP_SINK_TOPIC));
416 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(DMAAP_SINK_TOPIC));
418 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(null));
419 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(""));
423 public void getDmaapTopicSource() {
424 TopicEndpoint manager = new TopicEndpointProxy();
425 manager.addTopicSources(configuration);
427 assertSame(DMAAP_SOURCE_TOPIC, manager.getDmaapTopicSource(DMAAP_SOURCE_TOPIC).getTopic());
429 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(NOOP_SOURCE_TOPIC));
430 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(UEB_SOURCE_TOPIC));
432 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(null));
433 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(""));
437 public void getDmaapTopicSink() {
438 TopicEndpoint manager = new TopicEndpointProxy();
439 manager.addTopicSinks(configuration);
441 assertSame(DMAAP_SINK_TOPIC, manager.getDmaapTopicSink(DMAAP_SINK_TOPIC).getTopic());
443 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(NOOP_SINK_TOPIC));
444 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(UEB_SINK_TOPIC));
446 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(null));
447 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(""));
452 public void getNoopTopicSource() {
453 TopicEndpoint manager = new TopicEndpointProxy();
454 manager.addTopicSources(configuration);
456 assertSame(NOOP_SOURCE_TOPIC, manager.getNoopTopicSource(NOOP_SOURCE_TOPIC).getTopic());
458 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(DMAAP_SOURCE_TOPIC));
459 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(UEB_SOURCE_TOPIC));
461 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(null));
462 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(""));
466 public void getNoopTopicSink() {
467 TopicEndpoint manager = new TopicEndpointProxy();
468 manager.addTopicSinks(configuration);
470 assertSame(NOOP_SINK_TOPIC, manager.getNoopTopicSink(NOOP_SINK_TOPIC).getTopic());
472 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(DMAAP_SINK_TOPIC));
473 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(UEB_SINK_TOPIC));
475 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(null));
476 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(""));