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.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.anyLong;
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 com.att.nsa.cambria.client.CambriaBatchingPublisher;
34 import com.att.nsa.mr.client.impl.MRSimplerBatchPublisher;
35 import com.att.nsa.mr.client.response.MRPublisherResponse;
36 import com.att.nsa.mr.test.clients.ProtocolTypeConstants;
37 import java.io.IOException;
38 import java.util.Collections;
39 import java.util.concurrent.TimeUnit;
40 import org.junit.Before;
41 import org.junit.Test;
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 {
56 public void testCambriaPublisherWrapper() {
57 // verify that different wrappers can be built
58 new CambriaPublisherWrapper(makeBuilder().build());
59 new CambriaPublisherWrapper(makeBuilder().useHttps(false).build());
60 new CambriaPublisherWrapper(makeBuilder().useHttps(true).build());
61 new CambriaPublisherWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(false).build());
62 new CambriaPublisherWrapper(makeBuilder().useHttps(true).allowSelfSignedCerts(true).build());
63 new CambriaPublisherWrapper(makeBuilder().apiKey(null).build());
64 new CambriaPublisherWrapper(makeBuilder().apiSecret(null).build());
65 new CambriaPublisherWrapper(makeBuilder().apiKey(null).apiSecret(null).build());
66 new CambriaPublisherWrapper(makeBuilder().userName(null).build());
67 new CambriaPublisherWrapper(makeBuilder().password(null).build());
68 new CambriaPublisherWrapper(makeBuilder().userName(null).password(null).build());
72 public void testCambriaPublisherWrapperSend() throws Exception {
73 CambriaBatchingPublisher pub = mock(CambriaBatchingPublisher.class);
74 CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
75 cambria.publisher = pub;
77 assertTrue(cambria.send(MY_PARTITION, MY_MESSAGE));
79 // publisher exception
80 when(pub.send(anyString(), anyString())).thenThrow(new IOException(EXPECTED));
81 assertFalse(cambria.send(MY_PARTITION2, MY_MESSAGE2));
84 @Test(expected = IllegalArgumentException.class)
85 public void testCambriaPublisherWrapperSend_InvalidMsg() {
86 CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
87 cambria.publisher = mock(CambriaBatchingPublisher.class);
89 cambria.send(MY_PARTITION, null);
93 public void testCambriaPublisherWrapperClose() throws Exception {
94 CambriaBatchingPublisher pub = mock(CambriaBatchingPublisher.class);
95 CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
96 cambria.publisher = pub;
101 // try again, this time with an exception
102 doThrow(new RuntimeException(EXPECTED)).when(pub).close();
107 public void testDmaapPublisherWrapper() {
108 // verify with different constructor arguments
109 new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASSWD, true);
110 new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASSWD, false);
111 new DmaapPublisherWrapper(ProtocolTypeConstants.DME2, servers, MY_TOPIC, MY_USERNAME, MY_PASSWD, true) {};
114 @Test(expected = IllegalArgumentException.class)
115 public void testDmaapPublisherWrapper_InvalidTopic() {
116 new DmaapPublisherWrapper(ProtocolTypeConstants.DME2, servers, "", MY_USERNAME, MY_PASSWD, true) {};
119 @Test(expected = IllegalArgumentException.class)
120 public void testDmaapPublisherWrapper_Aaf_NullServers() {
121 new DmaapAafPublisherWrapper(null, MY_TOPIC, MY_USERNAME, MY_PASSWD, true);
124 @Test(expected = IllegalArgumentException.class)
125 public void testDmaapPublisherWrapper_Aaf_NoServers() {
126 new DmaapAafPublisherWrapper(Collections.emptyList(), MY_TOPIC, MY_USERNAME, MY_PASSWD, true);
129 @Test(expected = IllegalArgumentException.class)
130 public void testDmaapPublisherWrapper_InvalidProtocol() {
131 new DmaapPublisherWrapper(ProtocolTypeConstants.HTTPNOAUTH, servers, MY_TOPIC, MY_USERNAME, MY_PASSWD, true) {};
135 public void testDmaapPublisherWrapperClose() throws Exception {
136 MRSimplerBatchPublisher pub = mock(MRSimplerBatchPublisher.class);
137 DmaapPublisherWrapper dmaap = new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASSWD, true);
138 dmaap.publisher = pub;
141 verify(pub).close(anyLong(), any(TimeUnit.class));
143 // close, but with exception from publisher
144 doThrow(new IOException(EXPECTED)).when(pub).close(anyLong(), any(TimeUnit.class));
149 public void testDmaapPublisherWrapperSend() throws Exception {
150 MRSimplerBatchPublisher pub = mock(MRSimplerBatchPublisher.class);
151 DmaapPublisherWrapper dmaap = new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASSWD, true);
152 dmaap.publisher = pub;
155 assertTrue(dmaap.send(MY_PARTITION, MY_MESSAGE));
156 verify(pub).setPubResponse(any(MRPublisherResponse.class));
157 verify(pub).send(MY_PARTITION, MY_MESSAGE);
160 pub = mock(MRSimplerBatchPublisher.class);
161 dmaap.publisher = pub;
163 MRPublisherResponse resp = new MRPublisherResponse();
164 when(pub.sendBatchWithResponse()).thenReturn(resp);
165 assertTrue(dmaap.send(MY_PARTITION, MY_MESSAGE));
166 verify(pub).setPubResponse(any(MRPublisherResponse.class));
167 verify(pub).send(MY_PARTITION, MY_MESSAGE);
170 @Test(expected = IllegalArgumentException.class)
171 public void testDmaapPublisherWrapperSend_NullMessage() throws Exception {
172 MRSimplerBatchPublisher pub = mock(MRSimplerBatchPublisher.class);
173 DmaapPublisherWrapper dmaap = new DmaapAafPublisherWrapper(servers, MY_TOPIC, MY_USERNAME, MY_PASSWD, true);
174 dmaap.publisher = pub;
176 dmaap.send(MY_PARTITION, null);
180 public void testDmaapDmePublisherWrapper() {
181 // verify with different parameters
182 new DmaapDmePublisherWrapper(makeBuilder().build());
183 new DmaapDmePublisherWrapper(makeBuilder().additionalProps(null).build());
185 addProps.put(ROUTE_PROP, MY_ROUTE);
186 new DmaapDmePublisherWrapper(makeBuilder().build());
187 new DmaapDmePublisherWrapper(makeBuilder().partner(null).build());
189 addProps.put("null-value", null);
190 new DmaapDmePublisherWrapper(makeBuilder().build());
193 @Test(expected = IllegalArgumentException.class)
194 public void testDmaapDmePublisherWrapper_InvalidEnv() {
195 new DmaapDmePublisherWrapper(makeBuilder().environment(null).build());
198 @Test(expected = IllegalArgumentException.class)
199 public void testDmaapDmePublisherWrapper_InvalidAft() {
200 new DmaapDmePublisherWrapper(makeBuilder().aftEnvironment(null).build());
203 @Test(expected = IllegalArgumentException.class)
204 public void testDmaapDmePublisherWrapper_InvalidLat() {
205 new DmaapDmePublisherWrapper(makeBuilder().latitude(null).build());
208 @Test(expected = IllegalArgumentException.class)
209 public void testDmaapDmePublisherWrapper_InvalidLong() {
210 new DmaapDmePublisherWrapper(makeBuilder().longitude(null).build());
213 @Test(expected = IllegalArgumentException.class)
214 public void testDmaapDmePublisherWrapper_InvalidPartner() {
215 new DmaapDmePublisherWrapper(makeBuilder().partner(null).build());