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;
37 public class TopicEndpointProxyTest {
39 private static final String NOOP_SOURCE_TOPIC = "noop-source";
40 private static final String NOOP_SINK_TOPIC = "noop-sink";
42 private static final String UEB_SOURCE_TOPIC = "ueb-source";
43 private static final String UEB_SINK_TOPIC = "ueb-sink";
45 private static final String DMAAP_SOURCE_TOPIC = "dmaap-source";
46 private static final String DMAAP_SINK_TOPIC = "dmaap-sink";
48 private Properties configuration = new Properties();
53 public TopicEndpointProxyTest() {
54 Properties noopSourceProperties =
55 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS)
56 .makeTopic(NOOP_SOURCE_TOPIC).build();
58 Properties noopSinkProperties =
59 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS)
60 .makeTopic(NOOP_SINK_TOPIC).build();
62 Properties uebSourceProperties =
63 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS)
64 .makeTopic(UEB_SOURCE_TOPIC).build();
66 Properties uebSinkProperties =
67 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS)
68 .makeTopic(UEB_SINK_TOPIC).build();
70 Properties dmaapSourceProperties =
71 new DmaapTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS)
72 .makeTopic(DMAAP_SOURCE_TOPIC).build();
74 Properties dmaapSinkProperties =
75 new DmaapTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS)
76 .makeTopic(DMAAP_SINK_TOPIC).build();
78 configuration.putAll(noopSourceProperties);
79 configuration.putAll(noopSinkProperties);
80 configuration.putAll(uebSourceProperties);
81 configuration.putAll(uebSinkProperties);
82 configuration.putAll(dmaapSourceProperties);
83 configuration.putAll(dmaapSinkProperties);
86 private <T extends Topic> boolean exists(List<T> topics, String topicName) {
87 return topics.stream().map(Topic::getTopic).anyMatch(topicName::equals);
90 private <T extends Topic> boolean allSources(List<T> topics) {
91 return exists(topics, NOOP_SOURCE_TOPIC)
92 && exists(topics, UEB_SOURCE_TOPIC)
93 && exists(topics, DMAAP_SOURCE_TOPIC);
96 private <T extends Topic> boolean allSinks(List<T> topics) {
97 return exists(topics, NOOP_SINK_TOPIC)
98 && exists(topics, UEB_SINK_TOPIC)
99 && exists(topics, DMAAP_SINK_TOPIC);
102 private <T extends Topic> boolean anySource(List<T> topics) {
103 return exists(topics, NOOP_SOURCE_TOPIC)
104 || exists(topics, UEB_SOURCE_TOPIC)
105 || exists(topics, DMAAP_SOURCE_TOPIC);
108 private <T extends Topic> boolean anySink(List<T> topics) {
109 return exists(topics, NOOP_SINK_TOPIC)
110 || exists(topics, UEB_SINK_TOPIC)
111 || exists(topics, DMAAP_SINK_TOPIC);
115 public void addTopicSources() {
116 TopicEndpoint manager = new TopicEndpointProxy();
118 List<TopicSource> sources = manager.addTopicSources(configuration);
119 assertSame(3, sources.size());
121 assertTrue(allSources(sources));
122 assertFalse(anySink(sources));
126 public void addTopicSinks() {
127 TopicEndpoint manager = new TopicEndpointProxy();
129 List<TopicSink> sinks = manager.addTopicSinks(configuration);
130 assertSame(3, sinks.size());
132 assertFalse(anySource(sinks));
133 assertTrue(allSinks(sinks));
137 public void getTopicSources() {
138 TopicEndpoint manager = new TopicEndpointProxy();
140 manager.addTopicSources(configuration);
141 manager.addTopicSinks(configuration);
143 List<TopicSource> sources = manager.getTopicSources();
144 assertSame(3, sources.size());
146 assertTrue(allSources(sources));
147 assertFalse(anySink(sources));
151 public void getTopicSinks() {
152 TopicEndpoint manager = new TopicEndpointProxy();
154 manager.addTopicSources(configuration);
155 manager.addTopicSinks(configuration);
157 List<TopicSink> sinks = manager.getTopicSinks();
158 assertSame(3, sinks.size());
160 assertFalse(anySource(sinks));
161 assertTrue(allSinks(sinks));
165 public void getUebTopicSources() {
166 TopicEndpoint manager = new TopicEndpointProxy();
168 manager.addTopicSources(configuration);
169 assertSame(1, manager.getUebTopicSources().size());
173 public void getDmaapTopicSources() {
174 TopicEndpoint manager = new TopicEndpointProxy();
176 manager.addTopicSources(configuration);
177 assertSame(1, manager.getDmaapTopicSources().size());
181 public void getNoopTopicSources() {
182 TopicEndpoint manager = new TopicEndpointProxy();
184 manager.addTopicSources(configuration);
185 assertSame(1, manager.getNoopTopicSources().size());
189 public void getUebTopicSinks() {
190 TopicEndpoint manager = new TopicEndpointProxy();
192 manager.addTopicSinks(configuration);
193 assertSame(1, manager.getUebTopicSinks().size());
197 public void getDmaapTopicSinks() {
198 TopicEndpoint manager = new TopicEndpointProxy();
200 manager.addTopicSinks(configuration);
201 assertSame(1, manager.getDmaapTopicSinks().size());
205 public void getNoopTopicSinks() {
206 TopicEndpoint manager = new TopicEndpointProxy();
208 manager.addTopicSinks(configuration);
209 assertSame(1, manager.getNoopTopicSinks().size());
213 public void lifecycle() {
214 TopicEndpoint manager = new TopicEndpointProxy();
216 assertTrue(manager.start());
217 assertTrue(manager.isAlive());
219 assertTrue(manager.stop());
220 assertFalse(manager.isAlive());
222 assertTrue(manager.start());
223 assertTrue(manager.isAlive());
226 assertFalse(manager.isAlive());
231 TopicEndpoint manager = new TopicEndpointProxy();
234 assertTrue(manager.isLocked());
237 assertFalse(manager.isLocked());
241 public void getTopicSource() {
242 TopicEndpoint manager = new TopicEndpointProxy();
243 manager.addTopicSources(configuration);
245 assertSame(NOOP_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC).getTopic());
246 assertSame(UEB_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.UEB, UEB_SOURCE_TOPIC).getTopic());
247 assertSame(DMAAP_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.DMAAP, DMAAP_SOURCE_TOPIC).getTopic());
249 assertThatIllegalStateException()
250 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SINK_TOPIC));
251 assertThatIllegalStateException()
252 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.UEB, UEB_SINK_TOPIC));
253 assertThatIllegalStateException()
254 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.DMAAP, DMAAP_SINK_TOPIC));
258 public void getTopicSink() {
259 TopicEndpoint manager = new TopicEndpointProxy();
260 manager.addTopicSinks(configuration);
262 assertSame(NOOP_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SINK_TOPIC).getTopic());
263 assertSame(UEB_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.UEB, UEB_SINK_TOPIC).getTopic());
264 assertSame(DMAAP_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.DMAAP, DMAAP_SINK_TOPIC).getTopic());
266 assertThatIllegalStateException()
267 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC));
268 assertThatIllegalStateException()
269 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.UEB, UEB_SOURCE_TOPIC));
270 assertThatIllegalStateException()
271 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.DMAAP, DMAAP_SOURCE_TOPIC));
275 public void getUebTopicSource() {
276 TopicEndpoint manager = new TopicEndpointProxy();
277 manager.addTopicSources(configuration);
279 assertSame(UEB_SOURCE_TOPIC, manager.getUebTopicSource(UEB_SOURCE_TOPIC).getTopic());
281 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(NOOP_SOURCE_TOPIC));
282 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(DMAAP_SOURCE_TOPIC));
284 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(null));
285 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(""));
289 public void getUebTopicSink() {
290 TopicEndpoint manager = new TopicEndpointProxy();
291 manager.addTopicSinks(configuration);
293 assertSame(UEB_SINK_TOPIC, manager.getUebTopicSink(UEB_SINK_TOPIC).getTopic());
295 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(NOOP_SINK_TOPIC));
296 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(DMAAP_SINK_TOPIC));
298 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(null));
299 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(""));
303 public void getDmaapTopicSource() {
304 TopicEndpoint manager = new TopicEndpointProxy();
305 manager.addTopicSources(configuration);
307 assertSame(DMAAP_SOURCE_TOPIC, manager.getDmaapTopicSource(DMAAP_SOURCE_TOPIC).getTopic());
309 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(NOOP_SOURCE_TOPIC));
310 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(UEB_SOURCE_TOPIC));
312 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(null));
313 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(""));
317 public void getDmaapTopicSink() {
318 TopicEndpoint manager = new TopicEndpointProxy();
319 manager.addTopicSinks(configuration);
321 assertSame(DMAAP_SINK_TOPIC, manager.getDmaapTopicSink(DMAAP_SINK_TOPIC).getTopic());
323 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(NOOP_SINK_TOPIC));
324 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(UEB_SINK_TOPIC));
326 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(null));
327 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(""));
332 public void getNoopTopicSource() {
333 TopicEndpoint manager = new TopicEndpointProxy();
334 manager.addTopicSources(configuration);
336 assertSame(NOOP_SOURCE_TOPIC, manager.getNoopTopicSource(NOOP_SOURCE_TOPIC).getTopic());
338 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(DMAAP_SOURCE_TOPIC));
339 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(UEB_SOURCE_TOPIC));
341 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(null));
342 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(""));
346 public void getNoopTopicSink() {
347 TopicEndpoint manager = new TopicEndpointProxy();
348 manager.addTopicSinks(configuration);
350 assertSame(NOOP_SINK_TOPIC, manager.getNoopTopicSink(NOOP_SINK_TOPIC).getTopic());
352 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(DMAAP_SINK_TOPIC));
353 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(UEB_SINK_TOPIC));
355 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(null));
356 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(""));