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