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