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