2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.common.endpoints.event.comm.bus.internal;
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;
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;
43 public class TopicBaseTest extends TopicTestBase {
45 private TopicBaseImpl base;
48 * Creates the object to be tested.
55 base = new TopicBaseImpl(servers, MY_TOPIC);
58 @Test(expected = IllegalArgumentException.class)
59 public void testTopicBase_NullServers() {
60 new TopicBaseImpl(null, MY_TOPIC);
63 @Test(expected = IllegalArgumentException.class)
64 public void testTopicBase_EmptyServers() {
65 new TopicBaseImpl(Collections.emptyList(), MY_TOPIC);
68 @Test(expected = IllegalArgumentException.class)
69 public void testTopicBase_NullTopic() {
70 new TopicBaseImpl(servers, null);
73 @Test(expected = IllegalArgumentException.class)
74 public void testTopicBase_EmptyTopic() {
75 new TopicBaseImpl(servers, "");
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());
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());
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());
100 public void testSerialize() {
101 assertThatCode(() -> new GsonTestUtils().compareGson(base, TopicBaseTest.class)).doesNotThrowAnyException();
105 public void testRegister() {
106 TopicListener listener = mock(TopicListener.class);
107 base.register(listener);
108 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
110 // re-register - list should be unchanged
111 base.register(listener);
112 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
114 // register a new listener
115 TopicListener listener2 = mock(TopicListener.class);
116 base.register(listener2);
117 assertEquals(Arrays.asList(listener, listener2), base.snapshotTopicListeners());
120 @Test(expected = IllegalArgumentException.class)
121 public void testRegister_NullListener() {
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);
134 base.unregister(listener);
135 assertEquals(Arrays.asList(listener2), base.snapshotTopicListeners());
137 // unregister the other
138 base.unregister(listener2);
139 assertTrue(base.snapshotTopicListeners().isEmpty());
142 base.unregister(listener2);
143 assertTrue(base.snapshotTopicListeners().isEmpty());
146 @Test(expected = IllegalArgumentException.class)
147 public void testUnregister_NullListener() {
148 base.register(mock(TopicListener.class));
149 base.unregister(null);
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);
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);
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);
175 public void testLock_testUnlock() {
176 assertFalse(base.isLocked());
177 assertTrue(base.lock());
178 assertEquals(0, base.startCount);
179 assertEquals(1, base.stopCount);
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);
187 assertTrue(base.isLocked());
188 assertTrue(base.unlock());
189 assertEquals(1, base.startCount);
190 assertEquals(1, base.stopCount);
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);
200 * Tests lock/unlock when the stop/start methods return false.
203 public void testLock_testUnlock_FalseReturns() {
205 // lock, but stop returns false
206 base.stopReturn = false;
207 assertFalse(base.lock());
208 assertTrue(base.isLocked());
209 assertTrue(base.lock());
211 // unlock, but start returns false
212 base.startReturn = false;
213 assertFalse(base.unlock());
214 assertFalse(base.isLocked());
215 assertTrue(base.unlock());
219 * Tests lock/unlock when the start method throws an exception.
222 public void testLock_testUnlock_Exception() {
224 // lock & re-lock, but start throws an exception
226 assertTrue(base.lock());
227 assertFalse(base.unlock());
228 assertFalse(base.isLocked());
229 assertTrue(base.unlock());
233 public void testIsLocked() {
234 assertFalse(base.isLocked());
236 assertTrue(base.isLocked());
238 assertFalse(base.isLocked());
242 public void testGetTopic() {
243 assertEquals(MY_TOPIC, base.getTopic());
247 public void testGetEffectiveTopic() {
248 assertEquals(MY_TOPIC, base.getTopic());
249 assertEquals(MY_TOPIC, base.getEffectiveTopic());
253 public void testIsAlive() {
254 assertFalse(base.isAlive());
256 assertTrue(base.isAlive());
258 assertFalse(base.isAlive());
262 public void testGetServers() {
263 assertEquals(servers, base.getServers());
267 public void testGetRecentEvents() {
268 assertEquals(0, base.getRecentEvents().length);
270 base.addEvent("recent-A");
271 base.addEvent("recent-B");
273 String[] recent = base.getRecentEvents();
274 assertEquals(2, recent.length);
275 assertEquals("recent-A", recent[0]);
276 assertEquals("recent-B", recent[1]);
280 public void testToString() {
281 assertNotNull(base.toString());
285 * Implementation of TopicBase.
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;
297 * @param servers list of servers
298 * @param topic topic name
300 public TopicBaseImpl(List<String> servers, String topic) {
301 super(servers, topic);
307 * @param servers list of servers
308 * @param topic topic name
309 * @param effectiveTopic effective topic name for network communication
311 public TopicBaseImpl(List<String> servers, String topic, String effectiveTopic) {
312 super(servers, topic, effectiveTopic);
316 public CommInfrastructure getTopicCommInfrastructure() {
317 return CommInfrastructure.NOOP;
321 public boolean start() {
325 throw new RuntimeException(EXPECTED);
333 public boolean stop() {
340 public void shutdown() {
345 * Adds an event to the list of recent events.
347 * @param event event to be added
349 public void addEvent(String event) {
350 recentEvents.add(event);