2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
20 package com.att.nsa.dmaap.service;
\r
22 import java.util.Date;
\r
24 import org.junit.After;
\r
25 import org.junit.Before;
\r
26 import org.junit.Test;
\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
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
49 import static org.junit.Assert.assertTrue;
\r
51 import java.io.IOException;
\r
52 import java.io.InputStream;
\r
53 import java.util.Date;
\r
55 import javax.servlet.ServletInputStream;
\r
56 import javax.servlet.ServletOutputStream;
\r
57 import javax.servlet.http.HttpServletRequest;
\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
64 @RunWith(PowerMockRunner.class)
\r
65 @PrepareForTest({ PropertiesMapBean.class })
\r
66 public class EventsRestServiceTest {
\r
69 EventsRestService eventsRestRestService;
\r
72 private EventsService eventsService;
\r
75 ErrorResponse errorResponse;
\r
78 DMaaPContext dmaapContext;
\r
81 InputStream iStream;
\r
84 ServletInputStream servletInputStream;
\r
87 HttpServletRequest request;
\r
90 private DMaaPErrorMessages errorMessages;
\r
93 public void setUp() throws Exception {
\r
94 MockitoAnnotations.initMocks(this);
\r
98 public void tearDown() throws Exception {
\r
102 public void testGetEvents() throws CambriaApiException {
\r
104 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
\r
109 public void testGetEvents_error() {
\r
112 PowerMockito.doThrow(new IOException()).when(eventsService).getEvents(any(), any(),
\r
114 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
\r
115 | UnavailableException | IOException excp) {
\r
117 } catch (CambriaApiException e) {
\r
122 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
\r
123 } catch (CambriaApiException e) {
\r
128 PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).getEvents(any(), any(),
\r
130 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
\r
131 | UnavailableException | IOException excp) {
\r
133 } catch (CambriaApiException e) {
\r
138 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
\r
139 } catch (CambriaApiException e) {
\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
149 } catch (CambriaApiException e) {
\r
154 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
\r
155 } catch (CambriaApiException e) {
\r
161 @Test(expected = TopicExistsException.class)
\r
162 public void testGetEvents_TopicExistException() throws CambriaApiException, ConfigDbException, TopicExistsException,
\r
163 UnavailableException, IOException, AccessDeniedException {
\r
165 Mockito.doThrow(new TopicExistsException("topic exists")).when(eventsService).getEvents(any(),
\r
166 any(), any(), any());
\r
168 eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid");
\r
172 @Test(expected = DMaaPAccessDeniedException.class)
\r
173 public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException,
\r
174 TopicExistsException, UnavailableException, IOException, AccessDeniedException {
\r
176 Mockito.doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(any(),
\r
177 any(), any(), any());
\r
179 eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid");
\r
184 * @Test(expected = DMaaPAccessDeniedException.class) public void
\r
185 * testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException,
\r
186 * ConfigDbException, TopicExistsException, UnavailableException,
\r
187 * IOException, AccessDeniedException {
\r
189 * Mockito.doThrow(new
\r
190 * DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(
\r
191 * dmaapContext, "topicName", "consumergroup", "consumerid");
\r
193 * eventsService.getEvents(dmaapContext, "topicName", "consumergroup",
\r
200 public void testPushEvents() throws CambriaApiException {
\r
202 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
\r
207 public void testPushEvents_error() {
\r
210 PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(),
\r
212 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
\r
213 | missingReqdSetting | IOException excp) {
\r
215 } catch (CambriaApiException e) {
\r
220 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
\r
221 } catch (CambriaApiException e) {
\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
231 } catch (CambriaApiException e) {
\r
236 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
\r
237 } catch (CambriaApiException e) {
\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
247 } catch (CambriaApiException e) {
\r
252 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
\r
253 } catch (CambriaApiException e) {
\r
260 public void testPushEvents_TopicExistException() throws CambriaApiException {
\r
262 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
\r
267 public void tesTPushEventsWithTransaction() throws CambriaApiException, IOException {
\r
268 when(request.getInputStream()).thenReturn(servletInputStream);
\r
269 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
\r
274 public void tesTPushEventsWithTransaction_error() throws IOException {
\r
275 when(request.getInputStream()).thenReturn(servletInputStream);
\r
276 ServletInputStream stream = request.getInputStream();
\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
284 } catch (CambriaApiException e) {
\r
289 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
\r
290 } catch (CambriaApiException e) {
\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
300 } catch (CambriaApiException e) {
\r
305 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
\r
306 } catch (CambriaApiException e) {
\r
311 PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(),
\r
313 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
\r
314 | missingReqdSetting | IOException excp) {
\r
316 } catch (CambriaApiException e) {
\r
321 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
\r
322 } catch (CambriaApiException e) {
\r