44cc1a3f1498fefbaf52389e8f9d1d3ed78edab5
[dmaap/messagerouter/messageservice.git] / src / test / java / org / onap / dmaap / service / EventsRestServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 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  package org.onap.dmaap.service;
21
22 import java.util.Date;
23
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27
28 import org.junit.runner.RunWith;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import static org.mockito.Matchers.any;
34
35 import org.powermock.core.classloader.annotations.PowerMockIgnore;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38 import org.powermock.api.mockito.PowerMockito;
39 import static org.mockito.Mockito.when;
40
41 import com.att.ajsc.beans.PropertiesMapBean;
42 import org.onap.dmaap.dmf.mr.CambriaApiException;
43 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory.UnavailableException;
44 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
45 import org.onap.dmaap.dmf.mr.service.EventsService;
46 import com.att.nsa.configs.ConfigDbException;
47 import org.onap.dmaap.dmf.mr.utils.Utils;
48 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
49 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
50
51 import static org.junit.Assert.assertTrue;
52
53 import java.io.IOException;
54 import java.io.InputStream;
55 import java.util.Date;
56
57 import javax.servlet.ServletInputStream;
58 import javax.servlet.ServletOutputStream;
59 import javax.servlet.http.HttpServletRequest;
60
61 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
62 import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;
63 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
64 import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;
65
66 @RunWith(PowerMockRunner.class)
67 @PowerMockIgnore("jdk.internal.reflect.*")
68 @PrepareForTest({ PropertiesMapBean.class })
69 public class EventsRestServiceTest {
70
71         @InjectMocks
72         EventsRestService eventsRestRestService;
73
74         @Mock
75         private EventsService eventsService;
76
77         @Mock
78         ErrorResponse errorResponse;
79
80         @Mock
81         DMaaPContext dmaapContext;
82
83         @Mock
84         InputStream iStream;
85
86         @Mock
87         ServletInputStream servletInputStream;
88
89         @Mock
90         HttpServletRequest request;
91
92         @Mock
93         private DMaaPErrorMessages errorMessages;
94
95         @Before
96         public void setUp() throws Exception {
97                 MockitoAnnotations.initMocks(this);
98         }
99
100         @After
101         public void tearDown() throws Exception {
102         }
103
104         @Test
105         public void testGetEvents() throws CambriaApiException {
106
107                 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
108
109         }
110
111         @Test
112         public void testGetEvents_error() {
113
114                 try {
115                         PowerMockito.doThrow(new IOException()).when(eventsService).getEvents(any(), any(),
116                                         any(), any());
117                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
118                                 | UnavailableException | IOException excp) {
119                         assertTrue(false);
120                 } catch (CambriaApiException e) {
121                         assertTrue(false);
122                 }
123
124                 try {
125                         eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
126                 } catch (CambriaApiException e) {
127                         assertTrue(true);
128                 }
129
130                 try {
131                         PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).getEvents(any(), any(),
132                                         any(), any());
133                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
134                                 | UnavailableException | IOException excp) {
135                         assertTrue(false);
136                 } catch (CambriaApiException e) {
137                         assertTrue(false);
138                 }
139
140                 try {
141                         eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
142                 } catch (CambriaApiException e) {
143                         assertTrue(true);
144                 }
145
146                 try {
147                         PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).getEvents(any(),
148                                         any(), any(), any());
149                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
150                                 | UnavailableException | IOException excp) {
151                         assertTrue(false);
152                 } catch (CambriaApiException e) {
153                         assertTrue(false);
154                 }
155
156                 try {
157                         eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
158                 } catch (CambriaApiException e) {
159                         assertTrue(true);
160                 }
161
162         }
163
164         @Test(expected = TopicExistsException.class)
165         public void testGetEvents_TopicExistException() throws CambriaApiException, ConfigDbException, TopicExistsException,
166                         UnavailableException, IOException, AccessDeniedException {
167
168                 Mockito.doThrow(new TopicExistsException("topic exists")).when(eventsService).getEvents(any(),
169                                 any(), any(), any());
170
171                 eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid");
172
173         }
174
175         @Test(expected = DMaaPAccessDeniedException.class)
176         public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException,
177                         TopicExistsException, UnavailableException, IOException, AccessDeniedException {
178
179                 Mockito.doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(any(),
180                                 any(), any(), any());
181
182                 eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid");
183
184         }
185
186         /*
187          * @Test(expected = DMaaPAccessDeniedException.class) public void
188          * testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException,
189          * ConfigDbException, TopicExistsException, UnavailableException,
190          * IOException, AccessDeniedException {
191          * 
192          * Mockito.doThrow(new
193          * DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(
194          * dmaapContext, "topicName", "consumergroup", "consumerid");
195          * 
196          * eventsService.getEvents(dmaapContext, "topicName", "consumergroup",
197          * "consumerid");
198          * 
199          * }
200          */
201
202         @Test
203         public void testPushEvents() throws CambriaApiException {
204
205                 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
206
207         }
208
209         @Test
210         public void testPushEvents_error() {
211
212                 try {
213                         PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(),
214                                         any(), any());
215                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
216                                 | missingReqdSetting | IOException excp) {
217                         assertTrue(false);
218                 } catch (CambriaApiException e) {
219                         assertTrue(false);
220                 }
221
222                 try {
223                         eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
224                 } catch (CambriaApiException e) {
225                         assertTrue(true);
226                 }
227
228                 try {
229                         PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(any(), any(),
230                                         any(), any(), any());
231                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
232                                 | missingReqdSetting | IOException excp) {
233                         assertTrue(false);
234                 } catch (CambriaApiException e) {
235                         assertTrue(false);
236                 }
237
238                 try {
239                         eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
240                 } catch (CambriaApiException e) {
241                         assertTrue(true);
242                 }
243
244                 try {
245                         PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(any(),
246                                         any(), any(), any(), any());
247                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
248                                 | missingReqdSetting | IOException excp) {
249                         assertTrue(false);
250                 } catch (CambriaApiException e) {
251                         assertTrue(false);
252                 }
253
254                 try {
255                         eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
256                 } catch (CambriaApiException e) {
257                         assertTrue(true);
258                 }
259
260         }
261
262         @Test
263         public void testGetEventsToException() throws CambriaApiException {
264                 try {
265                         eventsRestRestService.getEventsToException("/topic");
266                 } catch (CambriaApiException e) {
267                         assertTrue(true);
268                 }
269         }
270         
271         @Test
272         public void testGetEventsToExceptionWithConsumerGroup() throws CambriaApiException {
273                 try {
274                         eventsRestRestService.getEventsToException("/topic", "1234");
275                 } catch (CambriaApiException e) {
276                         assertTrue(true);
277                 }               
278         }
279         
280         @Test
281         public void testPushEvents_TopicExistException() throws CambriaApiException {
282
283                 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
284
285         }
286
287         @Test
288         public void tesTPushEventsWithTransaction() throws CambriaApiException, IOException {
289                 when(request.getInputStream()).thenReturn(servletInputStream);
290                 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
291
292         }
293
294         @Test
295         public void tesTPushEventsWithTransaction_error() throws IOException {
296                 when(request.getInputStream()).thenReturn(servletInputStream);
297                 ServletInputStream stream = request.getInputStream();
298
299                 try {
300                         PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(any(),
301                                         any(), any(), any(), any());
302                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
303                                 | missingReqdSetting | IOException excp) {
304                         assertTrue(false);
305                 } catch (CambriaApiException e) {
306                         assertTrue(false);
307                 }
308
309                 try {
310                         eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
311                 } catch (CambriaApiException e) {
312                         assertTrue(true);
313                 }
314
315                 try {
316                         PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(any(),any(),
317                                         any(), any(), any());
318                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
319                                 | missingReqdSetting | IOException excp) {
320                         assertTrue(false);
321                 } catch (CambriaApiException e) {
322                         assertTrue(false);
323                 }
324
325                 try {
326                         eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
327                 } catch (CambriaApiException e) {
328                         assertTrue(true);
329                 }
330
331                 try {
332                         PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(),
333                                         any(), any());
334                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
335                                 | missingReqdSetting | IOException excp) {
336                         assertTrue(false);
337                 } catch (CambriaApiException e) {
338                         assertTrue(false);
339                 }
340
341                 try {
342                         eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
343                 } catch (CambriaApiException e) {
344                         assertTrue(true);
345                 }
346
347         }
348
349 }