2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.common.endpoints.event.comm.bus.internal;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Matchers.anyString;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.util.Arrays;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.endpoints.event.comm.TopicListener;
39 import org.onap.policy.common.endpoints.event.comm.bus.BusTopicTestBase;
41 public class InlineBusTopicSinkTest extends BusTopicTestBase {
43 private InlineBusTopicSinkImpl sink;
46 * Creates the object to be tested.
52 sink = new InlineBusTopicSinkImpl(makeBuilder().build());
56 public void tearDown() {
61 public void testInlineBusTopicSinkImpl() {
62 // verify that different wrappers can be built
63 sink = new InlineBusTopicSinkImpl(makeBuilder().build());
64 assertEquals(MY_PARTITION, sink.getPartitionKey());
66 sink = new InlineBusTopicSinkImpl(makeBuilder().partitionId(null).build());
67 assertNotNull(sink.getPartitionKey());
71 public void testStart() {
72 assertTrue(sink.start());
73 assertEquals(1, sink.initCount);
75 // re-start, init() should not be invoked again
76 assertTrue(sink.start());
77 assertEquals(1, sink.initCount);
80 @Test(expected = IllegalStateException.class)
81 public void testStart_Locked() {
87 public void testStop() {
88 BusPublisher pub = mock(BusPublisher.class);
91 assertTrue(sink.stop());
94 // stop again, shouldn't not invoke close() again
95 assertFalse(sink.stop());
98 // publisher throws exception
99 sink = new InlineBusTopicSinkImpl(makeBuilder().build());
100 sink.publisher = pub;
101 doThrow(new RuntimeException(EXPECTED)).when(pub).close();
102 assertTrue(sink.stop());
106 public void testSend() {
108 BusPublisher pub = mock(BusPublisher.class);
109 sink.publisher = pub;
111 TopicListener listener = mock(TopicListener.class);
112 sink.register(listener);
114 assertTrue(sink.send(MY_MESSAGE));
116 verify(pub).send(MY_PARTITION, MY_MESSAGE);
117 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
118 assertEquals(Arrays.asList(MY_MESSAGE), Arrays.asList(sink.getRecentEvents()));
120 // arrange for send to throw an exception
121 when(pub.send(anyString(), anyString())).thenThrow(new RuntimeException(EXPECTED));
123 assertFalse(sink.send(MY_MESSAGE));
125 // no more event deliveries
126 verify(listener).onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, MY_MESSAGE);
129 @Test(expected = IllegalArgumentException.class)
130 public void testSend_NullMessage() {
132 BusPublisher pub = mock(BusPublisher.class);
133 sink.publisher = pub;
138 @Test(expected = IllegalArgumentException.class)
139 public void testSend_EmptyMessage() {
141 BusPublisher pub = mock(BusPublisher.class);
142 sink.publisher = pub;
147 @Test(expected = IllegalStateException.class)
148 public void testSend_NotStarted() {
149 BusPublisher pub = mock(BusPublisher.class);
150 sink.publisher = pub;
152 sink.send(MY_MESSAGE);
156 public void testSetPartitionKey_getPartitionKey() {
157 assertEquals(MY_PARTITION, sink.getPartitionKey());
159 sink.setPartitionKey("part-B");
160 assertEquals("part-B", sink.getPartitionKey());
164 public void testShutdown() {
165 BusPublisher pub = mock(BusPublisher.class);
166 sink.publisher = pub;
173 public void testAnyNullOrEmpty() {
174 assertFalse(sink.anyNullOrEmpty());
175 assertFalse(sink.anyNullOrEmpty("any-none-null", "any-none-null-B"));
177 assertTrue(sink.anyNullOrEmpty(null, "any-first-null"));
178 assertTrue(sink.anyNullOrEmpty("any-middle-null", null, "any-middle-null-B"));
179 assertTrue(sink.anyNullOrEmpty("any-last-null", null));
180 assertTrue(sink.anyNullOrEmpty("any-empty", ""));
184 public void testAllNullOrEmpty() {
185 assertTrue(sink.allNullOrEmpty());
186 assertTrue(sink.allNullOrEmpty(""));
187 assertTrue(sink.allNullOrEmpty(null, ""));
189 assertFalse(sink.allNullOrEmpty("all-ok-only-one"));
190 assertFalse(sink.allNullOrEmpty("all-ok-one", "all-ok-two"));
191 assertFalse(sink.allNullOrEmpty("all-ok-null", null));
192 assertFalse(sink.allNullOrEmpty("", "all-ok-empty"));
193 assertFalse(sink.allNullOrEmpty("", "all-one-ok", null));
197 public void testToString() {
198 assertTrue(sink.toString().startsWith("InlineBusTopicSink ["));
202 * Implementation of InlineBusTopicSink that tracks the number of times that init() is
205 private static class InlineBusTopicSinkImpl extends InlineBusTopicSink {
207 private int initCount = 0;
209 public InlineBusTopicSinkImpl(BusTopicParams busTopicParams) {
210 super(busTopicParams);
214 public CommInfrastructure getTopicCommInfrastructure() {
215 return CommInfrastructure.NOOP;