2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dmaap.service;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.doThrow;
25 import static org.mockito.Mockito.when;
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;
50 @RunWith(MockitoJUnitRunner.class)
52 public class EventsRestServiceTest {
55 EventsRestService eventsRestService;
58 private EventsService eventsService;
61 ErrorResponse errorResponse;
67 ServletInputStream servletInputStream;
70 HttpServletRequest request;
73 DMaaPErrorMessages errorMessages;
76 public void testGetEvents() throws CambriaApiException {
78 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
82 @Test(expected = CambriaApiException.class)
83 public void testGetEvents_TopicExistException() throws CambriaApiException, ConfigDbException, TopicExistsException,
84 UnavailableException, IOException, AccessDeniedException {
86 doThrow(new TopicExistsException("topic exists")).when(eventsService).getEvents(any(),
89 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
92 @Test(expected = CambriaApiException.class)
93 public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException,
94 TopicExistsException, UnavailableException, IOException, AccessDeniedException {
96 doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(any(),
99 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
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));
107 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
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));
115 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
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));
123 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
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));
131 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
136 public void testPushEvents() throws CambriaApiException {
138 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
142 @Test(expected = CambriaApiException.class)
143 public void testPushEvents_TopicExistsException()
144 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
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());
148 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
152 @Test(expected = CambriaApiException.class)
153 public void testPushEvents_DMaaPAccessDeniedException()
154 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
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());
158 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
162 @Test(expected = CambriaApiException.class)
163 public void testPushEvents_ConfigDbException()
164 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
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());
168 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
172 @Test(expected = CambriaApiException.class)
173 public void testPushEvents_IOException()
174 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
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());
178 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
182 @Test(expected = CambriaApiException.class)
183 public void testPushEvents_missingReqdSetting()
184 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
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());
188 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
192 @Test(expected = CambriaApiException.class)
193 public void testPushEvents_AccessDeniedException()
194 throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
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());
198 eventsRestService.pushEvents("topicName", iStream, "partitionKey");
202 @Test(expected = CambriaApiException.class)
203 public void testGetEventsToException() throws CambriaApiException {
205 eventsRestService.getEventsToException("/topic");
208 @Test(expected = CambriaApiException.class)
209 public void testGetEventsToExceptionWithConsumerGroup() throws CambriaApiException {
210 eventsRestService.getEventsToException("/topic", "1234");
214 public void testPushEventsWithTransaction() throws CambriaApiException, IOException {
215 when(request.getInputStream()).thenReturn(servletInputStream);
216 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
219 @Test(expected = CambriaApiException.class)
220 public void testPushEventsWithTransaction_TopicExistsException()
221 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
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");
228 @Test(expected = CambriaApiException.class)
229 public void testPushEventsWithTransaction_AccessDeniedException()
230 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
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");
237 @Test(expected = CambriaApiException.class)
238 public void testPushEventsWithTransaction_DMaaPAccessDeniedException()
239 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
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));
244 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
247 @Test(expected = CambriaApiException.class)
248 public void testPushEventsWithTransaction_missingReqdSetting()
249 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
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));
254 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
257 @Test(expected = CambriaApiException.class)
258 public void testPushEventsWithTransaction_IOException()
259 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
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));
264 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
267 @Test(expected = CambriaApiException.class)
268 public void testPushEventsWithTransaction_ConfigDbException()
269 throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
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));
274 eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");