198cdc2c0690af76f701bee3a21329b5d0f325a3
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.event.comm;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
26 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertFalse;
29 import static org.junit.jupiter.api.Assertions.assertSame;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31
32 import java.util.LinkedList;
33 import java.util.List;
34 import java.util.Properties;
35 import org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
39 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicPropertyBuilder;
40 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
41 import org.onap.policy.common.endpoints.parameters.TopicParameters;
42 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
43 import org.onap.policy.common.utils.gson.GsonTestUtils;
44
45 class TopicEndpointProxyTest {
46
47     private static final String NOOP_SOURCE_TOPIC = "noop-source";
48     private static final String NOOP_SINK_TOPIC = "noop-sink";
49
50     private final Properties configuration = new Properties();
51     private final TopicParameterGroup group = new TopicParameterGroup();
52
53     /**
54      * Constructor.
55      */
56     public TopicEndpointProxyTest() {
57         group.setTopicSinks(new LinkedList<>());
58         group.setTopicSources(new LinkedList<>());
59
60         NoopTopicPropertyBuilder noopSourceBuilder =
61                 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS)
62                         .makeTopic(NOOP_SOURCE_TOPIC);
63         configuration.putAll(noopSourceBuilder.build());
64         group.getTopicSources().add(noopSourceBuilder.getParams());
65
66         NoopTopicPropertyBuilder noopSinkBuilder =
67                 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS)
68                         .makeTopic(NOOP_SINK_TOPIC);
69         configuration.putAll(noopSinkBuilder.build());
70         group.getTopicSinks().add(noopSinkBuilder.getParams());
71
72         TopicParameters invalidCommInfraParams =
73                 new NoopTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS)
74                         .makeTopic(NOOP_SOURCE_TOPIC).getParams();
75         invalidCommInfraParams.setTopicCommInfrastructure(Topic.CommInfrastructure.REST.name());
76         group.getTopicSources().add(invalidCommInfraParams);
77         group.getTopicSinks().add(invalidCommInfraParams);
78     }
79
80     private <T extends Topic> boolean exists(List<T> topics, String topicName) {
81         return topics.stream().map(Topic::getTopic).anyMatch(topicName::equals);
82     }
83
84     private <T extends Topic> boolean allSources(List<T> topics) {
85         return exists(topics, NOOP_SOURCE_TOPIC);
86     }
87
88     private <T extends Topic> boolean allSinks(List<T> topics) {
89         return exists(topics, NOOP_SINK_TOPIC);
90     }
91
92     private <T extends Topic> boolean anySource(List<T> topics) {
93         return exists(topics, NOOP_SOURCE_TOPIC);
94     }
95
96     private <T extends Topic> boolean anySink(List<T> topics) {
97         return exists(topics, NOOP_SINK_TOPIC);
98     }
99
100     /**
101      * Destroys all managed topics.
102      */
103     @AfterEach
104     public void tearDown() {
105         NoopTopicFactories.getSinkFactory().destroy();
106         NoopTopicFactories.getSourceFactory().destroy();
107     }
108
109     @Test
110     void testSerialize() {
111         TopicEndpoint manager = new TopicEndpointProxy();
112
113         manager.addTopicSources(configuration);
114         manager.addTopicSinks(configuration);
115
116         assertThatCode(() -> new GsonTestUtils().compareGson(manager, TopicEndpointProxyTest.class))
117                 .doesNotThrowAnyException();
118     }
119
120     @Test
121     void testAddTopicSourcesListOfTopicParameters() {
122         TopicEndpoint manager = new TopicEndpointProxy();
123
124         List<TopicSource> sources = manager.addTopicSources(group.getTopicSources());
125         assertSame(1, sources.size());
126
127         assertTrue(allSources(sources));
128         assertFalse(anySink(sources));
129     }
130
131     @Test
132     void testAddTopicSourcesProperties() {
133         TopicEndpoint manager = new TopicEndpointProxy();
134
135         List<TopicSource> sources = manager.addTopicSources(configuration);
136         assertSame(1, sources.size());
137
138         assertTrue(allSources(sources));
139         assertFalse(anySink(sources));
140     }
141
142     @Test
143     void testAddTopicSinksListOfTopicParameters() {
144         TopicEndpoint manager = new TopicEndpointProxy();
145
146         List<TopicSink> sinks = manager.addTopicSinks(group.getTopicSinks());
147         assertSame(1, sinks.size());
148
149         assertFalse(anySource(sinks));
150         assertTrue(allSinks(sinks));
151     }
152
153     @Test
154     void testAddTopicSinksProperties() {
155         TopicEndpoint manager = new TopicEndpointProxy();
156
157         List<TopicSink> sinks = manager.addTopicSinks(configuration);
158         assertSame(1, sinks.size());
159
160         assertFalse(anySource(sinks));
161         assertTrue(allSinks(sinks));
162     }
163
164     @Test
165     void testAddTopicsProperties() {
166         TopicEndpoint manager = new TopicEndpointProxy();
167
168         List<Topic> topics = manager.addTopics(configuration);
169         assertSame(2, topics.size());
170
171         assertTrue(allSources(topics));
172         assertTrue(allSinks(topics));
173     }
174
175     @Test
176     void testAddTopicsTopicParameterGroup() {
177         TopicEndpoint manager = new TopicEndpointProxy();
178
179         List<Topic> topics = manager.addTopics(group);
180         assertSame(2, topics.size());
181
182         assertTrue(allSources(topics));
183         assertTrue(allSinks(topics));
184     }
185
186     @Test
187     void testAddTopicsTopicParameterGroupNull() {
188         TopicEndpoint manager = new TopicEndpointProxy();
189
190         List<Topic> topics = manager.addTopics(new TopicParameterGroup());
191         assertEquals(0, topics.size());
192     }
193
194     @Test
195     void testLockSinks_lockSources_locked() {
196         TopicEndpoint manager = new TopicEndpointProxy();
197         manager.lock();
198         for (Topic topic : manager.addTopics(group)) {
199             assertTrue(topic.isLocked());
200         }
201     }
202
203     @Test
204     void testLockSinks_lockSources_unlocked() {
205         TopicEndpoint manager = new TopicEndpointProxy();
206         for (Topic topic : manager.addTopics(group)) {
207             assertFalse(topic.isLocked());
208         }
209     }
210
211     @Test
212     void testGetTopicSources() {
213         TopicEndpoint manager = new TopicEndpointProxy();
214
215         manager.addTopicSources(configuration);
216         manager.addTopicSinks(configuration);
217
218         List<TopicSource> sources = manager.getTopicSources();
219         assertSame(1, sources.size());
220
221         assertTrue(allSources(sources));
222         assertFalse(anySink(sources));
223     }
224
225     @Test
226     void testGetTopicSinks() {
227         TopicEndpoint manager = new TopicEndpointProxy();
228
229         manager.addTopicSources(configuration);
230         manager.addTopicSinks(configuration);
231
232         List<TopicSink> sinks = manager.getTopicSinks();
233         assertSame(1, sinks.size());
234
235         assertFalse(anySource(sinks));
236         assertTrue(allSinks(sinks));
237     }
238
239     @Test
240     void testGetNoopTopicSources() {
241         TopicEndpoint manager = new TopicEndpointProxy();
242
243         manager.addTopicSources(configuration);
244         assertSame(1, manager.getNoopTopicSources().size());
245     }
246
247     @Test
248     void testGetNoopTopicSinks() {
249         TopicEndpoint manager = new TopicEndpointProxy();
250
251         manager.addTopicSinks(configuration);
252         assertSame(1, manager.getNoopTopicSinks().size());
253     }
254
255     @Test
256     void testLifecycle() {
257         TopicEndpoint manager = new TopicEndpointProxy();
258
259         assertTrue(manager.start());
260         assertTrue(manager.isAlive());
261
262         assertTrue(manager.stop());
263         assertFalse(manager.isAlive());
264
265         assertTrue(manager.start());
266         assertTrue(manager.isAlive());
267
268         manager.shutdown();
269         assertFalse(manager.isAlive());
270     }
271
272     @Test
273     void testLock() {
274         TopicEndpoint manager = new TopicEndpointProxy();
275
276         manager.lock();
277         assertTrue(manager.isLocked());
278
279         manager.unlock();
280         assertFalse(manager.isLocked());
281     }
282
283     @Test
284     void testGetTopicSource() {
285         TopicEndpoint manager = new TopicEndpointProxy();
286         manager.addTopicSources(configuration);
287
288         assertSame(NOOP_SOURCE_TOPIC, manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC).getTopic());
289
290         assertThatIllegalStateException()
291                 .isThrownBy(() -> manager.getTopicSource(CommInfrastructure.NOOP, NOOP_SINK_TOPIC));
292     }
293
294     @Test
295     void testGetTopicSink() {
296         TopicEndpoint manager = new TopicEndpointProxy();
297         manager.addTopicSinks(configuration);
298
299         assertSame(NOOP_SINK_TOPIC, manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SINK_TOPIC).getTopic());
300
301         assertThatIllegalStateException()
302                 .isThrownBy(() -> manager.getTopicSink(CommInfrastructure.NOOP, NOOP_SOURCE_TOPIC));
303     }
304
305     @Test
306     void testGetNoopTopicSource() {
307         TopicEndpoint manager = new TopicEndpointProxy();
308         manager.addTopicSources(configuration);
309
310         assertSame(NOOP_SOURCE_TOPIC, manager.getNoopTopicSource(NOOP_SOURCE_TOPIC).getTopic());
311
312         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(null));
313         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSource(""));
314     }
315
316     @Test
317     void testGetNoopTopicSink() {
318         TopicEndpoint manager = new TopicEndpointProxy();
319         manager.addTopicSinks(configuration);
320
321         assertSame(NOOP_SINK_TOPIC, manager.getNoopTopicSink(NOOP_SINK_TOPIC).getTopic());
322
323         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(null));
324         assertThatIllegalArgumentException().isThrownBy(() -> manager.getNoopTopicSink(""));
325     }
326 }