2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 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.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.io.IOException;
34 import java.net.MalformedURLException;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.invocation.InvocationOnMock;
41 import org.mockito.stubbing.Answer;
42 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
43 import org.onap.policy.common.endpoints.event.comm.TopicListener;
44 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
45 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusConsumer.FilterableBusConsumer;
46 import org.onap.policy.common.utils.gson.GsonTestUtils;
48 public class SingleThreadedBusTopicSourceTest extends TopicTestBase {
49 private Thread thread;
50 private BusConsumer cons;
51 private TopicListener listener;
52 private SingleThreadedBusTopicSourceImpl source;
55 * Creates the object to be tested, as well as various mocks.
62 thread = mock(Thread.class);
63 cons = mock(BusConsumer.class);
64 listener = mock(TopicListener.class);
65 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().build());
69 public void tearDown() {
74 public void testSerialize() {
75 new GsonTestUtils().compareGson(source, SingleThreadedBusTopicSourceTest.class);
79 public void testRegister() {
80 source.register(listener);
81 assertEquals(1, source.initCount);
82 source.offer(MY_MESSAGE);
83 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
85 // register another - should not re-init
86 TopicListener listener2 = mock(TopicListener.class);
87 source.register(listener2);
88 assertEquals(1, source.initCount);
89 source.offer(MY_MESSAGE + "z");
90 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE + "z");
91 verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE + "z");
93 // re-register - should not re-init
94 source.register(listener);
95 assertEquals(1, source.initCount);
96 source.offer(MY_MESSAGE2);
97 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE2);
99 // lock & register - should not init
100 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().build());
102 source.register(listener);
103 assertEquals(0, source.initCount);
105 // exception during init
106 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().build());
107 source.initEx = true;
108 source.register(listener);
112 public void testUnregister() {
113 TopicListener listener2 = mock(TopicListener.class);
114 source.register(listener);
115 source.register(listener2);
117 // unregister first listener - should NOT invoke close
118 source.unregister(listener);
119 verify(cons, never()).close();
120 assertEquals(Arrays.asList(listener2), source.snapshotTopicListeners());
122 // unregister same listener - should not invoke close
123 source.unregister(listener);
124 verify(cons, never()).close();
125 assertEquals(Arrays.asList(listener2), source.snapshotTopicListeners());
127 // unregister second listener - SHOULD invoke close
128 source.unregister(listener2);
129 verify(cons).close();
130 assertTrue(source.snapshotTopicListeners().isEmpty());
132 // unregister same listener - should not invoke close again
133 source.unregister(listener2);
134 verify(cons).close();
135 assertTrue(source.snapshotTopicListeners().isEmpty());
139 public void testToString() {
140 assertTrue(source.toString().startsWith("SingleThreadedBusTopicSource ["));
144 public void testMakePollerThread() {
145 SingleThreadedBusTopicSource source2 = new SingleThreadedBusTopicSource(makeBuilder().build()) {
147 public CommInfrastructure getTopicCommInfrastructure() {
148 return CommInfrastructure.NOOP;
152 public void init() throws MalformedURLException {
157 assertNotNull(source2.makePollerThread());
161 public void testSingleThreadedBusTopicSource() {
162 // verify that different wrappers can be built
163 new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerGroup(null).build());
164 new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerInstance(null).build());
165 new SingleThreadedBusTopicSourceImpl(makeBuilder().fetchTimeout(-1).build());
166 new SingleThreadedBusTopicSourceImpl(makeBuilder().fetchLimit(-1).build());
170 public void testStart() {
172 assertTrue(source.isAlive());
173 assertEquals(1, source.initCount);
174 verify(thread).start();
176 // attempt to start again - nothing should be invoked again
178 assertTrue(source.isAlive());
179 assertEquals(1, source.initCount);
180 verify(thread).start();
185 assertTrue(source.isAlive());
186 assertEquals(2, source.initCount);
187 verify(thread, times(2)).start();
190 @Test(expected = IllegalStateException.class)
191 public void testStart_Locked() {
196 @Test(expected = IllegalStateException.class)
197 public void testStart_InitEx() {
198 source.initEx = true;
203 public void testStop() {
206 verify(cons).close();
208 // stop it again - not re-closed
210 verify(cons).close();
212 // start & stop again, but with an exception
213 doThrow(new RuntimeException(EXPECTED)).when(cons).close();
219 public void testRun() throws Exception {
220 source.register(listener);
223 * Die in the middle of fetching messages. Also, throw an exception during the
224 * first fetch attempt.
226 when(cons.fetch()).thenAnswer(new Answer<Iterable<String>>() {
230 public Iterable<String> answer(InvocationOnMock invocation) throws Throwable {
232 source.alive = false;
233 return Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
236 throw new IOException(EXPECTED);
242 assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(source.getRecentEvents()));
243 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
244 verify(listener, never()).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE2);
247 * Die AFTER fetching messages.
249 final String msga = "message-A";
250 final String msgb = "message-B";
251 when(cons.fetch()).thenAnswer(new Answer<Iterable<String>>() {
255 public Iterable<String> answer(InvocationOnMock invocation) throws Throwable {
257 source.alive = false;
258 return Collections.emptyList();
261 return Arrays.asList(msga, msgb);
267 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msga);
268 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msgb);
270 assertEquals(Arrays.asList(MY_MESSAGE, msga, msgb), Arrays.asList(source.getRecentEvents()));
274 public void testOffer() {
275 source.register(listener);
276 source.offer(MY_MESSAGE);
277 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
278 assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(source.getRecentEvents()));
281 @Test(expected = IllegalStateException.class)
282 public void testOffer_NotStarted() {
283 source.offer(MY_MESSAGE);
287 public void testSetFilter() {
288 FilterableBusConsumer filt = mock(FilterableBusConsumer.class);
292 source.setFilter("my-filter");
293 verify(filt).setFilter("my-filter");
296 @Test(expected = UnsupportedOperationException.class)
297 public void testSetFilter_Unsupported() {
299 source.setFilter("unsupported-filter");
303 public void testGetConsumerGroup() {
304 assertEquals(MY_CONS_GROUP, source.getConsumerGroup());
308 public void testGetConsumerInstance() {
309 assertEquals(MY_CONS_INST, source.getConsumerInstance());
313 public void testShutdown() {
314 source.register(listener);
317 verify(cons).close();
318 assertTrue(source.snapshotTopicListeners().isEmpty());
322 public void testGetFetchTimeout() {
323 assertEquals(MY_FETCH_TIMEOUT, source.getFetchTimeout());
327 public void testGetFetchLimit() {
328 assertEquals(MY_FETCH_LIMIT, source.getFetchLimit());
332 * Implementation of SingleThreadedBusTopicSource that counts the number of times
335 private class SingleThreadedBusTopicSourceImpl extends SingleThreadedBusTopicSource {
337 private int initCount = 0;
338 private boolean initEx = false;
340 public SingleThreadedBusTopicSourceImpl(BusTopicParams busTopicParams) {
341 super(busTopicParams);
345 public CommInfrastructure getTopicCommInfrastructure() {
346 return CommInfrastructure.NOOP;
350 public void init() throws MalformedURLException {
354 throw new MalformedURLException(EXPECTED);
361 protected Thread makePollerThread() {