DMAAP-MR Unit test improvements
[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
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.doThrow;
25 import static org.mockito.Mockito.when;
26
27 import com.att.nsa.configs.ConfigDbException;
28 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
29 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import javax.servlet.ServletInputStream;
33 import javax.servlet.http.HttpServletRequest;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.ArgumentMatchers;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
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.beans.DMaaPContext;
43 import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;
44 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
45 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
46 import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;
47 import org.onap.dmaap.dmf.mr.service.EventsService;
48 import org.springframework.test.context.ContextConfiguration;
49
50 @RunWith(MockitoJUnitRunner.class)
51 @ContextConfiguration
52 public class EventsRestServiceTest {
53
54         @InjectMocks
55         EventsRestService eventsRestService;
56
57         @Mock
58         private EventsService eventsService;
59
60         @Mock
61         ErrorResponse errorResponse;
62
63         @Mock
64         InputStream iStream;
65
66         @Mock
67         ServletInputStream servletInputStream;
68
69         @Mock
70         HttpServletRequest request;
71
72         @Mock
73         DMaaPErrorMessages errorMessages;
74
75         @Test
76         public void testGetEvents() throws CambriaApiException {
77
78                 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
79
80         }
81
82         @Test(expected = CambriaApiException.class)
83         public void testGetEvents_TopicExistException() throws CambriaApiException, ConfigDbException, TopicExistsException,
84                         UnavailableException, IOException, AccessDeniedException {
85
86                 doThrow(new TopicExistsException("topic exists")).when(eventsService).getEvents(any(),
87                                 any(), any(), any());
88
89                 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
90         }
91
92         @Test(expected = CambriaApiException.class)
93         public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException,
94                         TopicExistsException, UnavailableException, IOException, AccessDeniedException {
95
96                 doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(any(),
97                                 any(), any(), any());
98
99                 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
100         }
101
102          @Test(expected = CambriaApiException.class)
103          public void testGetEvents_AccessDeniedException() throws CambriaApiException,
104                  ConfigDbException, TopicExistsException, UnavailableException, IOException, AccessDeniedException {
105                  doThrow(new ConfigDbException("error")).when(eventsService).getEvents( ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));
106
107                  eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
108          }
109
110         @Test(expected = CambriaApiException.class)
111         public void testGetEvents_ConfigDbException() throws CambriaApiException,
112                 ConfigDbException, TopicExistsException, UnavailableException, IOException, AccessDeniedException {
113                 doThrow(new ConfigDbException("error")).when(eventsService).getEvents( ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));
114
115                 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
116         }
117
118         @Test(expected = CambriaApiException.class)
119         public void testGetEvents_UnavailableException() throws CambriaApiException,
120                 ConfigDbException, TopicExistsException, UnavailableException, IOException, AccessDeniedException {
121                 doThrow(new ConfigDbException("error")).when(eventsService).getEvents( ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));
122
123                 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
124         }
125
126         @Test(expected = CambriaApiException.class)
127         public void testGetEvents_IOException() throws CambriaApiException,
128                 ConfigDbException, TopicExistsException, UnavailableException, IOException, AccessDeniedException {
129                 doThrow(new ConfigDbException("error")).when(eventsService).getEvents( ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));
130
131                 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
132         }
133
134
135         @Test
136         public void testPushEvents() throws CambriaApiException {
137
138                 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
139
140         }
141
142         @Test(expected = CambriaApiException.class)
143         public void testPushEvents_TopicExistsException()
144                 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
145                 missingReqdSetting {
146                 doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.isNull());
147
148                 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
149
150         }
151
152         @Test(expected = CambriaApiException.class)
153         public void testPushEvents_DMaaPAccessDeniedException()
154                 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
155                 missingReqdSetting {
156                 doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.isNull());
157
158                 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
159
160         }
161
162         @Test(expected = CambriaApiException.class)
163         public void testPushEvents_ConfigDbException()
164                 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
165                 missingReqdSetting {
166                 doThrow(new ConfigDbException("error")).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.isNull());
167
168                 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
169
170         }
171
172         @Test(expected = CambriaApiException.class)
173         public void testPushEvents_IOException()
174                 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
175                 missingReqdSetting {
176                 doThrow(new IOException("error")).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.isNull());
177
178                 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
179
180         }
181
182         @Test(expected = CambriaApiException.class)
183         public void testPushEvents_missingReqdSetting()
184                 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
185                 missingReqdSetting {
186                 doThrow(new missingReqdSetting("error")).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.isNull());
187
188                 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
189
190         }
191
192         @Test(expected = CambriaApiException.class)
193         public void testPushEvents_AccessDeniedException()
194                 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
195                 missingReqdSetting {
196                 doThrow(new AccessDeniedException("error")).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.isNull());
197
198                 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
199
200         }
201
202         @Test(expected = CambriaApiException.class)
203         public void testGetEventsToException() throws CambriaApiException {
204
205                 eventsRestService.getEventsToException("/topic");
206         }
207
208         @Test(expected = CambriaApiException.class)
209         public void testGetEventsToExceptionWithConsumerGroup() throws CambriaApiException {
210                 eventsRestService.getEventsToException("/topic", "1234");
211         }
212
213         @Test
214         public void testPushEventsWithTransaction() throws CambriaApiException, IOException {
215                 when(request.getInputStream()).thenReturn(servletInputStream);
216                 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
217         }
218
219         @Test(expected = CambriaApiException.class)
220         public void testPushEventsWithTransaction_TopicExistsException()
221                 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
222                 missingReqdSetting {
223                 when(request.getInputStream()).thenReturn(servletInputStream);
224                 doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));
225                 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
226         }
227
228         @Test(expected = CambriaApiException.class)
229         public void testPushEventsWithTransaction_AccessDeniedException()
230                 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
231                 missingReqdSetting {
232                 when(request.getInputStream()).thenReturn(servletInputStream);
233                 doThrow(new AccessDeniedException("error")).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));
234                 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
235         }
236
237         @Test(expected = CambriaApiException.class)
238         public void testPushEventsWithTransaction_DMaaPAccessDeniedException()
239                 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
240                 missingReqdSetting {
241                 when(request.getInputStream()).thenReturn(servletInputStream);
242                 doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));
243
244                 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
245         }
246
247         @Test(expected = CambriaApiException.class)
248         public void testPushEventsWithTransaction_missingReqdSetting()
249                 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
250                 missingReqdSetting {
251                 when(request.getInputStream()).thenReturn(servletInputStream);
252                 doThrow(new missingReqdSetting("error")).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));
253
254                 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
255         }
256
257         @Test(expected = CambriaApiException.class)
258         public void testPushEventsWithTransaction_IOException()
259                 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
260                 missingReqdSetting {
261                 when(request.getInputStream()).thenReturn(servletInputStream);
262                 doThrow(new IOException("error")).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));
263
264                 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
265         }
266
267         @Test(expected = CambriaApiException.class)
268         public void testPushEventsWithTransaction_ConfigDbException()
269                 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
270                 missingReqdSetting {
271                 when(request.getInputStream()).thenReturn(servletInputStream);
272                 doThrow(new ConfigDbException("error")).when(eventsService).pushEvents(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));
273
274                 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
275         }
276 }