2 * ============LICENSE_START=======================================================
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
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.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;
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;
48 public class TopicEndpointProxyTest {
50 private static final String NOOP_SOURCE_TOPIC = "noop-source";
51 private static final String NOOP_SINK_TOPIC = "noop-sink";
53 private static final String UEB_SOURCE_TOPIC = "ueb-source";
54 private static final String UEB_SINK_TOPIC = "ueb-sink";
56 private static final String DMAAP_SOURCE_TOPIC = "dmaap-source";
57 private static final String DMAAP_SINK_TOPIC = "dmaap-sink";
59 private Properties configuration = new Properties();
60 private TopicParameterGroup group = new TopicParameterGroup();
65 public TopicEndpointProxyTest() {
66 group.setTopicSinks(new LinkedList<>());
67 group.setTopicSources(new LinkedList<>());
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());
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());
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());
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());
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());
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());
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);
113 private <T extends Topic> boolean exists(List<T> topics, String topicName) {
114 return topics.stream().map(Topic::getTopic).anyMatch(topicName::equals);
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);
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);
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);
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);
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 assertThatCode(() -> new GsonTestUtils().compareGson(manager, TopicEndpointProxyTest.class))
158 .doesNotThrowAnyException();
162 public void testAddTopicSourcesListOfTopicParameters() {
163 TopicEndpoint manager = new TopicEndpointProxy();
165 List<TopicSource> sources = manager.addTopicSources(group.getTopicSources());
166 assertSame(3, sources.size());
168 assertTrue(allSources(sources));
169 assertFalse(anySink(sources));
173 public void testAddTopicSourcesProperties() {
174 TopicEndpoint manager = new TopicEndpointProxy();
176 List<TopicSource> sources = manager.addTopicSources(configuration);
177 assertSame(3, sources.size());
179 assertTrue(allSources(sources));
180 assertFalse(anySink(sources));
184 public void testAddTopicSinksListOfTopicParameters() {
185 TopicEndpoint manager = new TopicEndpointProxy();
187 List<TopicSink> sinks = manager.addTopicSinks(group.getTopicSinks());
188 assertSame(3, sinks.size());
190 assertFalse(anySource(sinks));
191 assertTrue(allSinks(sinks));
195 public void testAddTopicSinksProperties() {
196 TopicEndpoint manager = new TopicEndpointProxy();
198 List<TopicSink> sinks = manager.addTopicSinks(configuration);
199 assertSame(3, sinks.size());
201 assertFalse(anySource(sinks));
202 assertTrue(allSinks(sinks));
206 public void testAddTopicsProperties() {
207 TopicEndpoint manager = new TopicEndpointProxy();
209 List<Topic> topics = manager.addTopics(configuration);
210 assertSame(6, topics.size());
212 assertTrue(allSources(topics));
213 assertTrue(allSinks(topics));
217 public void testAddTopicsTopicParameterGroup() {
218 TopicEndpoint manager = new TopicEndpointProxy();
220 List<Topic> topics = manager.addTopics(group);
221 assertSame(6, topics.size());
223 assertTrue(allSources(topics));
224 assertTrue(allSinks(topics));
228 public void testAddTopicsTopicParameterGroupNull() {
229 TopicEndpoint manager = new TopicEndpointProxy();
231 List<Topic> topics = manager.addTopics(new TopicParameterGroup());
232 assertEquals(0, topics.size());
236 public void testLockSinks_lockSources_locked() {
237 TopicEndpoint manager = new TopicEndpointProxy();
239 for (Topic topic : manager.addTopics(group)) {
240 assertTrue(topic.isLocked());
245 public void testLockSinks_lockSources_unlocked() {
246 TopicEndpoint manager = new TopicEndpointProxy();
247 for (Topic topic : manager.addTopics(group)) {
248 assertFalse(topic.isLocked());
253 public void testGetTopicSources() {
254 TopicEndpoint manager = new TopicEndpointProxy();
256 manager.addTopicSources(configuration);
257 manager.addTopicSinks(configuration);
259 List<TopicSource> sources = manager.getTopicSources();
260 assertSame(3, sources.size());
262 assertTrue(allSources(sources));
263 assertFalse(anySink(sources));
267 public void testGetTopicSinks() {
268 TopicEndpoint manager = new TopicEndpointProxy();
270 manager.addTopicSources(configuration);
271 manager.addTopicSinks(configuration);
273 List<TopicSink> sinks = manager.getTopicSinks();
274 assertSame(3, sinks.size());
276 assertFalse(anySource(sinks));
277 assertTrue(allSinks(sinks));
281 public void testGetUebTopicSources() {
282 TopicEndpoint manager = new TopicEndpointProxy();
284 manager.addTopicSources(configuration);
285 assertSame(1, manager.getUebTopicSources().size());
289 public void testGetDmaapTopicSources() {
290 TopicEndpoint manager = new TopicEndpointProxy();
292 manager.addTopicSources(configuration);
293 assertSame(1, manager.getDmaapTopicSources().size());
297 public void testGetNoopTopicSources() {
298 TopicEndpoint manager = new TopicEndpointProxy();
300 manager.addTopicSources(configuration);
301 assertSame(1, manager.getNoopTopicSources().size());
305 public void testGetUebTopicSinks() {
306 TopicEndpoint manager = new TopicEndpointProxy();
308 manager.addTopicSinks(configuration);
309 assertSame(1, manager.getUebTopicSinks().size());
313 public void testGetDmaapTopicSinks() {
314 TopicEndpoint manager = new TopicEndpointProxy();
316 manager.addTopicSinks(configuration);
317 assertSame(1, manager.getDmaapTopicSinks().size());
321 public void testGetNoopTopicSinks() {
322 TopicEndpoint manager = new TopicEndpointProxy();
324 manager.addTopicSinks(configuration);
325 assertSame(1, manager.getNoopTopicSinks().size());
329 public void testLifecycle() {
330 TopicEndpoint manager = new TopicEndpointProxy();
332 assertTrue(manager.start());
333 assertTrue(manager.isAlive());
335 assertTrue(manager.stop());
336 assertFalse(manager.isAlive());
338 assertTrue(manager.start());
339 assertTrue(manager.isAlive());
342 assertFalse(manager.isAlive());
346 public void testLock() {
347 TopicEndpoint manager = new TopicEndpointProxy();
350 assertTrue(manager.isLocked());
353 assertFalse(manager.isLocked());
357 public void testGetTopicSource() {
358 TopicEndpoint manager = new TopicEndpointProxy();
359 manager.addTopicSources(configuration);
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());
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));
374 public void testGetTopicSink() {
375 TopicEndpoint manager = new TopicEndpointProxy();
376 manager.addTopicSinks(configuration);
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());
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));
391 public void testGetUebTopicSource() {
392 TopicEndpoint manager = new TopicEndpointProxy();
393 manager.addTopicSources(configuration);
395 assertSame(UEB_SOURCE_TOPIC, manager.getUebTopicSource(UEB_SOURCE_TOPIC).getTopic());
397 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(NOOP_SOURCE_TOPIC));
398 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(DMAAP_SOURCE_TOPIC));
400 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(null));
401 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(""));
405 public void testGetUebTopicSink() {
406 TopicEndpoint manager = new TopicEndpointProxy();
407 manager.addTopicSinks(configuration);
409 assertSame(UEB_SINK_TOPIC, manager.getUebTopicSink(UEB_SINK_TOPIC).getTopic());
411 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(NOOP_SINK_TOPIC));
412 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(DMAAP_SINK_TOPIC));
414 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(null));
415 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(""));
419 public void testGetDmaapTopicSource() {
420 TopicEndpoint manager = new TopicEndpointProxy();
421 manager.addTopicSources(configuration);
423 assertSame(DMAAP_SOURCE_TOPIC, manager.getDmaapTopicSource(DMAAP_SOURCE_TOPIC).getTopic());
425 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(NOOP_SOURCE_TOPIC));
426 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(UEB_SOURCE_TOPIC));
428 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(null));
429 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(""));
433 public void testGetDmaapTopicSink() {
434 TopicEndpoint manager = new TopicEndpointProxy();
435 manager.addTopicSinks(configuration);
437 assertSame(DMAAP_SINK_TOPIC, manager.getDmaapTopicSink(DMAAP_SINK_TOPIC).getTopic());
439 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(NOOP_SINK_TOPIC));
440 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(UEB_SINK_TOPIC));
442 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(null));
443 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(""));
447 public void testGetNoopTopicSource() {
448 TopicEndpoint manager = new TopicEndpointProxy();
449 manager.addTopicSources(configuration);
451 assertSame(NOOP_SOURCE_TOPIC, manager.getNoopTopicSource(NOOP_SOURCE_TOPIC).getTopic());
453 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(DMAAP_SOURCE_TOPIC));
454 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(UEB_SOURCE_TOPIC));
456 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(null));
457 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(""));
461 public void testGetNoopTopicSink() {
462 TopicEndpoint manager = new TopicEndpointProxy();
463 manager.addTopicSinks(configuration);
465 assertSame(NOOP_SINK_TOPIC, manager.getNoopTopicSink(NOOP_SINK_TOPIC).getTopic());
467 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(DMAAP_SINK_TOPIC));
468 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(UEB_SINK_TOPIC));
470 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(null));
471 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(""));