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