2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.common.endpoints.event.comm.bus.internal;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.doThrow;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
41 import org.onap.policy.common.endpoints.event.comm.TopicListener;
42 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
43 import org.onap.policy.common.utils.gson.GsonTestUtils;
45 class TopicBaseTest extends TopicTestBase {
47 private TopicBaseImpl base;
50 * Creates the object to be tested.
57 base = new TopicBaseImpl(servers, MY_TOPIC);
61 void testTopicBase_NullServers() {
62 assertThatThrownBy(() -> new TopicBaseImpl(null, MY_TOPIC)).isInstanceOf(IllegalArgumentException.class);
66 void testTopicBase_EmptyServers() {
67 List<String> testList = Collections.emptyList();
68 assertThatThrownBy(() -> new TopicBaseImpl(testList, MY_TOPIC))
69 .isInstanceOf(IllegalArgumentException.class);
73 void testTopicBase_NullTopic() {
74 assertThatThrownBy(() -> new TopicBaseImpl(servers, null)).isInstanceOf(IllegalArgumentException.class);
78 void testTopicBase_EmptyTopic() {
79 assertThatThrownBy(() -> new TopicBaseImpl(servers, "")).isInstanceOf(IllegalArgumentException.class);
83 void testTopicBase_EffectiveTopic() {
84 TopicBase baseEf = new TopicBaseImpl(servers, MY_TOPIC, MY_EFFECTIVE_TOPIC);
85 assertEquals(MY_TOPIC, baseEf.getTopic());
86 assertEquals(MY_EFFECTIVE_TOPIC, baseEf.getEffectiveTopic());
90 void testTopicBase_NullEffectiveTopic() {
91 TopicBase baseEf = new TopicBaseImpl(servers, MY_TOPIC, null);
92 assertEquals(MY_TOPIC, baseEf.getTopic());
93 assertEquals(MY_TOPIC, baseEf.getEffectiveTopic());
97 void testTopicBase_EmptyEffectiveTopic() {
98 TopicBase baseEf = new TopicBaseImpl(servers, MY_TOPIC, "");
99 assertEquals(MY_TOPIC, baseEf.getTopic());
100 assertEquals(MY_TOPIC, baseEf.getEffectiveTopic());
104 void testSerialize() {
105 assertThatCode(() -> new GsonTestUtils().compareGson(base, TopicBaseTest.class)).doesNotThrowAnyException();
109 void testRegister() {
110 TopicListener listener = mock(TopicListener.class);
111 base.register(listener);
112 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
114 // re-register - list should be unchanged
115 base.register(listener);
116 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
118 // register a new listener
119 TopicListener listener2 = mock(TopicListener.class);
120 base.register(listener2);
121 assertEquals(Arrays.asList(listener, listener2), base.snapshotTopicListeners());
125 void testRegister_NullListener() {
126 assertThatThrownBy(() -> base.register(null)).isInstanceOf(IllegalArgumentException.class);
130 void testUnregister() {
131 // register two listeners
132 TopicListener listener = mock(TopicListener.class);
133 TopicListener listener2 = mock(TopicListener.class);
134 base.register(listener);
135 base.register(listener2);
138 base.unregister(listener);
139 assertEquals(Arrays.asList(listener2), base.snapshotTopicListeners());
141 // unregister the other
142 base.unregister(listener2);
143 assertTrue(base.snapshotTopicListeners().isEmpty());
146 base.unregister(listener2);
147 assertTrue(base.snapshotTopicListeners().isEmpty());
151 void testUnregister_NullListener() {
152 base.register(mock(TopicListener.class));
153 assertThatThrownBy(() -> base.unregister(null)).isInstanceOf(IllegalArgumentException.class);
157 void testBroadcast() {
158 // register two listeners
159 TopicListener listener = mock(TopicListener.class);
160 TopicListener listener2 = mock(TopicListener.class);
161 base.register(listener);
162 base.register(listener2);
164 // broadcast a message
165 final String msg1 = "message-A";
166 base.broadcast(msg1);
167 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg1);
168 verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg1);
170 // broadcast another message, with an exception
171 final String msg2 = "message-B";
172 doThrow(new RuntimeException(EXPECTED)).when(listener).onTopicEvent(any(), any(), any());
173 base.broadcast(msg2);
174 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg2);
175 verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg2);
179 void testLock_testUnlock() {
180 assertFalse(base.isLocked());
181 assertTrue(base.lock());
182 assertEquals(0, base.startCount);
183 assertEquals(1, base.stopCount);
185 // lock again - should not stop again
186 assertTrue(base.isLocked());
187 assertTrue(base.lock());
188 assertEquals(0, base.startCount);
189 assertEquals(1, base.stopCount);
191 assertTrue(base.isLocked());
192 assertTrue(base.unlock());
193 assertEquals(1, base.startCount);
194 assertEquals(1, base.stopCount);
196 // unlock again - should not start again
197 assertFalse(base.isLocked());
198 assertTrue(base.unlock());
199 assertEquals(1, base.startCount);
200 assertEquals(1, base.stopCount);
204 * Tests lock/unlock when the stop/start methods return false.
207 void testLock_testUnlock_FalseReturns() {
209 // lock, but stop returns false
210 base.stopReturn = false;
211 assertFalse(base.lock());
212 assertTrue(base.isLocked());
213 assertTrue(base.lock());
215 // unlock, but start returns false
216 base.startReturn = false;
217 assertFalse(base.unlock());
218 assertFalse(base.isLocked());
219 assertTrue(base.unlock());
223 * Tests lock/unlock when the start method throws an exception.
226 void testLock_testUnlock_Exception() {
228 // lock & re-lock, but start throws an exception
230 assertTrue(base.lock());
231 assertFalse(base.unlock());
232 assertFalse(base.isLocked());
233 assertTrue(base.unlock());
237 void testIsLocked() {
238 assertFalse(base.isLocked());
240 assertTrue(base.isLocked());
242 assertFalse(base.isLocked());
246 void testGetTopic() {
247 assertEquals(MY_TOPIC, base.getTopic());
251 void testGetEffectiveTopic() {
252 assertEquals(MY_TOPIC, base.getTopic());
253 assertEquals(MY_TOPIC, base.getEffectiveTopic());
258 assertFalse(base.isAlive());
260 assertTrue(base.isAlive());
262 assertFalse(base.isAlive());
266 void testGetServers() {
267 assertEquals(servers, base.getServers());
271 void testGetRecentEvents() {
272 assertEquals(0, base.getRecentEvents().length);
274 base.addEvent("recent-A");
275 base.addEvent("recent-B");
277 String[] recent = base.getRecentEvents();
278 assertEquals(2, recent.length);
279 assertEquals("recent-A", recent[0]);
280 assertEquals("recent-B", recent[1]);
284 void testToString() {
285 assertNotNull(base.toString());
289 * Implementation of TopicBase.
291 private static class TopicBaseImpl extends TopicBase {
292 private int startCount = 0;
293 private int stopCount = 0;
294 private boolean startReturn = true;
295 private boolean stopReturn = true;
296 private boolean startEx = false;
301 * @param servers list of servers
302 * @param topic topic name
304 public TopicBaseImpl(List<String> servers, String topic) {
305 super(servers, topic);
311 * @param servers list of servers
312 * @param topic topic name
313 * @param effectiveTopic effective topic name for network communication
315 public TopicBaseImpl(List<String> servers, String topic, String effectiveTopic) {
316 super(servers, topic, effectiveTopic);
320 public CommInfrastructure getTopicCommInfrastructure() {
321 return CommInfrastructure.NOOP;
325 public boolean start() {
329 throw new RuntimeException(EXPECTED);
337 public boolean stop() {
344 public void shutdown() {
349 * Adds an event to the list of recent events.
351 * @param event event to be added
353 public void addEvent(String event) {
354 recentEvents.add(event);