ce3632692c2bf91d52efc0d32f81490bb207044f
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.event.comm.bus.internal;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.mockito.ArgumentMatchers.anyString;
31 import static org.mockito.Mockito.doThrow;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35
36 import java.util.Arrays;
37 import java.util.List;
38 import org.junit.jupiter.api.AfterEach;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
42 import org.onap.policy.common.endpoints.event.comm.TopicListener;
43 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
44 import org.onap.policy.common.utils.gson.GsonTestUtils;
45
46 class InlineBusTopicSinkTest extends TopicTestBase {
47
48     private InlineBusTopicSinkImpl sink;
49
50     /**
51      * Creates the object to be tested.
52      */
53     @BeforeEach
54     @Override
55     public void setUp() {
56         super.setUp();
57
58         sink = new InlineBusTopicSinkImpl(makeBuilder().build());
59     }
60
61     @AfterEach
62     public void tearDown() {
63         sink.shutdown();
64     }
65
66     @Test
67     void testSerialize() {
68         assertThatCode(() -> new GsonTestUtils().compareGson(sink, InlineBusTopicSinkTest.class))
69                         .doesNotThrowAnyException();
70     }
71
72     @Test
73     void testInlineBusTopicSinkImpl() {
74         // verify that different wrappers can be built
75         sink = new InlineBusTopicSinkImpl(makeBuilder().build());
76         assertEquals(MY_PARTITION, sink.getPartitionKey());
77
78         sink = new InlineBusTopicSinkImpl(makeBuilder().partitionId(null).build());
79         assertNotNull(sink.getPartitionKey());
80     }
81
82     @Test
83     void testStart() {
84         assertTrue(sink.start());
85         assertEquals(1, sink.initCount);
86
87         // re-start, init() should not be invoked again
88         assertTrue(sink.start());
89         assertEquals(1, sink.initCount);
90     }
91
92     @Test
93     void testStart_Locked() {
94         sink.lock();
95         assertThatThrownBy(() -> sink.start()).isInstanceOf(IllegalStateException.class);
96     }
97
98     @Test
99     void testStop() {
100         BusPublisher pub = mock(BusPublisher.class);
101         sink.publisher = pub;
102
103         assertTrue(sink.stop());
104         verify(pub).close();
105
106         // stop again, shouldn't not invoke close() again
107         assertFalse(sink.stop());
108         verify(pub).close();
109
110         // publisher throws exception
111         sink = new InlineBusTopicSinkImpl(makeBuilder().build());
112         sink.publisher = pub;
113         doThrow(new RuntimeException(EXPECTED)).when(pub).close();
114         assertTrue(sink.stop());
115     }
116
117     @Test
118     void testSend() {
119         sink.start();
120         BusPublisher pub = mock(BusPublisher.class);
121         sink.publisher = pub;
122
123         TopicListener listener = mock(TopicListener.class);
124         sink.register(listener);
125
126         assertTrue(sink.send(MY_MESSAGE));
127
128         verify(pub).send(MY_PARTITION, MY_MESSAGE);
129         verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
130         assertEquals(List.of(MY_MESSAGE), Arrays.asList(sink.getRecentEvents()));
131
132         // arrange for send to throw an exception
133         when(pub.send(anyString(), anyString())).thenThrow(new RuntimeException(EXPECTED));
134
135         assertFalse(sink.send(MY_MESSAGE));
136
137         // no more event deliveries
138         verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
139     }
140
141     @Test
142     void testSend_NullMessage() {
143         sink.start();
144         sink.publisher = mock(BusPublisher.class);
145
146         assertThatThrownBy(() -> sink.send(null)).isInstanceOf(IllegalArgumentException.class);
147     }
148
149     @Test
150     void testSend_EmptyMessage() {
151         sink.start();
152         sink.publisher = mock(BusPublisher.class);
153
154         assertThatThrownBy(() -> sink.send("")).isInstanceOf(IllegalArgumentException.class);
155     }
156
157     @Test
158     void testSend_NotStarted() {
159         sink.publisher = mock(BusPublisher.class);
160         assertThatThrownBy(() -> sink.send(MY_MESSAGE)).isInstanceOf(IllegalStateException.class);
161     }
162
163     @Test
164     void testSetPartitionKey_getPartitionKey() {
165         assertEquals(MY_PARTITION, sink.getPartitionKey());
166
167         sink.setPartitionKey("part-B");
168         assertEquals("part-B", sink.getPartitionKey());
169     }
170
171     @Test
172     void testShutdown() {
173         BusPublisher pub = mock(BusPublisher.class);
174         sink.publisher = pub;
175
176         sink.shutdown();
177         verify(pub).close();
178     }
179
180     @Test
181     void testAnyNullOrEmpty() {
182         assertFalse(sink.anyNullOrEmpty());
183         assertFalse(sink.anyNullOrEmpty("any-none-null", "any-none-null-B"));
184
185         assertTrue(sink.anyNullOrEmpty(null, "any-first-null"));
186         assertTrue(sink.anyNullOrEmpty("any-middle-null", null, "any-middle-null-B"));
187         assertTrue(sink.anyNullOrEmpty("any-last-null", null));
188         assertTrue(sink.anyNullOrEmpty("any-empty", ""));
189     }
190
191     @Test
192     void testAllNullOrEmpty() {
193         assertTrue(sink.allNullOrEmpty());
194         assertTrue(sink.allNullOrEmpty(""));
195         assertTrue(sink.allNullOrEmpty(null, ""));
196
197         assertFalse(sink.allNullOrEmpty("all-ok-only-one"));
198         assertFalse(sink.allNullOrEmpty("all-ok-one", "all-ok-two"));
199         assertFalse(sink.allNullOrEmpty("all-ok-null", null));
200         assertFalse(sink.allNullOrEmpty("", "all-ok-empty"));
201         assertFalse(sink.allNullOrEmpty("", "all-one-ok", null));
202     }
203
204     @Test
205     void testToString() {
206         assertTrue(sink.toString().startsWith("InlineBusTopicSink ["));
207     }
208
209     /**
210      * Implementation of InlineBusTopicSink that tracks the number of times that init() is
211      * invoked.
212      */
213     private static class InlineBusTopicSinkImpl extends InlineBusTopicSink {
214
215         private int initCount = 0;
216
217         public InlineBusTopicSinkImpl(BusTopicParams busTopicParams) {
218             super(busTopicParams);
219         }
220
221         @Override
222         public CommInfrastructure getTopicCommInfrastructure() {
223             return CommInfrastructure.NOOP;
224         }
225
226         @Override
227         public void init() {
228             ++initCount;
229         }
230
231     }
232 }