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