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.cambria.service.impl;
\r
23 import static org.mockito.Matchers.anyBoolean;
\r
24 import static org.mockito.Matchers.anyInt;
\r
25 import static org.mockito.Matchers.anyString;
\r
26 import static org.mockito.Mockito.times;
\r
27 import static org.mockito.Mockito.verify;
\r
28 import static org.mockito.Mockito.when;
\r
30 import java.io.IOException;
\r
31 import java.util.Arrays;
\r
32 import java.util.HashSet;
\r
34 import javax.servlet.ServletOutputStream;
\r
35 import javax.servlet.http.HttpServletRequest;
\r
36 import javax.servlet.http.HttpServletResponse;
\r
38 import org.json.JSONArray;
\r
39 import org.json.JSONException;
\r
40 import org.json.JSONObject;
\r
41 import org.junit.Assert;
\r
42 import org.junit.Before;
\r
43 import org.junit.Test;
\r
44 import org.junit.runner.RunWith;
\r
45 import org.mockito.InjectMocks;
\r
46 import org.mockito.Mock;
\r
47 import org.mockito.MockitoAnnotations;
\r
48 import org.powermock.api.mockito.PowerMockito;
\r
49 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
50 import org.powermock.modules.junit4.PowerMockRunner;
\r
52 import com.att.ajsc.beans.PropertiesMapBean;
\r
53 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
\r
54 import com.att.nsa.cambria.CambriaApiException;
\r
55 import com.att.nsa.cambria.beans.DMaaPContext;
\r
56 import com.att.nsa.cambria.beans.DMaaPKafkaMetaBroker;
\r
57 import com.att.nsa.cambria.beans.TopicBean;
\r
58 import com.att.nsa.cambria.constants.CambriaConstants;
\r
59 import com.att.nsa.cambria.exception.DMaaPAccessDeniedException;
\r
60 import com.att.nsa.cambria.exception.DMaaPErrorMessages;
\r
61 import com.att.nsa.cambria.metabroker.Broker.TopicExistsException;
\r
62 import com.att.nsa.cambria.metabroker.Topic;
\r
63 import com.att.nsa.cambria.security.DMaaPAAFAuthenticator;
\r
64 import com.att.nsa.cambria.security.DMaaPAuthenticator;
\r
65 import com.att.nsa.cambria.utils.ConfigurationReader;
\r
66 import com.att.nsa.cambria.utils.DMaaPResponseBuilder;
\r
67 import com.att.nsa.configs.ConfigDbException;
\r
68 import com.att.nsa.security.NsaAcl;
\r
69 import com.att.nsa.security.NsaApiKey;
\r
70 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
\r
71 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
\r
73 //@RunWith(MockitoJUnitRunner.class)
\r
74 @RunWith(PowerMockRunner.class)
\r
75 @PrepareForTest({ PropertiesMapBean.class })
\r
76 public class TopicServiceImplTest22 {
\r
79 TopicServiceImpl topicService;
\r
82 private DMaaPErrorMessages errorMessages;
\r
85 DMaaPContext dmaapContext;
\r
88 ConfigurationReader configReader;
\r
91 ServletOutputStream oStream;
\r
94 DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
\r
97 DMaaPAAFAuthenticator dmaapAAFauthenticator;
\r
102 NsaSimpleApiKey nsaSimpleApiKey;
\r
105 HttpServletRequest httpServReq;
\r
108 HttpServletResponse httpServRes;
\r
111 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
\r
114 Topic createdTopic;
\r
120 JSONObject jsonObj;
\r
123 JSONArray jsonArray;
\r
126 public void setUp() {
\r
127 MockitoAnnotations.initMocks(this);
\r
128 topicService = new TopicServiceImpl();
\r
129 topicService.setErrorMessages(errorMessages);
\r
132 @Test(expected = DMaaPAccessDeniedException.class)
\r
133 public void testCreateTopicWithEnforcedName()
\r
134 throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
\r
136 Assert.assertNotNull(topicService);
\r
138 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
140 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))
\r
141 .thenReturn("enfTopicName");
\r
143 when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
\r
144 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
145 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
\r
146 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
147 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
149 TopicBean topicBean = new TopicBean();
\r
150 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
152 topicService.createTopic(dmaapContext, topicBean);
\r
156 public void testCreateTopicWithTopicNameNotEnforced()
\r
157 throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
\r
159 Assert.assertNotNull(topicService);
\r
161 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
162 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
164 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))
\r
165 .thenReturn("enfTopicName");
\r
167 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
168 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
170 when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
\r
171 when(httpServReq.getMethod()).thenReturn("HEAD");
\r
173 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
\r
174 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
175 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
176 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
178 when(nsaAcl.isActive()).thenReturn(true);
\r
179 when(nsaAcl.getUsers()).thenReturn(new HashSet<>(Arrays.asList("user1,user2".split(","))));
\r
181 when(createdTopic.getName()).thenReturn("topicName");
\r
182 when(createdTopic.getOwner()).thenReturn("Owner");
\r
183 when(createdTopic.getDescription()).thenReturn("Description");
\r
184 when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);
\r
185 when(createdTopic.getWriterAcl()).thenReturn(nsaAcl);
\r
187 when(dmaapKafkaMetaBroker.createTopic(anyString(), anyString(), anyString(), anyInt(), anyInt(), anyBoolean()))
\r
188 .thenReturn(createdTopic);
\r
190 TopicBean topicBean = new TopicBean();
\r
191 topicBean.setTopicName("NotEnforcedTopicName");
\r
193 topicService.createTopic(dmaapContext, topicBean);
\r
195 verify(dmaapKafkaMetaBroker, times(1)).createTopic(anyString(), anyString(), anyString(), anyInt(), anyInt(),
\r
199 @Test(expected = DMaaPAccessDeniedException.class)
\r
200 public void testCreateTopicNoUserInContextAndNoAuthHeader()
\r
201 throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
\r
203 Assert.assertNotNull(topicService);
\r
205 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
207 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))
\r
208 .thenReturn("enfTopicName");
\r
210 when(httpServReq.getHeader("Authorization")).thenReturn(null);
\r
211 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
212 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
\r
214 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
215 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
217 TopicBean topicBean = new TopicBean();
\r
218 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
220 topicService.createTopic(dmaapContext, topicBean);
\r
223 @Test(expected = DMaaPAccessDeniedException.class)
\r
224 public void testCreateTopicNoUserInContextAndAuthHeaderAndPermitted()
\r
225 throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
\r
227 Assert.assertNotNull(topicService);
\r
229 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
231 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))
\r
232 .thenReturn("enfTopicName");
\r
234 when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
\r
235 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
236 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
\r
238 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
239 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
241 // when(dmaapAAFauthenticator.aafAuthentication(httpServReq,
\r
242 // anyString())).thenReturn(false);
\r
244 TopicBean topicBean = new TopicBean();
\r
245 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
247 topicService.createTopic(dmaapContext, topicBean);
\r
250 @Test(expected = TopicExistsException.class)
\r
251 public void testGetTopics_null_topic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
252 TopicExistsException, JSONException, ConfigDbException {
\r
254 Assert.assertNotNull(topicService);
\r
255 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
256 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
257 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
258 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
259 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
260 when(httpServReq.getMethod()).thenReturn("HEAD");
\r
262 when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
\r
264 topicService.getTopic(dmaapContext, "topicName");
\r
268 public void testGetTopics_NonNull_topic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
269 TopicExistsException, JSONException, ConfigDbException {
\r
271 Assert.assertNotNull(topicService);
\r
272 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
273 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
274 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
275 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
276 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
278 when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(createdTopic);
\r
280 when(createdTopic.getName()).thenReturn("topicName");
\r
281 when(createdTopic.getDescription()).thenReturn("topicDescription");
\r
282 when(createdTopic.getOwners()).thenReturn(new HashSet<>(Arrays.asList("user1,user2".split(","))));
\r
284 when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);
\r
285 when(createdTopic.getWriterAcl()).thenReturn(nsaAcl);
\r
287 when(httpServReq.getMethod()).thenReturn("HEAD");
\r
289 when(httpServRes.getOutputStream()).thenReturn(oStream);
\r
291 topicService.getTopic(dmaapContext, "topicName");
\r
294 @Test(expected = TopicExistsException.class)
\r
295 public void testGetPublishersByTopicName_nullTopic() throws DMaaPAccessDeniedException, CambriaApiException,
\r
296 IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
\r
298 Assert.assertNotNull(topicService);
\r
300 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
301 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
302 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
303 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
304 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
305 when(httpServReq.getMethod()).thenReturn("HEAD");
\r
307 when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(null);
\r
309 topicService.getPublishersByTopicName(dmaapContext, "topicNamespace.name");
\r
314 public void testGetPublishersByTopicName_nonNullTopic() throws DMaaPAccessDeniedException, CambriaApiException,
\r
315 IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
\r
317 Assert.assertNotNull(topicService);
\r
319 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
320 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
321 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
322 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
323 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
324 when(httpServReq.getMethod()).thenReturn("HEAD");
\r
326 when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(createdTopic);
\r
327 when(createdTopic.getWriterAcl()).thenReturn(nsaAcl);
\r
328 topicService.getPublishersByTopicName(dmaapContext, "topicNamespace.name");
\r
331 @Test(expected = TopicExistsException.class)
\r
332 public void testGetConsumersByTopicName_nullTopic() throws DMaaPAccessDeniedException, CambriaApiException,
\r
333 IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
\r
335 Assert.assertNotNull(topicService);
\r
337 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
338 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
339 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
340 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
341 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
342 when(httpServReq.getMethod()).thenReturn("HEAD");
\r
344 when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(null);
\r
346 topicService.getConsumersByTopicName(dmaapContext, "topicNamespace.name");
\r
351 public void testGetConsumersByTopicName_nonNullTopic() throws DMaaPAccessDeniedException, CambriaApiException,
\r
352 IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
\r
354 Assert.assertNotNull(topicService);
\r
356 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
357 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
358 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
359 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
360 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
361 when(httpServReq.getMethod()).thenReturn("HEAD");
\r
363 when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(createdTopic);
\r
365 when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);
\r
367 topicService.getConsumersByTopicName(dmaapContext, "topicNamespace.name");
\r
371 // public void testDeleteTopic() throws DMaaPAccessDeniedException, CambriaApiException,
\r
372 // IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
\r
374 // Assert.assertNotNull(topicService);
\r
376 // PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
378 // PowerMockito.mockStatic(AJSCPropertiesMap.class);
\r
380 // PowerMockito.when(AJSCPropertiesMap.
\r
381 // getProperty(CambriaConstants.msgRtr_prop,"msgRtr.topicfactory.aaf")).thenReturn("topicFactoryAAF");
\r
384 // when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
385 // when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
386 // when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
387 // when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
388 // when(httpServReq.getMethod()).thenReturn("HEAD");
\r
390 // when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(createdTopic);
\r
392 // when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);
\r
394 // topicService.getPublishersByTopicName(dmaapContext, "topicNamespace.name");
\r
400 /*@Test public void testdeleteTopic() throws DMaaPAccessDeniedException,
\r
401 CambriaApiException, IOException, TopicExistsException, JSONException,
\r
402 ConfigDbException, AccessDeniedException {
\r
404 Assert.assertNotNull(topicService);
\r
406 //PowerMockito.mockStatic(AJSCPropertiesMap.class);
\r
407 PowerMockito.mockStatic(AJSCPropertiesMap.class);
\r
408 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.
\r
409 msgRtr_prop,"msgRtr.topicfactory.aaf")) .thenReturn("hello");
\r
411 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
\r
412 when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
\r
413 when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
\r
414 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
416 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
\r
417 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
419 TopicBean topicBean = new TopicBean();
\r
420 topicBean.setTopicName("enfTopicNamePlusExtra");
\r
422 topicService.deleteTopic(dmaapContext, "topicNamespace.topic"); }*/
\r