f287e8c5b4ac68d7f094fc8b43525728c1eb1d68
[dmaap/messagerouter/msgrtr.git] / src / test / java / org / onap / dmaap / dmf / mr / service / impl / TopicServiceImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.dmaap.dmf.mr.service.impl;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Matchers.anyBoolean;
28 import static org.mockito.Matchers.anyInt;
29 import static org.mockito.Matchers.anyString;
30 import static org.mockito.Matchers.contains;
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.never;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.verifyZeroInteractions;
37 import static org.mockito.Mockito.when;
38
39 import com.att.nsa.configs.ConfigDbException;
40 import com.att.nsa.security.NsaAcl;
41 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
42 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
43 import java.io.IOException;
44 import java.util.Arrays;
45 import java.util.HashSet;
46 import javax.servlet.ServletOutputStream;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49 import org.json.JSONException;
50 import org.json.JSONObject;
51 import org.junit.Assert;
52 import org.junit.Before;
53 import org.junit.Rule;
54 import org.junit.Test;
55 import org.junit.rules.ExpectedException;
56 import org.junit.runner.RunWith;
57 import org.mockito.Mock;
58 import org.mockito.Spy;
59 import org.mockito.runners.MockitoJUnitRunner;
60 import org.onap.dmaap.dmf.mr.CambriaApiException;
61 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
62 import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;
63 import org.onap.dmaap.dmf.mr.beans.TopicBean;
64 import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;
65 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
66 import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;
67 import org.onap.dmaap.dmf.mr.metabroker.Broker1;
68 import org.onap.dmaap.dmf.mr.metabroker.Topic;
69 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticator;
70 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
71 import sun.security.acl.PrincipalImpl;
72
73
74 @RunWith(MockitoJUnitRunner.class)
75 public class TopicServiceImplTest {
76
77
78     private static final String TOPIC_CREATE_PEM = "org.onap.dmaap.mr.topicFactory|:org.onap.dmaap.mr.topic:org.onap.dmaap.mr|create";
79     private static final String TOPIC_DELETE_PEM = "org.onap.dmaap.mr.topicFactory|:org.onap.dmaap.mr.topic:org.onap.dmaap.mr|destroy";
80     private NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
81     private TopicBean topicBean;
82
83     @Spy
84     private TopicServiceImpl topicService;
85
86     @Mock
87     private DMaaPErrorMessages errorMessages;
88
89     @Mock
90     private DMaaPContext dmaapContext;
91
92     @Mock
93     private ConfigurationReader configReader;
94
95     @Mock
96     private ServletOutputStream oStream;
97
98     @Mock
99     private DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
100
101     @Mock
102     private HttpServletRequest httpServReq;
103
104     @Mock
105     private HttpServletResponse httpServRes;
106
107     @Mock
108     private DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
109
110     @Mock
111     private Topic createdTopic;
112
113     @Mock
114     private NsaAcl nsaAcl;
115
116     @Rule
117     public ExpectedException thrown = ExpectedException.none();
118
119     @Before
120     public void setUp() throws Exception {
121         configureSpyInstance();
122         topicService.setErrorMessages(errorMessages);
123
124         when(dmaapContext.getRequest()).thenReturn(httpServReq);
125     }
126
127     private void configureSpyInstance() throws Exception {
128         doReturn(user).when(topicService).getDmaapAuthenticatedUser(any(DMaaPContext.class));
129         doReturn(dmaapKafkaMetaBroker).when(topicService).getMetaBroker(any(DMaaPContext.class));
130         doNothing().when(topicService).respondOk(any(DMaaPContext.class),anyString());
131         doNothing().when(topicService).respondOk(any(DMaaPContext.class),any(JSONObject.class));
132         when(topicService.getPropertyFromAJSCbean("enforced.topic.name.AAF"))
133             .thenReturn("org.onap.dmaap.mr");
134         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf"))
135             .thenReturn("org.onap.dmaap.mr.topicFactory|:org.onap.dmaap.mr.topic:");
136     }
137
138     private void givenTopicBean(String topicName) {
139         topicBean = new TopicBean();
140         topicBean.setTopicName(topicName);
141     }
142
143
144     @Test
145     public void createTopic_shouldSkipAAFAuthorization_whenCadiIsEnabled_andTopicNameNotEnforced() throws Exception {
146         //given
147         String topicName = "UNAUTHENTICATED.PRH.REGISTRATION";
148         givenTopicBean(topicName);
149
150         when(dmaapKafkaMetaBroker.createTopic(eq(topicName), anyString(), anyString(), anyInt(), anyInt(), anyBoolean()))
151             .thenReturn(createdTopic);
152
153         //when
154         topicService.createTopic(dmaapContext, topicBean);
155
156         //then
157         verify(dmaapKafkaMetaBroker).createTopic(eq(topicName), anyString(), anyString(), anyInt(), anyInt(),
158             anyBoolean());
159         verify(topicService).respondOk(eq(dmaapContext), any(JSONObject.class));
160         verify(httpServReq, never()).isUserInRole(TOPIC_CREATE_PEM);
161     }
162
163     @Test
164     public void createTopic_shouldSkipAAFAuthorization_whenCADIdisabled() throws Exception {
165         //given
166         String topicName = "org.onap.dmaap.mr.topic-2";
167         givenTopicBean(topicName);
168
169         when(dmaapKafkaMetaBroker.createTopic(eq(topicName), anyString(), anyString(), anyInt(), anyInt(), anyBoolean()))
170             .thenReturn(createdTopic);
171
172         //when
173         topicService.createTopic(dmaapContext, topicBean);
174
175         //then
176         verify(dmaapKafkaMetaBroker).createTopic(eq(topicName), anyString(), anyString(), anyInt(), anyInt(),
177             anyBoolean());
178         verify(topicService).respondOk(eq(dmaapContext), any(JSONObject.class));
179         verify(httpServReq, never()).isUserInRole(TOPIC_CREATE_PEM);
180     }
181
182     @Test
183     public void createTopic_shouldPass_whenCADIisDisabled_andNoUserInDmaapContext() throws Exception {
184         //given
185         String topicName = "org.onap.dmaap.mr.topic-3";
186         givenTopicBean(topicName);
187
188         doReturn(null).when(topicService).getDmaapAuthenticatedUser(dmaapContext);
189         when(dmaapKafkaMetaBroker.createTopic(eq(topicName), anyString(), anyString(), anyInt(), anyInt(), anyBoolean()))
190             .thenReturn(createdTopic);
191
192         //when
193         topicService.createTopic(dmaapContext, topicBean);
194
195         //then
196         verify(dmaapKafkaMetaBroker).createTopic(eq(topicName), anyString(), anyString(), anyInt(), anyInt(),
197             anyBoolean());
198         verify(topicService).respondOk(eq(dmaapContext), any(JSONObject.class));
199     }
200
201     @Test
202     public void createTopic_shouldPassWithAAFauthorization_whenCadiIsEnabled_andTopicNameWithEnforcedPrefix() throws Exception {
203         //given
204         String topicName = "org.onap.dmaap.mr.topic-4";
205         givenTopicBean(topicName);
206
207         when(topicService.isCadiEnabled()).thenReturn(true);
208         when(httpServReq.isUserInRole(TOPIC_CREATE_PEM)).thenReturn(true);
209         when(httpServReq.getUserPrincipal()).thenReturn(new PrincipalImpl("user"));
210         when(dmaapKafkaMetaBroker.createTopic(eq(topicName), anyString(), eq("user"), anyInt(), anyInt(), anyBoolean()))
211             .thenReturn(createdTopic);
212
213         //when
214         topicService.createTopic(dmaapContext, topicBean);
215
216         //then
217         verify(httpServReq).isUserInRole(TOPIC_CREATE_PEM);
218         verify(dmaapKafkaMetaBroker).createTopic(eq(topicName), anyString(), eq("user"), anyInt(), anyInt(), anyBoolean());
219         verify(topicService).respondOk(eq(dmaapContext), any(JSONObject.class));
220         verify(topicService, never()).getDmaapAuthenticatedUser(dmaapContext);
221     }
222
223     @Test
224     public void createTopic_shouldFailWithAAFauthorization_whenCadiIsEnabled_andTopicNameWithEnforcedPrefix() throws Exception {
225         //given
226         thrown.expect(DMaaPAccessDeniedException.class);
227
228         String topicName = "org.onap.dmaap.mr.topic-5";
229         givenTopicBean(topicName);
230
231         when(topicService.isCadiEnabled()).thenReturn(true);
232         when(httpServReq.isUserInRole(TOPIC_CREATE_PEM)).thenReturn(false);
233         when(httpServReq.getUserPrincipal()).thenReturn(new PrincipalImpl("user"));
234
235         when(dmaapKafkaMetaBroker.createTopic(eq(topicName), anyString(), eq("user"), anyInt(), anyInt(), anyBoolean()))
236             .thenReturn(createdTopic);
237
238         //when
239         topicService.createTopic(dmaapContext, topicBean);
240
241         //then
242         verify(httpServReq).isUserInRole(TOPIC_CREATE_PEM);
243         verify(topicService, never()).getDmaapAuthenticatedUser(dmaapContext);
244         verifyZeroInteractions(dmaapKafkaMetaBroker);
245         verifyZeroInteractions(createdTopic);
246     }
247
248     @Test
249     public void createTopic_shouldThrowApiException_whenBrokerThrowsConfigDbException() throws Exception {
250         //given
251         thrown.expect(CambriaApiException.class);
252
253         String topicName = "org.onap.dmaap.mr.topic-6";
254         givenTopicBean(topicName);
255
256         when(dmaapKafkaMetaBroker.createTopic(eq(topicName), anyString(), anyString(), anyInt(), anyInt(), anyBoolean()))
257             .thenThrow(new ConfigDbException("fail"));
258
259         //when
260         topicService.createTopic(dmaapContext, topicBean);
261
262         //then
263         verifyZeroInteractions(createdTopic);
264     }
265
266     @Test
267     public void createTopic_shouldFailGracefully_whenTopicExistsExceptionOccurs() throws Exception {
268         //given
269         String topicName = "org.onap.dmaap.mr.topic-7";
270         givenTopicBean(topicName);
271
272         when(dmaapKafkaMetaBroker.createTopic(eq(topicName), anyString(), anyString(), anyInt(), anyInt(), anyBoolean()))
273             .thenThrow(new Broker1.TopicExistsException("enfTopicNamePlusExtra"));
274
275         //when
276         topicService.createTopic(dmaapContext, topicBean);
277
278         //then
279         verifyZeroInteractions(createdTopic);
280     }
281
282     @Test
283     public void getValueOrDefault_shouldParseDeafultAndReturnIt_whenGivenValueIsZero() {
284         //given
285         int value = 0;
286         String defaultPropertyName = "propertyName";
287         when(topicService.getPropertyFromAJSCmap(defaultPropertyName)).thenReturn("6");
288
289         //when
290         int extracted = topicService.getValueOrDefault(value, defaultPropertyName);
291
292         //then
293         assertEquals(6, extracted);
294     }
295
296     @Test
297     public void getValueOrDefault_shouldReturnGivenValue_whenGreaterThanZero() {
298         //given
299         int value = 3;
300         String defaultPropertyName = "propertyName";
301
302         //when
303         int extracted = topicService.getValueOrDefault(value, defaultPropertyName);
304
305         //then
306         assertEquals(value, extracted);
307         verify(topicService, never()).getPropertyFromAJSCmap(defaultPropertyName);
308     }
309
310     @Test
311     public void getValueOrDefault_shouldParseDeafultAndReturnIt_whenGivenValueIsNegative() {
312         //given
313         int value = -3;
314         String defaultPropertyName = "propertyName";
315         when(topicService.getPropertyFromAJSCmap(defaultPropertyName)).thenReturn("6");
316
317         //when
318         int extracted = topicService.getValueOrDefault(value, defaultPropertyName);
319
320         //then
321         assertEquals(6, extracted);
322     }
323
324     @Test
325     public void getValueOrDefault_shouldReturnOne_whenGivenValueIsZero_andDefaultNotProvided() {
326         //given
327         int value = 0;
328         String defaultPropertyName = "propertyName";
329         when(topicService.getPropertyFromAJSCmap(defaultPropertyName)).thenReturn("");
330
331         //when
332         int extracted = topicService.getValueOrDefault(value, defaultPropertyName);
333
334         //then
335         assertEquals(1, extracted);
336     }
337
338     @Test
339     public void getValueOrDefault_shouldReturnOne_whenGivenValueIsZero_andDefaultNaN() {
340         //given
341         int value = 0;
342         String defaultPropertyName = "propertyName";
343         when(topicService.getPropertyFromAJSCmap(defaultPropertyName)).thenReturn("a");
344
345         //when
346         int extracted = topicService.getValueOrDefault(value, defaultPropertyName);
347
348         //then
349         assertEquals(1, extracted);
350     }
351
352     @Test(expected = TopicExistsException.class)
353     public void testGetTopics_null_topic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
354         TopicExistsException, JSONException, ConfigDbException {
355
356         Assert.assertNotNull(topicService);
357         //PowerMockito.mockStatic(DMaaPResponseBuilder.class);
358         when(dmaapContext.getConfigReader()).thenReturn(configReader);
359         when(dmaapContext.getRequest()).thenReturn(httpServReq);
360         when(dmaapContext.getResponse()).thenReturn(httpServRes);
361         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
362         when(httpServReq.getMethod()).thenReturn("HEAD");
363
364         when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
365
366         topicService.getTopic(dmaapContext, "topicName");
367     }
368
369     @Test
370     public void testGetTopics_NonNull_topic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
371         TopicExistsException, JSONException, ConfigDbException {
372
373         Assert.assertNotNull(topicService);
374         //PowerMockito.mockStatic(DMaaPResponseBuilder.class);
375         when(dmaapContext.getConfigReader()).thenReturn(configReader);
376         when(dmaapContext.getRequest()).thenReturn(httpServReq);
377         when(dmaapContext.getResponse()).thenReturn(httpServRes);
378         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
379
380         when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(createdTopic);
381
382         when(createdTopic.getName()).thenReturn("topicName");
383         when(createdTopic.getDescription()).thenReturn("topicDescription");
384         when(createdTopic.getOwners()).thenReturn(new HashSet<>(Arrays.asList("user1,user2".split(","))));
385
386         when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);
387         when(createdTopic.getWriterAcl()).thenReturn(nsaAcl);
388
389         when(httpServReq.getMethod()).thenReturn("HEAD");
390
391         when(httpServRes.getOutputStream()).thenReturn(oStream);
392
393         topicService.getTopic(dmaapContext, "topicName");
394     }
395
396     @Test(expected = TopicExistsException.class)
397     public void testGetPublishersByTopicName_nullTopic() throws DMaaPAccessDeniedException, CambriaApiException,
398         IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
399
400         Assert.assertNotNull(topicService);
401
402         //PowerMockito.mockStatic(DMaaPResponseBuilder.class);
403         when(dmaapContext.getConfigReader()).thenReturn(configReader);
404         when(dmaapContext.getRequest()).thenReturn(httpServReq);
405         when(dmaapContext.getResponse()).thenReturn(httpServRes);
406         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
407         when(httpServReq.getMethod()).thenReturn("HEAD");
408
409         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(null);
410
411         topicService.getPublishersByTopicName(dmaapContext, "topicNamespace.name");
412
413     }
414
415     @Test
416     public void testGetPublishersByTopicName_nonNullTopic() throws DMaaPAccessDeniedException, CambriaApiException,
417         IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
418
419         Assert.assertNotNull(topicService);
420
421         //PowerMockito.mockStatic(DMaaPResponseBuilder.class);
422         when(dmaapContext.getConfigReader()).thenReturn(configReader);
423         when(dmaapContext.getRequest()).thenReturn(httpServReq);
424         when(dmaapContext.getResponse()).thenReturn(httpServRes);
425         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
426         when(httpServReq.getMethod()).thenReturn("HEAD");
427
428         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(createdTopic);
429         when(createdTopic.getWriterAcl()).thenReturn(nsaAcl);
430         topicService.getPublishersByTopicName(dmaapContext, "topicNamespace.name");
431     }
432
433     @Test(expected = TopicExistsException.class)
434     public void testGetConsumersByTopicName_nullTopic() throws DMaaPAccessDeniedException, CambriaApiException,
435         IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
436
437         Assert.assertNotNull(topicService);
438
439         //PowerMockito.mockStatic(DMaaPResponseBuilder.class);
440         when(dmaapContext.getConfigReader()).thenReturn(configReader);
441         when(dmaapContext.getRequest()).thenReturn(httpServReq);
442         when(dmaapContext.getResponse()).thenReturn(httpServRes);
443         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
444         when(httpServReq.getMethod()).thenReturn("HEAD");
445
446         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(null);
447
448         topicService.getConsumersByTopicName(dmaapContext, "topicNamespace.name");
449
450     }
451
452     @Test
453     public void testGetConsumersByTopicName_nonNullTopic() throws DMaaPAccessDeniedException, CambriaApiException,
454         IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
455
456         Assert.assertNotNull(topicService);
457
458         //PowerMockito.mockStatic(DMaaPResponseBuilder.class);
459         when(dmaapContext.getConfigReader()).thenReturn(configReader);
460         when(dmaapContext.getRequest()).thenReturn(httpServReq);
461         when(dmaapContext.getResponse()).thenReturn(httpServRes);
462         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
463         when(httpServReq.getMethod()).thenReturn("HEAD");
464
465         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(createdTopic);
466
467         when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);
468
469         topicService.getConsumersByTopicName(dmaapContext, "topicNamespace.name");
470     }
471
472     @Test
473     public void testGetPublishersByTopicName() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
474         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
475
476         Assert.assertNotNull(topicService);
477
478         //PowerMockito.mockStatic(DMaaPResponseBuilder.class);
479
480         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
481
482         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("topicFactoryAAF");
483
484         when(dmaapContext.getConfigReader()).thenReturn(configReader);
485         when(dmaapContext.getRequest()).thenReturn(httpServReq);
486         when(dmaapContext.getResponse()).thenReturn(httpServRes);
487         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
488         when(httpServReq.getMethod()).thenReturn("HEAD");
489
490         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(createdTopic);
491
492         when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);
493
494         topicService.getPublishersByTopicName(dmaapContext, "topicNamespace.name");
495     }
496
497     @Test(expected = TopicExistsException.class)
498     public void testGetPublishersByTopicNameError() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
499         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
500
501         Assert.assertNotNull(topicService);
502
503         //PowerMockito.mockStatic(DMaaPResponseBuilder.class);
504
505         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
506
507         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("topicFactoryAAF");
508
509         when(dmaapContext.getConfigReader()).thenReturn(configReader);
510         when(dmaapContext.getRequest()).thenReturn(httpServReq);
511         when(dmaapContext.getResponse()).thenReturn(httpServRes);
512         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
513         when(httpServReq.getMethod()).thenReturn("HEAD");
514
515         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(null);
516
517         when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);
518
519         topicService.getPublishersByTopicName(dmaapContext, "topicNamespace.name");
520     }
521
522     @Test
523     public void deleteTopic_shouldDeleteTopic_whenUserAuthorizedWithAAF_andTopicExists() throws Exception {
524         //given
525         String topicName = "org.onap.dmaap.mr.topic-9";
526         when(topicService.isCadiEnabled()).thenReturn(true);
527         when(httpServReq.isUserInRole(TOPIC_DELETE_PEM)).thenReturn(true);
528         when(dmaapKafkaMetaBroker.getTopic(topicName)).thenReturn(createdTopic);
529
530         //when
531         topicService.deleteTopic(dmaapContext, topicName);
532
533         //then
534         verify(httpServReq).isUserInRole(TOPIC_DELETE_PEM);
535         verify(topicService).respondOk(eq(dmaapContext), contains(topicName));
536         verify(topicService, never()).getDmaapAuthenticatedUser(dmaapContext);
537     }
538
539     @Test
540     public void deleteTopic_shouldSkipAAFauthorization_whenTopicNameNotEnforced() throws Exception {
541         //given
542         String topicName = "UNAUTHENTICATED.PRH.READY";
543         when(topicService.isCadiEnabled()).thenReturn(true);
544         when(dmaapKafkaMetaBroker.getTopic(topicName)).thenReturn(createdTopic);
545
546         //when
547         topicService.deleteTopic(dmaapContext, topicName);
548
549         //then
550         verify(httpServReq, never()).isUserInRole(TOPIC_DELETE_PEM);
551         verify(topicService).respondOk(eq(dmaapContext), contains(topicName));
552     }
553
554     @Test
555     public void deleteTopic_shouldDeleteTopic_whenUserAuthorizedInContext_andTopicExists() throws Exception {
556         //given
557         String topicName = "org.onap.dmaap.mr.topic-10";
558         when(dmaapKafkaMetaBroker.getTopic(topicName)).thenReturn(createdTopic);
559
560         //when
561         topicService.deleteTopic(dmaapContext, topicName);
562
563         //then
564         verify(httpServReq, never()).isUserInRole(TOPIC_DELETE_PEM);
565         verify(topicService).respondOk(eq(dmaapContext), contains(topicName));
566     }
567
568     @Test
569     public void deleteTopic_shouldNotDeleteTopic_whenUserNotAuthorizedByAAF() throws Exception {
570         //given
571         String topicName = "org.onap.dmaap.mr.topic-10";
572         thrown.expect(DMaaPAccessDeniedException.class);
573
574         when(topicService.isCadiEnabled()).thenReturn(true);
575         when(httpServReq.isUserInRole(TOPIC_DELETE_PEM)).thenReturn(false);
576         when(dmaapKafkaMetaBroker.getTopic(topicName)).thenReturn(createdTopic);
577
578         //when
579         topicService.deleteTopic(dmaapContext, topicName);
580
581         //then
582         verify(httpServReq).isUserInRole(TOPIC_DELETE_PEM);
583         verify(topicService, never()).respondOk(eq(dmaapContext), anyString());
584         verify(topicService, never()).getDmaapAuthenticatedUser(dmaapContext);
585     }
586
587     @Test
588     public void deleteTopic_shouldNotDeleteTopic_whenTopicDoesNotExist() throws Exception {
589         //given
590         String topicName = "org.onap.dmaap.mr.topic-10";
591         thrown.expect(TopicExistsException.class);
592
593         when(dmaapKafkaMetaBroker.getTopic(topicName)).thenReturn(null);
594
595         //when
596         topicService.deleteTopic(dmaapContext, topicName);
597
598         //then
599         verify(topicService, never()).respondOk(eq(dmaapContext), anyString());
600     }
601
602     @Test
603     public void testPermitConsumerForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
604         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
605
606         Assert.assertNotNull(topicService);
607
608         // PowerMockito.mockStatic(AJSCPropertiesMap.class);
609         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
610         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("hello");
611
612         when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
613         when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
614         when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
615         when(dmaapContext.getRequest()).thenReturn(httpServReq);
616
617         when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
618         when(dmaapContext.getConfigReader()).thenReturn(configReader);
619         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
620         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(createdTopic);
621         TopicBean topicBean = new TopicBean();
622         topicBean.setTopicName("enfTopicNamePlusExtra");
623
624         topicService.permitConsumerForTopic(dmaapContext, "topicNamespace.topic", "admin");
625     }
626
627     @Test(expected = TopicExistsException.class)
628     public void testPermitConsumerForTopic_nulltopic()
629         throws DMaaPAccessDeniedException, CambriaApiException, IOException,
630         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
631
632         Assert.assertNotNull(topicService);
633
634         // PowerMockito.mockStatic(AJSCPropertiesMap.class);
635         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
636         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("hello");
637
638         when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
639         when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
640         when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
641         when(dmaapContext.getRequest()).thenReturn(httpServReq);
642
643         when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
644         when(dmaapContext.getConfigReader()).thenReturn(configReader);
645         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
646         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(null);
647         TopicBean topicBean = new TopicBean();
648         topicBean.setTopicName("enfTopicNamePlusExtra");
649
650         topicService.permitConsumerForTopic(dmaapContext, "topicNamespace.topic", "admin");
651     }
652
653     @Test
654     public void testdenyConsumerForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
655         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
656
657         Assert.assertNotNull(topicService);
658
659         // PowerMockito.mockStatic(AJSCPropertiesMap.class);
660         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
661         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("hello");
662
663         when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
664         when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
665         when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
666         when(dmaapContext.getRequest()).thenReturn(httpServReq);
667
668         when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
669         when(dmaapContext.getConfigReader()).thenReturn(configReader);
670         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
671         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(createdTopic);
672         TopicBean topicBean = new TopicBean();
673         topicBean.setTopicName("enfTopicNamePlusExtra");
674
675         topicService.denyConsumerForTopic(dmaapContext, "topicNamespace.topic", "admin");
676     }
677
678     @Test(expected = TopicExistsException.class)
679     public void testdenyConsumerForTopic_nulltopic()
680         throws DMaaPAccessDeniedException, CambriaApiException, IOException,
681         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
682
683         Assert.assertNotNull(topicService);
684
685         // PowerMockito.mockStatic(AJSCPropertiesMap.class);
686         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
687         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("hello");
688
689         when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
690         when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
691         when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
692         when(dmaapContext.getRequest()).thenReturn(httpServReq);
693
694         when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
695         when(dmaapContext.getConfigReader()).thenReturn(configReader);
696         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
697         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(null);
698         TopicBean topicBean = new TopicBean();
699         topicBean.setTopicName("enfTopicNamePlusExtra");
700
701         topicService.denyConsumerForTopic(dmaapContext, "topicNamespace.topic", "admin");
702     }
703
704
705     @Test
706     public void testPermitPublisherForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
707         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
708
709         Assert.assertNotNull(topicService);
710
711         // PowerMockito.mockStatic(AJSCPropertiesMap.class);
712         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
713         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("hello");
714
715         when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
716         when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
717         when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
718         when(dmaapContext.getRequest()).thenReturn(httpServReq);
719
720         when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
721         when(dmaapContext.getConfigReader()).thenReturn(configReader);
722         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
723         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(createdTopic);
724         TopicBean topicBean = new TopicBean();
725         topicBean.setTopicName("enfTopicNamePlusExtra");
726
727         topicService.permitPublisherForTopic(dmaapContext, "topicNamespace.topic", "admin");
728     }
729
730     @Test(expected = TopicExistsException.class)
731     public void testPermitPublisherForTopic_nulltopic()
732         throws DMaaPAccessDeniedException, CambriaApiException, IOException,
733         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
734
735         Assert.assertNotNull(topicService);
736
737         // PowerMockito.mockStatic(AJSCPropertiesMap.class);
738         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
739         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("hello");
740
741         when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
742         when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
743         when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
744         when(dmaapContext.getRequest()).thenReturn(httpServReq);
745
746         when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
747         when(dmaapContext.getConfigReader()).thenReturn(configReader);
748         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
749         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(null);
750         TopicBean topicBean = new TopicBean();
751         topicBean.setTopicName("enfTopicNamePlusExtra");
752
753         topicService.permitPublisherForTopic(dmaapContext, "topicNamespace.topic", "admin");
754     }
755
756     @Test
757     public void testDenyPublisherForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
758         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
759
760         Assert.assertNotNull(topicService);
761
762         // PowerMockito.mockStatic(AJSCPropertiesMap.class);
763         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
764         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("hello");
765
766         when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
767         when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
768         when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
769         when(dmaapContext.getRequest()).thenReturn(httpServReq);
770         when(dmaapContext.getResponse()).thenReturn(httpServRes);
771
772         when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
773         when(dmaapContext.getConfigReader()).thenReturn(configReader);
774         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
775         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(createdTopic);
776         TopicBean topicBean = new TopicBean();
777         topicBean.setTopicName("enfTopicNamePlusExtra");
778
779         topicService.denyPublisherForTopic(dmaapContext, "topicNamespace.topic", "admin");
780         ;
781     }
782
783     @Test(expected = TopicExistsException.class)
784     public void testDenyPublisherForTopic_nulltopic()
785         throws DMaaPAccessDeniedException, CambriaApiException, IOException,
786         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
787
788         Assert.assertNotNull(topicService);
789
790         // PowerMockito.mockStatic(AJSCPropertiesMap.class);
791         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
792         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("hello");
793
794         when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
795         when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
796         when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
797         when(dmaapContext.getRequest()).thenReturn(httpServReq);
798
799         when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
800         when(dmaapContext.getConfigReader()).thenReturn(configReader);
801         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
802         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(null);
803         TopicBean topicBean = new TopicBean();
804         topicBean.setTopicName("enfTopicNamePlusExtra");
805
806         topicService.denyPublisherForTopic(dmaapContext, "topicNamespace.topic", "admin");
807         ;
808     }
809
810     @Test
811     public void testGetAllTopics() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
812         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
813
814         Assert.assertNotNull(topicService);
815
816         // PowerMockito.mockStatic(AJSCPropertiesMap.class);
817         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
818         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("hello");
819         //PowerMockito.mockStatic(DMaaPResponseBuilder.class);
820         when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
821         when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
822         when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
823         when(dmaapContext.getRequest()).thenReturn(httpServReq);
824
825         when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
826         when(dmaapContext.getConfigReader()).thenReturn(configReader);
827         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
828         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(createdTopic);
829         TopicBean topicBean = new TopicBean();
830         topicBean.setTopicName("enfTopicNamePlusExtra");
831
832         topicService.getAllTopics(dmaapContext);
833     }
834
835     @Test
836     public void testGetTopics() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
837         TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {
838
839         Assert.assertNotNull(topicService);
840
841         // PowerMockito.mockStatic(AJSCPropertiesMap.class);
842         //PowerMockito.mockStatic(AJSCPropertiesMap.class);
843         when(topicService.getPropertyFromAJSCmap("msgRtr.topicfactory.aaf")).thenReturn("hello");
844         //PowerMockito.mockStatic(DMaaPResponseBuilder.class);
845         when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);
846         when(httpServReq.getHeader("AppName")).thenReturn("MyApp");
847         when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
848         when(dmaapContext.getRequest()).thenReturn(httpServReq);
849
850         when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
851         when(dmaapContext.getConfigReader()).thenReturn(configReader);
852         when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
853         when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(createdTopic);
854         TopicBean topicBean = new TopicBean();
855         topicBean.setTopicName("enfTopicNamePlusExtra");
856
857         topicService.getTopics(dmaapContext);
858     }
859
860
861 }