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