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