985d38fb31c34bcf068f118501d99578c0562bd6
[dmaap/messagerouter/messageservice.git] / src / test / java / com / att / nsa / dmaap / service / EventsRestServiceTest.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP Policy Engine\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package com.att.nsa.dmaap.service;\r
21 \r
22 import java.util.Date;\r
23 \r
24 import org.junit.After;\r
25 import org.junit.Before;\r
26 import org.junit.Test;\r
27 \r
28 import org.junit.runner.RunWith;\r
29 import org.mockito.InjectMocks;\r
30 import org.mockito.Mock;\r
31 import org.mockito.Mockito;\r
32 import org.mockito.MockitoAnnotations;\r
33 import static org.mockito.Matchers.any;\r
34 import org.powermock.core.classloader.annotations.PrepareForTest;\r
35 import org.powermock.modules.junit4.PowerMockRunner;\r
36 import org.powermock.api.mockito.PowerMockito;\r
37 import static org.mockito.Mockito.when;\r
38 \r
39 import com.att.ajsc.beans.PropertiesMapBean;\r
40 import org.onap.dmaap.dmf.mr.CambriaApiException;\r
41 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory.UnavailableException;\r
42 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;\r
43 import org.onap.dmaap.dmf.mr.service.EventsService;\r
44 import com.att.nsa.configs.ConfigDbException;\r
45 import org.onap.dmaap.dmf.mr.utils.Utils;\r
46 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;\r
47 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;\r
48 \r
49 import static org.junit.Assert.assertTrue;\r
50 \r
51 import java.io.IOException;\r
52 import java.io.InputStream;\r
53 import java.util.Date;\r
54 \r
55 import javax.servlet.ServletInputStream;\r
56 import javax.servlet.ServletOutputStream;\r
57 import javax.servlet.http.HttpServletRequest;\r
58 \r
59 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;\r
60 import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;\r
61 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;\r
62 import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;\r
63 \r
64 @RunWith(PowerMockRunner.class)\r
65 @PrepareForTest({ PropertiesMapBean.class })\r
66 public class EventsRestServiceTest {\r
67 \r
68         @InjectMocks\r
69         EventsRestService eventsRestRestService;\r
70 \r
71         @Mock\r
72         private EventsService eventsService;\r
73 \r
74         @Mock\r
75         ErrorResponse errorResponse;\r
76 \r
77         @Mock\r
78         DMaaPContext dmaapContext;\r
79 \r
80         @Mock\r
81         InputStream iStream;\r
82 \r
83         @Mock\r
84         ServletInputStream servletInputStream;\r
85 \r
86         @Mock\r
87         HttpServletRequest request;\r
88 \r
89         @Mock\r
90         private DMaaPErrorMessages errorMessages;\r
91 \r
92         @Before\r
93         public void setUp() throws Exception {\r
94                 MockitoAnnotations.initMocks(this);\r
95         }\r
96 \r
97         @After\r
98         public void tearDown() throws Exception {\r
99         }\r
100 \r
101         @Test\r
102         public void testGetEvents() throws CambriaApiException {\r
103 \r
104                 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");\r
105 \r
106         }\r
107 \r
108         @Test\r
109         public void testGetEvents_error() {\r
110 \r
111                 try {\r
112                         PowerMockito.doThrow(new IOException()).when(eventsService).getEvents(any(), any(),\r
113                                         any(), any());\r
114                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException\r
115                                 | UnavailableException | IOException excp) {\r
116                         assertTrue(false);\r
117                 } catch (CambriaApiException e) {\r
118                         assertTrue(false);\r
119                 }\r
120 \r
121                 try {\r
122                         eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");\r
123                 } catch (CambriaApiException e) {\r
124                         assertTrue(true);\r
125                 }\r
126 \r
127                 try {\r
128                         PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).getEvents(any(), any(),\r
129                                         any(), any());\r
130                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException\r
131                                 | UnavailableException | IOException excp) {\r
132                         assertTrue(false);\r
133                 } catch (CambriaApiException e) {\r
134                         assertTrue(false);\r
135                 }\r
136 \r
137                 try {\r
138                         eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");\r
139                 } catch (CambriaApiException e) {\r
140                         assertTrue(true);\r
141                 }\r
142 \r
143                 try {\r
144                         PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).getEvents(any(),\r
145                                         any(), any(), any());\r
146                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException\r
147                                 | UnavailableException | IOException excp) {\r
148                         assertTrue(false);\r
149                 } catch (CambriaApiException e) {\r
150                         assertTrue(false);\r
151                 }\r
152 \r
153                 try {\r
154                         eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");\r
155                 } catch (CambriaApiException e) {\r
156                         assertTrue(true);\r
157                 }\r
158 \r
159         }\r
160 \r
161         @Test(expected = TopicExistsException.class)\r
162         public void testGetEvents_TopicExistException() throws CambriaApiException, ConfigDbException, TopicExistsException,\r
163                         UnavailableException, IOException, AccessDeniedException {\r
164 \r
165                 Mockito.doThrow(new TopicExistsException("topic exists")).when(eventsService).getEvents(any(),\r
166                                 any(), any(), any());\r
167 \r
168                 eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid");\r
169 \r
170         }\r
171 \r
172         @Test(expected = DMaaPAccessDeniedException.class)\r
173         public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException,\r
174                         TopicExistsException, UnavailableException, IOException, AccessDeniedException {\r
175 \r
176                 Mockito.doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(any(),\r
177                                 any(), any(), any());\r
178 \r
179                 eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid");\r
180 \r
181         }\r
182 \r
183         /*\r
184          * @Test(expected = DMaaPAccessDeniedException.class) public void\r
185          * testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException,\r
186          * ConfigDbException, TopicExistsException, UnavailableException,\r
187          * IOException, AccessDeniedException {\r
188          * \r
189          * Mockito.doThrow(new\r
190          * DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(\r
191          * dmaapContext, "topicName", "consumergroup", "consumerid");\r
192          * \r
193          * eventsService.getEvents(dmaapContext, "topicName", "consumergroup",\r
194          * "consumerid");\r
195          * \r
196          * }\r
197          */\r
198 \r
199         @Test\r
200         public void testPushEvents() throws CambriaApiException {\r
201 \r
202                 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");\r
203 \r
204         }\r
205 \r
206         @Test\r
207         public void testPushEvents_error() {\r
208 \r
209                 try {\r
210                         PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(),\r
211                                         any(), any());\r
212                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException\r
213                                 | missingReqdSetting | IOException excp) {\r
214                         assertTrue(false);\r
215                 } catch (CambriaApiException e) {\r
216                         assertTrue(false);\r
217                 }\r
218 \r
219                 try {\r
220                         eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");\r
221                 } catch (CambriaApiException e) {\r
222                         assertTrue(true);\r
223                 }\r
224 \r
225                 try {\r
226                         PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(any(), any(),\r
227                                         any(), any(), any());\r
228                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException\r
229                                 | missingReqdSetting | IOException excp) {\r
230                         assertTrue(false);\r
231                 } catch (CambriaApiException e) {\r
232                         assertTrue(false);\r
233                 }\r
234 \r
235                 try {\r
236                         eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");\r
237                 } catch (CambriaApiException e) {\r
238                         assertTrue(true);\r
239                 }\r
240 \r
241                 try {\r
242                         PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(any(),\r
243                                         any(), any(), any(), any());\r
244                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException\r
245                                 | missingReqdSetting | IOException excp) {\r
246                         assertTrue(false);\r
247                 } catch (CambriaApiException e) {\r
248                         assertTrue(false);\r
249                 }\r
250 \r
251                 try {\r
252                         eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");\r
253                 } catch (CambriaApiException e) {\r
254                         assertTrue(true);\r
255                 }\r
256 \r
257         }\r
258 \r
259         @Test\r
260         public void testPushEvents_TopicExistException() throws CambriaApiException {\r
261 \r
262                 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");\r
263 \r
264         }\r
265 \r
266         @Test\r
267         public void tesTPushEventsWithTransaction() throws CambriaApiException, IOException {\r
268                 when(request.getInputStream()).thenReturn(servletInputStream);\r
269                 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");\r
270 \r
271         }\r
272 \r
273         @Test\r
274         public void tesTPushEventsWithTransaction_error() throws IOException {\r
275                 when(request.getInputStream()).thenReturn(servletInputStream);\r
276                 ServletInputStream stream = request.getInputStream();\r
277 \r
278                 try {\r
279                         PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(any(),\r
280                                         any(), any(), any(), any());\r
281                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException\r
282                                 | missingReqdSetting | IOException excp) {\r
283                         assertTrue(false);\r
284                 } catch (CambriaApiException e) {\r
285                         assertTrue(false);\r
286                 }\r
287 \r
288                 try {\r
289                         eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");\r
290                 } catch (CambriaApiException e) {\r
291                         assertTrue(true);\r
292                 }\r
293 \r
294                 try {\r
295                         PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(any(),any(),\r
296                                         any(), any(), any());\r
297                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException\r
298                                 | missingReqdSetting | IOException excp) {\r
299                         assertTrue(false);\r
300                 } catch (CambriaApiException e) {\r
301                         assertTrue(false);\r
302                 }\r
303 \r
304                 try {\r
305                         eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");\r
306                 } catch (CambriaApiException e) {\r
307                         assertTrue(true);\r
308                 }\r
309 \r
310                 try {\r
311                         PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(),\r
312                                         any(), any());\r
313                 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException\r
314                                 | missingReqdSetting | IOException excp) {\r
315                         assertTrue(false);\r
316                 } catch (CambriaApiException e) {\r
317                         assertTrue(false);\r
318                 }\r
319 \r
320                 try {\r
321                         eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");\r
322                 } catch (CambriaApiException e) {\r
323                         assertTrue(true);\r
324                 }\r
325 \r
326         }\r
327 \r
328 }\r