2 * ============LICENSE_START=======================================================
3 * ONAP Policy Engine - Common Modules
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.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.BusTopicTestBase;
41 public class TopicBaseTest extends BusTopicTestBase {
43 private TopicBaseImpl base;
46 * Creates the object to be tested.
52 base = new TopicBaseImpl(servers, MY_TOPIC);
55 @Test(expected = IllegalArgumentException.class)
56 public void testTopicBase_NullServers() {
57 new TopicBaseImpl(null, MY_TOPIC);
60 @Test(expected = IllegalArgumentException.class)
61 public void testTopicBase_EmptyServers() {
62 new TopicBaseImpl(Collections.emptyList(), MY_TOPIC);
65 @Test(expected = IllegalArgumentException.class)
66 public void testTopicBase_NullTopic() {
67 new TopicBaseImpl(servers, null);
70 @Test(expected = IllegalArgumentException.class)
71 public void testTopicBase_EmptyTopic() {
72 new TopicBaseImpl(servers, "");
76 public void testRegister() {
77 TopicListener listener = mock(TopicListener.class);
78 base.register(listener);
79 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
81 // re-register - list should be unchanged
82 base.register(listener);
83 assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
85 // register a new listener
86 TopicListener listener2 = mock(TopicListener.class);
87 base.register(listener2);
88 assertEquals(Arrays.asList(listener, listener2), base.snapshotTopicListeners());
91 @Test(expected = IllegalArgumentException.class)
92 public void testRegister_NullListener() {
97 public void testUnregister() {
98 // register two listeners
99 TopicListener listener = mock(TopicListener.class);
100 TopicListener listener2 = mock(TopicListener.class);
101 base.register(listener);
102 base.register(listener2);
105 base.unregister(listener);
106 assertEquals(Arrays.asList(listener2), base.snapshotTopicListeners());
108 // unregister the other
109 base.unregister(listener2);
110 assertTrue(base.snapshotTopicListeners().isEmpty());
113 base.unregister(listener2);
114 assertTrue(base.snapshotTopicListeners().isEmpty());
117 @Test(expected = IllegalArgumentException.class)
118 public void testUnregister_NullListener() {
119 base.register(mock(TopicListener.class));
120 base.unregister(null);
124 public void testBroadcast() {
125 // register two listeners
126 TopicListener listener = mock(TopicListener.class);
127 TopicListener listener2 = mock(TopicListener.class);
128 base.register(listener);
129 base.register(listener2);
131 // broadcast a message
132 final String msg1 = "message-A";
133 base.broadcast(msg1);
134 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg1);
135 verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg1);
137 // broadcast another message, with an exception
138 final String msg2 = "message-B";
139 doThrow(new RuntimeException(EXPECTED)).when(listener).onTopicEvent(any(), any(), any());
140 base.broadcast(msg2);
141 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg2);
142 verify(listener2).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, msg2);
146 public void testLock_testUnlock() {
147 assertFalse(base.isLocked());
148 assertTrue(base.lock());
149 assertEquals(0, base.startCount);
150 assertEquals(1, base.stopCount);
152 // lock again - should not stop again
153 assertTrue(base.isLocked());
154 assertTrue(base.lock());
155 assertEquals(0, base.startCount);
156 assertEquals(1, base.stopCount);
158 assertTrue(base.isLocked());
159 assertTrue(base.unlock());
160 assertEquals(1, base.startCount);
161 assertEquals(1, base.stopCount);
163 // unlock again - should not start again
164 assertFalse(base.isLocked());
165 assertTrue(base.unlock());
166 assertEquals(1, base.startCount);
167 assertEquals(1, base.stopCount);
169 // lock, but stop returns false
170 base = new TopicBaseImpl(servers, MY_TOPIC);
171 base.stopReturn = false;
172 assertFalse(base.lock());
173 assertTrue(base.isLocked());
174 assertTrue(base.lock());
176 // unlock, but start returns false
177 base.startReturn = false;
178 assertFalse(base.unlock());
179 assertFalse(base.isLocked());
180 assertTrue(base.unlock());
182 // lock & re-lock, but start throws an exception
183 base = new TopicBaseImpl(servers, MY_TOPIC);
185 assertTrue(base.lock());
186 assertFalse(base.unlock());
187 assertFalse(base.isLocked());
188 assertTrue(base.unlock());
192 public void testIsLocked() {
193 assertFalse(base.isLocked());
195 assertTrue(base.isLocked());
197 assertFalse(base.isLocked());
201 public void testGetTopic() {
202 assertEquals(MY_TOPIC, base.getTopic());
206 public void testIsAlive() {
207 assertFalse(base.isAlive());
209 assertTrue(base.isAlive());
211 assertFalse(base.isAlive());
215 public void testGetServers() {
216 assertEquals(servers, base.getServers());
220 public void testGetRecentEvents() {
221 assertEquals(0, base.getRecentEvents().length);
223 base.addEvent("recent-A");
224 base.addEvent("recent-B");
226 String[] recent = base.getRecentEvents();
227 assertEquals(2, recent.length);
228 assertEquals("recent-A", recent[0]);
229 assertEquals("recent-B", recent[1]);
233 public void testToString() {
234 assertNotNull(base.toString());
238 * Implementation of TopicBase.
240 private static class TopicBaseImpl extends TopicBase {
241 private int startCount = 0;
242 private int stopCount = 0;
243 private boolean startReturn = true;
244 private boolean stopReturn = true;
245 private boolean startEx = false;
250 * @param servers list of servers
251 * @param topic topic name
253 public TopicBaseImpl(List<String> servers, String topic) {
254 super(servers, topic);
258 public CommInfrastructure getTopicCommInfrastructure() {
259 return CommInfrastructure.NOOP;
263 public boolean start() {
267 throw new RuntimeException(EXPECTED);
275 public boolean stop() {
282 public void shutdown() {
287 * Adds an event to the list of recent events.
289 * @param event event to be added
291 public void addEvent(String event) {
292 recentEvents.add(event);