76883b3ce994c6c9a26f27df70a1d4899a2f5ee1
[policy/common.git] /
1 /*
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.event.comm.bus.internal;
22
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;
31
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
41 public class TopicBaseTest extends TopicTestBase {
42
43     private TopicBaseImpl base;
44
45     /**
46      * Creates the object to be tested.
47      */
48     @Before
49     public void setUp() {
50         super.setUp();
51         
52         base = new TopicBaseImpl(servers, MY_TOPIC);
53     }
54
55     @Test(expected = IllegalArgumentException.class)
56     public void testTopicBase_NullServers() {
57         new TopicBaseImpl(null, MY_TOPIC);
58     }
59
60     @Test(expected = IllegalArgumentException.class)
61     public void testTopicBase_EmptyServers() {
62         new TopicBaseImpl(Collections.emptyList(), MY_TOPIC);
63     }
64
65     @Test(expected = IllegalArgumentException.class)
66     public void testTopicBase_NullTopic() {
67         new TopicBaseImpl(servers, null);
68     }
69
70     @Test(expected = IllegalArgumentException.class)
71     public void testTopicBase_EmptyTopic() {
72         new TopicBaseImpl(servers, "");
73     }
74
75     @Test
76     public void testRegister() {
77         TopicListener listener = mock(TopicListener.class);
78         base.register(listener);
79         assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
80
81         // re-register - list should be unchanged
82         base.register(listener);
83         assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
84
85         // register a new listener
86         TopicListener listener2 = mock(TopicListener.class);
87         base.register(listener2);
88         assertEquals(Arrays.asList(listener, listener2), base.snapshotTopicListeners());
89     }
90
91     @Test(expected = IllegalArgumentException.class)
92     public void testRegister_NullListener() {
93         base.register(null);
94     }
95
96     @Test
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);
103
104         // unregister one
105         base.unregister(listener);
106         assertEquals(Arrays.asList(listener2), base.snapshotTopicListeners());
107
108         // unregister the other
109         base.unregister(listener2);
110         assertTrue(base.snapshotTopicListeners().isEmpty());
111
112         // unregister again
113         base.unregister(listener2);
114         assertTrue(base.snapshotTopicListeners().isEmpty());
115     }
116
117     @Test(expected = IllegalArgumentException.class)
118     public void testUnregister_NullListener() {
119         base.register(mock(TopicListener.class));
120         base.unregister(null);
121     }
122
123     @Test
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);
130
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);
136
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);
143     }
144
145     @Test
146     public void testLock_testUnlock() {
147         assertFalse(base.isLocked());
148         assertTrue(base.lock());
149         assertEquals(0, base.startCount);
150         assertEquals(1, base.stopCount);
151
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);
157
158         assertTrue(base.isLocked());
159         assertTrue(base.unlock());
160         assertEquals(1, base.startCount);
161         assertEquals(1, base.stopCount);
162
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);
168
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());
175
176         // unlock, but start returns false
177         base.startReturn = false;
178         assertFalse(base.unlock());
179         assertFalse(base.isLocked());
180         assertTrue(base.unlock());
181
182         // lock & re-lock, but start throws an exception
183         base = new TopicBaseImpl(servers, MY_TOPIC);
184         base.startEx = true;
185         assertTrue(base.lock());
186         assertFalse(base.unlock());
187         assertFalse(base.isLocked());
188         assertTrue(base.unlock());
189     }
190
191     @Test
192     public void testIsLocked() {
193         assertFalse(base.isLocked());
194         base.lock();
195         assertTrue(base.isLocked());
196         base.unlock();
197         assertFalse(base.isLocked());
198     }
199
200     @Test
201     public void testGetTopic() {
202         assertEquals(MY_TOPIC, base.getTopic());
203     }
204
205     @Test
206     public void testIsAlive() {
207         assertFalse(base.isAlive());
208         base.start();
209         assertTrue(base.isAlive());
210         base.stop();
211         assertFalse(base.isAlive());
212     }
213
214     @Test
215     public void testGetServers() {
216         assertEquals(servers, base.getServers());
217     }
218
219     @Test
220     public void testGetRecentEvents() {
221         assertEquals(0, base.getRecentEvents().length);
222
223         base.addEvent("recent-A");
224         base.addEvent("recent-B");
225
226         String[] recent = base.getRecentEvents();
227         assertEquals(2, recent.length);
228         assertEquals("recent-A", recent[0]);
229         assertEquals("recent-B", recent[1]);
230     }
231
232     @Test
233     public void testToString() {
234         assertNotNull(base.toString());
235     }
236
237     /**
238      * Implementation of TopicBase.
239      */
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;
246
247         /**
248          * Constructor.
249          * 
250          * @param servers list of servers
251          * @param topic topic name
252          */
253         public TopicBaseImpl(List<String> servers, String topic) {
254             super(servers, topic);
255         }
256
257         @Override
258         public CommInfrastructure getTopicCommInfrastructure() {
259             return CommInfrastructure.NOOP;
260         }
261
262         @Override
263         public boolean start() {
264             ++startCount;
265
266             if (startEx) {
267                 throw new RuntimeException(EXPECTED);
268             }
269
270             alive = true;
271             return startReturn;
272         }
273
274         @Override
275         public boolean stop() {
276             ++stopCount;
277             alive = false;
278             return stopReturn;
279         }
280
281         @Override
282         public void shutdown() {
283             // do nothing
284         }
285
286         /**
287          * Adds an event to the list of recent events.
288          * 
289          * @param event event to be added
290          */
291         public void addEvent(String event) {
292             recentEvents.add(event);
293         }
294     }
295 }