2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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;
47 public class SingleThreadedBusTopicSourceTest extends TopicTestBase {
48 private Thread thread;
49 private BusConsumer cons;
50 private TopicListener listener;
51 private SingleThreadedBusTopicSourceImpl source;
54 * Creates the object to be tested, as well as various mocks.
60 thread = mock(Thread.class);
61 cons = mock(BusConsumer.class);
62 listener = mock(TopicListener.class);
63 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().build());
67 public void tearDown() {
72 public void testRegister() {
73 source.register(listener);
74 assertEquals(1, source.initCount);
75 source.offer(MY_MESSAGE);
76 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
78 // register another - should not re-init
79 TopicListener listener2 = mock(TopicListener.class);
80 source.register(listener2);
81 assertEquals(1, source.initCount);
82 source.offer(MY_MESSAGE + "z");
83 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE + "z");
84 verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE + "z");
86 // re-register - should not re-init
87 source.register(listener);
88 assertEquals(1, source.initCount);
89 source.offer(MY_MESSAGE2);
90 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE2);
92 // lock & register - should not init
93 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().build());
95 source.register(listener);
96 assertEquals(0, source.initCount);
98 // exception during init
99 source = new SingleThreadedBusTopicSourceImpl(makeBuilder().build());
100 source.initEx = true;
101 source.register(listener);
105 public void testUnregister() {
106 TopicListener listener2 = mock(TopicListener.class);
107 source.register(listener);
108 source.register(listener2);
110 // unregister first listener - should NOT invoke close
111 source.unregister(listener);
112 verify(cons, never()).close();
113 assertEquals(Arrays.asList(listener2), source.snapshotTopicListeners());
115 // unregister same listener - should not invoke close
116 source.unregister(listener);
117 verify(cons, never()).close();
118 assertEquals(Arrays.asList(listener2), source.snapshotTopicListeners());
120 // unregister second listener - SHOULD invoke close
121 source.unregister(listener2);
122 verify(cons).close();
123 assertTrue(source.snapshotTopicListeners().isEmpty());
125 // unregister same listener - should not invoke close again
126 source.unregister(listener2);
127 verify(cons).close();
128 assertTrue(source.snapshotTopicListeners().isEmpty());
132 public void testToString() {
133 assertTrue(source.toString().startsWith("SingleThreadedBusTopicSource ["));
137 public void testMakePollerThread() {
138 SingleThreadedBusTopicSource source2 = new SingleThreadedBusTopicSource(makeBuilder().build()) {
140 public CommInfrastructure getTopicCommInfrastructure() {
141 return CommInfrastructure.NOOP;
145 public void init() throws MalformedURLException {
150 assertNotNull(source2.makePollerThread());
154 public void testSingleThreadedBusTopicSource() {
155 // verify that different wrappers can be built
156 new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerGroup(null).build());
157 new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerInstance(null).build());
158 new SingleThreadedBusTopicSourceImpl(makeBuilder().fetchTimeout(-1).build());
159 new SingleThreadedBusTopicSourceImpl(makeBuilder().fetchLimit(-1).build());
163 public void testStart() {
165 assertTrue(source.isAlive());
166 assertEquals(1, source.initCount);
167 verify(thread).start();
169 // attempt to start again - nothing should be invoked again
171 assertTrue(source.isAlive());
172 assertEquals(1, source.initCount);
173 verify(thread).start();
178 assertTrue(source.isAlive());
179 assertEquals(2, source.initCount);
180 verify(thread, times(2)).start();
183 @Test(expected = IllegalStateException.class)
184 public void testStart_Locked() {
189 @Test(expected = IllegalStateException.class)
190 public void testStart_InitEx() {
191 source.initEx = true;
196 public void testStop() {
199 verify(cons).close();
201 // stop it again - not re-closed
203 verify(cons).close();
205 // start & stop again, but with an exception
206 doThrow(new RuntimeException(EXPECTED)).when(cons).close();
212 public void testRun() throws Exception {
213 source.register(listener);
216 * Die in the middle of fetching messages. Also, throw an exception during the
217 * first fetch attempt.
219 when(cons.fetch()).thenAnswer(new Answer<Iterable<String>>() {
223 public Iterable<String> answer(InvocationOnMock invocation) throws Throwable {
225 source.alive = false;
226 return Arrays.asList(MY_MESSAGE, MY_MESSAGE2);
229 throw new IOException(EXPECTED);
235 assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(source.getRecentEvents()));
236 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
237 verify(listener, never()).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE2);
240 * Die AFTER fetching messages.
242 final String msga = "message-A";
243 final String msgb = "message-B";
244 when(cons.fetch()).thenAnswer(new Answer<Iterable<String>>() {
248 public Iterable<String> answer(InvocationOnMock invocation) throws Throwable {
250 source.alive = false;
251 return Collections.emptyList();
254 return Arrays.asList(msga, msgb);
260 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msga);
261 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msgb);
263 assertEquals(Arrays.asList(MY_MESSAGE, msga, msgb), Arrays.asList(source.getRecentEvents()));
267 public void testOffer() {
268 source.register(listener);
269 source.offer(MY_MESSAGE);
270 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
271 assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(source.getRecentEvents()));
274 @Test(expected = IllegalStateException.class)
275 public void testOffer_NotStarted() {
276 source.offer(MY_MESSAGE);
280 public void testSetFilter() {
281 FilterableBusConsumer filt = mock(FilterableBusConsumer.class);
285 source.setFilter("my-filter");
286 verify(filt).setFilter("my-filter");
289 @Test(expected = UnsupportedOperationException.class)
290 public void testSetFilter_Unsupported() {
292 source.setFilter("unsupported-filter");
296 public void testGetConsumerGroup() {
297 assertEquals(MY_CONS_GROUP, source.getConsumerGroup());
301 public void testGetConsumerInstance() {
302 assertEquals(MY_CONS_INST, source.getConsumerInstance());
306 public void testShutdown() {
307 source.register(listener);
310 verify(cons).close();
311 assertTrue(source.snapshotTopicListeners().isEmpty());
315 public void testGetFetchTimeout() {
316 assertEquals(MY_FETCH_TIMEOUT, source.getFetchTimeout());
320 public void testGetFetchLimit() {
321 assertEquals(MY_FETCH_LIMIT, source.getFetchLimit());
325 * Implementation of SingleThreadedBusTopicSource that counts the number of times
328 private class SingleThreadedBusTopicSourceImpl extends SingleThreadedBusTopicSource {
330 private int initCount = 0;
331 private boolean initEx = false;
333 public SingleThreadedBusTopicSourceImpl(BusTopicParams busTopicParams) {
334 super(busTopicParams);
338 public CommInfrastructure getTopicCommInfrastructure() {
339 return CommInfrastructure.NOOP;
343 public void init() throws MalformedURLException {
347 throw new MalformedURLException(EXPECTED);
354 protected Thread makePollerThread() {