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.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;
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;
42 public class TopicBaseTest extends TopicTestBase {
44 private TopicBaseImpl base;
47 * Creates the object to be tested.
54 base = new TopicBaseImpl(servers, MY_TOPIC);
57 @Test(expected = IllegalArgumentException.class)
58 public void testTopicBase_NullServers() {
59 new TopicBaseImpl(null, MY_TOPIC);
62 @Test(expected = IllegalArgumentException.class)
63 public void testTopicBase_EmptyServers() {
64 new TopicBaseImpl(Collections.emptyList(), MY_TOPIC);
67 @Test(expected = IllegalArgumentException.class)
68 public void testTopicBase_NullTopic() {
69 new TopicBaseImpl(servers, null);
72 @Test(expected = IllegalArgumentException.class)
73 public void testTopicBase_EmptyTopic() {
74 new TopicBaseImpl(servers, "");
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());
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());
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());
99 public void testSerialize() {
100 new GsonTestUtils().compareGson(base, TopicBaseTest.class);
104 public void testRegister() {
105 TopicListener listener = mock(TopicListener.class);
106 base.register(listener);
107 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
109 // re-register - list should be unchanged
110 base.register(listener);
111 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
113 // register a new listener
114 TopicListener listener2 = mock(TopicListener.class);
115 base.register(listener2);
116 assertEquals(Arrays.asList(listener, listener2), base.snapshotTopicListeners());
119 @Test(expected = IllegalArgumentException.class)
120 public void testRegister_NullListener() {
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);
133 base.unregister(listener);
134 assertEquals(Arrays.asList(listener2), base.snapshotTopicListeners());
136 // unregister the other
137 base.unregister(listener2);
138 assertTrue(base.snapshotTopicListeners().isEmpty());
141 base.unregister(listener2);
142 assertTrue(base.snapshotTopicListeners().isEmpty());
145 @Test(expected = IllegalArgumentException.class)
146 public void testUnregister_NullListener() {
147 base.register(mock(TopicListener.class));
148 base.unregister(null);
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);
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);
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);
174 public void testLock_testUnlock() {
175 assertFalse(base.isLocked());
176 assertTrue(base.lock());
177 assertEquals(0, base.startCount);
178 assertEquals(1, base.stopCount);
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);
186 assertTrue(base.isLocked());
187 assertTrue(base.unlock());
188 assertEquals(1, base.startCount);
189 assertEquals(1, base.stopCount);
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);
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());
204 // unlock, but start returns false
205 base.startReturn = false;
206 assertFalse(base.unlock());
207 assertFalse(base.isLocked());
208 assertTrue(base.unlock());
210 // lock & re-lock, but start throws an exception
211 base = new TopicBaseImpl(servers, MY_TOPIC);
213 assertTrue(base.lock());
214 assertFalse(base.unlock());
215 assertFalse(base.isLocked());
216 assertTrue(base.unlock());
220 public void testIsLocked() {
221 assertFalse(base.isLocked());
223 assertTrue(base.isLocked());
225 assertFalse(base.isLocked());
229 public void testGetTopic() {
230 assertEquals(MY_TOPIC, base.getTopic());
234 public void testGetEffectiveTopic() {
235 assertEquals(MY_TOPIC, base.getTopic());
236 assertEquals(MY_TOPIC, base.getEffectiveTopic());
240 public void testIsAlive() {
241 assertFalse(base.isAlive());
243 assertTrue(base.isAlive());
245 assertFalse(base.isAlive());
249 public void testGetServers() {
250 assertEquals(servers, base.getServers());
254 public void testGetRecentEvents() {
255 assertEquals(0, base.getRecentEvents().length);
257 base.addEvent("recent-A");
258 base.addEvent("recent-B");
260 String[] recent = base.getRecentEvents();
261 assertEquals(2, recent.length);
262 assertEquals("recent-A", recent[0]);
263 assertEquals("recent-B", recent[1]);
267 public void testToString() {
268 assertNotNull(base.toString());
272 * Implementation of TopicBase.
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;
284 * @param servers list of servers
285 * @param topic topic name
287 public TopicBaseImpl(List<String> servers, String topic) {
288 super(servers, topic);
294 * @param servers list of servers
295 * @param topic topic name
296 * @param effectiveTopic effective topic name for network communication
298 public TopicBaseImpl(List<String> servers, String topic, String effectiveTopic) {
299 super(servers, topic, effectiveTopic);
303 public CommInfrastructure getTopicCommInfrastructure() {
304 return CommInfrastructure.NOOP;
308 public boolean start() {
312 throw new RuntimeException(EXPECTED);
320 public boolean stop() {
327 public void shutdown() {
332 * Adds an event to the list of recent events.
334 * @param event event to be added
336 public void addEvent(String event) {
337 recentEvents.add(event);