2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2024 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.common.message.bus.event.base;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.never;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 import java.io.IOException;
36 import java.net.MalformedURLException;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import org.junit.jupiter.api.AfterEach;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.mockito.invocation.InvocationOnMock;
43 import org.mockito.stubbing.Answer;
44 import org.onap.policy.common.message.bus.event.Topic.CommInfrastructure;
45 import org.onap.policy.common.message.bus.event.TopicListener;
46 import org.onap.policy.common.parameters.topic.BusTopicParams;
47 import org.onap.policy.common.utils.gson.GsonTestUtils;
48 import org.onap.policy.common.utils.network.NetworkUtil;
50 class SingleThreadedBusTopicSourceTest extends TopicTestBase {
51 private Thread thread;
52 private BusConsumer cons;
53 private TopicListener listener;
54 private SingleThreadedBusTopicSourceImpl source;
57 * Creates the object to be tested, as well as various mocks.
64 thread = mock(Thread.class);
65 cons = mock(BusConsumer.class);
66 listener = mock(TopicListener.class);
67 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().build());
71 public void tearDown() {
76 void testSerialize() {
77 assertThatCode(() -> new GsonTestUtils().compareGson(source, SingleThreadedBusTopicSourceTest.class))
78 .doesNotThrowAnyException();
83 source.register(listener);
84 assertEquals(1, source.initCount);
85 source.offer(MY_MESSAGE);
86 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
88 // register another - should not re-init
89 TopicListener listener2 = mock(TopicListener.class);
90 source.register(listener2);
91 assertEquals(1, source.initCount);
92 source.offer(MY_MESSAGE + "z");
93 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE + "z");
94 verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE + "z");
96 // re-register - should not re-init
97 source.register(listener);
98 assertEquals(1, source.initCount);
99 source.offer(MY_MESSAGE2);
100 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE2);
102 // lock & register - should not init
103 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().build());
105 source.register(listener);
106 assertEquals(0, source.initCount);
108 // exception during init
109 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().build());
110 source.initEx = true;
111 source.register(listener);
115 void testUnregister() {
116 TopicListener listener2 = mock(TopicListener.class);
117 source.register(listener);
118 source.register(listener2);
120 // unregister first listener - should NOT invoke close
121 source.unregister(listener);
122 verify(cons, never()).close();
123 assertEquals(Arrays.asList(listener2), source.snapshotTopicListeners());
125 // unregister same listener - should not invoke close
126 source.unregister(listener);
127 verify(cons, never()).close();
128 assertEquals(Arrays.asList(listener2), source.snapshotTopicListeners());
130 // unregister second listener - SHOULD invoke close
131 source.unregister(listener2);
132 verify(cons).close();
133 assertTrue(source.snapshotTopicListeners().isEmpty());
135 // unregister same listener - should not invoke close again
136 source.unregister(listener2);
137 verify(cons).close();
138 assertTrue(source.snapshotTopicListeners().isEmpty());
142 void testToString() {
143 assertTrue(source.toString().startsWith("SingleThreadedBusTopicSource ["));
147 void testMakePollerThread() {
148 SingleThreadedBusTopicSource source2 = new SingleThreadedBusTopicSource(makeBuilder().build()) {
150 public CommInfrastructure getTopicCommInfrastructure() {
151 return CommInfrastructure.NOOP;
155 public void init() throws MalformedURLException {
160 assertNotNull(source2.makePollerThread());
164 void testSingleThreadedBusTopicSource() {
165 // Note: if the value contains "-", it's probably a UUID
167 // verify that different wrappers can be built
168 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().build());
169 assertThat(source.getConsumerGroup()).isEqualTo(MY_CONS_GROUP);
170 assertThat(source.getConsumerInstance()).isEqualTo(MY_CONS_INST);
172 // group is null => group is UUID, instance is as provided
173 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerGroup(null).build());
174 assertThat(source.getConsumerGroup()).contains("-").isNotEqualTo(NetworkUtil.getHostname());
175 assertThat(source.getConsumerInstance()).isEqualTo(MY_CONS_INST);
177 // instance is null => group is as provided, instance is UUID
178 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerInstance(null).build());
179 assertThat(source.getConsumerGroup()).isEqualTo(MY_CONS_GROUP);
180 assertThat(source.getConsumerInstance()).contains("-").isNotEqualTo(NetworkUtil.getHostname());
182 // group & instance are null => group is UUID, instance is hostname
183 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerGroup(null).consumerInstance(null).build());
184 assertThat(source.getConsumerGroup()).contains("-").isNotEqualTo(NetworkUtil.getHostname());
185 assertThat(source.getConsumerInstance()).isEqualTo(NetworkUtil.getHostname());
187 assertThatCode(() -> new SingleThreadedBusTopicSourceImpl(
188 makeBuilder().fetchLimit(-1).fetchTimeout(-1).build())).doesNotThrowAnyException();
194 assertTrue(source.isAlive());
195 assertEquals(1, source.initCount);
196 verify(thread).start();
198 // attempt to start again - nothing should be invoked again
200 assertTrue(source.isAlive());
201 assertEquals(1, source.initCount);
202 verify(thread).start();
207 assertTrue(source.isAlive());
208 assertEquals(2, source.initCount);
209 verify(thread, times(2)).start();
213 void testStart_Locked() {
215 assertThatThrownBy(() -> source.start()).isInstanceOf(IllegalStateException.class);
219 void testStart_InitEx() {
220 assertThatThrownBy(() -> {
221 source.initEx = true;
224 }).isInstanceOf(IllegalStateException.class);
231 verify(cons).close();
233 // stop it again - not re-closed
235 verify(cons).close();
237 // start & stop again, but with an exception
238 doThrow(new RuntimeException(EXPECTED)).when(cons).close();
244 void testRun() throws Exception {
245 source.register(listener);
248 * Die in the middle of fetching messages. Also, throw an exception during the
249 * first fetch attempt.
251 when(cons.fetch()).thenAnswer(new Answer<Iterable<String>>() {
255 public Iterable<String> answer(InvocationOnMock invocation) throws Throwable {
257 source.alive = false;
258 return Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
261 throw new IOException(EXPECTED);
267 assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(source.getRecentEvents()));
268 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
269 verify(listener, never()).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE2);
272 * Die AFTER fetching messages.
274 final String msga = "message-A";
275 final String msgb = "message-B";
276 when(cons.fetch()).thenAnswer(new Answer<Iterable<String>>() {
280 public Iterable<String> answer(InvocationOnMock invocation) throws Throwable {
282 source.alive = false;
283 return Collections.emptyList();
286 return Arrays.asList(msga, msgb);
292 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msga);
293 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msgb);
295 assertEquals(Arrays.asList(MY_MESSAGE, msga, msgb), Arrays.asList(source.getRecentEvents()));
300 source.register(listener);
301 source.offer(MY_MESSAGE);
302 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
303 assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(source.getRecentEvents()));
307 void testOffer_NotStarted() {
308 assertThatThrownBy(() -> source.offer(MY_MESSAGE)).isInstanceOf(IllegalStateException.class);
312 void testGetConsumerGroup() {
313 assertEquals(MY_CONS_GROUP, source.getConsumerGroup());
317 void testGetConsumerInstance() {
318 assertEquals(MY_CONS_INST, source.getConsumerInstance());
322 void testShutdown() {
323 source.register(listener);
326 verify(cons).close();
327 assertTrue(source.snapshotTopicListeners().isEmpty());
331 void testGetFetchTimeout() {
332 assertEquals(MY_FETCH_TIMEOUT, source.getFetchTimeout());
336 void testGetFetchLimit() {
337 assertEquals(MY_FETCH_LIMIT, source.getFetchLimit());
341 * Implementation of SingleThreadedBusTopicSource that counts the number of times
344 private class SingleThreadedBusTopicSourceImpl extends SingleThreadedBusTopicSource {
346 private int initCount = 0;
347 private boolean initEx = false;
349 public SingleThreadedBusTopicSourceImpl(BusTopicParams busTopicParams) {
350 super(busTopicParams);
354 public CommInfrastructure getTopicCommInfrastructure() {
355 return CommInfrastructure.NOOP;
359 public void init() throws MalformedURLException {
363 throw new MalformedURLException(EXPECTED);
370 protected Thread makePollerThread() {