e6eec7991829abaf1f080a274132417e8e86d706
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2018-2020 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.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.Arrays;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
39 import org.onap.policy.common.endpoints.event.comm.TopicListener;
40 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
41 import org.onap.policy.common.utils.gson.GsonTestUtils;
42
43 public class InlineBusTopicSinkTest extends TopicTestBase {
44
45     private InlineBusTopicSinkImpl sink;
46
47     /**
48      * Creates the object to be tested.
49      */
50     @Before
51     @Override
52     public void setUp() {
53         super.setUp();
54
55         sink = new InlineBusTopicSinkImpl(makeBuilder().build());
56     }
57
58     @After
59     public void tearDown() {
60         sink.shutdown();
61     }
62
63     @Test
64     public void testSerialize() {
65         assertThatCode(() -> new GsonTestUtils().compareGson(sink, InlineBusTopicSinkTest.class))
66                         .doesNotThrowAnyException();
67     }
68
69     @Test
70     public void testInlineBusTopicSinkImpl() {
71         // verify that different wrappers can be built
72         sink = new InlineBusTopicSinkImpl(makeBuilder().build());
73         assertEquals(MY_PARTITION, sink.getPartitionKey());
74
75         sink = new InlineBusTopicSinkImpl(makeBuilder().partitionId(null).build());
76         assertNotNull(sink.getPartitionKey());
77     }
78
79     @Test
80     public void testStart() {
81         assertTrue(sink.start());
82         assertEquals(1, sink.initCount);
83
84         // re-start, init() should not be invoked again
85         assertTrue(sink.start());
86         assertEquals(1, sink.initCount);
87     }
88
89     @Test(expected = IllegalStateException.class)
90     public void testStart_Locked() {
91         sink.lock();
92         sink.start();
93     }
94
95     @Test
96     public void testStop() {
97         BusPublisher pub = mock(BusPublisher.class);
98         sink.publisher = pub;
99
100         assertTrue(sink.stop());
101         verify(pub).close();
102
103         // stop again, shouldn't not invoke close() again
104         assertFalse(sink.stop());
105         verify(pub).close();
106
107         // publisher throws exception
108         sink = new InlineBusTopicSinkImpl(makeBuilder().build());
109         sink.publisher = pub;
110         doThrow(new RuntimeException(EXPECTED)).when(pub).close();
111         assertTrue(sink.stop());
112     }
113
114     @Test
115     public void testSend() {
116         sink.start();
117         BusPublisher pub = mock(BusPublisher.class);
118         sink.publisher = pub;
119
120         TopicListener listener = mock(TopicListener.class);
121         sink.register(listener);
122
123         assertTrue(sink.send(MY_MESSAGE));
124
125         verify(pub).send(MY_PARTITION, MY_MESSAGE);
126         verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
127         assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(sink.getRecentEvents()));
128
129         // arrange for send to throw an exception
130         when(pub.send(anyString(), anyString())).thenThrow(new RuntimeException(EXPECTED));
131
132         assertFalse(sink.send(MY_MESSAGE));
133
134         // no more event deliveries
135         verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
136     }
137
138     @Test(expected = IllegalArgumentException.class)
139     public void testSend_NullMessage() {
140         sink.start();
141         BusPublisher pub = mock(BusPublisher.class);
142         sink.publisher = pub;
143
144         sink.send(null);
145     }
146
147     @Test(expected = IllegalArgumentException.class)
148     public void testSend_EmptyMessage() {
149         sink.start();
150         BusPublisher pub = mock(BusPublisher.class);
151         sink.publisher = pub;
152
153         sink.send("");
154     }
155
156     @Test(expected = IllegalStateException.class)
157     public void testSend_NotStarted() {
158         BusPublisher pub = mock(BusPublisher.class);
159         sink.publisher = pub;
160
161         sink.send(MY_MESSAGE);
162     }
163
164     @Test
165     public void testSetPartitionKey_getPartitionKey() {
166         assertEquals(MY_PARTITION, sink.getPartitionKey());
167
168         sink.setPartitionKey("part-B");
169         assertEquals("part-B", sink.getPartitionKey());
170     }
171
172     @Test
173     public void testShutdown() {
174         BusPublisher pub = mock(BusPublisher.class);
175         sink.publisher = pub;
176
177         sink.shutdown();
178         verify(pub).close();
179     }
180
181     @Test
182     public void testAnyNullOrEmpty() {
183         assertFalse(sink.anyNullOrEmpty());
184         assertFalse(sink.anyNullOrEmpty("any-none-null", "any-none-null-B"));
185
186         assertTrue(sink.anyNullOrEmpty(null, "any-first-null"));
187         assertTrue(sink.anyNullOrEmpty("any-middle-null", null, "any-middle-null-B"));
188         assertTrue(sink.anyNullOrEmpty("any-last-null", null));
189         assertTrue(sink.anyNullOrEmpty("any-empty", ""));
190     }
191
192     @Test
193     public void testAllNullOrEmpty() {
194         assertTrue(sink.allNullOrEmpty());
195         assertTrue(sink.allNullOrEmpty(""));
196         assertTrue(sink.allNullOrEmpty(null, ""));
197
198         assertFalse(sink.allNullOrEmpty("all-ok-only-one"));
199         assertFalse(sink.allNullOrEmpty("all-ok-one", "all-ok-two"));
200         assertFalse(sink.allNullOrEmpty("all-ok-null", null));
201         assertFalse(sink.allNullOrEmpty("", "all-ok-empty"));
202         assertFalse(sink.allNullOrEmpty("", "all-one-ok", null));
203     }
204
205     @Test
206     public void testToString() {
207         assertTrue(sink.toString().startsWith("InlineBusTopicSink ["));
208     }
209
210     /**
211      * Implementation of InlineBusTopicSink that tracks the number of times that init() is
212      * invoked.
213      */
214     private static class InlineBusTopicSinkImpl extends InlineBusTopicSink {
215
216         private int initCount = 0;
217
218         public InlineBusTopicSinkImpl(BusTopicParams busTopicParams) {
219             super(busTopicParams);
220         }
221
222         @Override
223         public CommInfrastructure getTopicCommInfrastructure() {
224             return CommInfrastructure.NOOP;
225         }
226
227         @Override
228         public void init() {
229             ++initCount;
230         }
231
232     }
233 }