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);
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());
205 // unlock, but start returns false
206 base.startReturn = false;
207 assertFalse(base.unlock());
208 assertFalse(base.isLocked());
209 assertTrue(base.unlock());
211 // lock & re-lock, but start throws an exception
212 base = new TopicBaseImpl(servers, MY_TOPIC);
214 assertTrue(base.lock());
215 assertFalse(base.unlock());
216 assertFalse(base.isLocked());
217 assertTrue(base.unlock());
221 public void testIsLocked() {
222 assertFalse(base.isLocked());
224 assertTrue(base.isLocked());
226 assertFalse(base.isLocked());
230 public void testGetTopic() {
231 assertEquals(MY_TOPIC, base.getTopic());
235 public void testGetEffectiveTopic() {
236 assertEquals(MY_TOPIC, base.getTopic());
237 assertEquals(MY_TOPIC, base.getEffectiveTopic());
241 public void testIsAlive() {
242 assertFalse(base.isAlive());
244 assertTrue(base.isAlive());
246 assertFalse(base.isAlive());
250 public void testGetServers() {
251 assertEquals(servers, base.getServers());
255 public void testGetRecentEvents() {
256 assertEquals(0, base.getRecentEvents().length);
258 base.addEvent("recent-A");
259 base.addEvent("recent-B");
261 String[] recent = base.getRecentEvents();
262 assertEquals(2, recent.length);
263 assertEquals("recent-A", recent[0]);
264 assertEquals("recent-B", recent[1]);
268 public void testToString() {
269 assertNotNull(base.toString());
273 * Implementation of TopicBase.
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;
285 * @param servers list of servers
286 * @param topic topic name
288 public TopicBaseImpl(List<String> servers, String topic) {
289 super(servers, topic);
295 * @param servers list of servers
296 * @param topic topic name
297 * @param effectiveTopic effective topic name for network communication
299 public TopicBaseImpl(List<String> servers, String topic, String effectiveTopic) {
300 super(servers, topic, effectiveTopic);
304 public CommInfrastructure getTopicCommInfrastructure() {
305 return CommInfrastructure.NOOP;
309 public boolean start() {
313 throw new RuntimeException(EXPECTED);
321 public boolean stop() {
328 public void shutdown() {
333 * Adds an event to the list of recent events.
335 * @param event event to be added
337 public void addEvent(String event) {
338 recentEvents.add(event);