2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 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.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyLong;
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 com.att.nsa.cambria.client.CambriaBatchingPublisher;
35 import java.io.IOException;
36 import java.util.Collections;
37 import java.util.concurrent.TimeUnit;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.dmaap.mr.client.impl.MRSimplerBatchPublisher;
41 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
42 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
43 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
44 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher.CambriaPublisherWrapper;
45 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher.DmaapAafPublisherWrapper;
46 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher.DmaapDmePublisherWrapper;
47 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher.DmaapPublisherWrapper;
49 public class BusPublisherTest extends TopicTestBase {
58 public void testCambriaPublisherWrapper() {
59 // verify that different wrappers can be built
60 new CambriaPublisherWrapper(makeBuilder().build());
61 new CambriaPublisherWrapper(makeBuilder().useHttps(false).build());
62 new CambriaPublisherWrapper(makeBuilder().useHttps(true).build());
63 new CambriaPublisherWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(false).build());
64 new CambriaPublisherWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(true).build());
65 new CambriaPublisherWrapper(makeBuilder().apiKey(null).build());
66 new CambriaPublisherWrapper(makeBuilder().apiSecret(null).build());
67 new CambriaPublisherWrapper(makeBuilder().apiKey(null).apiSecret(null).build());
68 new CambriaPublisherWrapper(makeBuilder().userName(null).build());
69 new CambriaPublisherWrapper(makeBuilder().password(null).build());
70 assertThatCode(() -> new CambriaPublisherWrapper(makeBuilder().userName(null).password(null).build()))
71 .doesNotThrowAnyException();
75 public void testCambriaPublisherWrapperSend() throws Exception {
76 CambriaBatchingPublisher pub = mock(CambriaBatchingPublisher.class);
77 CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
78 cambria.publisher = pub;
80 assertTrue(cambria.send(MY_PARTITION, MY_MESSAGE));
82 // publisher exception
83 when(pub.send(anyString(), anyString())).thenThrow(new IOException(EXPECTED));
84 assertFalse(cambria.send(MY_PARTITION2, MY_MESSAGE2));
87 @Test(expected = IllegalArgumentException.class)
88 public void testCambriaPublisherWrapperSend_InvalidMsg() {
89 CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
90 cambria.publisher = mock(CambriaBatchingPublisher.class);
92 cambria.send(MY_PARTITION, null);
96 public void testCambriaPublisherWrapperClose() {
97 CambriaBatchingPublisher pub = mock(CambriaBatchingPublisher.class);
98 CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
99 cambria.publisher = pub;
104 // try again, this time with an exception
105 doThrow(new RuntimeException(EXPECTED)).when(pub).close();
110 public void testDmaapPublisherWrapper() {
111 // verify with different constructor arguments
112 new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, true);
113 new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, false);
114 assertThatCode(() -> new DmaapPublisherWrapper(ProtocolTypeConstants.DME2, servers, MY_TOPIC, MY_USERNAME,
115 MY_PASS, true) {}).doesNotThrowAnyException();
118 @Test(expected = IllegalArgumentException.class)
119 public void testDmaapPublisherWrapper_InvalidTopic() {
120 new DmaapPublisherWrapper(ProtocolTypeConstants.DME2, servers, "", MY_USERNAME, MY_PASS, true) {};
123 @Test(expected = IllegalArgumentException.class)
124 public void testDmaapPublisherWrapper_Aaf_NullServers() {
125 new DmaapAafPublisherWrapper(null, MY_TOPIC, MY_USERNAME, MY_PASS, true);
128 @Test(expected = IllegalArgumentException.class)
129 public void testDmaapPublisherWrapper_Aaf_NoServers() {
130 new DmaapAafPublisherWrapper(Collections.emptyList(), MY_TOPIC, MY_USERNAME, MY_PASS, true);
133 @Test(expected = IllegalArgumentException.class)
134 public void testDmaapPublisherWrapper_InvalidProtocol() {
135 new DmaapPublisherWrapper(ProtocolTypeConstants.HTTPNOAUTH, servers, MY_TOPIC, MY_USERNAME, MY_PASS, true) {};
139 public void testDmaapPublisherWrapperClose() throws Exception {
140 MRSimplerBatchPublisher pub = mock(MRSimplerBatchPublisher.class);
141 DmaapPublisherWrapper dmaap = new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, true);
142 dmaap.publisher = pub;
145 verify(pub).close(anyLong(), any(TimeUnit.class));
147 // close, but with exception from publisher
148 doThrow(new IOException(EXPECTED)).when(pub).close(anyLong(), any(TimeUnit.class));
153 public void testDmaapPublisherWrapperSend() {
154 MRSimplerBatchPublisher pub = mock(MRSimplerBatchPublisher.class);
155 DmaapPublisherWrapper dmaap = new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, true);
156 dmaap.publisher = pub;
159 assertTrue(dmaap.send(MY_PARTITION, MY_MESSAGE));
160 verify(pub).setPubResponse(any(MRPublisherResponse.class));
161 verify(pub).send(MY_PARTITION, MY_MESSAGE);
164 pub = mock(MRSimplerBatchPublisher.class);
165 dmaap.publisher = pub;
167 MRPublisherResponse resp = new MRPublisherResponse();
168 when(pub.sendBatchWithResponse()).thenReturn(resp);
169 assertTrue(dmaap.send(MY_PARTITION, MY_MESSAGE));
170 verify(pub).setPubResponse(any(MRPublisherResponse.class));
171 verify(pub).send(MY_PARTITION, MY_MESSAGE);
174 @Test(expected = IllegalArgumentException.class)
175 public void testDmaapPublisherWrapperSend_NullMessage() {
176 MRSimplerBatchPublisher pub = mock(MRSimplerBatchPublisher.class);
177 DmaapPublisherWrapper dmaap = new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, true);
178 dmaap.publisher = pub;
180 dmaap.send(MY_PARTITION, null);
184 public void testDmaapDmePublisherWrapper() {
185 // verify with different parameters
186 new DmaapDmePublisherWrapper(makeBuilder().build());
187 new DmaapDmePublisherWrapper(makeBuilder().additionalProps(null).build());
189 addProps.put(ROUTE_PROP, MY_ROUTE);
190 new DmaapDmePublisherWrapper(makeBuilder().build());
191 new DmaapDmePublisherWrapper(makeBuilder().partner(null).build());
193 addProps.put("null-value", null);
194 assertThatCode(() -> new DmaapDmePublisherWrapper(makeBuilder().build())).doesNotThrowAnyException();
197 @Test(expected = IllegalArgumentException.class)
198 public void testDmaapDmePublisherWrapper_InvalidEnv() {
199 new DmaapDmePublisherWrapper(makeBuilder().environment(null).build());
202 @Test(expected = IllegalArgumentException.class)
203 public void testDmaapDmePublisherWrapper_InvalidAft() {
204 new DmaapDmePublisherWrapper(makeBuilder().aftEnvironment(null).build());
207 @Test(expected = IllegalArgumentException.class)
208 public void testDmaapDmePublisherWrapper_InvalidLat() {
209 new DmaapDmePublisherWrapper(makeBuilder().latitude(null).build());
212 @Test(expected = IllegalArgumentException.class)
213 public void testDmaapDmePublisherWrapper_InvalidLong() {
214 new DmaapDmePublisherWrapper(makeBuilder().longitude(null).build());
217 @Test(expected = IllegalArgumentException.class)
218 public void testDmaapDmePublisherWrapper_InvalidPartner() {
219 new DmaapDmePublisherWrapper(makeBuilder().partner(null).build());