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.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;
35 import org.junit.After;
36 import org.junit.Test;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicFactories;
39 import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicPropertyBuilder;
40 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
41 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicPropertyBuilder;
42 import org.onap.policy.common.endpoints.event.comm.bus.UebTopicFactories;
43 import org.onap.policy.common.endpoints.event.comm.bus.UebTopicPropertyBuilder;
44 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
45 import org.onap.policy.common.endpoints.parameters.TopicParameters;
46 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
47 import org.onap.policy.common.utils.gson.GsonTestUtils;
49 public class TopicEndpointProxyTest {
51 private static final String NOOP_SOURCE_TOPIC = "noop-source";
52 private static final String NOOP_SINK_TOPIC = "noop-sink";
54 private static final String UEB_SOURCE_TOPIC = "ueb-source";
55 private static final String UEB_SINK_TOPIC = "ueb-sink";
57 private static final String DMAAP_SOURCE_TOPIC = "dmaap-source";
58 private static final String DMAAP_SINK_TOPIC = "dmaap-sink";
60 private Properties configuration = new Properties();
61 private TopicParameterGroup group = new TopicParameterGroup();
66 public TopicEndpointProxyTest() {
67 group.setTopicSinks(new LinkedList<>());
68 group.setTopicSources(new LinkedList<>());
70 NoopTopicPropertyBuilder noopSourceBuilder =
71 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS)
72 .makeTopic(NOOP_SOURCE_TOPIC);
73 configuration.putAll(noopSourceBuilder.build());
74 group.getTopicSources().add(noopSourceBuilder.getParams());
76 NoopTopicPropertyBuilder noopSinkBuilder =
77 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS)
78 .makeTopic(NOOP_SINK_TOPIC);
79 configuration.putAll(noopSinkBuilder.build());
80 group.getTopicSinks().add(noopSinkBuilder.getParams());
82 UebTopicPropertyBuilder uebSourceBuilder =
83 new UebTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS)
84 .makeTopic(UEB_SOURCE_TOPIC);
85 configuration.putAll(uebSourceBuilder.build());
86 group.getTopicSources().add(uebSourceBuilder.getParams());
88 UebTopicPropertyBuilder uebSinkBuilder =
89 new UebTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS)
90 .makeTopic(UEB_SINK_TOPIC);
91 configuration.putAll(uebSinkBuilder.build());
92 group.getTopicSinks().add(uebSinkBuilder.getParams());
94 DmaapTopicPropertyBuilder dmaapSourceBuilder =
95 new DmaapTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS)
96 .makeTopic(DMAAP_SOURCE_TOPIC);
97 configuration.putAll(dmaapSourceBuilder.build());
98 group.getTopicSources().add(dmaapSourceBuilder.getParams());
100 DmaapTopicPropertyBuilder dmaapSinkBuilder =
101 new DmaapTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS)
102 .makeTopic(DMAAP_SINK_TOPIC);
103 configuration.putAll(dmaapSinkBuilder.build());
104 group.getTopicSinks().add(dmaapSinkBuilder.getParams());
106 TopicParameters invalidCommInfraParams =
107 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS)
108 .makeTopic(NOOP_SOURCE_TOPIC).getParams();
109 invalidCommInfraParams.setTopicCommInfrastructure(Topic.CommInfrastructure.REST.name());
110 group.getTopicSources().add(invalidCommInfraParams);
111 group.getTopicSinks().add(invalidCommInfraParams);
114 private <T extends Topic> boolean exists(List<T> topics, String topicName) {
115 return topics.stream().map(Topic::getTopic).anyMatch(topicName::equals);
118 private <T extends Topic> boolean allSources(List<T> topics) {
119 return exists(topics, NOOP_SOURCE_TOPIC) && exists(topics, UEB_SOURCE_TOPIC)
120 && exists(topics, DMAAP_SOURCE_TOPIC);
123 private <T extends Topic> boolean allSinks(List<T> topics) {
124 return exists(topics, NOOP_SINK_TOPIC) && exists(topics, UEB_SINK_TOPIC) && exists(topics, DMAAP_SINK_TOPIC);
127 private <T extends Topic> boolean anySource(List<T> topics) {
128 return exists(topics, NOOP_SOURCE_TOPIC) || exists(topics, UEB_SOURCE_TOPIC)
129 || exists(topics, DMAAP_SOURCE_TOPIC);
132 private <T extends Topic> boolean anySink(List<T> topics) {
133 return exists(topics, NOOP_SINK_TOPIC) || exists(topics, UEB_SINK_TOPIC) || exists(topics, DMAAP_SINK_TOPIC);
137 * Destroys all managed topics.
140 public void tearDown() {
141 NoopTopicFactories.getSinkFactory().destroy();
142 NoopTopicFactories.getSourceFactory().destroy();
144 UebTopicFactories.getSinkFactory().destroy();
145 UebTopicFactories.getSourceFactory().destroy();
147 DmaapTopicFactories.getSinkFactory().destroy();
148 DmaapTopicFactories.getSourceFactory().destroy();
152 public void testSerialize() {
153 TopicEndpoint manager = new TopicEndpointProxy();
155 manager.addTopicSources(configuration);
156 manager.addTopicSinks(configuration);
158 assertThatCode(() -> new GsonTestUtils().compareGson(manager, TopicEndpointProxyTest.class))
159 .doesNotThrowAnyException();
163 public void testAddTopicSourcesListOfTopicParameters() {
164 TopicEndpoint manager = new TopicEndpointProxy();
166 List<TopicSource> sources = manager.addTopicSources(group.getTopicSources());
167 assertSame(3, sources.size());
169 assertTrue(allSources(sources));
170 assertFalse(anySink(sources));
174 public void testAddTopicSourcesProperties() {
175 TopicEndpoint manager = new TopicEndpointProxy();
177 List<TopicSource> sources = manager.addTopicSources(configuration);
178 assertSame(3, sources.size());
180 assertTrue(allSources(sources));
181 assertFalse(anySink(sources));
185 public void testAddTopicSinksListOfTopicParameters() {
186 TopicEndpoint manager = new TopicEndpointProxy();
188 List<TopicSink> sinks = manager.addTopicSinks(group.getTopicSinks());
189 assertSame(3, sinks.size());
191 assertFalse(anySource(sinks));
192 assertTrue(allSinks(sinks));
196 public void testAddTopicSinksProperties() {
197 TopicEndpoint manager = new TopicEndpointProxy();
199 List<TopicSink> sinks = manager.addTopicSinks(configuration);
200 assertSame(3, sinks.size());
202 assertFalse(anySource(sinks));
203 assertTrue(allSinks(sinks));
207 public void testAddTopicsProperties() {
208 TopicEndpoint manager = new TopicEndpointProxy();
210 List<Topic> topics = manager.addTopics(configuration);
211 assertSame(6, topics.size());
213 assertTrue(allSources(topics));
214 assertTrue(allSinks(topics));
218 public void testAddTopicsTopicParameterGroup() {
219 TopicEndpoint manager = new TopicEndpointProxy();
221 List<Topic> topics = manager.addTopics(group);
222 assertSame(6, topics.size());
224 assertTrue(allSources(topics));
225 assertTrue(allSinks(topics));
229 public void testAddTopicsTopicParameterGroupNull() {
230 TopicEndpoint manager = new TopicEndpointProxy();
232 List<Topic> topics = manager.addTopics(new TopicParameterGroup());
233 assertEquals(0, topics.size());
237 public void testLockSinks_lockSources_locked() {
238 TopicEndpoint manager = new TopicEndpointProxy();
240 for (Topic topic : manager.addTopics(group)) {
241 assertTrue(topic.isLocked());
246 public void testLockSinks_lockSources_unlocked() {
247 TopicEndpoint manager = new TopicEndpointProxy();
248 for (Topic topic : manager.addTopics(group)) {
249 assertFalse(topic.isLocked());
254 public void testGetTopicSources() {
255 TopicEndpoint manager = new TopicEndpointProxy();
257 manager.addTopicSources(configuration);
258 manager.addTopicSinks(configuration);
260 List<TopicSource> sources = manager.getTopicSources();
261 assertSame(3, sources.size());
263 assertTrue(allSources(sources));
264 assertFalse(anySink(sources));
268 public void testGetTopicSinks() {
269 TopicEndpoint manager = new TopicEndpointProxy();
271 manager.addTopicSources(configuration);
272 manager.addTopicSinks(configuration);
274 List<TopicSink> sinks = manager.getTopicSinks();
275 assertSame(3, sinks.size());
277 assertFalse(anySource(sinks));
278 assertTrue(allSinks(sinks));
282 public void testGetUebTopicSources() {
283 TopicEndpoint manager = new TopicEndpointProxy();
285 manager.addTopicSources(configuration);
286 assertSame(1, manager.getUebTopicSources().size());
290 public void testGetDmaapTopicSources() {
291 TopicEndpoint manager = new TopicEndpointProxy();
293 manager.addTopicSources(configuration);
294 assertSame(1, manager.getDmaapTopicSources().size());
298 public void testGetNoopTopicSources() {
299 TopicEndpoint manager = new TopicEndpointProxy();
301 manager.addTopicSources(configuration);
302 assertSame(1, manager.getNoopTopicSources().size());
306 public void testGetUebTopicSinks() {
307 TopicEndpoint manager = new TopicEndpointProxy();
309 manager.addTopicSinks(configuration);
310 assertSame(1, manager.getUebTopicSinks().size());
314 public void testGetDmaapTopicSinks() {
315 TopicEndpoint manager = new TopicEndpointProxy();
317 manager.addTopicSinks(configuration);
318 assertSame(1, manager.getDmaapTopicSinks().size());
322 public void testGetNoopTopicSinks() {
323 TopicEndpoint manager = new TopicEndpointProxy();
325 manager.addTopicSinks(configuration);
326 assertSame(1, manager.getNoopTopicSinks().size());
330 public void testLifecycle() {
331 TopicEndpoint manager = new TopicEndpointProxy();
333 assertTrue(manager.start());
334 assertTrue(manager.isAlive());
336 assertTrue(manager.stop());
337 assertFalse(manager.isAlive());
339 assertTrue(manager.start());
340 assertTrue(manager.isAlive());
343 assertFalse(manager.isAlive());
347 public void testLock() {
348 TopicEndpoint manager = new TopicEndpointProxy();
351 assertTrue(manager.isLocked());
354 assertFalse(manager.isLocked());
358 public void testGetTopicSource() {
359 TopicEndpoint manager = new TopicEndpointProxy();
360 manager.addTopicSources(configuration);
362 assertSame(NOOP_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC).getTopic());
363 assertSame(UEB_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.UEB, UEB_SOURCE_TOPIC).getTopic());
364 assertSame(DMAAP_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.DMAAP, DMAAP_SOURCE_TOPIC).getTopic());
366 assertThatIllegalStateException()
367 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SINK_TOPIC));
368 assertThatIllegalStateException()
369 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.UEB, UEB_SINK_TOPIC));
370 assertThatIllegalStateException()
371 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.DMAAP, DMAAP_SINK_TOPIC));
375 public void testGetTopicSink() {
376 TopicEndpoint manager = new TopicEndpointProxy();
377 manager.addTopicSinks(configuration);
379 assertSame(NOOP_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SINK_TOPIC).getTopic());
380 assertSame(UEB_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.UEB, UEB_SINK_TOPIC).getTopic());
381 assertSame(DMAAP_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.DMAAP, DMAAP_SINK_TOPIC).getTopic());
383 assertThatIllegalStateException()
384 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC));
385 assertThatIllegalStateException()
386 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.UEB, UEB_SOURCE_TOPIC));
387 assertThatIllegalStateException()
388 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.DMAAP, DMAAP_SOURCE_TOPIC));
392 public void testGetUebTopicSource() {
393 TopicEndpoint manager = new TopicEndpointProxy();
394 manager.addTopicSources(configuration);
396 assertSame(UEB_SOURCE_TOPIC, manager.getUebTopicSource(UEB_SOURCE_TOPIC).getTopic());
398 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(NOOP_SOURCE_TOPIC));
399 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(DMAAP_SOURCE_TOPIC));
401 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(null));
402 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(""));
406 public void testGetUebTopicSink() {
407 TopicEndpoint manager = new TopicEndpointProxy();
408 manager.addTopicSinks(configuration);
410 assertSame(UEB_SINK_TOPIC, manager.getUebTopicSink(UEB_SINK_TOPIC).getTopic());
412 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(NOOP_SINK_TOPIC));
413 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(DMAAP_SINK_TOPIC));
415 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(null));
416 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(""));
420 public void testGetDmaapTopicSource() {
421 TopicEndpoint manager = new TopicEndpointProxy();
422 manager.addTopicSources(configuration);
424 assertSame(DMAAP_SOURCE_TOPIC, manager.getDmaapTopicSource(DMAAP_SOURCE_TOPIC).getTopic());
426 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(NOOP_SOURCE_TOPIC));
427 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(UEB_SOURCE_TOPIC));
429 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(null));
430 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(""));
434 public void testGetDmaapTopicSink() {
435 TopicEndpoint manager = new TopicEndpointProxy();
436 manager.addTopicSinks(configuration);
438 assertSame(DMAAP_SINK_TOPIC, manager.getDmaapTopicSink(DMAAP_SINK_TOPIC).getTopic());
440 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(NOOP_SINK_TOPIC));
441 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(UEB_SINK_TOPIC));
443 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(null));
444 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(""));
449 public void testGetNoopTopicSource() {
450 TopicEndpoint manager = new TopicEndpointProxy();
451 manager.addTopicSources(configuration);
453 assertSame(NOOP_SOURCE_TOPIC, manager.getNoopTopicSource(NOOP_SOURCE_TOPIC).getTopic());
455 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(DMAAP_SOURCE_TOPIC));
456 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(UEB_SOURCE_TOPIC));
458 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(null));
459 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(""));
463 public void testGetNoopTopicSink() {
464 TopicEndpoint manager = new TopicEndpointProxy();
465 manager.addTopicSinks(configuration);
467 assertSame(NOOP_SINK_TOPIC, manager.getNoopTopicSink(NOOP_SINK_TOPIC).getTopic());
469 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(DMAAP_SINK_TOPIC));
470 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(UEB_SINK_TOPIC));
472 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(null));
473 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(""));