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.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.endpoints.event.comm.TopicListener;
39 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
40 import org.onap.policy.common.utils.gson.GsonTestUtils;
42 public class TopicBaseTest extends TopicTestBase {
44 private TopicBaseImpl base;
47 * Creates the object to be tested.
53 base = new TopicBaseImpl(servers, MY_TOPIC);
56 @Test(expected = IllegalArgumentException.class)
57 public void testTopicBase_NullServers() {
58 new TopicBaseImpl(null, MY_TOPIC);
61 @Test(expected = IllegalArgumentException.class)
62 public void testTopicBase_EmptyServers() {
63 new TopicBaseImpl(Collections.emptyList(), MY_TOPIC);
66 @Test(expected = IllegalArgumentException.class)
67 public void testTopicBase_NullTopic() {
68 new TopicBaseImpl(servers, null);
71 @Test(expected = IllegalArgumentException.class)
72 public void testTopicBase_EmptyTopic() {
73 new TopicBaseImpl(servers, "");
77 public void testTopicBase_EffectiveTopic() {
78 TopicBase baseEf = new TopicBaseImpl(servers, MY_TOPIC, MY_EFFECTIVE_TOPIC);
79 assertEquals(MY_TOPIC, baseEf.getTopic());
80 assertEquals(MY_EFFECTIVE_TOPIC, baseEf.getEffectiveTopic());
84 public void testTopicBase_NullEffectiveTopic() {
85 TopicBase baseEf = new TopicBaseImpl(servers, MY_TOPIC, null);
86 assertEquals(MY_TOPIC, baseEf.getTopic());
87 assertEquals(MY_TOPIC, baseEf.getEffectiveTopic());
91 public void testTopicBase_EmptyEffectiveTopic() {
92 TopicBase baseEf = new TopicBaseImpl(servers, MY_TOPIC, "");
93 assertEquals(MY_TOPIC, baseEf.getTopic());
94 assertEquals(MY_TOPIC, baseEf.getEffectiveTopic());
98 public void testSerialize() {
99 new GsonTestUtils().compareGson(base, TopicBaseTest.class);
103 public void testRegister() {
104 TopicListener listener = mock(TopicListener.class);
105 base.register(listener);
106 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
108 // re-register - list should be unchanged
109 base.register(listener);
110 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
112 // register a new listener
113 TopicListener listener2 = mock(TopicListener.class);
114 base.register(listener2);
115 assertEquals(Arrays.asList(listener, listener2), base.snapshotTopicListeners());
118 @Test(expected = IllegalArgumentException.class)
119 public void testRegister_NullListener() {
124 public void testUnregister() {
125 // register two listeners
126 TopicListener listener = mock(TopicListener.class);
127 TopicListener listener2 = mock(TopicListener.class);
128 base.register(listener);
129 base.register(listener2);
132 base.unregister(listener);
133 assertEquals(Arrays.asList(listener2), base.snapshotTopicListeners());
135 // unregister the other
136 base.unregister(listener2);
137 assertTrue(base.snapshotTopicListeners().isEmpty());
140 base.unregister(listener2);
141 assertTrue(base.snapshotTopicListeners().isEmpty());
144 @Test(expected = IllegalArgumentException.class)
145 public void testUnregister_NullListener() {
146 base.register(mock(TopicListener.class));
147 base.unregister(null);
151 public void testBroadcast() {
152 // register two listeners
153 TopicListener listener = mock(TopicListener.class);
154 TopicListener listener2 = mock(TopicListener.class);
155 base.register(listener);
156 base.register(listener2);
158 // broadcast a message
159 final String msg1 = "message-A";
160 base.broadcast(msg1);
161 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg1);
162 verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg1);
164 // broadcast another message, with an exception
165 final String msg2 = "message-B";
166 doThrow(new RuntimeException(EXPECTED)).when(listener).onTopicEvent(any(), any(), any());
167 base.broadcast(msg2);
168 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg2);
169 verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg2);
173 public void testLock_testUnlock() {
174 assertFalse(base.isLocked());
175 assertTrue(base.lock());
176 assertEquals(0, base.startCount);
177 assertEquals(1, base.stopCount);
179 // lock again - should not stop again
180 assertTrue(base.isLocked());
181 assertTrue(base.lock());
182 assertEquals(0, base.startCount);
183 assertEquals(1, base.stopCount);
185 assertTrue(base.isLocked());
186 assertTrue(base.unlock());
187 assertEquals(1, base.startCount);
188 assertEquals(1, base.stopCount);
190 // unlock again - should not start again
191 assertFalse(base.isLocked());
192 assertTrue(base.unlock());
193 assertEquals(1, base.startCount);
194 assertEquals(1, base.stopCount);
196 // lock, but stop returns false
197 base = new TopicBaseImpl(servers, MY_TOPIC);
198 base.stopReturn = false;
199 assertFalse(base.lock());
200 assertTrue(base.isLocked());
201 assertTrue(base.lock());
203 // unlock, but start returns false
204 base.startReturn = false;
205 assertFalse(base.unlock());
206 assertFalse(base.isLocked());
207 assertTrue(base.unlock());
209 // lock & re-lock, but start throws an exception
210 base = new TopicBaseImpl(servers, MY_TOPIC);
212 assertTrue(base.lock());
213 assertFalse(base.unlock());
214 assertFalse(base.isLocked());
215 assertTrue(base.unlock());
219 public void testIsLocked() {
220 assertFalse(base.isLocked());
222 assertTrue(base.isLocked());
224 assertFalse(base.isLocked());
228 public void testGetTopic() {
229 assertEquals(MY_TOPIC, base.getTopic());
233 public void testGetEffectiveTopic() {
234 assertEquals(MY_TOPIC, base.getTopic());
235 assertEquals(MY_TOPIC, base.getEffectiveTopic());
239 public void testIsAlive() {
240 assertFalse(base.isAlive());
242 assertTrue(base.isAlive());
244 assertFalse(base.isAlive());
248 public void testGetServers() {
249 assertEquals(servers, base.getServers());
253 public void testGetRecentEvents() {
254 assertEquals(0, base.getRecentEvents().length);
256 base.addEvent("recent-A");
257 base.addEvent("recent-B");
259 String[] recent = base.getRecentEvents();
260 assertEquals(2, recent.length);
261 assertEquals("recent-A", recent[0]);
262 assertEquals("recent-B", recent[1]);
266 public void testToString() {
267 assertNotNull(base.toString());
271 * Implementation of TopicBase.
273 private static class TopicBaseImpl extends TopicBase {
274 private int startCount = 0;
275 private int stopCount = 0;
276 private boolean startReturn = true;
277 private boolean stopReturn = true;
278 private boolean startEx = false;
283 * @param servers list of servers
284 * @param topic topic name
286 public TopicBaseImpl(List<String> servers, String topic) {
287 super(servers, topic);
293 * @param servers list of servers
294 * @param topic topic name
295 * @param effectiveTopic effective topic name for network communication
297 public TopicBaseImpl(List<String> servers, String topic, String effectiveTopic) {
298 super(servers, topic, effectiveTopic);
302 public CommInfrastructure getTopicCommInfrastructure() {
303 return CommInfrastructure.NOOP;
307 public boolean start() {
311 throw new RuntimeException(EXPECTED);
319 public boolean stop() {
326 public void shutdown() {
331 * Adds an event to the list of recent events.
333 * @param event event to be added
335 public void addEvent(String event) {
336 recentEvents.add(event);