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