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.assertFalse;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
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;
45 public class TopicEndpointProxyTest {
47 private static final String NOOP_SOURCE_TOPIC = "noop-source";
48 private static final String NOOP_SINK_TOPIC = "noop-sink";
50 private static final String UEB_SOURCE_TOPIC = "ueb-source";
51 private static final String UEB_SINK_TOPIC = "ueb-sink";
53 private static final String DMAAP_SOURCE_TOPIC = "dmaap-source";
54 private static final String DMAAP_SINK_TOPIC = "dmaap-sink";
56 private Properties configuration = new Properties();
57 private TopicParameterGroup group = new TopicParameterGroup();
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());
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());
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());
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());
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());
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());
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);
107 private <T extends Topic> boolean exists(List<T> topics, String topicName) {
108 return topics.stream().map(Topic::getTopic).anyMatch(topicName::equals);
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);
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);
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);
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);
136 * Destroys all managed topics.
139 public void tearDown() {
140 NoopTopicFactories.getSinkFactory().destroy();
141 NoopTopicFactories.getSourceFactory().destroy();
143 UebTopicFactories.getSinkFactory().destroy();
144 UebTopicFactories.getSourceFactory().destroy();
146 DmaapTopicFactories.getSinkFactory().destroy();
147 DmaapTopicFactories.getSourceFactory().destroy();
151 public void testSerialize() {
152 TopicEndpoint manager = new TopicEndpointProxy();
154 manager.addTopicSources(configuration);
155 manager.addTopicSinks(configuration);
157 new GsonTestUtils().compareGson(manager, TopicEndpointProxyTest.class);
161 public void addTopicSourcesListOfTopicParameters() {
162 TopicEndpoint manager = new TopicEndpointProxy();
164 List<TopicSource> sources = manager.addTopicSources(group.getTopicSources());
165 assertSame(3, sources.size());
167 assertTrue(allSources(sources));
168 assertFalse(anySink(sources));
172 public void addTopicSourcesProperties() {
173 TopicEndpoint manager = new TopicEndpointProxy();
175 List<TopicSource> sources = manager.addTopicSources(configuration);
176 assertSame(3, sources.size());
178 assertTrue(allSources(sources));
179 assertFalse(anySink(sources));
183 public void addTopicSinksListOfTopicParameters() {
184 TopicEndpoint manager = new TopicEndpointProxy();
186 List<TopicSink> sinks = manager.addTopicSinks(group.getTopicSinks());
187 assertSame(3, sinks.size());
189 assertFalse(anySource(sinks));
190 assertTrue(allSinks(sinks));
194 public void addTopicSinksProperties() {
195 TopicEndpoint manager = new TopicEndpointProxy();
197 List<TopicSink> sinks = manager.addTopicSinks(configuration);
198 assertSame(3, sinks.size());
200 assertFalse(anySource(sinks));
201 assertTrue(allSinks(sinks));
205 public void addTopicsProperties() {
206 TopicEndpoint manager = new TopicEndpointProxy();
208 List<Topic> topics = manager.addTopics(configuration);
209 assertSame(6, topics.size());
211 assertTrue(allSources(topics));
212 assertTrue(allSinks(topics));
216 public void addTopicsTopicParameterGroup() {
217 TopicEndpoint manager = new TopicEndpointProxy();
219 List<Topic> topics = manager.addTopics(group);
220 assertSame(6, topics.size());
222 assertTrue(allSources(topics));
223 assertTrue(allSinks(topics));
227 public void lockSinks_lockSources_locked() {
228 TopicEndpoint manager = new TopicEndpointProxy();
230 for (Topic topic : manager.addTopics(group)) {
231 assertTrue(topic.isLocked());
236 public void lockSinks_lockSources_unlocked() {
237 TopicEndpoint manager = new TopicEndpointProxy();
238 for (Topic topic : manager.addTopics(group)) {
239 assertFalse(topic.isLocked());
244 public void getTopicSources() {
245 TopicEndpoint manager = new TopicEndpointProxy();
247 manager.addTopicSources(configuration);
248 manager.addTopicSinks(configuration);
250 List<TopicSource> sources = manager.getTopicSources();
251 assertSame(3, sources.size());
253 assertTrue(allSources(sources));
254 assertFalse(anySink(sources));
258 public void getTopicSinks() {
259 TopicEndpoint manager = new TopicEndpointProxy();
261 manager.addTopicSources(configuration);
262 manager.addTopicSinks(configuration);
264 List<TopicSink> sinks = manager.getTopicSinks();
265 assertSame(3, sinks.size());
267 assertFalse(anySource(sinks));
268 assertTrue(allSinks(sinks));
272 public void getUebTopicSources() {
273 TopicEndpoint manager = new TopicEndpointProxy();
275 manager.addTopicSources(configuration);
276 assertSame(1, manager.getUebTopicSources().size());
280 public void getDmaapTopicSources() {
281 TopicEndpoint manager = new TopicEndpointProxy();
283 manager.addTopicSources(configuration);
284 assertSame(1, manager.getDmaapTopicSources().size());
288 public void getNoopTopicSources() {
289 TopicEndpoint manager = new TopicEndpointProxy();
291 manager.addTopicSources(configuration);
292 assertSame(1, manager.getNoopTopicSources().size());
296 public void getUebTopicSinks() {
297 TopicEndpoint manager = new TopicEndpointProxy();
299 manager.addTopicSinks(configuration);
300 assertSame(1, manager.getUebTopicSinks().size());
304 public void getDmaapTopicSinks() {
305 TopicEndpoint manager = new TopicEndpointProxy();
307 manager.addTopicSinks(configuration);
308 assertSame(1, manager.getDmaapTopicSinks().size());
312 public void getNoopTopicSinks() {
313 TopicEndpoint manager = new TopicEndpointProxy();
315 manager.addTopicSinks(configuration);
316 assertSame(1, manager.getNoopTopicSinks().size());
320 public void lifecycle() {
321 TopicEndpoint manager = new TopicEndpointProxy();
323 assertTrue(manager.start());
324 assertTrue(manager.isAlive());
326 assertTrue(manager.stop());
327 assertFalse(manager.isAlive());
329 assertTrue(manager.start());
330 assertTrue(manager.isAlive());
333 assertFalse(manager.isAlive());
338 TopicEndpoint manager = new TopicEndpointProxy();
341 assertTrue(manager.isLocked());
344 assertFalse(manager.isLocked());
348 public void getTopicSource() {
349 TopicEndpoint manager = new TopicEndpointProxy();
350 manager.addTopicSources(configuration);
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());
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));
365 public void getTopicSink() {
366 TopicEndpoint manager = new TopicEndpointProxy();
367 manager.addTopicSinks(configuration);
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());
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));
382 public void getUebTopicSource() {
383 TopicEndpoint manager = new TopicEndpointProxy();
384 manager.addTopicSources(configuration);
386 assertSame(UEB_SOURCE_TOPIC, manager.getUebTopicSource(UEB_SOURCE_TOPIC).getTopic());
388 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(NOOP_SOURCE_TOPIC));
389 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(DMAAP_SOURCE_TOPIC));
391 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(null));
392 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(""));
396 public void getUebTopicSink() {
397 TopicEndpoint manager = new TopicEndpointProxy();
398 manager.addTopicSinks(configuration);
400 assertSame(UEB_SINK_TOPIC, manager.getUebTopicSink(UEB_SINK_TOPIC).getTopic());
402 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(NOOP_SINK_TOPIC));
403 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(DMAAP_SINK_TOPIC));
405 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(null));
406 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(""));
410 public void getDmaapTopicSource() {
411 TopicEndpoint manager = new TopicEndpointProxy();
412 manager.addTopicSources(configuration);
414 assertSame(DMAAP_SOURCE_TOPIC, manager.getDmaapTopicSource(DMAAP_SOURCE_TOPIC).getTopic());
416 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(NOOP_SOURCE_TOPIC));
417 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(UEB_SOURCE_TOPIC));
419 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(null));
420 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(""));
424 public void getDmaapTopicSink() {
425 TopicEndpoint manager = new TopicEndpointProxy();
426 manager.addTopicSinks(configuration);
428 assertSame(DMAAP_SINK_TOPIC, manager.getDmaapTopicSink(DMAAP_SINK_TOPIC).getTopic());
430 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(NOOP_SINK_TOPIC));
431 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(UEB_SINK_TOPIC));
433 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(null));
434 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(""));
439 public void getNoopTopicSource() {
440 TopicEndpoint manager = new TopicEndpointProxy();
441 manager.addTopicSources(configuration);
443 assertSame(NOOP_SOURCE_TOPIC, manager.getNoopTopicSource(NOOP_SOURCE_TOPIC).getTopic());
445 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(DMAAP_SOURCE_TOPIC));
446 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(UEB_SOURCE_TOPIC));
448 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(null));
449 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(""));
453 public void getNoopTopicSink() {
454 TopicEndpoint manager = new TopicEndpointProxy();
455 manager.addTopicSinks(configuration);
457 assertSame(NOOP_SINK_TOPIC, manager.getNoopTopicSink(NOOP_SINK_TOPIC).getTopic());
459 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(DMAAP_SINK_TOPIC));
460 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(UEB_SINK_TOPIC));
462 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(null));
463 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(""));