a311c0b0a1420578a9f28fe33fb470e2edb1fe0e
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.event.comm;
22
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;
28
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;
37
38 public class TopicEndpointProxyTest {
39
40     private static final String NOOP_SOURCE_TOPIC = "noop-source";
41     private static final String NOOP_SINK_TOPIC = "noop-sink";
42
43     private static final String UEB_SOURCE_TOPIC = "ueb-source";
44     private static final String UEB_SINK_TOPIC = "ueb-sink";
45
46     private static final String DMAAP_SOURCE_TOPIC = "dmaap-source";
47     private static final String DMAAP_SINK_TOPIC = "dmaap-sink";
48
49     private Properties configuration = new Properties();
50
51     /**
52      * Constructor.
53      */
54     public TopicEndpointProxyTest() {
55         Properties noopSourceProperties =
56             new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS)
57                 .makeTopic(NOOP_SOURCE_TOPIC).build();
58
59         Properties noopSinkProperties =
60             new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS)
61                 .makeTopic(NOOP_SINK_TOPIC).build();
62
63         Properties uebSourceProperties =
64             new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS)
65                 .makeTopic(UEB_SOURCE_TOPIC).build();
66
67         Properties uebSinkProperties =
68             new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_UEB_SINK_TOPICS)
69                 .makeTopic(UEB_SINK_TOPIC).build();
70
71         Properties dmaapSourceProperties =
72             new DmaapTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS)
73                 .makeTopic(DMAAP_SOURCE_TOPIC).build();
74
75         Properties dmaapSinkProperties =
76             new DmaapTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS)
77                 .makeTopic(DMAAP_SINK_TOPIC).build();
78
79         configuration.putAll(noopSourceProperties);
80         configuration.putAll(noopSinkProperties);
81         configuration.putAll(uebSourceProperties);
82         configuration.putAll(uebSinkProperties);
83         configuration.putAll(dmaapSourceProperties);
84         configuration.putAll(dmaapSinkProperties);
85     }
86
87     private <T extends Topic> boolean exists(List<T> topics, String topicName) {
88         return topics.stream().map(Topic::getTopic).anyMatch(topicName::equals);
89     }
90
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);
95     }
96
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);
101     }
102
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);
107     }
108
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);
113     }
114
115     @Test
116     public void testSerialize() {
117         TopicEndpoint manager = new TopicEndpointProxy();
118
119         manager.addTopicSources(configuration);
120         manager.addTopicSinks(configuration);
121
122         new GsonTestUtils().compareGson(manager, TopicEndpointProxyTest.class);
123     }
124
125     @Test
126     public void addTopicSources() {
127         TopicEndpoint manager = new TopicEndpointProxy();
128
129         List<TopicSource>  sources = manager.addTopicSources(configuration);
130         assertSame(3, sources.size());
131
132         assertTrue(allSources(sources));
133         assertFalse(anySink(sources));
134     }
135
136     @Test
137     public void addTopicSinks() {
138         TopicEndpoint manager = new TopicEndpointProxy();
139
140         List<TopicSink>  sinks = manager.addTopicSinks(configuration);
141         assertSame(3, sinks.size());
142
143         assertFalse(anySource(sinks));
144         assertTrue(allSinks(sinks));
145     }
146
147     @Test
148     public void getTopicSources() {
149         TopicEndpoint manager = new TopicEndpointProxy();
150
151         manager.addTopicSources(configuration);
152         manager.addTopicSinks(configuration);
153
154         List<TopicSource>  sources = manager.getTopicSources();
155         assertSame(3, sources.size());
156
157         assertTrue(allSources(sources));
158         assertFalse(anySink(sources));
159     }
160
161     @Test
162     public void getTopicSinks() {
163         TopicEndpoint manager = new TopicEndpointProxy();
164
165         manager.addTopicSources(configuration);
166         manager.addTopicSinks(configuration);
167
168         List<TopicSink>  sinks = manager.getTopicSinks();
169         assertSame(3, sinks.size());
170
171         assertFalse(anySource(sinks));
172         assertTrue(allSinks(sinks));
173     }
174
175     @Test
176     public void getUebTopicSources() {
177         TopicEndpoint manager = new TopicEndpointProxy();
178
179         manager.addTopicSources(configuration);
180         assertSame(1, manager.getUebTopicSources().size());
181     }
182
183     @Test
184     public void getDmaapTopicSources() {
185         TopicEndpoint manager = new TopicEndpointProxy();
186
187         manager.addTopicSources(configuration);
188         assertSame(1, manager.getDmaapTopicSources().size());
189     }
190
191     @Test
192     public void getNoopTopicSources() {
193         TopicEndpoint manager = new TopicEndpointProxy();
194
195         manager.addTopicSources(configuration);
196         assertSame(1, manager.getNoopTopicSources().size());
197     }
198
199     @Test
200     public void getUebTopicSinks() {
201         TopicEndpoint manager = new TopicEndpointProxy();
202
203         manager.addTopicSinks(configuration);
204         assertSame(1, manager.getUebTopicSinks().size());
205     }
206
207     @Test
208     public void getDmaapTopicSinks() {
209         TopicEndpoint manager = new TopicEndpointProxy();
210
211         manager.addTopicSinks(configuration);
212         assertSame(1, manager.getDmaapTopicSinks().size());
213     }
214
215     @Test
216     public void getNoopTopicSinks() {
217         TopicEndpoint manager = new TopicEndpointProxy();
218
219         manager.addTopicSinks(configuration);
220         assertSame(1, manager.getNoopTopicSinks().size());
221     }
222
223     @Test
224     public void lifecycle() {
225         TopicEndpoint manager = new TopicEndpointProxy();
226
227         assertTrue(manager.start());
228         assertTrue(manager.isAlive());
229
230         assertTrue(manager.stop());
231         assertFalse(manager.isAlive());
232
233         assertTrue(manager.start());
234         assertTrue(manager.isAlive());
235
236         manager.shutdown();
237         assertFalse(manager.isAlive());
238     }
239
240     @Test
241     public void lock() {
242         TopicEndpoint manager = new TopicEndpointProxy();
243
244         manager.lock();
245         assertTrue(manager.isLocked());
246
247         manager.unlock();
248         assertFalse(manager.isLocked());
249     }
250
251     @Test
252     public void getTopicSource() {
253         TopicEndpoint manager = new TopicEndpointProxy();
254         manager.addTopicSources(configuration);
255
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());
259
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));
266     }
267
268     @Test
269     public void getTopicSink() {
270         TopicEndpoint manager = new TopicEndpointProxy();
271         manager.addTopicSinks(configuration);
272
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());
276
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));
283     }
284
285     @Test
286     public void getUebTopicSource() {
287         TopicEndpoint manager = new TopicEndpointProxy();
288         manager.addTopicSources(configuration);
289
290         assertSame(UEB_SOURCE_TOPIC, manager.getUebTopicSource(UEB_SOURCE_TOPIC).getTopic());
291
292         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(NOOP_SOURCE_TOPIC));
293         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(DMAAP_SOURCE_TOPIC));
294
295         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(null));
296         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(""));
297     }
298
299     @Test
300     public void getUebTopicSink() {
301         TopicEndpoint manager = new TopicEndpointProxy();
302         manager.addTopicSinks(configuration);
303
304         assertSame(UEB_SINK_TOPIC, manager.getUebTopicSink(UEB_SINK_TOPIC).getTopic());
305
306         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(NOOP_SINK_TOPIC));
307         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(DMAAP_SINK_TOPIC));
308
309         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(null));
310         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(""));
311     }
312
313     @Test
314     public void getDmaapTopicSource() {
315         TopicEndpoint manager = new TopicEndpointProxy();
316         manager.addTopicSources(configuration);
317
318         assertSame(DMAAP_SOURCE_TOPIC, manager.getDmaapTopicSource(DMAAP_SOURCE_TOPIC).getTopic());
319
320         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(NOOP_SOURCE_TOPIC));
321         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(UEB_SOURCE_TOPIC));
322
323         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(null));
324         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(""));
325     }
326
327     @Test
328     public void getDmaapTopicSink() {
329         TopicEndpoint manager = new TopicEndpointProxy();
330         manager.addTopicSinks(configuration);
331
332         assertSame(DMAAP_SINK_TOPIC, manager.getDmaapTopicSink(DMAAP_SINK_TOPIC).getTopic());
333
334         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(NOOP_SINK_TOPIC));
335         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(UEB_SINK_TOPIC));
336
337         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(null));
338         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(""));
339     }
340
341
342     @Test
343     public void getNoopTopicSource() {
344         TopicEndpoint manager = new TopicEndpointProxy();
345         manager.addTopicSources(configuration);
346
347         assertSame(NOOP_SOURCE_TOPIC, manager.getNoopTopicSource(NOOP_SOURCE_TOPIC).getTopic());
348
349         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(DMAAP_SOURCE_TOPIC));
350         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(UEB_SOURCE_TOPIC));
351
352         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(null));
353         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(""));
354     }
355
356     @Test
357     public void getNoopTopicSink() {
358         TopicEndpoint manager = new TopicEndpointProxy();
359         manager.addTopicSinks(configuration);
360
361         assertSame(NOOP_SINK_TOPIC, manager.getNoopTopicSink(NOOP_SINK_TOPIC).getTopic());
362
363         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(DMAAP_SINK_TOPIC));
364         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(UEB_SINK_TOPIC));
365
366         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(null));
367         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(""));
368     }
369 }