a8ea84fcc7359a0c13dc82ae08641e8f189f0332
[policy/common.git] /
1 /*
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
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 import org.onap.policy.common.utils.gson.GsonTestUtils;
41
42 public class TopicBaseTest extends TopicTestBase {
43
44     private TopicBaseImpl base;
45
46     /**
47      * Creates the object to be tested.
48      */
49     @Before
50     public void setUp() {
51         super.setUp();
52
53         base = new TopicBaseImpl(servers, MY_TOPIC);
54     }
55
56     @Test(expected = IllegalArgumentException.class)
57     public void testTopicBase_NullServers() {
58         new TopicBaseImpl(null, MY_TOPIC);
59     }
60
61     @Test(expected = IllegalArgumentException.class)
62     public void testTopicBase_EmptyServers() {
63         new TopicBaseImpl(Collections.emptyList(), MY_TOPIC);
64     }
65
66     @Test(expected = IllegalArgumentException.class)
67     public void testTopicBase_NullTopic() {
68         new TopicBaseImpl(servers, null);
69     }
70
71     @Test(expected = IllegalArgumentException.class)
72     public void testTopicBase_EmptyTopic() {
73         new TopicBaseImpl(servers, "");
74     }
75
76     @Test
77     public void testSerialize() {
78         new GsonTestUtils().compareGson(base, TopicBaseTest.class);
79     }
80
81     @Test
82     public void testRegister() {
83         TopicListener listener = mock(TopicListener.class);
84         base.register(listener);
85         assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
86
87         // re-register - list should be unchanged
88         base.register(listener);
89         assertEquals(Arrays.asList(listener), base.snapshotTopicListeners());
90
91         // register a new listener
92         TopicListener listener2 = mock(TopicListener.class);
93         base.register(listener2);
94         assertEquals(Arrays.asList(listener, listener2), base.snapshotTopicListeners());
95     }
96
97     @Test(expected = IllegalArgumentException.class)
98     public void testRegister_NullListener() {
99         base.register(null);
100     }
101
102     @Test
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);
109
110         // unregister one
111         base.unregister(listener);
112         assertEquals(Arrays.asList(listener2), base.snapshotTopicListeners());
113
114         // unregister the other
115         base.unregister(listener2);
116         assertTrue(base.snapshotTopicListeners().isEmpty());
117
118         // unregister again
119         base.unregister(listener2);
120         assertTrue(base.snapshotTopicListeners().isEmpty());
121     }
122
123     @Test(expected = IllegalArgumentException.class)
124     public void testUnregister_NullListener() {
125         base.register(mock(TopicListener.class));
126         base.unregister(null);
127     }
128
129     @Test
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);
136
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);
142
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);
149     }
150
151     @Test
152     public void testLock_testUnlock() {
153         assertFalse(base.isLocked());
154         assertTrue(base.lock());
155         assertEquals(0, base.startCount);
156         assertEquals(1, base.stopCount);
157
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);
163
164         assertTrue(base.isLocked());
165         assertTrue(base.unlock());
166         assertEquals(1, base.startCount);
167         assertEquals(1, base.stopCount);
168
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);
174
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());
181
182         // unlock, but start returns false
183         base.startReturn = false;
184         assertFalse(base.unlock());
185         assertFalse(base.isLocked());
186         assertTrue(base.unlock());
187
188         // lock & re-lock, but start throws an exception
189         base = new TopicBaseImpl(servers, MY_TOPIC);
190         base.startEx = true;
191         assertTrue(base.lock());
192         assertFalse(base.unlock());
193         assertFalse(base.isLocked());
194         assertTrue(base.unlock());
195     }
196
197     @Test
198     public void testIsLocked() {
199         assertFalse(base.isLocked());
200         base.lock();
201         assertTrue(base.isLocked());
202         base.unlock();
203         assertFalse(base.isLocked());
204     }
205
206     @Test
207     public void testGetTopic() {
208         assertEquals(MY_TOPIC, base.getTopic());
209     }
210
211     @Test
212     public void testIsAlive() {
213         assertFalse(base.isAlive());
214         base.start();
215         assertTrue(base.isAlive());
216         base.stop();
217         assertFalse(base.isAlive());
218     }
219
220     @Test
221     public void testGetServers() {
222         assertEquals(servers, base.getServers());
223     }
224
225     @Test
226     public void testGetRecentEvents() {
227         assertEquals(0, base.getRecentEvents().length);
228
229         base.addEvent("recent-A");
230         base.addEvent("recent-B");
231
232         String[] recent = base.getRecentEvents();
233         assertEquals(2, recent.length);
234         assertEquals("recent-A", recent[0]);
235         assertEquals("recent-B", recent[1]);
236     }
237
238     @Test
239     public void testToString() {
240         assertNotNull(base.toString());
241     }
242
243     /**
244      * Implementation of TopicBase.
245      */
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;
252
253         /**
254          * Constructor.
255          *
256          * @param servers list of servers
257          * @param topic topic name
258          */
259         public TopicBaseImpl(List<String> servers, String topic) {
260             super(servers, topic);
261         }
262
263         @Override
264         public CommInfrastructure getTopicCommInfrastructure() {
265             return CommInfrastructure.NOOP;
266         }
267
268         @Override
269         public boolean start() {
270             ++startCount;
271
272             if (startEx) {
273                 throw new RuntimeException(EXPECTED);
274             }
275
276             alive = true;
277             return startReturn;
278         }
279
280         @Override
281         public boolean stop() {
282             ++stopCount;
283             alive = false;
284             return stopReturn;
285         }
286
287         @Override
288         public void shutdown() {
289             // do nothing
290         }
291
292         /**
293          * Adds an event to the list of recent events.
294          *
295          * @param event event to be added
296          */
297         public void addEvent(String event) {
298             recentEvents.add(event);
299         }
300     }
301 }