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.Test;
32 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
33 import org.onap.policy.common.endpoints.event.comm.bus.DmaapTopicPropertyBuilder;
34 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicPropertyBuilder;
35 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
36 import org.onap.policy.common.utils.gson.GsonTestUtils;
38 public class TopicEndpointProxyTest {
40 private static final String NOOP_SOURCE_TOPIC = "noop-source";
41 private static final String NOOP_SINK_TOPIC = "noop-sink";
43 private static final String UEB_SOURCE_TOPIC = "ueb-source";
44 private static final String UEB_SINK_TOPIC = "ueb-sink";
46 private static final String DMAAP_SOURCE_TOPIC = "dmaap-source";
47 private static final String DMAAP_SINK_TOPIC = "dmaap-sink";
49 private Properties configuration = new Properties();
54 public TopicEndpointProxyTest() {
55 Properties noopSourceProperties =
56 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS)
57 .makeTopic(NOOP_SOURCE_TOPIC).build();
59 Properties noopSinkProperties =
60 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS)
61 .makeTopic(NOOP_SINK_TOPIC).build();
63 Properties uebSourceProperties =
64 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS)
65 .makeTopic(UEB_SOURCE_TOPIC).build();
67 Properties uebSinkProperties =
68 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS)
69 .makeTopic(UEB_SINK_TOPIC).build();
71 Properties dmaapSourceProperties =
72 new DmaapTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS)
73 .makeTopic(DMAAP_SOURCE_TOPIC).build();
75 Properties dmaapSinkProperties =
76 new DmaapTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS)
77 .makeTopic(DMAAP_SINK_TOPIC).build();
79 configuration.putAll(noopSourceProperties);
80 configuration.putAll(noopSinkProperties);
81 configuration.putAll(uebSourceProperties);
82 configuration.putAll(uebSinkProperties);
83 configuration.putAll(dmaapSourceProperties);
84 configuration.putAll(dmaapSinkProperties);
87 private <T extends Topic> boolean exists(List<T> topics, String topicName) {
88 return topics.stream().map(Topic::getTopic).anyMatch(topicName::equals);
91 private <T extends Topic> boolean allSources(List<T> topics) {
92 return exists(topics, NOOP_SOURCE_TOPIC)
93 && exists(topics, UEB_SOURCE_TOPIC)
94 && exists(topics, DMAAP_SOURCE_TOPIC);
97 private <T extends Topic> boolean allSinks(List<T> topics) {
98 return exists(topics, NOOP_SINK_TOPIC)
99 && exists(topics, UEB_SINK_TOPIC)
100 && exists(topics, DMAAP_SINK_TOPIC);
103 private <T extends Topic> boolean anySource(List<T> topics) {
104 return exists(topics, NOOP_SOURCE_TOPIC)
105 || exists(topics, UEB_SOURCE_TOPIC)
106 || exists(topics, DMAAP_SOURCE_TOPIC);
109 private <T extends Topic> boolean anySink(List<T> topics) {
110 return exists(topics, NOOP_SINK_TOPIC)
111 || exists(topics, UEB_SINK_TOPIC)
112 || exists(topics, DMAAP_SINK_TOPIC);
116 public void testSerialize() {
117 TopicEndpoint manager = new TopicEndpointProxy();
119 manager.addTopicSources(configuration);
120 manager.addTopicSinks(configuration);
122 new GsonTestUtils().compareGson(manager, TopicEndpointProxyTest.class);
126 public void addTopicSources() {
127 TopicEndpoint manager = new TopicEndpointProxy();
129 List<TopicSource> sources = manager.addTopicSources(configuration);
130 assertSame(3, sources.size());
132 assertTrue(allSources(sources));
133 assertFalse(anySink(sources));
137 public void addTopicSinks() {
138 TopicEndpoint manager = new TopicEndpointProxy();
140 List<TopicSink> sinks = manager.addTopicSinks(configuration);
141 assertSame(3, sinks.size());
143 assertFalse(anySource(sinks));
144 assertTrue(allSinks(sinks));
148 public void addTopics() {
149 TopicEndpoint manager = new TopicEndpointProxy();
151 List<Topic> topics = manager.addTopics(configuration);
152 assertSame(6, topics.size());
154 assertTrue(allSources(topics));
155 assertTrue(allSinks(topics));
159 public void getTopicSources() {
160 TopicEndpoint manager = new TopicEndpointProxy();
162 manager.addTopicSources(configuration);
163 manager.addTopicSinks(configuration);
165 List<TopicSource> sources = manager.getTopicSources();
166 assertSame(3, sources.size());
168 assertTrue(allSources(sources));
169 assertFalse(anySink(sources));
173 public void getTopicSinks() {
174 TopicEndpoint manager = new TopicEndpointProxy();
176 manager.addTopicSources(configuration);
177 manager.addTopicSinks(configuration);
179 List<TopicSink> sinks = manager.getTopicSinks();
180 assertSame(3, sinks.size());
182 assertFalse(anySource(sinks));
183 assertTrue(allSinks(sinks));
187 public void getUebTopicSources() {
188 TopicEndpoint manager = new TopicEndpointProxy();
190 manager.addTopicSources(configuration);
191 assertSame(1, manager.getUebTopicSources().size());
195 public void getDmaapTopicSources() {
196 TopicEndpoint manager = new TopicEndpointProxy();
198 manager.addTopicSources(configuration);
199 assertSame(1, manager.getDmaapTopicSources().size());
203 public void getNoopTopicSources() {
204 TopicEndpoint manager = new TopicEndpointProxy();
206 manager.addTopicSources(configuration);
207 assertSame(1, manager.getNoopTopicSources().size());
211 public void getUebTopicSinks() {
212 TopicEndpoint manager = new TopicEndpointProxy();
214 manager.addTopicSinks(configuration);
215 assertSame(1, manager.getUebTopicSinks().size());
219 public void getDmaapTopicSinks() {
220 TopicEndpoint manager = new TopicEndpointProxy();
222 manager.addTopicSinks(configuration);
223 assertSame(1, manager.getDmaapTopicSinks().size());
227 public void getNoopTopicSinks() {
228 TopicEndpoint manager = new TopicEndpointProxy();
230 manager.addTopicSinks(configuration);
231 assertSame(1, manager.getNoopTopicSinks().size());
235 public void lifecycle() {
236 TopicEndpoint manager = new TopicEndpointProxy();
238 assertTrue(manager.start());
239 assertTrue(manager.isAlive());
241 assertTrue(manager.stop());
242 assertFalse(manager.isAlive());
244 assertTrue(manager.start());
245 assertTrue(manager.isAlive());
248 assertFalse(manager.isAlive());
253 TopicEndpoint manager = new TopicEndpointProxy();
256 assertTrue(manager.isLocked());
259 assertFalse(manager.isLocked());
263 public void getTopicSource() {
264 TopicEndpoint manager = new TopicEndpointProxy();
265 manager.addTopicSources(configuration);
267 assertSame(NOOP_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC).getTopic());
268 assertSame(UEB_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.UEB, UEB_SOURCE_TOPIC).getTopic());
269 assertSame(DMAAP_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.DMAAP, DMAAP_SOURCE_TOPIC).getTopic());
271 assertThatIllegalStateException()
272 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SINK_TOPIC));
273 assertThatIllegalStateException()
274 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.UEB, UEB_SINK_TOPIC));
275 assertThatIllegalStateException()
276 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.DMAAP, DMAAP_SINK_TOPIC));
280 public void getTopicSink() {
281 TopicEndpoint manager = new TopicEndpointProxy();
282 manager.addTopicSinks(configuration);
284 assertSame(NOOP_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SINK_TOPIC).getTopic());
285 assertSame(UEB_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.UEB, UEB_SINK_TOPIC).getTopic());
286 assertSame(DMAAP_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.DMAAP, DMAAP_SINK_TOPIC).getTopic());
288 assertThatIllegalStateException()
289 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC));
290 assertThatIllegalStateException()
291 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.UEB, UEB_SOURCE_TOPIC));
292 assertThatIllegalStateException()
293 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.DMAAP, DMAAP_SOURCE_TOPIC));
297 public void getUebTopicSource() {
298 TopicEndpoint manager = new TopicEndpointProxy();
299 manager.addTopicSources(configuration);
301 assertSame(UEB_SOURCE_TOPIC, manager.getUebTopicSource(UEB_SOURCE_TOPIC).getTopic());
303 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(NOOP_SOURCE_TOPIC));
304 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(DMAAP_SOURCE_TOPIC));
306 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(null));
307 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(""));
311 public void getUebTopicSink() {
312 TopicEndpoint manager = new TopicEndpointProxy();
313 manager.addTopicSinks(configuration);
315 assertSame(UEB_SINK_TOPIC, manager.getUebTopicSink(UEB_SINK_TOPIC).getTopic());
317 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(NOOP_SINK_TOPIC));
318 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(DMAAP_SINK_TOPIC));
320 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(null));
321 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(""));
325 public void getDmaapTopicSource() {
326 TopicEndpoint manager = new TopicEndpointProxy();
327 manager.addTopicSources(configuration);
329 assertSame(DMAAP_SOURCE_TOPIC, manager.getDmaapTopicSource(DMAAP_SOURCE_TOPIC).getTopic());
331 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(NOOP_SOURCE_TOPIC));
332 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(UEB_SOURCE_TOPIC));
334 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(null));
335 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(""));
339 public void getDmaapTopicSink() {
340 TopicEndpoint manager = new TopicEndpointProxy();
341 manager.addTopicSinks(configuration);
343 assertSame(DMAAP_SINK_TOPIC, manager.getDmaapTopicSink(DMAAP_SINK_TOPIC).getTopic());
345 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(NOOP_SINK_TOPIC));
346 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(UEB_SINK_TOPIC));
348 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(null));
349 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(""));
354 public void getNoopTopicSource() {
355 TopicEndpoint manager = new TopicEndpointProxy();
356 manager.addTopicSources(configuration);
358 assertSame(NOOP_SOURCE_TOPIC, manager.getNoopTopicSource(NOOP_SOURCE_TOPIC).getTopic());
360 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(DMAAP_SOURCE_TOPIC));
361 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(UEB_SOURCE_TOPIC));
363 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(null));
364 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(""));
368 public void getNoopTopicSink() {
369 TopicEndpoint manager = new TopicEndpointProxy();
370 manager.addTopicSinks(configuration);
372 assertSame(NOOP_SINK_TOPIC, manager.getNoopTopicSink(NOOP_SINK_TOPIC).getTopic());
374 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(DMAAP_SINK_TOPIC));
375 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(UEB_SINK_TOPIC));
377 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(null));
378 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(""));