67b84ea8817dc81f5e4939c04d8b5281cc3d11fa
[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         // lock, but stop returns false
199         base = new TopicBaseImpl(servers, MY_TOPIC);
200         base.stopReturn = false;
201         assertFalse(base.lock());
202         assertTrue(base.isLocked());
203         assertTrue(base.lock());
204
205         // unlock, but start returns false
206         base.startReturn = false;
207         assertFalse(base.unlock());
208         assertFalse(base.isLocked());
209         assertTrue(base.unlock());
210
211         // lock & re-lock, but start throws an exception
212         base = new TopicBaseImpl(servers, MY_TOPIC);
213         base.startEx = true;
214         assertTrue(base.lock());
215         assertFalse(base.unlock());
216         assertFalse(base.isLocked());
217         assertTrue(base.unlock());
218     }
219
220     @Test
221     public void testIsLocked() {
222         assertFalse(base.isLocked());
223         base.lock();
224         assertTrue(base.isLocked());
225         base.unlock();
226         assertFalse(base.isLocked());
227     }
228
229     @Test
230     public void testGetTopic() {
231         assertEquals(MY_TOPIC, base.getTopic());
232     }
233
234     @Test
235     public void testGetEffectiveTopic() {
236         assertEquals(MY_TOPIC, base.getTopic());
237         assertEquals(MY_TOPIC, base.getEffectiveTopic());
238     }
239
240     @Test
241     public void testIsAlive() {
242         assertFalse(base.isAlive());
243         base.start();
244         assertTrue(base.isAlive());
245         base.stop();
246         assertFalse(base.isAlive());
247     }
248
249     @Test
250     public void testGetServers() {
251         assertEquals(servers, base.getServers());
252     }
253
254     @Test
255     public void testGetRecentEvents() {
256         assertEquals(0, base.getRecentEvents().length);
257
258         base.addEvent("recent-A");
259         base.addEvent("recent-B");
260
261         String[] recent = base.getRecentEvents();
262         assertEquals(2, recent.length);
263         assertEquals("recent-A", recent[0]);
264         assertEquals("recent-B", recent[1]);
265     }
266
267     @Test
268     public void testToString() {
269         assertNotNull(base.toString());
270     }
271
272     /**
273      * Implementation of TopicBase.
274      */
275     private static class TopicBaseImpl extends TopicBase {
276         private int startCount = 0;
277         private int stopCount = 0;
278         private boolean startReturn = true;
279         private boolean stopReturn = true;
280         private boolean startEx = false;
281
282         /**
283          * Constructor.
284          *
285          * @param servers list of servers
286          * @param topic topic name
287          */
288         public TopicBaseImpl(List<String> servers, String topic) {
289             super(servers, topic);
290         }
291
292         /**
293          * Constructor.
294          *
295          * @param servers list of servers
296          * @param topic topic name
297          * @param effectiveTopic effective topic name for network communication
298          */
299         public TopicBaseImpl(List<String> servers, String topic, String effectiveTopic) {
300             super(servers, topic, effectiveTopic);
301         }
302
303         @Override
304         public CommInfrastructure getTopicCommInfrastructure() {
305             return CommInfrastructure.NOOP;
306         }
307
308         @Override
309         public boolean start() {
310             ++startCount;
311
312             if (startEx) {
313                 throw new RuntimeException(EXPECTED);
314             }
315
316             alive = true;
317             return startReturn;
318         }
319
320         @Override
321         public boolean stop() {
322             ++stopCount;
323             alive = false;
324             return stopReturn;
325         }
326
327         @Override
328         public void shutdown() {
329             // do nothing
330         }
331
332         /**
333          * Adds an event to the list of recent events.
334          *
335          * @param event event to be added
336          */
337         public void addEvent(String event) {
338             recentEvents.add(event);
339         }
340     }
341 }