2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2021 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.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.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.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.invocation.InvocationOnMock;
43 import org.mockito.stubbing.Answer;
44 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
45 import org.onap.policy.common.endpoints.event.comm.TopicListener;
46 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
47 import org.onap.policy.common.utils.gson.GsonTestUtils;
48 import org.onap.policy.common.utils.network.NetworkUtil;
50 public 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 public void testSerialize() {
77 assertThatCode(() -> new GsonTestUtils().compareGson(source, SingleThreadedBusTopicSourceTest.class))
78 .doesNotThrowAnyException();
82 public void testRegister() {
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 public 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 public void testToString() {
143 assertTrue(source.toString().startsWith("SingleThreadedBusTopicSource ["));
147 public 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 public 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();
192 public void testStart() {
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();
212 @Test(expected = IllegalStateException.class)
213 public void testStart_Locked() {
218 @Test(expected = IllegalStateException.class)
219 public void testStart_InitEx() {
220 source.initEx = true;
225 public void testStop() {
228 verify(cons).close();
230 // stop it again - not re-closed
232 verify(cons).close();
234 // start & stop again, but with an exception
235 doThrow(new RuntimeException(EXPECTED)).when(cons).close();
241 public void testRun() throws Exception {
242 source.register(listener);
245 * Die in the middle of fetching messages. Also, throw an exception during the
246 * first fetch attempt.
248 when(cons.fetch()).thenAnswer(new Answer<Iterable<String>>() {
252 public Iterable<String> answer(InvocationOnMock invocation) throws Throwable {
254 source.alive = false;
255 return Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
258 throw new IOException(EXPECTED);
264 assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(source.getRecentEvents()));
265 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
266 verify(listener, never()).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE2);
269 * Die AFTER fetching messages.
271 final String msga = "message-A";
272 final String msgb = "message-B";
273 when(cons.fetch()).thenAnswer(new Answer<Iterable<String>>() {
277 public Iterable<String> answer(InvocationOnMock invocation) throws Throwable {
279 source.alive = false;
280 return Collections.emptyList();
283 return Arrays.asList(msga, msgb);
289 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msga);
290 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msgb);
292 assertEquals(Arrays.asList(MY_MESSAGE, msga, msgb), Arrays.asList(source.getRecentEvents()));
296 public void testOffer() {
297 source.register(listener);
298 source.offer(MY_MESSAGE);
299 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
300 assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(source.getRecentEvents()));
303 @Test(expected = IllegalStateException.class)
304 public void testOffer_NotStarted() {
305 source.offer(MY_MESSAGE);
309 public void testGetConsumerGroup() {
310 assertEquals(MY_CONS_GROUP, source.getConsumerGroup());
314 public void testGetConsumerInstance() {
315 assertEquals(MY_CONS_INST, source.getConsumerInstance());
319 public void testShutdown() {
320 source.register(listener);
323 verify(cons).close();
324 assertTrue(source.snapshotTopicListeners().isEmpty());
328 public void testGetFetchTimeout() {
329 assertEquals(MY_FETCH_TIMEOUT, source.getFetchTimeout());
333 public void testGetFetchLimit() {
334 assertEquals(MY_FETCH_LIMIT, source.getFetchLimit());
338 * Implementation of SingleThreadedBusTopicSource that counts the number of times
341 private class SingleThreadedBusTopicSourceImpl extends SingleThreadedBusTopicSource {
343 private int initCount = 0;
344 private boolean initEx = false;
346 public SingleThreadedBusTopicSourceImpl(BusTopicParams busTopicParams) {
347 super(busTopicParams);
351 public CommInfrastructure getTopicCommInfrastructure() {
352 return CommInfrastructure.NOOP;
356 public void init() throws MalformedURLException {
360 throw new MalformedURLException(EXPECTED);
367 protected Thread makePollerThread() {