fe719848afe716a187a58bfb464441dcadfd01a7
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-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.bus.internal;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.doThrow;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
34
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
41 import org.onap.policy.common.endpoints.event.comm.TopicListener;
42 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
43 import org.onap.policy.common.utils.gson.GsonTestUtils;
44
45 class TopicBaseTest extends TopicTestBase {
46
47     private TopicBaseImpl base;
48
49     /**
50      * Creates the object to be tested.
51      */
52     @BeforeEach
53     @Override
54     public void setUp() {
55         super.setUp();
56
57         base = new TopicBaseImpl(servers, MY_TOPIC);
58     }
59
60     @Test
61     void testTopicBase_NullServers() {
62         assertThatThrownBy(() -> new TopicBaseImpl(null, MY_TOPIC)).isInstanceOf(IllegalArgumentException.class);
63     }
64
65     @Test
66     void testTopicBase_EmptyServers() {
67         List<String> testList = Collections.emptyList();
68         assertThatThrownBy(() -> new TopicBaseImpl(testList, MY_TOPIC))
69             .isInstanceOf(IllegalArgumentException.class);
70     }
71
72     @Test
73     void testTopicBase_NullTopic() {
74         assertThatThrownBy(() -> new TopicBaseImpl(servers, null)).isInstanceOf(IllegalArgumentException.class);
75     }
76
77     @Test
78     void testTopicBase_EmptyTopic() {
79         assertThatThrownBy(() -> new TopicBaseImpl(servers, "")).isInstanceOf(IllegalArgumentException.class);
80     }
81
82     @Test
83     void testTopicBase_EffectiveTopic() {
84         TopicBase baseEf = new TopicBaseImpl(servers, MY_TOPIC, MY_EFFECTIVE_TOPIC);
85         assertEquals(MY_TOPIC, baseEf.getTopic());
86         assertEquals(MY_EFFECTIVE_TOPIC, baseEf.getEffectiveTopic());
87     }
88
89     @Test
90     void testTopicBase_NullEffectiveTopic() {
91         TopicBase baseEf = new TopicBaseImpl(servers, MY_TOPIC, null);
92         assertEquals(MY_TOPIC, baseEf.getTopic());
93         assertEquals(MY_TOPIC, baseEf.getEffectiveTopic());
94     }
95
96     @Test
97     void testTopicBase_EmptyEffectiveTopic() {
98         TopicBase baseEf = new TopicBaseImpl(servers, MY_TOPIC, "");
99         assertEquals(MY_TOPIC, baseEf.getTopic());
100         assertEquals(MY_TOPIC, baseEf.getEffectiveTopic());
101     }
102
103     @Test
104     void testSerialize() {
105         assertThatCode(() -> new GsonTestUtils().compareGson(base, TopicBaseTest.class)).doesNotThrowAnyException();
106     }
107
108     @Test
109     void testRegister() {
110         TopicListener listener = mock(TopicListener.class);
111         base.register(listener);
112         assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
113
114         // re-register - list should be unchanged
115         base.register(listener);
116         assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
117
118         // register a new listener
119         TopicListener listener2 = mock(TopicListener.class);
120         base.register(listener2);
121         assertEquals(Arrays.asList(listener, listener2), base.snapshotTopicListeners());
122     }
123
124     @Test
125     void testRegister_NullListener() {
126         assertThatThrownBy(() -> base.register(null)).isInstanceOf(IllegalArgumentException.class);
127     }
128
129     @Test
130     void testUnregister() {
131         // register two listeners
132         TopicListener listener = mock(TopicListener.class);
133         TopicListener listener2 = mock(TopicListener.class);
134         base.register(listener);
135         base.register(listener2);
136
137         // unregister one
138         base.unregister(listener);
139         assertEquals(Arrays.asList(listener2), base.snapshotTopicListeners());
140
141         // unregister the other
142         base.unregister(listener2);
143         assertTrue(base.snapshotTopicListeners().isEmpty());
144
145         // unregister again
146         base.unregister(listener2);
147         assertTrue(base.snapshotTopicListeners().isEmpty());
148     }
149
150     @Test
151     void testUnregister_NullListener() {
152         base.register(mock(TopicListener.class));
153         assertThatThrownBy(() -> base.unregister(null)).isInstanceOf(IllegalArgumentException.class);
154     }
155
156     @Test
157     void testBroadcast() {
158         // register two listeners
159         TopicListener listener = mock(TopicListener.class);
160         TopicListener listener2 = mock(TopicListener.class);
161         base.register(listener);
162         base.register(listener2);
163
164         // broadcast a message
165         final String msg1 = "message-A";
166         base.broadcast(msg1);
167         verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg1);
168         verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg1);
169
170         // broadcast another message, with an exception
171         final String msg2 = "message-B";
172         doThrow(new RuntimeException(EXPECTED)).when(listener).onTopicEvent(any(), any(), any());
173         base.broadcast(msg2);
174         verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg2);
175         verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg2);
176     }
177
178     @Test
179     void testLock_testUnlock() {
180         assertFalse(base.isLocked());
181         assertTrue(base.lock());
182         assertEquals(0, base.startCount);
183         assertEquals(1, base.stopCount);
184
185         // lock again - should not stop again
186         assertTrue(base.isLocked());
187         assertTrue(base.lock());
188         assertEquals(0, base.startCount);
189         assertEquals(1, base.stopCount);
190
191         assertTrue(base.isLocked());
192         assertTrue(base.unlock());
193         assertEquals(1, base.startCount);
194         assertEquals(1, base.stopCount);
195
196         // unlock again - should not start again
197         assertFalse(base.isLocked());
198         assertTrue(base.unlock());
199         assertEquals(1, base.startCount);
200         assertEquals(1, base.stopCount);
201     }
202
203     /**
204      * Tests lock/unlock when the stop/start methods return false.
205      */
206     @Test
207     void testLock_testUnlock_FalseReturns() {
208
209         // lock, but stop returns false
210         base.stopReturn = false;
211         assertFalse(base.lock());
212         assertTrue(base.isLocked());
213         assertTrue(base.lock());
214
215         // unlock, but start returns false
216         base.startReturn = false;
217         assertFalse(base.unlock());
218         assertFalse(base.isLocked());
219         assertTrue(base.unlock());
220     }
221
222     /**
223      * Tests lock/unlock when the start method throws an exception.
224      */
225     @Test
226     void testLock_testUnlock_Exception() {
227
228         // lock & re-lock, but start throws an exception
229         base.startEx = true;
230         assertTrue(base.lock());
231         assertFalse(base.unlock());
232         assertFalse(base.isLocked());
233         assertTrue(base.unlock());
234     }
235
236     @Test
237     void testIsLocked() {
238         assertFalse(base.isLocked());
239         base.lock();
240         assertTrue(base.isLocked());
241         base.unlock();
242         assertFalse(base.isLocked());
243     }
244
245     @Test
246     void testGetTopic() {
247         assertEquals(MY_TOPIC, base.getTopic());
248     }
249
250     @Test
251     void testGetEffectiveTopic() {
252         assertEquals(MY_TOPIC, base.getTopic());
253         assertEquals(MY_TOPIC, base.getEffectiveTopic());
254     }
255
256     @Test
257     void testIsAlive() {
258         assertFalse(base.isAlive());
259         base.start();
260         assertTrue(base.isAlive());
261         base.stop();
262         assertFalse(base.isAlive());
263     }
264
265     @Test
266     void testGetServers() {
267         assertEquals(servers, base.getServers());
268     }
269
270     @Test
271     void testGetRecentEvents() {
272         assertEquals(0, base.getRecentEvents().length);
273
274         base.addEvent("recent-A");
275         base.addEvent("recent-B");
276
277         String[] recent = base.getRecentEvents();
278         assertEquals(2, recent.length);
279         assertEquals("recent-A", recent[0]);
280         assertEquals("recent-B", recent[1]);
281     }
282
283     @Test
284     void testToString() {
285         assertNotNull(base.toString());
286     }
287
288     /**
289      * Implementation of TopicBase.
290      */
291     private static class TopicBaseImpl extends TopicBase {
292         private int startCount = 0;
293         private int stopCount = 0;
294         private boolean startReturn = true;
295         private boolean stopReturn = true;
296         private boolean startEx = false;
297
298         /**
299          * Constructor.
300          *
301          * @param servers list of servers
302          * @param topic topic name
303          */
304         public TopicBaseImpl(List<String> servers, String topic) {
305             super(servers, topic);
306         }
307
308         /**
309          * Constructor.
310          *
311          * @param servers list of servers
312          * @param topic topic name
313          * @param effectiveTopic effective topic name for network communication
314          */
315         public TopicBaseImpl(List<String> servers, String topic, String effectiveTopic) {
316             super(servers, topic, effectiveTopic);
317         }
318
319         @Override
320         public CommInfrastructure getTopicCommInfrastructure() {
321             return CommInfrastructure.NOOP;
322         }
323
324         @Override
325         public boolean start() {
326             ++startCount;
327
328             if (startEx) {
329                 throw new RuntimeException(EXPECTED);
330             }
331
332             alive = true;
333             return startReturn;
334         }
335
336         @Override
337         public boolean stop() {
338             ++stopCount;
339             alive = false;
340             return stopReturn;
341         }
342
343         @Override
344         public void shutdown() {
345             // do nothing
346         }
347
348         /**
349          * Adds an event to the list of recent events.
350          *
351          * @param event event to be added
352          */
353         public void addEvent(String event) {
354             recentEvents.add(event);
355         }
356     }
357 }