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;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.junit4.PowerMockRunner;
36 import org.powermock.api.mockito.PowerMockito;
37 import static org.mockito.Mockito.when;
39 import com.att.ajsc.beans.PropertiesMapBean;
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.exception.DMaaPErrorMessages;
43 import org.onap.dmaap.dmf.mr.service.EventsService;
44 import com.att.nsa.configs.ConfigDbException;
45 import org.onap.dmaap.dmf.mr.utils.Utils;
46 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
47 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
49 import static org.junit.Assert.assertTrue;
51 import java.io.IOException;
52 import java.io.InputStream;
53 import java.util.Date;
55 import javax.servlet.ServletInputStream;
56 import javax.servlet.ServletOutputStream;
57 import javax.servlet.http.HttpServletRequest;
59 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
60 import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;
61 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
62 import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;
64 @RunWith(PowerMockRunner.class)
65 @PrepareForTest({ PropertiesMapBean.class })
66 public class EventsRestServiceTest {
69 EventsRestService eventsRestRestService;
72 private EventsService eventsService;
75 ErrorResponse errorResponse;
78 DMaaPContext dmaapContext;
84 ServletInputStream servletInputStream;
87 HttpServletRequest request;
90 private DMaaPErrorMessages errorMessages;
93 public void setUp() throws Exception {
94 MockitoAnnotations.initMocks(this);
98 public void tearDown() throws Exception {
102 public void testGetEvents() throws CambriaApiException {
104 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
109 public void testGetEvents_error() {
112 PowerMockito.doThrow(new IOException()).when(eventsService).getEvents(any(), any(),
114 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
115 | UnavailableException | IOException excp) {
117 } catch (CambriaApiException e) {
122 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
123 } catch (CambriaApiException e) {
128 PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).getEvents(any(), any(),
130 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
131 | UnavailableException | IOException excp) {
133 } catch (CambriaApiException e) {
138 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
139 } catch (CambriaApiException e) {
144 PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).getEvents(any(),
145 any(), any(), any());
146 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
147 | UnavailableException | IOException excp) {
149 } catch (CambriaApiException e) {
154 eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid");
155 } catch (CambriaApiException e) {
161 @Test(expected = TopicExistsException.class)
162 public void testGetEvents_TopicExistException() throws CambriaApiException, ConfigDbException, TopicExistsException,
163 UnavailableException, IOException, AccessDeniedException {
165 Mockito.doThrow(new TopicExistsException("topic exists")).when(eventsService).getEvents(any(),
166 any(), any(), any());
168 eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid");
172 @Test(expected = DMaaPAccessDeniedException.class)
173 public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException,
174 TopicExistsException, UnavailableException, IOException, AccessDeniedException {
176 Mockito.doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(any(),
177 any(), any(), any());
179 eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid");
184 * @Test(expected = DMaaPAccessDeniedException.class) public void
185 * testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException,
186 * ConfigDbException, TopicExistsException, UnavailableException,
187 * IOException, AccessDeniedException {
189 * Mockito.doThrow(new
190 * DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(
191 * dmaapContext, "topicName", "consumergroup", "consumerid");
193 * eventsService.getEvents(dmaapContext, "topicName", "consumergroup",
200 public void testPushEvents() throws CambriaApiException {
202 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
207 public void testPushEvents_error() {
210 PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(),
212 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
213 | missingReqdSetting | IOException excp) {
215 } catch (CambriaApiException e) {
220 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
221 } catch (CambriaApiException e) {
226 PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(any(), any(),
227 any(), any(), any());
228 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
229 | missingReqdSetting | IOException excp) {
231 } catch (CambriaApiException e) {
236 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
237 } catch (CambriaApiException e) {
242 PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(any(),
243 any(), any(), any(), any());
244 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
245 | missingReqdSetting | IOException excp) {
247 } catch (CambriaApiException e) {
252 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
253 } catch (CambriaApiException e) {
260 public void testPushEvents_TopicExistException() throws CambriaApiException {
262 eventsRestRestService.pushEvents("topicName", iStream, "partitionKey");
267 public void tesTPushEventsWithTransaction() throws CambriaApiException, IOException {
268 when(request.getInputStream()).thenReturn(servletInputStream);
269 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
274 public void tesTPushEventsWithTransaction_error() throws IOException {
275 when(request.getInputStream()).thenReturn(servletInputStream);
276 ServletInputStream stream = request.getInputStream();
279 PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(any(),
280 any(), any(), any(), any());
281 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
282 | missingReqdSetting | IOException excp) {
284 } catch (CambriaApiException e) {
289 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
290 } catch (CambriaApiException e) {
295 PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(any(),any(),
296 any(), any(), any());
297 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
298 | missingReqdSetting | IOException excp) {
300 } catch (CambriaApiException e) {
305 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
306 } catch (CambriaApiException e) {
311 PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(),
313 } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException
314 | missingReqdSetting | IOException excp) {
316 } catch (CambriaApiException e) {
321 eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey");
322 } catch (CambriaApiException e) {