513673bd7906f6cf3d1e181c29b77657c5ccc6ab
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
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
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.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;
33
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;
48
49 public class BusPublisherTest extends TopicTestBase {
50
51     @Before
52     @Override
53     public void setUp() {
54         super.setUp();
55     }
56
57     @Test
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();
72     }
73
74     @Test
75     public void testCambriaPublisherWrapperSend() throws Exception {
76         CambriaBatchingPublisher pub = mock(CambriaBatchingPublisher.class);
77         CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
78         cambria.publisher = pub;
79
80         assertTrue(cambria.send(MY_PARTITION, MY_MESSAGE));
81
82         // publisher exception
83         when(pub.send(anyString(), anyString())).thenThrow(new IOException(EXPECTED));
84         assertFalse(cambria.send(MY_PARTITION2, MY_MESSAGE2));
85     }
86
87     @Test(expected = IllegalArgumentException.class)
88     public void testCambriaPublisherWrapperSend_InvalidMsg() {
89         CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
90         cambria.publisher = mock(CambriaBatchingPublisher.class);
91
92         cambria.send(MY_PARTITION, null);
93     }
94
95     @Test
96     public void testCambriaPublisherWrapperClose() {
97         CambriaBatchingPublisher pub = mock(CambriaBatchingPublisher.class);
98         CambriaPublisherWrapper cambria = new CambriaPublisherWrapper(makeBuilder().build());
99         cambria.publisher = pub;
100
101         cambria.close();
102         verify(pub).close();
103
104         // try again, this time with an exception
105         doThrow(new RuntimeException(EXPECTED)).when(pub).close();
106         cambria.close();
107     }
108
109     @Test
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();
116     }
117
118     @Test(expected = IllegalArgumentException.class)
119     public void testDmaapPublisherWrapper_InvalidTopic() {
120         new DmaapPublisherWrapper(ProtocolTypeConstants.DME2, servers, "", MY_USERNAME, MY_PASS, true) {};
121     }
122
123     @Test(expected = IllegalArgumentException.class)
124     public void testDmaapPublisherWrapper_Aaf_NullServers() {
125         new DmaapAafPublisherWrapper(null, MY_TOPIC, MY_USERNAME, MY_PASS, true);
126     }
127
128     @Test(expected = IllegalArgumentException.class)
129     public void testDmaapPublisherWrapper_Aaf_NoServers() {
130         new DmaapAafPublisherWrapper(Collections.emptyList(), MY_TOPIC, MY_USERNAME, MY_PASS, true);
131     }
132
133     @Test(expected = IllegalArgumentException.class)
134     public void testDmaapPublisherWrapper_InvalidProtocol() {
135         new DmaapPublisherWrapper(ProtocolTypeConstants.HTTPNOAUTH, servers, MY_TOPIC, MY_USERNAME, MY_PASS, true) {};
136     }
137
138     @Test
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;
143
144         dmaap.close();
145         verify(pub).close(anyLong(), any(TimeUnit.class));
146
147         // close, but with exception from publisher
148         doThrow(new IOException(EXPECTED)).when(pub).close(anyLong(), any(TimeUnit.class));
149         dmaap.close();
150     }
151
152     @Test
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;
157
158         // null response
159         assertTrue(dmaap.send(MY_PARTITION, MY_MESSAGE));
160         verify(pub).setPubResponse(any(MRPublisherResponse.class));
161         verify(pub).send(MY_PARTITION, MY_MESSAGE);
162
163         // with response
164         pub = mock(MRSimplerBatchPublisher.class);
165         dmaap.publisher = pub;
166
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);
172     }
173
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;
179
180         dmaap.send(MY_PARTITION, null);
181     }
182
183     @Test
184     public void testDmaapDmePublisherWrapper() {
185         // verify with different parameters
186         new DmaapDmePublisherWrapper(makeBuilder().build());
187         new DmaapDmePublisherWrapper(makeBuilder().additionalProps(null).build());
188
189         addProps.put(ROUTE_PROP, MY_ROUTE);
190         new DmaapDmePublisherWrapper(makeBuilder().build());
191         new DmaapDmePublisherWrapper(makeBuilder().partner(null).build());
192
193         addProps.put("null-value", null);
194         assertThatCode(() -> new DmaapDmePublisherWrapper(makeBuilder().build())).doesNotThrowAnyException();
195     }
196
197     @Test(expected = IllegalArgumentException.class)
198     public void testDmaapDmePublisherWrapper_InvalidEnv() {
199         new DmaapDmePublisherWrapper(makeBuilder().environment(null).build());
200     }
201
202     @Test(expected = IllegalArgumentException.class)
203     public void testDmaapDmePublisherWrapper_InvalidAft() {
204         new DmaapDmePublisherWrapper(makeBuilder().aftEnvironment(null).build());
205     }
206
207     @Test(expected = IllegalArgumentException.class)
208     public void testDmaapDmePublisherWrapper_InvalidLat() {
209         new DmaapDmePublisherWrapper(makeBuilder().latitude(null).build());
210     }
211
212     @Test(expected = IllegalArgumentException.class)
213     public void testDmaapDmePublisherWrapper_InvalidLong() {
214         new DmaapDmePublisherWrapper(makeBuilder().longitude(null).build());
215     }
216
217     @Test(expected = IllegalArgumentException.class)
218     public void testDmaapDmePublisherWrapper_InvalidPartner() {
219         new DmaapDmePublisherWrapper(makeBuilder().partner(null).build());
220     }
221 }