5c1ce9785e40944e76826be06a631c9ac06438d0
[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(expected = DMaaPAccessDeniedException.class)\r
189         public void testGetAllTopics() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
190                         TopicExistsException, JSONException, ConfigDbException {\r
191 \r
192                 Assert.assertNotNull(topicService);\r
193 \r
194                 PowerMockito.mockStatic(PropertiesMapBean.class);\r
195 \r
196                 assertTrue(true);\r
197                 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))\r
198                                 .thenReturn("namespace");\r
199 \r
200                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
201                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
202                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
203                 when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");\r
204 \r
205                 when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
206 \r
207                 topicService.getAllTopics();\r
208         }\r
209 \r
210         @Test\r
211         public void testGetAllTopics_nullAuth() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
212                         TopicExistsException, JSONException, ConfigDbException {\r
213 \r
214                 Assert.assertNotNull(topicService);\r
215                 PowerMockito.mockStatic(PropertiesMapBean.class);\r
216 \r
217                 assertTrue(true);\r
218                 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))\r
219                                 .thenReturn("namespace");\r
220 \r
221                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
222                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
223                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
224                 when(httpServReq.getHeader("Authorization")).thenReturn(null);\r
225 \r
226                 when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
227 \r
228                 topicService.getAllTopics();\r
229         }\r
230 \r
231         @Test(expected = DMaaPAccessDeniedException.class)\r
232         public void testGetTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
233                         TopicExistsException, JSONException, ConfigDbException {\r
234 \r
235                 Assert.assertNotNull(topicService);\r
236 \r
237                 PowerMockito.mockStatic(PropertiesMapBean.class);\r
238 \r
239                 assertTrue(true);\r
240                 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))\r
241                                 .thenReturn("enfTopicName");\r
242 \r
243                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
244                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
245                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
246                 when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");\r
247 \r
248                 when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
249 \r
250                 topicService.getTopic("topicName");\r
251         }\r
252 \r
253         @Test\r
254         public void testGetTopic_nullAuth() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
255                         TopicExistsException, JSONException, ConfigDbException {\r
256 \r
257                 Assert.assertNotNull(topicService);\r
258 \r
259                 PowerMockito.mockStatic(PropertiesMapBean.class);\r
260 \r
261                 assertTrue(true);\r
262                 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))\r
263                                 .thenReturn("enfTopicName");\r
264 \r
265                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
266                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
267                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
268                 when(httpServReq.getHeader("Authorization")).thenReturn(null);\r
269 \r
270                 when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
271 \r
272                 topicService.getTopic("topicName");\r
273         }\r
274 \r
275         @Test\r
276         public void testCreateTopic()\r
277                         throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
278 \r
279                 Assert.assertNotNull(topicService);\r
280 \r
281                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
282                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
283                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
284                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
285 \r
286                 TopicBean topicBean = new TopicBean();\r
287                 topicBean.setTopicName("enfTopicNamePlusExtra");\r
288 \r
289                 topicService.createTopic(topicBean);\r
290         }\r
291 \r
292         @Test\r
293         public void testDeleteTopic()\r
294                         throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
295 \r
296                 Assert.assertNotNull(topicService);\r
297 \r
298                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
299                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
300                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
301                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
302 \r
303                 TopicBean topicBean = new TopicBean();\r
304                 topicBean.setTopicName("enfTopicNamePlusExtra");\r
305 \r
306                 topicService.deleteTopic("enfTopicNamePlusExtra");\r
307         }\r
308 \r
309         @Test\r
310         public void testGetPublishersByTopicName()\r
311                         throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
312 \r
313                 Assert.assertNotNull(topicService);\r
314 \r
315                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
316                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
317                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
318                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
319 \r
320                 TopicBean topicBean = new TopicBean();\r
321                 topicBean.setTopicName("enfTopicNamePlusExtra");\r
322 \r
323                 topicService.getPublishersByTopicName("enfTopicNamePlusExtra");\r
324         }\r
325 \r
326         @Test\r
327         public void testPermitPublisherForTopic()\r
328                         throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
329 \r
330                 Assert.assertNotNull(topicService);\r
331 \r
332                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
333                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
334                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
335                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
336 \r
337                 TopicBean topicBean = new TopicBean();\r
338                 topicBean.setTopicName("enfTopicNamePlusExtra");\r
339 \r
340                 topicService.permitPublisherForTopic("enfTopicNamePlusExtra", "producerID");\r
341         }\r
342 \r
343         @Test\r
344         public void testDenyPublisherForTopic()\r
345                         throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
346 \r
347                 Assert.assertNotNull(topicService);\r
348 \r
349                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
350                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
351                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
352                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
353 \r
354                 TopicBean topicBean = new TopicBean();\r
355                 topicBean.setTopicName("enfTopicNamePlusExtra");\r
356 \r
357                 topicService.denyPublisherForTopic("enfTopicNamePlusExtra", "producerID");\r
358         }\r
359 \r
360         @Test\r
361         public void testGetConsumersByTopicName() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
362                         TopicExistsException, AccessDeniedException {\r
363 \r
364                 Assert.assertNotNull(topicService);\r
365 \r
366                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
367                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
368                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
369                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
370 \r
371                 TopicBean topicBean = new TopicBean();\r
372                 topicBean.setTopicName("enfTopicNamePlusExtra");\r
373 \r
374                 topicService.getConsumersByTopicName("enfTopicNamePlusExtra");\r
375         }\r
376 \r
377         @Test\r
378         public void testPermitConsumerForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
379                         TopicExistsException, AccessDeniedException {\r
380 \r
381                 Assert.assertNotNull(topicService);\r
382 \r
383                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
384                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
385                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
386                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
387 \r
388                 TopicBean topicBean = new TopicBean();\r
389                 topicBean.setTopicName("enfTopicNamePlusExtra");\r
390 \r
391                 topicService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID");\r
392         }\r
393 \r
394         @Test\r
395         public void testDenyConsumerForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
396                         TopicExistsException, AccessDeniedException {\r
397 \r
398                 Assert.assertNotNull(topicService);\r
399 \r
400                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
401                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
402                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
403                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
404 \r
405                 TopicBean topicBean = new TopicBean();\r
406                 topicBean.setTopicName("enfTopicNamePlusExtra");\r
407 \r
408                 topicService.denyConsumerForTopic("enfTopicNamePlusExtra", "consumerID");\r
409         }\r
410 \r
411 }\r