820fc2c3154f02ee2b45cfbfd85b95eecbc837ab
[policy/clamp.git] /
1 /*
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  */
19
20 package org.onap.policy.common.message.bus.event.base;
21
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;
33
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;
43
44 class InlineBusTopicSinkTest extends TopicTestBase {
45
46     private InlineBusTopicSinkImpl sink;
47
48     /**
49      * Creates the object to be tested.
50      */
51     @BeforeEach
52     @Override
53     public void setUp() {
54         super.setUp();
55
56         sink = new InlineBusTopicSinkImpl(makeBuilder().build());
57     }
58
59     @AfterEach
60     public void tearDown() {
61         sink.shutdown();
62     }
63
64     @Test
65     void testSerialize() {
66         assertThatCode(() -> new GsonTestUtils().compareGson(sink, InlineBusTopicSinkTest.class))
67                         .doesNotThrowAnyException();
68     }
69
70     @Test
71     void testInlineBusTopicSinkImpl() {
72         // verify that different wrappers can be built
73         sink = new InlineBusTopicSinkImpl(makeBuilder().build());
74         assertEquals(MY_PARTITION, sink.getPartitionKey());
75
76         sink = new InlineBusTopicSinkImpl(makeBuilder().partitionId(null).build());
77         assertNotNull(sink.getPartitionKey());
78     }
79
80     @Test
81     void testStart() {
82         assertTrue(sink.start());
83         assertEquals(1, sink.initCount);
84
85         // re-start, init() should not be invoked again
86         assertTrue(sink.start());
87         assertEquals(1, sink.initCount);
88     }
89
90     @Test
91     void testStart_Locked() {
92         sink.lock();
93         assertThatThrownBy(() -> sink.start()).isInstanceOf(IllegalStateException.class);
94     }
95
96     @Test
97     void testStop() {
98         BusPublisher pub = mock(BusPublisher.class);
99         sink.publisher = pub;
100
101         assertTrue(sink.stop());
102         verify(pub).close();
103
104         // stop again, shouldn't not invoke close() again
105         assertFalse(sink.stop());
106         verify(pub).close();
107
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());
113     }
114
115     @Test
116     void testSend() {
117         sink.start();
118         BusPublisher pub = mock(BusPublisher.class);
119         sink.publisher = pub;
120
121         TopicListener listener = mock(TopicListener.class);
122         sink.register(listener);
123
124         assertTrue(sink.send(MY_MESSAGE));
125
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()));
129
130         // arrange for send to throw an exception
131         when(pub.send(anyString(), anyString())).thenThrow(new RuntimeException(EXPECTED));
132
133         assertFalse(sink.send(MY_MESSAGE));
134
135         // no more event deliveries
136         verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
137     }
138
139     @Test
140     void testSend_NullMessage() {
141         sink.start();
142         sink.publisher = mock(BusPublisher.class);
143
144         assertThatThrownBy(() -> sink.send(null)).isInstanceOf(IllegalArgumentException.class);
145     }
146
147     @Test
148     void testSend_EmptyMessage() {
149         sink.start();
150         sink.publisher = mock(BusPublisher.class);
151
152         assertThatThrownBy(() -> sink.send("")).isInstanceOf(IllegalArgumentException.class);
153     }
154
155     @Test
156     void testSend_NotStarted() {
157         sink.publisher = mock(BusPublisher.class);
158         assertThatThrownBy(() -> sink.send(MY_MESSAGE)).isInstanceOf(IllegalStateException.class);
159     }
160
161     @Test
162     void testSetPartitionKey_getPartitionKey() {
163         assertEquals(MY_PARTITION, sink.getPartitionKey());
164
165         sink.setPartitionKey("part-B");
166         assertEquals("part-B", sink.getPartitionKey());
167     }
168
169     @Test
170     void testShutdown() {
171         BusPublisher pub = mock(BusPublisher.class);
172         sink.publisher = pub;
173
174         sink.shutdown();
175         verify(pub).close();
176     }
177
178     @Test
179     void testAnyNullOrEmpty() {
180         assertFalse(sink.anyNullOrEmpty());
181         assertFalse(sink.anyNullOrEmpty("any-none-null", "any-none-null-B"));
182
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", ""));
187     }
188
189     @Test
190     void testAllNullOrEmpty() {
191         assertTrue(sink.allNullOrEmpty());
192         assertTrue(sink.allNullOrEmpty(""));
193         assertTrue(sink.allNullOrEmpty(null, ""));
194
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));
200     }
201
202     @Test
203     void testToString() {
204         assertTrue(sink.toString().startsWith("InlineBusTopicSink ["));
205     }
206
207     /**
208      * Implementation of InlineBusTopicSink that tracks the number of times that init() is
209      * invoked.
210      */
211     private static class InlineBusTopicSinkImpl extends InlineBusTopicSink {
212
213         private int initCount = 0;
214
215         public InlineBusTopicSinkImpl(BusTopicParams busTopicParams) {
216             super(busTopicParams);
217         }
218
219         @Override
220         public CommInfrastructure getTopicCommInfrastructure() {
221             return CommInfrastructure.NOOP;
222         }
223
224         @Override
225         public void init() {
226             ++initCount;
227         }
228
229     }
230 }