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