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