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;
22 import java.util.Date;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
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;
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;
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;
51 import static org.junit.Assert.assertTrue;
53 import java.io.IOException;
54 import java.io.InputStream;
55 import java.util.Date;
57 import javax.servlet.ServletInputStream;
58 import javax.servlet.ServletOutputStream;
59 import javax.servlet.http.HttpServletRequest;
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;
66 @RunWith(PowerMockRunner.class)
67 @PowerMockIgnore("jdk.internal.reflect.*")
68 @PrepareForTest({ PropertiesMapBean.class })
69 public class EventsRestServiceTest {
72 EventsRestService eventsRestRestService;
75 private EventsService eventsService;
78 ErrorResponse errorResponse;
81 DMaaPContext dmaapContext;
87 ServletInputStream servletInputStream;
90 HttpServletRequest request;
93 private DMaaPErrorMessages errorMessages;
96 public void setUp() throws Exception {
97 MockitoAnnotations.initMocks(this);
101 public void tearDown() throws Exception {
105 public void testGetEvents() throws CambriaApiException {
107 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
112 public void testGetEvents_error() {
115 PowerMockito.doThrow(new IOException()).when(eventsService).getEvents(any(), any(),
117 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
118 | UnavailableException | IOException excp) {
120 } catch (CambriaApiException e) {
125 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
126 } catch (CambriaApiException e) {
131 PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).getEvents(any(), any(),
133 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
134 | UnavailableException | IOException excp) {
136 } catch (CambriaApiException e) {
141 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
142 } catch (CambriaApiException e) {
147 PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).getEvents(any(),
148 any(), any(), any());
149 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
150 | UnavailableException | IOException excp) {
152 } catch (CambriaApiException e) {
157 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
158 } catch (CambriaApiException e) {
164 @Test(expected = TopicExistsException.class)
165 public void testGetEvents_TopicExistException() throws CambriaApiException, ConfigDbException, TopicExistsException,
166 UnavailableException, IOException, AccessDeniedException {
168 Mockito.doThrow(new TopicExistsException("topic exists")).when(eventsService).getEvents(any(),
169 any(), any(), any());
171 eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid");
175 @Test(expected = DMaaPAccessDeniedException.class)
176 public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException,
177 TopicExistsException, UnavailableException, IOException, AccessDeniedException {
179 Mockito.doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(any(),
180 any(), any(), any());
182 eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid");
187 * @Test(expected = DMaaPAccessDeniedException.class) public void
188 * testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException,
189 * ConfigDbException, TopicExistsException, UnavailableException,
190 * IOException, AccessDeniedException {
192 * Mockito.doThrow(new
193 * DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(
194 * dmaapContext, "topicName", "consumergroup", "consumerid");
196 * eventsService.getEvents(dmaapContext, "topicName", "consumergroup",
203 public void testPushEvents() throws CambriaApiException {
205 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
210 public void testPushEvents_error() {
213 PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(),
215 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
216 | missingReqdSetting | IOException excp) {
218 } catch (CambriaApiException e) {
223 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
224 } catch (CambriaApiException e) {
229 PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(any(), any(),
230 any(), any(), any());
231 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
232 | missingReqdSetting | IOException excp) {
234 } catch (CambriaApiException e) {
239 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
240 } catch (CambriaApiException e) {
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) {
250 } catch (CambriaApiException e) {
255 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
256 } catch (CambriaApiException e) {
263 public void testGetEventsToException() throws CambriaApiException {
265 eventsRestRestService.getEventsToException("/topic");
266 } catch (CambriaApiException e) {
272 public void testGetEventsToExceptionWithConsumerGroup() throws CambriaApiException {
274 eventsRestRestService.getEventsToException("/topic", "1234");
275 } catch (CambriaApiException e) {
281 public void testPushEvents_TopicExistException() throws CambriaApiException {
283 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
288 public void tesTPushEventsWithTransaction() throws CambriaApiException, IOException {
289 when(request.getInputStream()).thenReturn(servletInputStream);
290 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
295 public void tesTPushEventsWithTransaction_error() throws IOException {
296 when(request.getInputStream()).thenReturn(servletInputStream);
297 ServletInputStream stream = request.getInputStream();
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) {
305 } catch (CambriaApiException e) {
310 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
311 } catch (CambriaApiException e) {
316 PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(any(),any(),
317 any(), any(), any());
318 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
319 | missingReqdSetting | IOException excp) {
321 } catch (CambriaApiException e) {
326 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
327 } catch (CambriaApiException e) {
332 PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(),
334 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
335 | missingReqdSetting | IOException excp) {
337 } catch (CambriaApiException e) {
342 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
343 } catch (CambriaApiException e) {