commiting code for test coverage
[dmaap/messagerouter/msgrtr.git] / src / test / java / com / att / nsa / cambria / service / impl / TopicServiceImplTest22.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.cambria.service.impl;\r
22 \r
23 import static org.mockito.Matchers.anyBoolean;\r
24 import static org.mockito.Matchers.anyInt;\r
25 import static org.mockito.Matchers.anyString;\r
26 import static org.mockito.Mockito.times;\r
27 import static org.mockito.Mockito.verify;\r
28 import static org.mockito.Mockito.when;\r
29 \r
30 import java.io.IOException;\r
31 import java.util.Arrays;\r
32 import java.util.HashSet;\r
33 \r
34 import javax.servlet.ServletOutputStream;\r
35 import javax.servlet.http.HttpServletRequest;\r
36 import javax.servlet.http.HttpServletResponse;\r
37 \r
38 import org.json.JSONArray;\r
39 import org.json.JSONException;\r
40 import org.json.JSONObject;\r
41 import org.junit.Assert;\r
42 import org.junit.Before;\r
43 import org.junit.Test;\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 com.att.ajsc.filemonitor.AJSCPropertiesMap;\r
54 import com.att.nsa.cambria.CambriaApiException;\r
55 import com.att.nsa.cambria.beans.DMaaPContext;\r
56 import com.att.nsa.cambria.beans.DMaaPKafkaMetaBroker;\r
57 import com.att.nsa.cambria.beans.TopicBean;\r
58 import com.att.nsa.cambria.constants.CambriaConstants;\r
59 import com.att.nsa.cambria.exception.DMaaPAccessDeniedException;\r
60 import com.att.nsa.cambria.exception.DMaaPErrorMessages;\r
61 import com.att.nsa.cambria.metabroker.Broker.TopicExistsException;\r
62 import com.att.nsa.cambria.metabroker.Topic;\r
63 import com.att.nsa.cambria.security.DMaaPAAFAuthenticator;\r
64 import com.att.nsa.cambria.security.DMaaPAuthenticator;\r
65 import com.att.nsa.cambria.utils.ConfigurationReader;\r
66 import com.att.nsa.cambria.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 TopicServiceImplTest22 {\r
77 \r
78         \r
79         TopicServiceImpl topicService;\r
80 \r
81         @Mock\r
82         private DMaaPErrorMessages errorMessages;\r
83 \r
84         @Mock\r
85         DMaaPContext dmaapContext;\r
86 \r
87         @Mock\r
88         ConfigurationReader configReader;\r
89 \r
90         @Mock\r
91         ServletOutputStream oStream;\r
92 \r
93         @Mock\r
94         DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;\r
95 \r
96         @Mock\r
97         DMaaPAAFAuthenticator dmaapAAFauthenticator;\r
98         @Mock\r
99         NsaApiKey user;\r
100 \r
101         @Mock\r
102         NsaSimpleApiKey nsaSimpleApiKey;\r
103 \r
104         @Mock\r
105         HttpServletRequest httpServReq;\r
106 \r
107         @Mock\r
108         HttpServletResponse httpServRes;\r
109 \r
110         @Mock\r
111         DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;\r
112 \r
113         @Mock\r
114         Topic createdTopic;\r
115 \r
116         @Mock\r
117         NsaAcl nsaAcl;\r
118 \r
119         @Mock\r
120         JSONObject jsonObj;\r
121 \r
122         @Mock\r
123         JSONArray jsonArray;\r
124 \r
125         @Before\r
126         public void setUp() {\r
127                 MockitoAnnotations.initMocks(this);\r
128                 topicService = new TopicServiceImpl();\r
129                 topicService.setErrorMessages(errorMessages);\r
130         }\r
131 \r
132         @Test(expected = DMaaPAccessDeniedException.class)\r
133         public void testCreateTopicWithEnforcedName()\r
134                         throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
135 \r
136                 Assert.assertNotNull(topicService);\r
137 \r
138                 PowerMockito.mockStatic(PropertiesMapBean.class);\r
139 \r
140                 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))\r
141                                 .thenReturn("enfTopicName");\r
142 \r
143                 when(httpServReq.getHeader("AppName")).thenReturn("MyApp");\r
144                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
145                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
146                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
147                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
148 \r
149                 TopicBean topicBean = new TopicBean();\r
150                 topicBean.setTopicName("enfTopicNamePlusExtra");\r
151 \r
152                 topicService.createTopic(dmaapContext, topicBean);\r
153         }\r
154 \r
155         @Test\r
156         public void testCreateTopicWithTopicNameNotEnforced()\r
157                         throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
158 \r
159                 Assert.assertNotNull(topicService);\r
160 \r
161                 PowerMockito.mockStatic(PropertiesMapBean.class);\r
162                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
163 \r
164                 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))\r
165                                 .thenReturn("enfTopicName");\r
166 \r
167                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
168                 when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
169 \r
170                 when(httpServReq.getHeader("AppName")).thenReturn("MyApp");\r
171                 when(httpServReq.getMethod()).thenReturn("HEAD");\r
172 \r
173                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
174                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
175                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
176                 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);\r
177 \r
178                 when(nsaAcl.isActive()).thenReturn(true);\r
179                 when(nsaAcl.getUsers()).thenReturn(new HashSet<>(Arrays.asList("user1,user2".split(","))));\r
180 \r
181                 when(createdTopic.getName()).thenReturn("topicName");\r
182                 when(createdTopic.getOwner()).thenReturn("Owner");\r
183                 when(createdTopic.getDescription()).thenReturn("Description");\r
184                 when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);\r
185                 when(createdTopic.getWriterAcl()).thenReturn(nsaAcl);\r
186 \r
187                 when(dmaapKafkaMetaBroker.createTopic(anyString(), anyString(), anyString(), anyInt(), anyInt(), anyBoolean()))\r
188                                 .thenReturn(createdTopic);\r
189 \r
190                 TopicBean topicBean = new TopicBean();\r
191                 topicBean.setTopicName("NotEnforcedTopicName");\r
192 \r
193                 topicService.createTopic(dmaapContext, topicBean);\r
194 \r
195                 verify(dmaapKafkaMetaBroker, times(1)).createTopic(anyString(), anyString(), anyString(), anyInt(), anyInt(),\r
196                                 anyBoolean());\r
197         }\r
198 \r
199         @Test(expected = DMaaPAccessDeniedException.class)\r
200         public void testCreateTopicNoUserInContextAndNoAuthHeader()\r
201                         throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
202 \r
203                 Assert.assertNotNull(topicService);\r
204 \r
205                 PowerMockito.mockStatic(PropertiesMapBean.class);\r
206 \r
207                 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))\r
208                                 .thenReturn("enfTopicName");\r
209 \r
210                 when(httpServReq.getHeader("Authorization")).thenReturn(null);\r
211                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
212                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);\r
213 \r
214                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
215                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
216 \r
217                 TopicBean topicBean = new TopicBean();\r
218                 topicBean.setTopicName("enfTopicNamePlusExtra");\r
219 \r
220                 topicService.createTopic(dmaapContext, topicBean);\r
221         }\r
222 \r
223         @Test(expected = DMaaPAccessDeniedException.class)\r
224         public void testCreateTopicNoUserInContextAndAuthHeaderAndPermitted()\r
225                         throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
226 \r
227                 Assert.assertNotNull(topicService);\r
228 \r
229                 PowerMockito.mockStatic(PropertiesMapBean.class);\r
230 \r
231                 when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))\r
232                                 .thenReturn("enfTopicName");\r
233 \r
234                 when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");\r
235                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
236                 when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);\r
237 \r
238                 when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
239                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
240 \r
241                 // when(dmaapAAFauthenticator.aafAuthentication(httpServReq,\r
242                 // anyString())).thenReturn(false);\r
243 \r
244                 TopicBean topicBean = new TopicBean();\r
245                 topicBean.setTopicName("enfTopicNamePlusExtra");\r
246 \r
247                 topicService.createTopic(dmaapContext, topicBean);\r
248         }\r
249 \r
250         @Test(expected = TopicExistsException.class)\r
251         public void testGetTopics_null_topic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
252                         TopicExistsException, JSONException, ConfigDbException {\r
253 \r
254                 Assert.assertNotNull(topicService);\r
255                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
256                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
257                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
258                 when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
259                 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);\r
260                 when(httpServReq.getMethod()).thenReturn("HEAD");\r
261 \r
262                 when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);\r
263 \r
264                 topicService.getTopic(dmaapContext, "topicName");\r
265         }\r
266 \r
267         @Test\r
268         public void testGetTopics_NonNull_topic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
269                         TopicExistsException, JSONException, ConfigDbException {\r
270 \r
271                 Assert.assertNotNull(topicService);\r
272                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
273                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
274                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
275                 when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
276                 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);\r
277 \r
278                 when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(createdTopic);\r
279 \r
280                 when(createdTopic.getName()).thenReturn("topicName");\r
281                 when(createdTopic.getDescription()).thenReturn("topicDescription");\r
282                 when(createdTopic.getOwners()).thenReturn(new HashSet<>(Arrays.asList("user1,user2".split(","))));\r
283 \r
284                 when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);\r
285                 when(createdTopic.getWriterAcl()).thenReturn(nsaAcl);\r
286 \r
287                 when(httpServReq.getMethod()).thenReturn("HEAD");\r
288 \r
289                 when(httpServRes.getOutputStream()).thenReturn(oStream);\r
290 \r
291                 topicService.getTopic(dmaapContext, "topicName");\r
292         }\r
293 \r
294         @Test(expected = TopicExistsException.class)\r
295         public void testGetPublishersByTopicName_nullTopic() throws DMaaPAccessDeniedException, CambriaApiException,\r
296                         IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {\r
297 \r
298                 Assert.assertNotNull(topicService);\r
299 \r
300                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
301                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
302                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
303                 when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
304                 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);\r
305                 when(httpServReq.getMethod()).thenReturn("HEAD");\r
306 \r
307                 when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(null);\r
308 \r
309                 topicService.getPublishersByTopicName(dmaapContext, "topicNamespace.name");\r
310 \r
311         }\r
312 \r
313         @Test\r
314         public void testGetPublishersByTopicName_nonNullTopic() throws DMaaPAccessDeniedException, CambriaApiException,\r
315                         IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {\r
316 \r
317                 Assert.assertNotNull(topicService);\r
318 \r
319                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
320                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
321                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
322                 when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
323                 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);\r
324                 when(httpServReq.getMethod()).thenReturn("HEAD");\r
325 \r
326                 when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(createdTopic);\r
327                 when(createdTopic.getWriterAcl()).thenReturn(nsaAcl);\r
328                 topicService.getPublishersByTopicName(dmaapContext, "topicNamespace.name");\r
329         }\r
330         \r
331         @Test(expected = TopicExistsException.class)\r
332         public void testGetConsumersByTopicName_nullTopic() throws DMaaPAccessDeniedException, CambriaApiException,\r
333                         IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {\r
334 \r
335                 Assert.assertNotNull(topicService);\r
336 \r
337                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
338                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
339                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
340                 when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
341                 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);\r
342                 when(httpServReq.getMethod()).thenReturn("HEAD");\r
343 \r
344                 when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(null);\r
345 \r
346                 topicService.getConsumersByTopicName(dmaapContext, "topicNamespace.name");\r
347 \r
348         }\r
349 \r
350         @Test\r
351         public void testGetConsumersByTopicName_nonNullTopic() throws DMaaPAccessDeniedException, CambriaApiException,\r
352                         IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {\r
353 \r
354                 Assert.assertNotNull(topicService);\r
355 \r
356                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
357                 when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
358                 when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
359                 when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
360                 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);\r
361                 when(httpServReq.getMethod()).thenReturn("HEAD");\r
362 \r
363                 when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(createdTopic);\r
364                 \r
365                 when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);\r
366 \r
367                 topicService.getConsumersByTopicName(dmaapContext, "topicNamespace.name");\r
368         }\r
369         \r
370 //      @Test\r
371 //      public void testDeleteTopic() throws DMaaPAccessDeniedException, CambriaApiException,\r
372 //                      IOException, TopicExistsException, JSONException, ConfigDbException, AccessDeniedException {\r
373 //\r
374 //              Assert.assertNotNull(topicService);\r
375 //\r
376 //              PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
377 //              \r
378 //               PowerMockito.mockStatic(AJSCPropertiesMap.class);\r
379 //               \r
380 //               PowerMockito.when(AJSCPropertiesMap.\r
381 //                               getProperty(CambriaConstants.msgRtr_prop,"msgRtr.topicfactory.aaf")).thenReturn("topicFactoryAAF");\r
382 //              \r
383 //              \r
384 //              when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
385 //              when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
386 //              when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
387 //              when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);\r
388 //              when(httpServReq.getMethod()).thenReturn("HEAD");\r
389 //\r
390 //              when(dmaapKafkaMetaBroker.getTopic("topicNamespace.name")).thenReturn(createdTopic);\r
391 //              \r
392 //              when(createdTopic.getReaderAcl()).thenReturn(nsaAcl);\r
393 //\r
394 //              topicService.getPublishersByTopicName(dmaapContext, "topicNamespace.name");\r
395 //      }\r
396 //      \r
397 //      \r
398 //      \r
399         \r
400           /*@Test public void testdeleteTopic() throws DMaaPAccessDeniedException,\r
401           CambriaApiException, IOException, TopicExistsException, JSONException,\r
402           ConfigDbException, AccessDeniedException {\r
403           \r
404           Assert.assertNotNull(topicService);\r
405           \r
406           //PowerMockito.mockStatic(AJSCPropertiesMap.class);\r
407           PowerMockito.mockStatic(AJSCPropertiesMap.class);\r
408           PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.\r
409           msgRtr_prop,"msgRtr.topicfactory.aaf")) .thenReturn("hello");\r
410           \r
411           when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(null);\r
412           when(httpServReq.getHeader("AppName")).thenReturn("MyApp");\r
413           when(httpServReq.getHeader("Authorization")).thenReturn("Admin");\r
414           when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
415           \r
416           when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
417           when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
418           \r
419           TopicBean topicBean = new TopicBean();\r
420           topicBean.setTopicName("enfTopicNamePlusExtra");\r
421           \r
422           topicService.deleteTopic(dmaapContext, "topicNamespace.topic"); }*/\r
423          \r
424         \r
425 }\r