2 * ============LICENSE_START=======================================================
3 * ONAP Policy Engine - Common Modules
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 testSerialize() {
78 new GsonTestUtils().compareGson(base, TopicBaseTest.class);
82 public void testRegister() {
83 TopicListener listener = mock(TopicListener.class);
84 base.register(listener);
85 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
87 // re-register - list should be unchanged
88 base.register(listener);
89 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
91 // register a new listener
92 TopicListener listener2 = mock(TopicListener.class);
93 base.register(listener2);
94 assertEquals(Arrays.asList(listener, listener2), base.snapshotTopicListeners());
97 @Test(expected = IllegalArgumentException.class)
98 public void testRegister_NullListener() {
103 public void testUnregister() {
104 // register two listeners
105 TopicListener listener = mock(TopicListener.class);
106 TopicListener listener2 = mock(TopicListener.class);
107 base.register(listener);
108 base.register(listener2);
111 base.unregister(listener);
112 assertEquals(Arrays.asList(listener2), base.snapshotTopicListeners());
114 // unregister the other
115 base.unregister(listener2);
116 assertTrue(base.snapshotTopicListeners().isEmpty());
119 base.unregister(listener2);
120 assertTrue(base.snapshotTopicListeners().isEmpty());
123 @Test(expected = IllegalArgumentException.class)
124 public void testUnregister_NullListener() {
125 base.register(mock(TopicListener.class));
126 base.unregister(null);
130 public void testBroadcast() {
131 // register two listeners
132 TopicListener listener = mock(TopicListener.class);
133 TopicListener listener2 = mock(TopicListener.class);
134 base.register(listener);
135 base.register(listener2);
137 // broadcast a message
138 final String msg1 = "message-A";
139 base.broadcast(msg1);
140 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg1);
141 verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg1);
143 // broadcast another message, with an exception
144 final String msg2 = "message-B";
145 doThrow(new RuntimeException(EXPECTED)).when(listener).onTopicEvent(any(), any(), any());
146 base.broadcast(msg2);
147 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg2);
148 verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg2);
152 public void testLock_testUnlock() {
153 assertFalse(base.isLocked());
154 assertTrue(base.lock());
155 assertEquals(0, base.startCount);
156 assertEquals(1, base.stopCount);
158 // lock again - should not stop again
159 assertTrue(base.isLocked());
160 assertTrue(base.lock());
161 assertEquals(0, base.startCount);
162 assertEquals(1, base.stopCount);
164 assertTrue(base.isLocked());
165 assertTrue(base.unlock());
166 assertEquals(1, base.startCount);
167 assertEquals(1, base.stopCount);
169 // unlock again - should not start again
170 assertFalse(base.isLocked());
171 assertTrue(base.unlock());
172 assertEquals(1, base.startCount);
173 assertEquals(1, base.stopCount);
175 // lock, but stop returns false
176 base = new TopicBaseImpl(servers, MY_TOPIC);
177 base.stopReturn = false;
178 assertFalse(base.lock());
179 assertTrue(base.isLocked());
180 assertTrue(base.lock());
182 // unlock, but start returns false
183 base.startReturn = false;
184 assertFalse(base.unlock());
185 assertFalse(base.isLocked());
186 assertTrue(base.unlock());
188 // lock & re-lock, but start throws an exception
189 base = new TopicBaseImpl(servers, MY_TOPIC);
191 assertTrue(base.lock());
192 assertFalse(base.unlock());
193 assertFalse(base.isLocked());
194 assertTrue(base.unlock());
198 public void testIsLocked() {
199 assertFalse(base.isLocked());
201 assertTrue(base.isLocked());
203 assertFalse(base.isLocked());
207 public void testGetTopic() {
208 assertEquals(MY_TOPIC, base.getTopic());
212 public void testIsAlive() {
213 assertFalse(base.isAlive());
215 assertTrue(base.isAlive());
217 assertFalse(base.isAlive());
221 public void testGetServers() {
222 assertEquals(servers, base.getServers());
226 public void testGetRecentEvents() {
227 assertEquals(0, base.getRecentEvents().length);
229 base.addEvent("recent-A");
230 base.addEvent("recent-B");
232 String[] recent = base.getRecentEvents();
233 assertEquals(2, recent.length);
234 assertEquals("recent-A", recent[0]);
235 assertEquals("recent-B", recent[1]);
239 public void testToString() {
240 assertNotNull(base.toString());
244 * Implementation of TopicBase.
246 private static class TopicBaseImpl extends TopicBase {
247 private int startCount = 0;
248 private int stopCount = 0;
249 private boolean startReturn = true;
250 private boolean stopReturn = true;
251 private boolean startEx = false;
256 * @param servers list of servers
257 * @param topic topic name
259 public TopicBaseImpl(List<String> servers, String topic) {
260 super(servers, topic);
264 public CommInfrastructure getTopicCommInfrastructure() {
265 return CommInfrastructure.NOOP;
269 public boolean start() {
273 throw new RuntimeException(EXPECTED);
281 public boolean stop() {
288 public void shutdown() {
293 * Adds an event to the list of recent events.
295 * @param event event to be added
297 public void addEvent(String event) {
298 recentEvents.add(event);