2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2024 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.common.message.bus.event.base;
22 import static org.assertj.core.api.Assertions.assertThatCode;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.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;
34 import java.util.Arrays;
35 import java.util.List;
36 import org.junit.jupiter.api.AfterEach;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.onap.policy.common.message.bus.event.Topic.CommInfrastructure;
40 import org.onap.policy.common.message.bus.event.TopicListener;
41 import org.onap.policy.common.parameters.topic.BusTopicParams;
42 import org.onap.policy.common.utils.gson.GsonTestUtils;
44 class InlineBusTopicSinkTest extends TopicTestBase {
46 private InlineBusTopicSinkImpl sink;
49 * Creates the object to be tested.
56 sink = new InlineBusTopicSinkImpl(makeBuilder().build());
60 public void tearDown() {
65 void testSerialize() {
66 assertThatCode(() -> new GsonTestUtils().compareGson(sink, InlineBusTopicSinkTest.class))
67 .doesNotThrowAnyException();
71 void testInlineBusTopicSinkImpl() {
72 // verify that different wrappers can be built
73 sink = new InlineBusTopicSinkImpl(makeBuilder().build());
74 assertEquals(MY_PARTITION, sink.getPartitionKey());
76 sink = new InlineBusTopicSinkImpl(makeBuilder().partitionId(null).build());
77 assertNotNull(sink.getPartitionKey());
82 assertTrue(sink.start());
83 assertEquals(1, sink.initCount);
85 // re-start, init() should not be invoked again
86 assertTrue(sink.start());
87 assertEquals(1, sink.initCount);
91 void testStart_Locked() {
93 assertThatThrownBy(() -> sink.start()).isInstanceOf(IllegalStateException.class);
98 BusPublisher pub = mock(BusPublisher.class);
101 assertTrue(sink.stop());
104 // stop again, shouldn't not invoke close() again
105 assertFalse(sink.stop());
108 // publisher throws exception
109 sink = new InlineBusTopicSinkImpl(makeBuilder().build());
110 sink.publisher = pub;
111 doThrow(new RuntimeException(EXPECTED)).when(pub).close();
112 assertTrue(sink.stop());
118 BusPublisher pub = mock(BusPublisher.class);
119 sink.publisher = pub;
121 TopicListener listener = mock(TopicListener.class);
122 sink.register(listener);
124 assertTrue(sink.send(MY_MESSAGE));
126 verify(pub).send(MY_PARTITION, MY_MESSAGE);
127 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
128 assertEquals(List.of(MY_MESSAGE), Arrays.asList(sink.getRecentEvents()));
130 // arrange for send to throw an exception
131 when(pub.send(anyString(), anyString())).thenThrow(new RuntimeException(EXPECTED));
133 assertFalse(sink.send(MY_MESSAGE));
135 // no more event deliveries
136 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
140 void testSend_NullMessage() {
142 sink.publisher = mock(BusPublisher.class);
144 assertThatThrownBy(() -> sink.send(null)).isInstanceOf(IllegalArgumentException.class);
148 void testSend_EmptyMessage() {
150 sink.publisher = mock(BusPublisher.class);
152 assertThatThrownBy(() -> sink.send("")).isInstanceOf(IllegalArgumentException.class);
156 void testSend_NotStarted() {
157 sink.publisher = mock(BusPublisher.class);
158 assertThatThrownBy(() -> sink.send(MY_MESSAGE)).isInstanceOf(IllegalStateException.class);
162 void testSetPartitionKey_getPartitionKey() {
163 assertEquals(MY_PARTITION, sink.getPartitionKey());
165 sink.setPartitionKey("part-B");
166 assertEquals("part-B", sink.getPartitionKey());
170 void testShutdown() {
171 BusPublisher pub = mock(BusPublisher.class);
172 sink.publisher = pub;
179 void testAnyNullOrEmpty() {
180 assertFalse(sink.anyNullOrEmpty());
181 assertFalse(sink.anyNullOrEmpty("any-none-null", "any-none-null-B"));
183 assertTrue(sink.anyNullOrEmpty(null, "any-first-null"));
184 assertTrue(sink.anyNullOrEmpty("any-middle-null", null, "any-middle-null-B"));
185 assertTrue(sink.anyNullOrEmpty("any-last-null", null));
186 assertTrue(sink.anyNullOrEmpty("any-empty", ""));
190 void testAllNullOrEmpty() {
191 assertTrue(sink.allNullOrEmpty());
192 assertTrue(sink.allNullOrEmpty(""));
193 assertTrue(sink.allNullOrEmpty(null, ""));
195 assertFalse(sink.allNullOrEmpty("all-ok-only-one"));
196 assertFalse(sink.allNullOrEmpty("all-ok-one", "all-ok-two"));
197 assertFalse(sink.allNullOrEmpty("all-ok-null", null));
198 assertFalse(sink.allNullOrEmpty("", "all-ok-empty"));
199 assertFalse(sink.allNullOrEmpty("", "all-one-ok", null));
203 void testToString() {
204 assertTrue(sink.toString().startsWith("InlineBusTopicSink ["));
208 * Implementation of InlineBusTopicSink that tracks the number of times that init() is
211 private static class InlineBusTopicSinkImpl extends InlineBusTopicSink {
213 private int initCount = 0;
215 public InlineBusTopicSinkImpl(BusTopicParams busTopicParams) {
216 super(busTopicParams);
220 public CommInfrastructure getTopicCommInfrastructure() {
221 return CommInfrastructure.NOOP;