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