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