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
21 package com.att.nsa.dmaap.service;
\r
23 import static org.junit.Assert.*;
\r
25 import static org.mockito.Matchers.anyString;
\r
26 import static org.mockito.Mockito.when;
\r
28 import java.io.IOException;
\r
29 import javax.servlet.ServletOutputStream;
\r
30 import javax.servlet.http.HttpServletRequest;
\r
31 import javax.servlet.http.HttpServletResponse;
\r
33 import org.junit.After;
\r
34 import org.json.JSONArray;
\r
35 import org.json.JSONException;
\r
36 import org.json.JSONObject;
\r
37 import org.junit.Assert;
\r
38 import org.junit.Before;
\r
39 import org.junit.Test;
\r
41 import org.junit.runner.RunWith;
\r
42 import org.mockito.InjectMocks;
\r
43 import org.mockito.Mock;
\r
44 import org.mockito.MockitoAnnotations;
\r
45 import org.powermock.api.mockito.PowerMockito;
\r
46 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
47 import org.powermock.modules.junit4.PowerMockRunner;
\r
49 import com.att.ajsc.beans.PropertiesMapBean;
\r
50 import com.att.nsa.cambria.CambriaApiException;
\r
51 import com.att.nsa.cambria.beans.DMaaPContext;
\r
52 import com.att.nsa.cambria.beans.DMaaPKafkaMetaBroker;
\r
53 import com.att.nsa.cambria.beans.TopicBean;
\r
54 import com.att.nsa.cambria.constants.CambriaConstants;
\r
55 import com.att.nsa.cambria.exception.DMaaPAccessDeniedException;
\r
56 import com.att.nsa.cambria.exception.DMaaPErrorMessages;
\r
57 import com.att.nsa.cambria.metabroker.Broker.TopicExistsException;
\r
58 import com.att.nsa.cambria.metabroker.Topic;
\r
59 import com.att.nsa.cambria.security.DMaaPAAFAuthenticator;
\r
60 import com.att.nsa.cambria.security.DMaaPAuthenticator;
\r
61 import com.att.nsa.cambria.service.TopicService;
\r
62 import com.att.nsa.cambria.utils.ConfigurationReader;
\r
63 import com.att.nsa.cambria.utils.DMaaPResponseBuilder;
\r
64 import com.att.nsa.configs.ConfigDbException;
\r
65 import com.att.nsa.security.NsaAcl;
\r
66 import com.att.nsa.security.NsaApiKey;
\r
67 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
\r
68 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
\r
70 //@RunWith(MockitoJUnitRunner.class)
\r
71 @RunWith(PowerMockRunner.class)
\r
72 @PrepareForTest({ PropertiesMapBean.class })
\r
73 public class TopicRestServiceTest {
\r
76 TopicRestService topicService;
\r
79 private TopicService tService;
\r
81 private TopicRestService service = new TopicRestService();
\r
83 private DMaaPErrorMessages errorMessages;
\r
86 DMaaPContext dmaapContext;
\r
89 ConfigurationReader configReader;
\r
92 ServletOutputStream oStream;
\r
95 DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
\r
98 DMaaPAAFAuthenticator dmaapAAFauthenticator;
\r
103 NsaSimpleApiKey nsaSimpleApiKey;
\r
106 HttpServletRequest httpServReq;
\r
109 HttpServletResponse httpServRes;
\r
112 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
\r
115 Topic createdTopic;
\r
121 JSONObject jsonObj;
\r
124 JSONArray jsonArray;
\r
127 public void setUp() throws Exception {
\r
129 MockitoAnnotations.initMocks(this);
\r
133 public void tearDown() throws Exception {
\r
136 @Test(expected = DMaaPAccessDeniedException.class)
\r
137 public void testGetTopics() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
138 TopicExistsException, JSONException, ConfigDbException {
\r
140 Assert.assertNotNull(topicService);
\r
142 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
145 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))
\r
146 .thenReturn("namespace");
\r
148 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
149 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
150 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
151 when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
\r
153 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
154 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
155 when(httpServReq.getMethod()).thenReturn("HEAD");
\r
157 when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
\r
159 topicService.getTopics();
\r
163 public void testGetTopics_nullAuth() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
164 TopicExistsException, JSONException, ConfigDbException {
\r
166 Assert.assertNotNull(topicService);
\r
168 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
171 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))
\r
172 .thenReturn("namespace");
\r
174 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
175 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
176 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
177 when(httpServReq.getHeader("Authorization")).thenReturn(null);
\r
179 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
180 String perms = "namespace" + "|" + "*" + "|" + "view";
\r
181 when(dmaapAAFauthenticator.aafAuthentication(httpServReq, perms)).thenReturn(true);
\r
183 when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
\r
185 topicService.getTopics();
\r
188 @Test(expected = DMaaPAccessDeniedException.class)
\r
189 public void testGetAllTopics() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
190 TopicExistsException, JSONException, ConfigDbException {
\r
192 Assert.assertNotNull(topicService);
\r
194 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
197 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))
\r
198 .thenReturn("namespace");
\r
200 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
201 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
202 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
203 when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
\r
205 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
207 topicService.getAllTopics();
\r
211 public void testGetAllTopics_nullAuth() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
212 TopicExistsException, JSONException, ConfigDbException {
\r
214 Assert.assertNotNull(topicService);
\r
215 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
218 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))
\r
219 .thenReturn("namespace");
\r
221 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
222 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
223 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
224 when(httpServReq.getHeader("Authorization")).thenReturn(null);
\r
226 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
228 topicService.getAllTopics();
\r
231 @Test(expected = DMaaPAccessDeniedException.class)
\r
232 public void testGetTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
233 TopicExistsException, JSONException, ConfigDbException {
\r
235 Assert.assertNotNull(topicService);
\r
237 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
240 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))
\r
241 .thenReturn("enfTopicName");
\r
243 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
244 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
245 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
246 when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
\r
248 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
250 topicService.getTopic("topicName");
\r
254 public void testGetTopic_nullAuth() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
255 TopicExistsException, JSONException, ConfigDbException {
\r
257 Assert.assertNotNull(topicService);
\r
259 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
262 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))
\r
263 .thenReturn("enfTopicName");
\r
265 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
266 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
267 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
268 when(httpServReq.getHeader("Authorization")).thenReturn(null);
\r
270 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
272 topicService.getTopic("topicName");
\r
276 public void testCreateTopic()
\r
277 throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
\r
279 Assert.assertNotNull(topicService);
\r
281 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
282 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
\r
283 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
284 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
286 TopicBean topicBean = new TopicBean();
\r
287 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
289 topicService.createTopic(topicBean);
\r
293 public void testDeleteTopic()
\r
294 throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
\r
296 Assert.assertNotNull(topicService);
\r
298 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
299 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
\r
300 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
301 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
303 TopicBean topicBean = new TopicBean();
\r
304 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
306 topicService.deleteTopic("enfTopicNamePlusExtra");
\r
310 public void testGetPublishersByTopicName()
\r
311 throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
\r
313 Assert.assertNotNull(topicService);
\r
315 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
316 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
\r
317 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
318 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
320 TopicBean topicBean = new TopicBean();
\r
321 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
323 topicService.getPublishersByTopicName("enfTopicNamePlusExtra");
\r
327 public void testPermitPublisherForTopic()
\r
328 throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
\r
330 Assert.assertNotNull(topicService);
\r
332 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
333 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
\r
334 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
335 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
337 TopicBean topicBean = new TopicBean();
\r
338 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
340 topicService.permitPublisherForTopic("enfTopicNamePlusExtra", "producerID");
\r
344 public void testDenyPublisherForTopic()
\r
345 throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
\r
347 Assert.assertNotNull(topicService);
\r
349 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
350 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
\r
351 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
352 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
354 TopicBean topicBean = new TopicBean();
\r
355 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
357 topicService.denyPublisherForTopic("enfTopicNamePlusExtra", "producerID");
\r
361 public void testGetConsumersByTopicName() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
362 TopicExistsException, AccessDeniedException {
\r
364 Assert.assertNotNull(topicService);
\r
366 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
367 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
\r
368 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
369 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
371 TopicBean topicBean = new TopicBean();
\r
372 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
374 topicService.getConsumersByTopicName("enfTopicNamePlusExtra");
\r
378 public void testPermitConsumerForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
379 TopicExistsException, AccessDeniedException {
\r
381 Assert.assertNotNull(topicService);
\r
383 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
384 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
\r
385 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
386 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
388 TopicBean topicBean = new TopicBean();
\r
389 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
391 topicService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID");
\r
395 public void testPermitConsumerForTopicWithException() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
396 TopicExistsException, AccessDeniedException {
\r
398 Assert.assertNotNull(topicService);
\r
400 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
401 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
\r
402 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
403 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
405 TopicBean topicBean = new TopicBean();
\r
406 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
408 topicService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID");
\r
412 public void testDenyConsumerForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
413 TopicExistsException, AccessDeniedException {
\r
415 Assert.assertNotNull(topicService);
\r
417 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
418 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
\r
419 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
420 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
422 TopicBean topicBean = new TopicBean();
\r
423 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
425 topicService.denyConsumerForTopic("enfTopicNamePlusExtra", "consumerID");
\r