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 getTopicSources() {
149 TopicEndpoint manager = new TopicEndpointProxy();
151 manager.addTopicSources(configuration);
152 manager.addTopicSinks(configuration);
154 List<TopicSource> sources = manager.getTopicSources();
155 assertSame(3, sources.size());
157 assertTrue(allSources(sources));
158 assertFalse(anySink(sources));
162 public void getTopicSinks() {
163 TopicEndpoint manager = new TopicEndpointProxy();
165 manager.addTopicSources(configuration);
166 manager.addTopicSinks(configuration);
168 List<TopicSink> sinks = manager.getTopicSinks();
169 assertSame(3, sinks.size());
171 assertFalse(anySource(sinks));
172 assertTrue(allSinks(sinks));
176 public void getUebTopicSources() {
177 TopicEndpoint manager = new TopicEndpointProxy();
179 manager.addTopicSources(configuration);
180 assertSame(1, manager.getUebTopicSources().size());
184 public void getDmaapTopicSources() {
185 TopicEndpoint manager = new TopicEndpointProxy();
187 manager.addTopicSources(configuration);
188 assertSame(1, manager.getDmaapTopicSources().size());
192 public void getNoopTopicSources() {
193 TopicEndpoint manager = new TopicEndpointProxy();
195 manager.addTopicSources(configuration);
196 assertSame(1, manager.getNoopTopicSources().size());
200 public void getUebTopicSinks() {
201 TopicEndpoint manager = new TopicEndpointProxy();
203 manager.addTopicSinks(configuration);
204 assertSame(1, manager.getUebTopicSinks().size());
208 public void getDmaapTopicSinks() {
209 TopicEndpoint manager = new TopicEndpointProxy();
211 manager.addTopicSinks(configuration);
212 assertSame(1, manager.getDmaapTopicSinks().size());
216 public void getNoopTopicSinks() {
217 TopicEndpoint manager = new TopicEndpointProxy();
219 manager.addTopicSinks(configuration);
220 assertSame(1, manager.getNoopTopicSinks().size());
224 public void lifecycle() {
225 TopicEndpoint manager = new TopicEndpointProxy();
227 assertTrue(manager.start());
228 assertTrue(manager.isAlive());
230 assertTrue(manager.stop());
231 assertFalse(manager.isAlive());
233 assertTrue(manager.start());
234 assertTrue(manager.isAlive());
237 assertFalse(manager.isAlive());
242 TopicEndpoint manager = new TopicEndpointProxy();
245 assertTrue(manager.isLocked());
248 assertFalse(manager.isLocked());
252 public void getTopicSource() {
253 TopicEndpoint manager = new TopicEndpointProxy();
254 manager.addTopicSources(configuration);
256 assertSame(NOOP_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC).getTopic());
257 assertSame(UEB_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.UEB, UEB_SOURCE_TOPIC).getTopic());
258 assertSame(DMAAP_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.DMAAP, DMAAP_SOURCE_TOPIC).getTopic());
260 assertThatIllegalStateException()
261 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SINK_TOPIC));
262 assertThatIllegalStateException()
263 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.UEB, UEB_SINK_TOPIC));
264 assertThatIllegalStateException()
265 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.DMAAP, DMAAP_SINK_TOPIC));
269 public void getTopicSink() {
270 TopicEndpoint manager = new TopicEndpointProxy();
271 manager.addTopicSinks(configuration);
273 assertSame(NOOP_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SINK_TOPIC).getTopic());
274 assertSame(UEB_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.UEB, UEB_SINK_TOPIC).getTopic());
275 assertSame(DMAAP_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.DMAAP, DMAAP_SINK_TOPIC).getTopic());
277 assertThatIllegalStateException()
278 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC));
279 assertThatIllegalStateException()
280 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.UEB, UEB_SOURCE_TOPIC));
281 assertThatIllegalStateException()
282 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.DMAAP, DMAAP_SOURCE_TOPIC));
286 public void getUebTopicSource() {
287 TopicEndpoint manager = new TopicEndpointProxy();
288 manager.addTopicSources(configuration);
290 assertSame(UEB_SOURCE_TOPIC, manager.getUebTopicSource(UEB_SOURCE_TOPIC).getTopic());
292 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(NOOP_SOURCE_TOPIC));
293 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(DMAAP_SOURCE_TOPIC));
295 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(null));
296 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(""));
300 public void getUebTopicSink() {
301 TopicEndpoint manager = new TopicEndpointProxy();
302 manager.addTopicSinks(configuration);
304 assertSame(UEB_SINK_TOPIC, manager.getUebTopicSink(UEB_SINK_TOPIC).getTopic());
306 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(NOOP_SINK_TOPIC));
307 assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(DMAAP_SINK_TOPIC));
309 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(null));
310 assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(""));
314 public void getDmaapTopicSource() {
315 TopicEndpoint manager = new TopicEndpointProxy();
316 manager.addTopicSources(configuration);
318 assertSame(DMAAP_SOURCE_TOPIC, manager.getDmaapTopicSource(DMAAP_SOURCE_TOPIC).getTopic());
320 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(NOOP_SOURCE_TOPIC));
321 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(UEB_SOURCE_TOPIC));
323 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(null));
324 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(""));
328 public void getDmaapTopicSink() {
329 TopicEndpoint manager = new TopicEndpointProxy();
330 manager.addTopicSinks(configuration);
332 assertSame(DMAAP_SINK_TOPIC, manager.getDmaapTopicSink(DMAAP_SINK_TOPIC).getTopic());
334 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(NOOP_SINK_TOPIC));
335 assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(UEB_SINK_TOPIC));
337 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(null));
338 assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(""));
343 public void getNoopTopicSource() {
344 TopicEndpoint manager = new TopicEndpointProxy();
345 manager.addTopicSources(configuration);
347 assertSame(NOOP_SOURCE_TOPIC, manager.getNoopTopicSource(NOOP_SOURCE_TOPIC).getTopic());
349 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(DMAAP_SOURCE_TOPIC));
350 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(UEB_SOURCE_TOPIC));
352 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(null));
353 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(""));
357 public void getNoopTopicSink() {
358 TopicEndpoint manager = new TopicEndpointProxy();
359 manager.addTopicSinks(configuration);
361 assertSame(NOOP_SINK_TOPIC, manager.getNoopTopicSink(NOOP_SINK_TOPIC).getTopic());
363 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(DMAAP_SINK_TOPIC));
364 assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(UEB_SINK_TOPIC));
366 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(null));
367 assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(""));