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=========================================================
21 package org.onap.dmaap.dmf.mr.service.impl;
23 import static org.hamcrest.CoreMatchers.containsString;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Matchers.anyInt;
29 import static org.mockito.Matchers.anyLong;
30 import static org.mockito.Matchers.anyString;
31 import static org.mockito.Matchers.eq;
32 import static org.mockito.Mockito.doNothing;
33 import static org.mockito.Mockito.doReturn;
34 import static org.mockito.Mockito.doThrow;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.never;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.when;
40 import com.att.nsa.limits.Blacklist;
41 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
42 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
43 import java.io.ByteArrayInputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.ConcurrentModificationException;
49 import javax.servlet.http.HttpServletRequest;
50 import joptsimple.internal.Strings;
51 import org.apache.http.HttpStatus;
52 import org.apache.kafka.clients.producer.ProducerRecord;
53 import org.json.JSONObject;
54 import org.junit.Before;
55 import org.junit.Rule;
56 import org.junit.Test;
57 import org.junit.rules.ExpectedException;
58 import org.junit.runner.RunWith;
59 import org.mockito.ArgumentCaptor;
60 import org.mockito.InOrder;
61 import org.mockito.Mock;
62 import org.mockito.Mockito;
63 import org.mockito.MockitoAnnotations;
64 import org.mockito.Spy;
65 import org.mockito.runners.MockitoJUnitRunner;
66 import org.onap.dmaap.dmf.mr.CambriaApiException;
67 import org.onap.dmaap.dmf.mr.backends.Consumer;
68 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory;
69 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory.UnavailableException;
70 import org.onap.dmaap.dmf.mr.backends.MetricsSet;
71 import org.onap.dmaap.dmf.mr.backends.Publisher;
72 import org.onap.dmaap.dmf.mr.beans.DMaaPCambriaLimiter;
73 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
74 import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;
75 import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;
76 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
77 import org.onap.dmaap.dmf.mr.metabroker.Topic;
78 import org.onap.dmaap.dmf.mr.resources.CambriaOutboundEventStream;
79 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticator;
80 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
81 import org.springframework.mock.web.MockHttpServletRequest;
82 import org.springframework.mock.web.MockHttpServletResponse;
84 @RunWith(MockitoJUnitRunner.class)
85 public class EventsServiceImplTest {
87 private InputStream iStream = null;
88 private DMaaPContext dMaapContext = new DMaaPContext();
89 private DMaaPErrorMessages pErrorMessages = new DMaaPErrorMessages();
91 private ConfigurationReader configurationReader;
93 private Blacklist blacklist;
95 private DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
97 private NsaSimpleApiKey nsaSimpleApiKey;
99 private DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
101 private Topic createdTopic;
103 private ConsumerFactory factory;
105 private Consumer consumer;
107 private Publisher publisher;
109 private DMaaPCambriaLimiter limiter;
111 private MetricsSet metrics;
113 private EventsServiceImpl eventsService;
117 public ExpectedException thrown = ExpectedException.none();
119 private MockHttpServletRequest request;
123 public void setUp() throws Exception {
124 MockitoAnnotations.initMocks(this);
125 String source = "source of my InputStream";
126 iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
128 request = new MockHttpServletRequest();
129 MockHttpServletResponse response = new MockHttpServletResponse();
130 dMaapContext.setRequest(request);
131 dMaapContext.setResponse(response);
132 when(blacklist.contains(anyString())).thenReturn(false);
133 when(configurationReader.getfIpBlackList()).thenReturn(blacklist);
134 when(configurationReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
135 dMaapContext.setConfigReader(configurationReader);
136 eventsService.setErrorMessages(pErrorMessages);
137 doReturn("100").when(eventsService).getPropertyFromAJSCmap("timeout");
141 public void getEvents_shouldFailOnAafAuthorization() throws Exception {
142 String topicPrefix = "org.onap.aaf.enforced";
143 String topicName = topicPrefix + ".topicName";
144 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
145 when(dmaapKafkaMetaBroker.getTopic(topicName)).thenReturn(createdTopic);
146 when(eventsService.getPropertyFromAJSCmap("enforced.topic.name.AAF")).thenReturn(topicPrefix);
147 when(eventsService.isCadiEnabled()).thenReturn(true);
149 thrown.expect(DMaaPAccessDeniedException.class);
150 thrown.expectMessage(containsString(String.valueOf(HttpStatus.SC_UNAUTHORIZED)));
152 eventsService.getEvents(dMaapContext, topicName, "CG1", "23");
156 public void getEvents_shouldFail_whenRemoteAddressIsBlacklisted() throws Exception {
157 String remoteIp = "10.154.17.115";
158 request.setRemoteAddr(remoteIp);
159 when(blacklist.contains(remoteIp)).thenReturn(true);
160 when(configurationReader.getfIpBlackList()).thenReturn(blacklist);
162 thrown.expect(CambriaApiException.class);
163 thrown.expectMessage(containsString(String.valueOf(HttpStatus.SC_FORBIDDEN)));
165 eventsService.getEvents(dMaapContext, "testTopic", "CG1", "23");
169 public void getEvents_shouldFail_whenRequestedTopicNotExists() throws Exception {
170 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
171 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(null);
173 thrown.expect(CambriaApiException.class);
174 thrown.expectMessage(containsString(String.valueOf(HttpStatus.SC_NOT_FOUND)));
176 eventsService.getEvents(dMaapContext, "testTopic", "CG1", "23");
180 public void getEvents_shouldFail_whenConsumerLockCannotBeAcquired() throws Exception {
182 String topicName = "testTopic345";
183 String consumerGroup = "CG5";
184 String clientId = "13";
185 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
186 when(configurationReader.getfRateLimiter()).thenReturn(limiter);
187 when(dmaapKafkaMetaBroker.getTopic(topicName)).thenReturn(createdTopic);
188 when(configurationReader.getfConsumerFactory()).thenReturn(factory);
189 doThrow(new UnavailableException("Could not acquire consumer lock")).when(factory)
190 .getConsumerFor(eq(topicName), eq(consumerGroup), eq(clientId), anyInt(), anyString());
192 thrown.expect(CambriaApiException.class);
193 thrown.expectMessage(containsString(String.valueOf(HttpStatus.SC_SERVICE_UNAVAILABLE)));
196 eventsService.getEvents(dMaapContext, topicName, consumerGroup, clientId);
199 verify(factory).getConsumerFor(eq(topicName), eq(consumerGroup), eq(clientId), anyInt(), anyString());
204 public void getEvents_shouldFail_whenBrokerServicesAreUnavailable() throws Exception {
205 String topicName = "testTopic";
206 String consumerGroup = "CG1";
207 String clientId = "23";
208 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
209 when(dmaapKafkaMetaBroker.getTopic(topicName)).thenReturn(createdTopic);
210 when(configurationReader.getfConsumerFactory()).thenReturn(factory);
212 givenUserAuthorizedWithAAF(request, topicName, "sub");
214 thrown.expect(CambriaApiException.class);
215 thrown.expectMessage(containsString(String.valueOf(HttpStatus.SC_SERVICE_UNAVAILABLE)));
218 eventsService.getEvents(dMaapContext, topicName, consumerGroup, clientId);
221 verify(factory).destroyConsumer(topicName, consumerGroup, clientId);
224 private void givenUserAuthorizedWithAAF(MockHttpServletRequest request, String topicName, String operation) {
225 String permission = "org.onap.dmaap.mr.topic|:topic." + topicName + "|" + operation;
226 request.addUserRole(permission);
230 public void getEvents_shouldHandleConcurrentModificationError() throws Exception {
231 String testTopic = "testTopic";
232 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
233 when(dmaapKafkaMetaBroker.getTopic(testTopic)).thenReturn(createdTopic);
234 when(configurationReader.getfRateLimiter()).thenThrow(new ConcurrentModificationException("Error occurred"));
235 givenUserAuthorizedWithAAF(request, testTopic, "sub");
237 thrown.expect(CambriaApiException.class);
238 thrown.expectMessage(containsString(String.valueOf(HttpStatus.SC_CONFLICT)));
240 eventsService.getEvents(dMaapContext, "testTopic", "CG1", "23");
244 public void getEvents_shouldNotAuthorizeClient_whenSubscribingToMetricsTopic() throws Exception {
246 HttpServletRequest permittedRequest = mock(HttpServletRequest.class);
247 when(permittedRequest.getHeaders(anyString())).thenReturn(Collections.<String>emptyEnumeration());
248 dMaapContext.setRequest(permittedRequest);
249 String metricsTopicName = "msgrtr.apinode.metrics.dmaap";
250 String consumerGroup = "CG5";
251 String clientId = "7";
252 givenConfiguredWithMocks(metricsTopicName);
253 when(factory.getConsumerFor(eq(metricsTopicName), eq(consumerGroup), eq(clientId), anyInt(), any()))
254 .thenReturn(consumer);
255 doNothing().when(eventsService).respondOkWithStream(eq(dMaapContext), any(CambriaOutboundEventStream.class));
258 eventsService.getEvents(dMaapContext, metricsTopicName, consumerGroup, clientId);
261 verify(eventsService).respondOkWithStream(eq(dMaapContext), any(CambriaOutboundEventStream.class));
262 verify(dmaaPAuthenticator, never()).authenticate(dMaapContext);
263 verify(permittedRequest, never()).isUserInRole(anyString());
267 public void getEvents_shouldNotAuthorizeClient_whenTopicNoteEnforcedWithAaf_andTopicHasNoOwnerSet()
270 String topicName = "someSimpleTopicName";
271 String consumerGroup = "CG5";
272 String clientId = "7";
273 HttpServletRequest permittedRequest = mock(HttpServletRequest.class);
274 when(permittedRequest.getHeaders(anyString())).thenReturn(Collections.<String>emptyEnumeration());
275 dMaapContext.setRequest(permittedRequest);
276 givenConfiguredWithMocks(topicName);
277 when(factory.getConsumerFor(eq(topicName), eq(consumerGroup), eq(clientId), anyInt(), any()))
278 .thenReturn(consumer);
279 doNothing().when(eventsService).respondOkWithStream(eq(dMaapContext), any(CambriaOutboundEventStream.class));
280 when(createdTopic.getOwner()).thenReturn(Strings.EMPTY);
283 eventsService.getEvents(dMaapContext, topicName, consumerGroup, clientId);
286 verify(eventsService).respondOkWithStream(eq(dMaapContext), any(CambriaOutboundEventStream.class));
287 verify(dmaaPAuthenticator, never()).authenticate(dMaapContext);
288 verify(permittedRequest, never()).isUserInRole(anyString());
292 public void getEvents_shouldFailDmaapAuthorization_whenTopicOwnerIsSet_andUserHasNoReadPermissionToTopic()
295 String topicName = "someSimpleTopicName";
296 String consumerGroup = "CG5";
297 String clientId = "7";
298 HttpServletRequest permittedRequest = mock(HttpServletRequest.class);
299 when(permittedRequest.getHeaders(anyString())).thenReturn(Collections.<String>emptyEnumeration());
300 dMaapContext.setRequest(permittedRequest);
301 givenConfiguredWithMocks(topicName);
302 when(createdTopic.getOwner()).thenReturn("SimpleTopicOwner");
303 when(dmaaPAuthenticator.authenticate(dMaapContext)).thenReturn(nsaSimpleApiKey);
304 doThrow(new AccessDeniedException("userName")).when(createdTopic).checkUserRead(nsaSimpleApiKey);
306 thrown.expect(AccessDeniedException.class);
309 eventsService.getEvents(dMaapContext, topicName, consumerGroup, clientId);
312 verify(createdTopic).checkUserRead(nsaSimpleApiKey);
313 verify(eventsService, never()).respondOkWithStream(eq(dMaapContext), any(CambriaOutboundEventStream.class));
314 verify(permittedRequest, never()).isUserInRole(anyString());
319 public void getEvents_shouldSuccessfullyRegisterConsumerToEventsStream_withAafAuthorization() throws Exception {
321 String topicName = "testTopic";
322 String consumerGroup = "CG2";
323 String clientId = "6";
324 String messageLimit = "10";
325 String timeout = "25";
327 String pretty = "on";
328 String cacheEnabled = "false";
330 givenConfiguredWithMocks(topicName);
331 givenConfiguredWithProperties(messageLimit, timeout, meta, pretty, cacheEnabled);
332 when(factory.getConsumerFor(eq(topicName), eq(consumerGroup), eq(clientId), anyInt(), anyString()))
333 .thenReturn(consumer);
334 givenUserAuthorizedWithAAF(request, topicName, "sub");
337 eventsService.getEvents(dMaapContext, topicName, consumerGroup, clientId);
340 ArgumentCaptor<CambriaOutboundEventStream> osWriter = ArgumentCaptor.forClass(CambriaOutboundEventStream.class);
341 verifyInvocationOrderForSuccessCase(topicName, consumerGroup, clientId, osWriter);
342 assertEventStreamProperties(osWriter.getValue(), messageLimit, timeout);
345 private void assertEventStreamProperties(CambriaOutboundEventStream stream, String messageLimit, String timeout) {
346 assertEquals(Integer.valueOf(messageLimit).intValue(), stream.getfLimit());
347 assertEquals(Integer.valueOf(timeout).intValue(), stream.getfTimeoutMs());
348 assertTrue(stream.isfWithMeta());
349 assertTrue(stream.isfPretty());
352 private void givenConfiguredWithProperties(String messageLimit, String timeout, String meta, String pretty,
353 String cacheEnabled) {
354 when(eventsService.getPropertyFromAJSCmap("meta")).thenReturn(meta);
355 when(eventsService.getPropertyFromAJSCmap("pretty")).thenReturn(pretty);
356 when(eventsService.getPropertyFromAJSCmap(ConsumerFactory.kSetting_EnableCache)).thenReturn(cacheEnabled);
357 request.addParameter("timeout", timeout);
358 request.addParameter("limit", messageLimit);
361 private void givenConfiguredWithMocks(String topicName) throws Exception {
362 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
363 when(configurationReader.getfRateLimiter()).thenReturn(limiter);
364 when(configurationReader.getfMetrics()).thenReturn(metrics);
365 when(dmaapKafkaMetaBroker.getTopic(topicName)).thenReturn(createdTopic);
366 when(configurationReader.getfConsumerFactory()).thenReturn(factory);
367 when(configurationReader.getfPublisher()).thenReturn(publisher);
370 private void verifyInvocationOrderForSuccessCase(String topicName, String consumerGroup, String clientId,
371 ArgumentCaptor<CambriaOutboundEventStream> osWriter) throws Exception {
373 InOrder inOrder = Mockito.inOrder(configurationReader, factory, metrics, limiter, consumer, eventsService);
374 inOrder.verify(configurationReader).getfMetrics();
375 inOrder.verify(configurationReader).getfRateLimiter();
376 inOrder.verify(limiter).onCall(eq(topicName), eq(consumerGroup), eq(clientId), anyString());
377 inOrder.verify(factory).getConsumerFor(eq(topicName), eq(consumerGroup), eq(clientId), anyInt(), anyString());
378 inOrder.verify(eventsService).respondOkWithStream(eq(dMaapContext), osWriter.capture());
379 inOrder.verify(consumer).commitOffsets();
380 inOrder.verify(metrics).consumeTick(anyInt());
381 inOrder.verify(limiter).onSend(eq(topicName), eq(consumerGroup), eq(clientId), anyLong());
382 inOrder.verify(consumer).close();
383 inOrder.verifyNoMoreInteractions();
387 public void pushEvents_shouldFail_whenRemoteAddressIsBlacklisted() throws Exception {
388 String remoteIp = "10.132.64.112";
389 request.setRemoteAddr(remoteIp);
390 when(configurationReader.getfIpBlackList()).thenReturn(blacklist);
391 when(blacklist.contains(anyString())).thenReturn(true);
393 thrown.expect(CambriaApiException.class);
394 thrown.expectMessage(containsString(String.valueOf(HttpStatus.SC_FORBIDDEN)));
396 eventsService.pushEvents(dMaapContext, "testTopic", iStream, "3", "12:00:00");
403 public void pushEvents_shouldFailDmaapAuthorization_whenTopicOwnerIsSet_andUserHasNoWritePermissionToTopic()
406 String topicName = "someSimpleTopicName";
408 HttpServletRequest permittedRequest = mock(HttpServletRequest.class);
409 when(permittedRequest.getHeaders(anyString())).thenReturn(Collections.<String>emptyEnumeration());
410 dMaapContext.setRequest(permittedRequest);
411 givenConfiguredWithMocks(topicName);
412 when(createdTopic.getOwner()).thenReturn("SimpleTopicOwner");
413 when(dmaaPAuthenticator.authenticate(dMaapContext)).thenReturn(nsaSimpleApiKey);
414 doThrow(new AccessDeniedException("userName")).when(createdTopic).checkUserWrite(nsaSimpleApiKey);
416 thrown.expect(AccessDeniedException.class);
419 eventsService.pushEvents(dMaapContext, topicName, iStream, "5", "13:00:00");
422 verify(createdTopic).checkUserWrite(nsaSimpleApiKey);
423 verify(eventsService, never()).respondOkWithStream(eq(dMaapContext), any(CambriaOutboundEventStream.class));
424 verify(permittedRequest, never()).isUserInRole(anyString());
428 public void pushEvents_shouldFailOnAafAuthorization_whenCadiIsEnabled_topicNameEnforced_andUserHasNoPermission()
431 String topicPrefix = "org.onap.aaf.enforced";
432 String topicName = topicPrefix + ".topicName";
433 String permission = "org.onap.dmaap.mr.topic|:topic." + topicName + "|pub";
434 HttpServletRequest deniedRequest = mock(HttpServletRequest.class);
435 when(deniedRequest.getHeaders(anyString())).thenReturn(Collections.<String>emptyEnumeration());
436 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
437 when(dmaapKafkaMetaBroker.getTopic(topicName)).thenReturn(createdTopic);
438 when(eventsService.getPropertyFromAJSCmap("enforced.topic.name.AAF")).thenReturn(topicPrefix);
439 when(eventsService.isCadiEnabled()).thenReturn(true);
440 dMaapContext.setRequest(deniedRequest);
442 thrown.expect(DMaaPAccessDeniedException.class);
443 thrown.expectMessage(containsString(String.valueOf(HttpStatus.SC_UNAUTHORIZED)));
446 eventsService.pushEvents(dMaapContext, topicName, iStream, "5", "13:00:00");
449 verify(deniedRequest).isUserInRole(permission);
454 public void pushEvents_shouldPublishMessagesWithoutTransaction() throws Exception {
456 String topicName = "topicWithoutTransaction";
457 givenConfiguredWithMocks(topicName);
458 doNothing().when(eventsService).respondOk(eq(dMaapContext), any(JSONObject.class));
461 eventsService.pushEvents(dMaapContext, topicName, iStream, "5", "13:00:00");
464 verify(publisher).sendBatchMessageNew(eq(topicName), Mockito.<ArrayList<ProducerRecord<String, String>>>any());
465 ArgumentCaptor<JSONObject> captor = ArgumentCaptor.forClass(JSONObject.class);
466 verify(eventsService).respondOk(eq(dMaapContext), captor.capture());
467 assertEquals(1, captor.getValue().getLong("count"));
471 public void pushEvents_shouldHandlePublisherError_whenPushWithoutTransaction() throws Exception {
473 String topicName = "topicWithoutTransaction";
474 givenConfiguredWithMocks(topicName);
475 doThrow(new IOException()).when(publisher)
476 .sendBatchMessageNew(eq(topicName), Mockito.<ArrayList<ProducerRecord<String, String>>>any());
478 thrown.expect(CambriaApiException.class);
479 thrown.expectMessage(containsString(String.valueOf(HttpStatus.SC_NOT_FOUND)));
482 eventsService.pushEvents(dMaapContext, topicName, iStream, "5", "13:00:00");
485 verify(publisher).sendBatchMessageNew(eq(topicName), Mockito.<ArrayList<ProducerRecord<String, String>>>any());
486 verify(eventsService, never()).respondOk(any(DMaaPContext.class), any(JSONObject.class));
491 public void pushEvents_shouldPublishMessagesWithTransaction() throws Exception {
493 String topicPrefix = "org.onap.dmaap.mr";
494 String topicName = topicPrefix + ".topicWithTransaction";
495 givenConfiguredWithMocks(topicName);
496 when(eventsService.getPropertyFromAJSCmap("enforced.topic.name.AAF")).thenReturn(topicPrefix);
497 when(eventsService.isCadiEnabled()).thenReturn(true);
498 doNothing().when(eventsService).respondOk(eq(dMaapContext), any(JSONObject.class));
500 request.addUserRole("org.onap.dmaap.mr.topic|:topic." + topicName + "|pub");
503 eventsService.pushEvents(dMaapContext, topicName, iStream, "5", "13:00:00");
506 verify(publisher).sendBatchMessageNew(eq(topicName), Mockito.<ArrayList<ProducerRecord<String, String>>>any());
507 ArgumentCaptor<JSONObject> captor = ArgumentCaptor.forClass(JSONObject.class);
508 verify(eventsService).respondOk(eq(dMaapContext), captor.capture());
509 assertEquals(1, captor.getValue().getLong("count"));
510 assertFalse(captor.getValue().getString("transactionId").isEmpty());
514 public void pushEvents_shouldHandlePublisherError_whenPushWithTransaction() throws Exception {
516 String topicPrefix = "org.onap.dmaap.mr";
517 String topicName = topicPrefix + ".topicWithTransaction";
518 givenConfiguredWithMocks(topicName);
519 when(eventsService.getPropertyFromAJSCmap("enforced.topic.name.AAF")).thenReturn(topicPrefix);
520 when(eventsService.isCadiEnabled()).thenReturn(true);
521 request.addUserRole("org.onap.dmaap.mr.topic|:topic." + topicName + "|pub");
522 doThrow(new IOException()).when(publisher)
523 .sendBatchMessageNew(eq(topicName), Mockito.<ArrayList<ProducerRecord<String, String>>>any());
525 thrown.expect(CambriaApiException.class);
526 thrown.expectMessage(containsString(String.valueOf(HttpStatus.SC_NOT_FOUND)));
529 eventsService.pushEvents(dMaapContext, topicName, iStream, "5", "13:00:00");
532 verify(publisher).sendBatchMessageNew(eq(topicName), Mockito.<ArrayList<ProducerRecord<String, String>>>any());
533 verify(eventsService, never()).respondOk(any(DMaaPContext.class), any(JSONObject.class));
537 public void pushEvents_shouldNotPerformAnyAuthorization_whenPublishToMetricTopic() throws Exception {
539 HttpServletRequest permittedRequest = mock(HttpServletRequest.class);
540 when(permittedRequest.getHeaders(anyString())).thenReturn(Collections.<String>emptyEnumeration());
541 dMaapContext.setRequest(permittedRequest);
542 String metricsTopicName = "msgrtr.apinode.metrics.dmaap";
543 givenConfiguredWithMocks(metricsTopicName);
544 doNothing().when(eventsService).respondOk(eq(dMaapContext), any(JSONObject.class));
547 eventsService.pushEvents(dMaapContext, metricsTopicName, iStream, "5", "13:00:00");
550 ArgumentCaptor<JSONObject> captor = ArgumentCaptor.forClass(JSONObject.class);
552 .sendBatchMessageNew(eq(metricsTopicName), Mockito.<ArrayList<ProducerRecord<String, String>>>any());
553 verify(eventsService).respondOk(eq(dMaapContext), captor.capture());
554 verify(permittedRequest, never()).isUserInRole(anyString());
555 verify(createdTopic, never()).checkUserWrite(any(NsaSimpleApiKey.class));
556 assertEquals(1, captor.getValue().getLong("count"));
560 public void pushEvents_shouldNotPerformAnyAuthorization_whenTopicHasNoOwner() throws Exception {
562 HttpServletRequest permittedRequest = mock(HttpServletRequest.class);
563 when(permittedRequest.getHeaders(anyString())).thenReturn(Collections.<String>emptyEnumeration());
564 dMaapContext.setRequest(permittedRequest);
565 String topicName = "notEnforcedAafTopic";
566 givenConfiguredWithMocks(topicName);
567 doNothing().when(eventsService).respondOk(eq(dMaapContext), any(JSONObject.class));
568 when(createdTopic.getOwner()).thenReturn(null);
571 eventsService.pushEvents(dMaapContext, topicName, iStream, "5", "13:00:00");
574 ArgumentCaptor<JSONObject> captor = ArgumentCaptor.forClass(JSONObject.class);
575 verify(publisher).sendBatchMessageNew(eq(topicName), Mockito.<ArrayList<ProducerRecord<String, String>>>any());
576 verify(eventsService).respondOk(eq(dMaapContext), captor.capture());
577 verify(permittedRequest, never()).isUserInRole(anyString());
578 verify(createdTopic, never()).checkUserWrite(any(NsaSimpleApiKey.class));
579 assertEquals(1, captor.getValue().getLong("count"));