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