2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.common.endpoints.event.comm.bus.internal;
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;
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;
46 class InlineBusTopicSinkTest extends TopicTestBase {
48 private InlineBusTopicSinkImpl sink;
51 * Creates the object to be tested.
58 sink = new InlineBusTopicSinkImpl(makeBuilder().build());
62 public void tearDown() {
67 void testSerialize() {
68 assertThatCode(() -> new GsonTestUtils().compareGson(sink, InlineBusTopicSinkTest.class))
69 .doesNotThrowAnyException();
73 void testInlineBusTopicSinkImpl() {
74 // verify that different wrappers can be built
75 sink = new InlineBusTopicSinkImpl(makeBuilder().build());
76 assertEquals(MY_PARTITION, sink.getPartitionKey());
78 sink = new InlineBusTopicSinkImpl(makeBuilder().partitionId(null).build());
79 assertNotNull(sink.getPartitionKey());
84 assertTrue(sink.start());
85 assertEquals(1, sink.initCount);
87 // re-start, init() should not be invoked again
88 assertTrue(sink.start());
89 assertEquals(1, sink.initCount);
93 void testStart_Locked() {
95 assertThatThrownBy(() -> sink.start()).isInstanceOf(IllegalStateException.class);
100 BusPublisher pub = mock(BusPublisher.class);
101 sink.publisher = pub;
103 assertTrue(sink.stop());
106 // stop again, shouldn't not invoke close() again
107 assertFalse(sink.stop());
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());
120 BusPublisher pub = mock(BusPublisher.class);
121 sink.publisher = pub;
123 TopicListener listener = mock(TopicListener.class);
124 sink.register(listener);
126 assertTrue(sink.send(MY_MESSAGE));
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()));
132 // arrange for send to throw an exception
133 when(pub.send(anyString(), anyString())).thenThrow(new RuntimeException(EXPECTED));
135 assertFalse(sink.send(MY_MESSAGE));
137 // no more event deliveries
138 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
142 void testSend_NullMessage() {
144 sink.publisher = mock(BusPublisher.class);
146 assertThatThrownBy(() -> sink.send(null)).isInstanceOf(IllegalArgumentException.class);
150 void testSend_EmptyMessage() {
152 sink.publisher = mock(BusPublisher.class);
154 assertThatThrownBy(() -> sink.send("")).isInstanceOf(IllegalArgumentException.class);
158 void testSend_NotStarted() {
159 sink.publisher = mock(BusPublisher.class);
160 assertThatThrownBy(() -> sink.send(MY_MESSAGE)).isInstanceOf(IllegalStateException.class);
164 void testSetPartitionKey_getPartitionKey() {
165 assertEquals(MY_PARTITION, sink.getPartitionKey());
167 sink.setPartitionKey("part-B");
168 assertEquals("part-B", sink.getPartitionKey());
172 void testShutdown() {
173 BusPublisher pub = mock(BusPublisher.class);
174 sink.publisher = pub;
181 void testAnyNullOrEmpty() {
182 assertFalse(sink.anyNullOrEmpty());
183 assertFalse(sink.anyNullOrEmpty("any-none-null", "any-none-null-B"));
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", ""));
192 void testAllNullOrEmpty() {
193 assertTrue(sink.allNullOrEmpty());
194 assertTrue(sink.allNullOrEmpty(""));
195 assertTrue(sink.allNullOrEmpty(null, ""));
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));
205 void testToString() {
206 assertTrue(sink.toString().startsWith("InlineBusTopicSink ["));
210 * Implementation of InlineBusTopicSink that tracks the number of times that init() is
213 private static class InlineBusTopicSinkImpl extends InlineBusTopicSink {
215 private int initCount = 0;
217 public InlineBusTopicSinkImpl(BusTopicParams busTopicParams) {
218 super(busTopicParams);
222 public CommInfrastructure getTopicCommInfrastructure() {
223 return CommInfrastructure.NOOP;