summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/dmaap/service/EventsRestServiceTest.java
blob: 44a6593e5e9a7e364f4640f5e84e1523d9eb8703 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy Engine
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
 package org.onap.dmaap.service;


import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;

import com.att.nsa.configs.ConfigDbException;
import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.dmaap.dmf.mr.CambriaApiException;
import org.onap.dmaap.dmf.mr.backends.ConsumerFactory.UnavailableException;
import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;
import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;
import org.onap.dmaap.dmf.mr.service.EventsService;
import org.springframework.test.context.ContextConfiguration;

@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration
public class EventsRestServiceTest {

	@InjectMocks
	EventsRestService eventsRestService;

	@Mock
	private EventsService eventsService;

	@Mock
	ErrorResponse errorResponse;

	@Mock
	InputStream iStream;

	@Mock
	ServletInputStream servletInputStream;

	@Mock
	HttpServletRequest request;

	@Mock
	DMaaPErrorMessages errorMessages;

	@Test
	public void testGetEvents() throws CambriaApiException {

		eventsRestService.getEvents("topicName", "consumergroup", "consumerid");

	}

	@Test(expected = CambriaApiException.class)
	public void testGetEvents_TopicExistException() throws CambriaApiException, ConfigDbException, TopicExistsException,
			UnavailableException, IOException, AccessDeniedException {

		doThrow(new TopicExistsException("topic exists")).when(eventsService).getEvents(any(),
				any(), any(), any());

		eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
	}

	@Test(expected = CambriaApiException.class)
	public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException,
			TopicExistsException, UnavailableException, IOException, AccessDeniedException {

		doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(any(),
				any(), any(), any());

		eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
	}

	 @Test(expected = CambriaApiException.class)
	 public void testGetEvents_AccessDeniedException() throws CambriaApiException,
		 ConfigDbException, TopicExistsException, UnavailableException, IOException, AccessDeniedException {
		 doThrow(new ConfigDbException("error")).when(eventsService).getEvents( ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));

		 eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
	 }

	@Test(expected = CambriaApiException.class)
	public void testGetEvents_ConfigDbException() throws CambriaApiException,
		ConfigDbException, TopicExistsException, UnavailableException, IOException, AccessDeniedException {
		doThrow(new ConfigDbException("error")).when(eventsService).getEvents( ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));

		eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
	}

	@Test(expected = CambriaApiException.class)
	public void testGetEvents_UnavailableException() throws CambriaApiException,
		ConfigDbException, TopicExistsException, UnavailableException, IOException, AccessDeniedException {
		doThrow(new ConfigDbException("error")).when(eventsService).getEvents( ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));

		eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
	}

	@Test(expected = CambriaApiException.class)
	public void testGetEvents_IOException() throws CambriaApiException,
		ConfigDbException, TopicExistsException, UnavailableException, IOException, AccessDeniedException {
		doThrow(new ConfigDbException("error")).when(eventsService).getEvents( ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(String.class));

		eventsRestService.getEvents("topicName", "consumergroup", "consumerid");
	}


	@Test
	public void testPushEvents() throws CambriaApiException {

		eventsRestService.pushEvents("topicName", iStream, "partitionKey");

	}

	@Test(expected = CambriaApiException.class)
	public void testPushEvents_TopicExistsException()
		throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		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());

		eventsRestService.pushEvents("topicName", iStream, "partitionKey");

	}

	@Test(expected = CambriaApiException.class)
	public void testPushEvents_DMaaPAccessDeniedException()
		throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		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());

		eventsRestService.pushEvents("topicName", iStream, "partitionKey");

	}

	@Test(expected = CambriaApiException.class)
	public void testPushEvents_ConfigDbException()
		throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		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());

		eventsRestService.pushEvents("topicName", iStream, "partitionKey");

	}

	@Test(expected = CambriaApiException.class)
	public void testPushEvents_IOException()
		throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		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());

		eventsRestService.pushEvents("topicName", iStream, "partitionKey");

	}

	@Test(expected = CambriaApiException.class)
	public void testPushEvents_missingReqdSetting()
		throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		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());

		eventsRestService.pushEvents("topicName", iStream, "partitionKey");

	}

	@Test(expected = CambriaApiException.class)
	public void testPushEvents_AccessDeniedException()
		throws AccessDeniedException, CambriaApiException, IOException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		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());

		eventsRestService.pushEvents("topicName", iStream, "partitionKey");

	}

	@Test(expected = CambriaApiException.class)
	public void testGetEventsToException() throws CambriaApiException {

		eventsRestService.getEventsToException("/topic");
	}

	@Test(expected = CambriaApiException.class)
	public void testGetEventsToExceptionWithConsumerGroup() throws CambriaApiException {
		eventsRestService.getEventsToException("/topic", "1234");
	}

	@Test
	public void testPushEventsWithTransaction() throws CambriaApiException, IOException {
		when(request.getInputStream()).thenReturn(servletInputStream);
		eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
	}

	@Test(expected = CambriaApiException.class)
	public void testPushEventsWithTransaction_TopicExistsException()
		throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		when(request.getInputStream()).thenReturn(servletInputStream);
		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));
		eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
	}

	@Test(expected = CambriaApiException.class)
	public void testPushEventsWithTransaction_AccessDeniedException()
		throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		when(request.getInputStream()).thenReturn(servletInputStream);
		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));
		eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
	}

	@Test(expected = CambriaApiException.class)
	public void testPushEventsWithTransaction_DMaaPAccessDeniedException()
		throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		when(request.getInputStream()).thenReturn(servletInputStream);
		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));

		eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
	}

	@Test(expected = CambriaApiException.class)
	public void testPushEventsWithTransaction_missingReqdSetting()
		throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		when(request.getInputStream()).thenReturn(servletInputStream);
		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));

		eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
	}

	@Test(expected = CambriaApiException.class)
	public void testPushEventsWithTransaction_IOException()
		throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		when(request.getInputStream()).thenReturn(servletInputStream);
		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));

		eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
	}

	@Test(expected = CambriaApiException.class)
	public void testPushEventsWithTransaction_ConfigDbException()
		throws IOException, CambriaApiException, AccessDeniedException, TopicExistsException, ConfigDbException,
		missingReqdSetting {
		when(request.getInputStream()).thenReturn(servletInputStream);
		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));

		eventsRestService.pushEventsWithTransaction("topicName", "partitionKey");
	}
}