ba5fe18ea77de4f9b31b7f516e2551c1dba3cdee
[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 addTopics() {
149         TopicEndpoint manager = new TopicEndpointProxy();
150
151         List<Topic>  topics = manager.addTopics(configuration);
152         assertSame(6, topics.size());
153
154         assertTrue(allSources(topics));
155         assertTrue(allSinks(topics));
156     }
157
158     @Test
159     public void getTopicSources() {
160         TopicEndpoint manager = new TopicEndpointProxy();
161
162         manager.addTopicSources(configuration);
163         manager.addTopicSinks(configuration);
164
165         List<TopicSource>  sources = manager.getTopicSources();
166         assertSame(3, sources.size());
167
168         assertTrue(allSources(sources));
169         assertFalse(anySink(sources));
170     }
171
172     @Test
173     public void getTopicSinks() {
174         TopicEndpoint manager = new TopicEndpointProxy();
175
176         manager.addTopicSources(configuration);
177         manager.addTopicSinks(configuration);
178
179         List<TopicSink>  sinks = manager.getTopicSinks();
180         assertSame(3, sinks.size());
181
182         assertFalse(anySource(sinks));
183         assertTrue(allSinks(sinks));
184     }
185
186     @Test
187     public void getUebTopicSources() {
188         TopicEndpoint manager = new TopicEndpointProxy();
189
190         manager.addTopicSources(configuration);
191         assertSame(1, manager.getUebTopicSources().size());
192     }
193
194     @Test
195     public void getDmaapTopicSources() {
196         TopicEndpoint manager = new TopicEndpointProxy();
197
198         manager.addTopicSources(configuration);
199         assertSame(1, manager.getDmaapTopicSources().size());
200     }
201
202     @Test
203     public void getNoopTopicSources() {
204         TopicEndpoint manager = new TopicEndpointProxy();
205
206         manager.addTopicSources(configuration);
207         assertSame(1, manager.getNoopTopicSources().size());
208     }
209
210     @Test
211     public void getUebTopicSinks() {
212         TopicEndpoint manager = new TopicEndpointProxy();
213
214         manager.addTopicSinks(configuration);
215         assertSame(1, manager.getUebTopicSinks().size());
216     }
217
218     @Test
219     public void getDmaapTopicSinks() {
220         TopicEndpoint manager = new TopicEndpointProxy();
221
222         manager.addTopicSinks(configuration);
223         assertSame(1, manager.getDmaapTopicSinks().size());
224     }
225
226     @Test
227     public void getNoopTopicSinks() {
228         TopicEndpoint manager = new TopicEndpointProxy();
229
230         manager.addTopicSinks(configuration);
231         assertSame(1, manager.getNoopTopicSinks().size());
232     }
233
234     @Test
235     public void lifecycle() {
236         TopicEndpoint manager = new TopicEndpointProxy();
237
238         assertTrue(manager.start());
239         assertTrue(manager.isAlive());
240
241         assertTrue(manager.stop());
242         assertFalse(manager.isAlive());
243
244         assertTrue(manager.start());
245         assertTrue(manager.isAlive());
246
247         manager.shutdown();
248         assertFalse(manager.isAlive());
249     }
250
251     @Test
252     public void lock() {
253         TopicEndpoint manager = new TopicEndpointProxy();
254
255         manager.lock();
256         assertTrue(manager.isLocked());
257
258         manager.unlock();
259         assertFalse(manager.isLocked());
260     }
261
262     @Test
263     public void getTopicSource() {
264         TopicEndpoint manager = new TopicEndpointProxy();
265         manager.addTopicSources(configuration);
266
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());
270
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));
277     }
278
279     @Test
280     public void getTopicSink() {
281         TopicEndpoint manager = new TopicEndpointProxy();
282         manager.addTopicSinks(configuration);
283
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());
287
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));
294     }
295
296     @Test
297     public void getUebTopicSource() {
298         TopicEndpoint manager = new TopicEndpointProxy();
299         manager.addTopicSources(configuration);
300
301         assertSame(UEB_SOURCE_TOPIC, manager.getUebTopicSource(UEB_SOURCE_TOPIC).getTopic());
302
303         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(NOOP_SOURCE_TOPIC));
304         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSource(DMAAP_SOURCE_TOPIC));
305
306         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(null));
307         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSource(""));
308     }
309
310     @Test
311     public void getUebTopicSink() {
312         TopicEndpoint manager = new TopicEndpointProxy();
313         manager.addTopicSinks(configuration);
314
315         assertSame(UEB_SINK_TOPIC, manager.getUebTopicSink(UEB_SINK_TOPIC).getTopic());
316
317         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(NOOP_SINK_TOPIC));
318         assertThatIllegalStateException().isThrownBy(() -> manager.getUebTopicSink(DMAAP_SINK_TOPIC));
319
320         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(null));
321         assertThatIllegalArgumentException().isThrownBy(() -> manager.getUebTopicSink(""));
322     }
323
324     @Test
325     public void getDmaapTopicSource() {
326         TopicEndpoint manager = new TopicEndpointProxy();
327         manager.addTopicSources(configuration);
328
329         assertSame(DMAAP_SOURCE_TOPIC, manager.getDmaapTopicSource(DMAAP_SOURCE_TOPIC).getTopic());
330
331         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(NOOP_SOURCE_TOPIC));
332         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSource(UEB_SOURCE_TOPIC));
333
334         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(null));
335         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSource(""));
336     }
337
338     @Test
339     public void getDmaapTopicSink() {
340         TopicEndpoint manager = new TopicEndpointProxy();
341         manager.addTopicSinks(configuration);
342
343         assertSame(DMAAP_SINK_TOPIC, manager.getDmaapTopicSink(DMAAP_SINK_TOPIC).getTopic());
344
345         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(NOOP_SINK_TOPIC));
346         assertThatIllegalStateException().isThrownBy(() -> manager.getDmaapTopicSink(UEB_SINK_TOPIC));
347
348         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(null));
349         assertThatIllegalArgumentException().isThrownBy(() -> manager.getDmaapTopicSink(""));
350     }
351
352
353     @Test
354     public void getNoopTopicSource() {
355         TopicEndpoint manager = new TopicEndpointProxy();
356         manager.addTopicSources(configuration);
357
358         assertSame(NOOP_SOURCE_TOPIC, manager.getNoopTopicSource(NOOP_SOURCE_TOPIC).getTopic());
359
360         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(DMAAP_SOURCE_TOPIC));
361         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSource(UEB_SOURCE_TOPIC));
362
363         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(null));
364         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(""));
365     }
366
367     @Test
368     public void getNoopTopicSink() {
369         TopicEndpoint manager = new TopicEndpointProxy();
370         manager.addTopicSinks(configuration);
371
372         assertSame(NOOP_SINK_TOPIC, manager.getNoopTopicSink(NOOP_SINK_TOPIC).getTopic());
373
374         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(DMAAP_SINK_TOPIC));
375         assertThatIllegalStateException().isThrownBy(() -> manager.getNoopTopicSink(UEB_SINK_TOPIC));
376
377         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(null));
378         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(""));
379     }
380 }