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.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyLong;
27 import static org.mockito.ArgumentMatchers.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 com.att.nsa.cambria.client.CambriaBatchingPublisher;
34 import java.io.IOException;
35 import java.util.Collections;
36 import java.util.concurrent.TimeUnit;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.dmaap.mr.client.impl.MRSimplerBatchPublisher;
40 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
41 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
42 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
43 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher.CambriaPublisherWrapper;
44 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher.DmaapAafPublisherWrapper;
45 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher.DmaapDmePublisherWrapper;
46 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusPublisher.DmaapPublisherWrapper;
48 public class BusPublisherTest extends TopicTestBase {
57 public void testCambriaPublisherWrapper() {
58 // verify that different wrappers can be built
59 new CambriaPublisherWrapper(makeBuilder().build());
60 new CambriaPublisherWrapper(makeBuilder().useHttps(false).build());
61 new CambriaPublisherWrapper(makeBuilder().useHttps(true).build());
62 new CambriaPublisherWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(false).build());
63 new CambriaPublisherWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(true).build());
64 new CambriaPublisherWrapper(makeBuilder().apiKey(null).build());
65 new CambriaPublisherWrapper(makeBuilder().apiSecret(null).build());
66 new CambriaPublisherWrapper(makeBuilder().apiKey(null).apiSecret(null).build());
67 new CambriaPublisherWrapper(makeBuilder().userName(null).build());
68 new CambriaPublisherWrapper(makeBuilder().password(null).build());
69 new CambriaPublisherWrapper(makeBuilder().userName(null).password(null).build());
73 public void testCambriaPublisherWrapperSend() throws Exception {
74 CambriaBatchingPublisher pub = mock(CambriaBatchingPublisher.class);
75 CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
76 cambria.publisher = pub;
78 assertTrue(cambria.send(MY_PARTITION, MY_MESSAGE));
80 // publisher exception
81 when(pub.send(anyString(), anyString())).thenThrow(new IOException(EXPECTED));
82 assertFalse(cambria.send(MY_PARTITION2, MY_MESSAGE2));
85 @Test(expected = IllegalArgumentException.class)
86 public void testCambriaPublisherWrapperSend_InvalidMsg() {
87 CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
88 cambria.publisher = mock(CambriaBatchingPublisher.class);
90 cambria.send(MY_PARTITION, null);
94 public void testCambriaPublisherWrapperClose() {
95 CambriaBatchingPublisher pub = mock(CambriaBatchingPublisher.class);
96 CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
97 cambria.publisher = pub;
102 // try again, this time with an exception
103 doThrow(new RuntimeException(EXPECTED)).when(pub).close();
108 public void testDmaapPublisherWrapper() {
109 // verify with different constructor arguments
110 new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, true);
111 new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, false);
112 new DmaapPublisherWrapper(ProtocolTypeConstants.DME2, servers, MY_TOPIC, MY_USERNAME, MY_PASS, true) {};
115 @Test(expected = IllegalArgumentException.class)
116 public void testDmaapPublisherWrapper_InvalidTopic() {
117 new DmaapPublisherWrapper(ProtocolTypeConstants.DME2, servers, "", MY_USERNAME, MY_PASS, true) {};
120 @Test(expected = IllegalArgumentException.class)
121 public void testDmaapPublisherWrapper_Aaf_NullServers() {
122 new DmaapAafPublisherWrapper(null, MY_TOPIC, MY_USERNAME, MY_PASS, true);
125 @Test(expected = IllegalArgumentException.class)
126 public void testDmaapPublisherWrapper_Aaf_NoServers() {
127 new DmaapAafPublisherWrapper(Collections.emptyList(), MY_TOPIC, MY_USERNAME, MY_PASS, true);
130 @Test(expected = IllegalArgumentException.class)
131 public void testDmaapPublisherWrapper_InvalidProtocol() {
132 new DmaapPublisherWrapper(ProtocolTypeConstants.HTTPNOAUTH, servers, MY_TOPIC, MY_USERNAME, MY_PASS, true) {};
136 public void testDmaapPublisherWrapperClose() throws Exception {
137 MRSimplerBatchPublisher pub = mock(MRSimplerBatchPublisher.class);
138 DmaapPublisherWrapper dmaap = new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, true);
139 dmaap.publisher = pub;
142 verify(pub).close(anyLong(), any(TimeUnit.class));
144 // close, but with exception from publisher
145 doThrow(new IOException(EXPECTED)).when(pub).close(anyLong(), any(TimeUnit.class));
150 public void testDmaapPublisherWrapperSend() {
151 MRSimplerBatchPublisher pub = mock(MRSimplerBatchPublisher.class);
152 DmaapPublisherWrapper dmaap = new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, true);
153 dmaap.publisher = pub;
156 assertTrue(dmaap.send(MY_PARTITION, MY_MESSAGE));
157 verify(pub).setPubResponse(any(MRPublisherResponse.class));
158 verify(pub).send(MY_PARTITION, MY_MESSAGE);
161 pub = mock(MRSimplerBatchPublisher.class);
162 dmaap.publisher = pub;
164 MRPublisherResponse resp = new MRPublisherResponse();
165 when(pub.sendBatchWithResponse()).thenReturn(resp);
166 assertTrue(dmaap.send(MY_PARTITION, MY_MESSAGE));
167 verify(pub).setPubResponse(any(MRPublisherResponse.class));
168 verify(pub).send(MY_PARTITION, MY_MESSAGE);
171 @Test(expected = IllegalArgumentException.class)
172 public void testDmaapPublisherWrapperSend_NullMessage() {
173 MRSimplerBatchPublisher pub = mock(MRSimplerBatchPublisher.class);
174 DmaapPublisherWrapper dmaap = new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASS, true);
175 dmaap.publisher = pub;
177 dmaap.send(MY_PARTITION, null);
181 public void testDmaapDmePublisherWrapper() {
182 // verify with different parameters
183 new DmaapDmePublisherWrapper(makeBuilder().build());
184 new DmaapDmePublisherWrapper(makeBuilder().additionalProps(null).build());
186 addProps.put(ROUTE_PROP, MY_ROUTE);
187 new DmaapDmePublisherWrapper(makeBuilder().build());
188 new DmaapDmePublisherWrapper(makeBuilder().partner(null).build());
190 addProps.put("null-value", null);
191 new DmaapDmePublisherWrapper(makeBuilder().build());
194 @Test(expected = IllegalArgumentException.class)
195 public void testDmaapDmePublisherWrapper_InvalidEnv() {
196 new DmaapDmePublisherWrapper(makeBuilder().environment(null).build());
199 @Test(expected = IllegalArgumentException.class)
200 public void testDmaapDmePublisherWrapper_InvalidAft() {
201 new DmaapDmePublisherWrapper(makeBuilder().aftEnvironment(null).build());
204 @Test(expected = IllegalArgumentException.class)
205 public void testDmaapDmePublisherWrapper_InvalidLat() {
206 new DmaapDmePublisherWrapper(makeBuilder().latitude(null).build());
209 @Test(expected = IllegalArgumentException.class)
210 public void testDmaapDmePublisherWrapper_InvalidLong() {
211 new DmaapDmePublisherWrapper(makeBuilder().longitude(null).build());
214 @Test(expected = IllegalArgumentException.class)
215 public void testDmaapDmePublisherWrapper_InvalidPartner() {
216 new DmaapDmePublisherWrapper(makeBuilder().partner(null).build());