commiting code for test coverage
[dmaap/messagerouter/msgrtr.git] / src / test / java / com / att / nsa / cambria / service / impl / EventsServiceImplTest.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 \r
22 package com.att.nsa.cambria.service.impl;\r
23 import static org.mockito.Mockito.when;\r
24 import static org.mockito.Matchers.anyString;\r
25 import static org.junit.Assert.assertTrue;\r
26 \r
27 import java.io.ByteArrayInputStream;\r
28 import java.io.File;\r
29 import java.io.IOException;\r
30 import java.io.InputStream;\r
31 import java.lang.reflect.Constructor;\r
32 import java.lang.reflect.InvocationTargetException;\r
33 import java.lang.reflect.Method;\r
34 import java.util.Map;\r
35 import java.util.Properties;\r
36 \r
37 import org.junit.After;\r
38 import org.junit.Before;\r
39 import org.junit.Test;\r
40 import org.junit.runner.RunWith;\r
41 import org.mockito.Mock;\r
42 import org.powermock.api.mockito.PowerMockito;\r
43 import org.powermock.core.classloader.annotations.PrepareForTest;\r
44 import org.powermock.modules.junit4.PowerMockRunner;\r
45 import org.springframework.beans.factory.annotation.Autowired;\r
46 import org.springframework.mock.web.MockHttpServletRequest;\r
47 import org.springframework.mock.web.MockHttpServletResponse;\r
48 \r
49 import com.att.ajsc.beans.PropertiesMapBean;\r
50 import com.att.ajsc.filemonitor.AJSCPropertiesMap;\r
51 import com.att.nsa.cambria.CambriaApiException;\r
52 import com.att.nsa.cambria.backends.ConsumerFactory.UnavailableException;\r
53 import com.att.nsa.cambria.beans.DMaaPCambriaLimiter;\r
54 import com.att.nsa.cambria.beans.DMaaPContext;\r
55 import com.att.nsa.cambria.constants.CambriaConstants;\r
56 import com.att.nsa.cambria.embed.EmbedConfigurationReader;\r
57 import com.att.nsa.cambria.exception.DMaaPAccessDeniedException;\r
58 import com.att.nsa.cambria.exception.DMaaPErrorMessages;\r
59 import com.att.nsa.cambria.metabroker.Topic;\r
60 import com.att.nsa.cambria.metabroker.Broker.TopicExistsException;\r
61 import com.att.nsa.cambria.security.DMaaPAuthenticator;\r
62 import com.att.nsa.cambria.security.DMaaPAuthenticatorImpl;\r
63 import com.att.nsa.cambria.utils.ConfigurationReader;\r
64 import com.att.nsa.cambria.utils.PropertyReader;\r
65 import com.att.nsa.configs.ConfigDbException;\r
66 import com.att.nsa.drumlin.till.nv.rrNvReadable.invalidSettingValue;\r
67 import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException;\r
68 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;\r
69 import com.att.nsa.limits.Blacklist;\r
70 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;\r
71 import com.att.nsa.security.db.simple.NsaSimpleApiKey;\r
72 \r
73 import kafka.admin.AdminUtils;\r
74 \r
75 \r
76 public class EventsServiceImplTest { \r
77 \r
78         private InputStream iStream = null;\r
79         DMaaPContext dMaapContext = new DMaaPContext();\r
80         EventsServiceImpl service = new EventsServiceImpl();\r
81         DMaaPErrorMessages pErrorMessages = new DMaaPErrorMessages();\r
82         \r
83         ConfigurationReader configurationReader;\r
84         EmbedConfigurationReader embedConfigurationReader = new EmbedConfigurationReader();\r
85         \r
86 \r
87         @Before\r
88         public void setUp() throws Exception {\r
89 \r
90                 String source = "source of my InputStream";\r
91                 iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));\r
92                 \r
93                 configurationReader = embedConfigurationReader.buildConfigurationReader();\r
94                 \r
95                 MockHttpServletRequest request = new MockHttpServletRequest();\r
96                 MockHttpServletResponse response = new MockHttpServletResponse();\r
97                 dMaapContext.setRequest(request);\r
98                 dMaapContext.setResponse(response);\r
99                 dMaapContext.setConfigReader(configurationReader);\r
100                 \r
101                 service.setErrorMessages(pErrorMessages);\r
102                 \r
103                 Map<String, String> map = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperties(CambriaConstants.msgRtr_prop);\r
104         map.put("timeout", "1000");\r
105                 \r
106         }\r
107 \r
108         @After\r
109         public void tearDown() throws Exception {\r
110                 embedConfigurationReader.tearDown();\r
111         }\r
112 \r
113         @Test(expected=NullPointerException.class)\r
114         public void testGetEventsForCambriaApiException() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException, TopicExistsException, AccessDeniedException, UnavailableException, IOException {\r
115                 service.getEvents(dMaapContext, "testTopic", "CG1", null);\r
116         }\r
117         \r
118         @Test(expected=CambriaApiException.class)\r
119         public void testGetEventsForNoTopic() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException, TopicExistsException, AccessDeniedException, UnavailableException, IOException {\r
120                                 \r
121                 service.getEvents(dMaapContext, "testTopic", "CG1", "23");\r
122         }\r
123         \r
124         \r
125         @Test(expected=CambriaApiException.class)\r
126         public void testPushEvents() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException, TopicExistsException, AccessDeniedException, UnavailableException, IOException, missingReqdSetting, invalidSettingValue, loadException {\r
127                 \r
128                 //AdminUtils.createTopic(configurationReader.getZk(), "testTopic", 10, 1, new Properties());\r
129                 \r
130                 configurationReader.setfRateLimiter(new DMaaPCambriaLimiter(new PropertyReader()));\r
131                 \r
132                 service.pushEvents(dMaapContext, "testTopic", iStream, "3", "12:00:00");\r
133                 \r
134                 service.getEvents(dMaapContext, "testTopic", "CG1", "23");\r
135                 \r
136                 String trueValue = "True";\r
137                 assertTrue(trueValue.equalsIgnoreCase("True"));\r
138 \r
139         }\r
140 \r
141         /*@Test\r
142         public void testPushEvents() {\r
143 \r
144                 EventsServiceImpl service = new EventsServiceImpl();\r
145 \r
146                 try {\r
147 \r
148                         // InputStream iStream = new\r
149                         // ByteArrayInputStream(source.getBytes("UTF-8"));\r
150                         service.pushEvents(new DMaaPContext(), "testTopic", iStream, "3", "12:00:00");\r
151 \r
152                 } catch (org.json.JSONException e) {\r
153                         // TODO Auto-generated catch block\r
154                         e.printStackTrace();\r
155                 } catch (IOException e) {\r
156                         // TODO Auto-generated catch block\r
157                         e.printStackTrace();\r
158                 } catch (DMaaPAccessDeniedException e) {\r
159                         // TODO Auto-generated catch block\r
160                         e.printStackTrace();\r
161                 } catch (CambriaApiException e) {\r
162                         // TODO Auto-generated catch block\r
163                         e.printStackTrace();\r
164                 } catch (ConfigDbException e) {\r
165                         // TODO Auto-generated catch block\r
166                         e.printStackTrace();\r
167                 } catch (TopicExistsException e) {\r
168                         // TODO Auto-generated catch block\r
169                         e.printStackTrace();\r
170                 } catch (AccessDeniedException e) {\r
171                         // TODO Auto-generated catch block\r
172                         e.printStackTrace();\r
173                 } catch (missingReqdSetting e) {\r
174                         // TODO Auto-generated catch block\r
175                         e.printStackTrace();\r
176                 } catch (NullPointerException e) {\r
177                         // TODO Auto-generated catch block\r
178                         // e.printStackTrace();\r
179                         assertTrue(true);\r
180                 }\r
181 \r
182                 String trueValue = "True";\r
183                 assertTrue(trueValue.equalsIgnoreCase("True"));\r
184 \r
185         }\r
186 \r
187         @Test\r
188         public void testPushEvents2() {\r
189                 Class clazz;\r
190                 try {\r
191                         clazz = Class.forName("EventsServiceImpl");\r
192                         Object obj = clazz.newInstance();\r
193                         Method method = clazz.getDeclaredMethod("pushEvents", null);\r
194                         method.setAccessible(true);\r
195                         method.invoke(obj, new DMaaPContext(), "testTopic", iStream, "partition", true, "media");\r
196 \r
197                 } catch (ClassNotFoundException e1) {\r
198                         // TODO Auto-generated catch block\r
199                         e1.printStackTrace();\r
200                 } catch (InstantiationException e) {\r
201                         // TODO Auto-generated catch block\r
202                         e.printStackTrace();\r
203                 } catch (IllegalAccessException e) {\r
204                         // TODO Auto-generated catch block\r
205                         e.printStackTrace();\r
206                 } catch (NoSuchMethodException e) {\r
207                         // TODO Auto-generated catch block\r
208                         e.printStackTrace();\r
209                 } catch (SecurityException e) {\r
210                         // TODO Auto-generated catch block\r
211                         e.printStackTrace();\r
212                 } catch (IllegalArgumentException e) {\r
213                         // TODO Auto-generated catch block\r
214                         e.printStackTrace();\r
215                 } catch (InvocationTargetException e) {\r
216                         // TODO Auto-generated catch block\r
217                         e.printStackTrace();\r
218                 }\r
219 \r
220                 String trueValue = "True";\r
221                 assertTrue(trueValue.equalsIgnoreCase("True"));\r
222 \r
223         }\r
224         \r
225         @Test\r
226         public void testPushEvents3() {\r
227                 Class clazz;\r
228                 try {\r
229                         clazz = Class.forName("EventsServiceImpl");\r
230                         Object obj = clazz.newInstance();\r
231                         Method method = clazz.getDeclaredMethod("pushEvents", null);\r
232                         method.setAccessible(true);\r
233                         method.invoke(obj, new DMaaPContext(), iStream, "testTopic", iStream, "partition", true, "media");\r
234 \r
235                 } catch (ClassNotFoundException e1) {\r
236                         // TODO Auto-generated catch block\r
237                         e1.printStackTrace();\r
238                 } catch (InstantiationException e) {\r
239                         // TODO Auto-generated catch block\r
240                         e.printStackTrace();\r
241                 } catch (IllegalAccessException e) {\r
242                         // TODO Auto-generated catch block\r
243                         e.printStackTrace();\r
244                 } catch (NoSuchMethodException e) {\r
245                         // TODO Auto-generated catch block\r
246                         e.printStackTrace();\r
247                 } catch (SecurityException e) {\r
248                         // TODO Auto-generated catch block\r
249                         e.printStackTrace();\r
250                 } catch (IllegalArgumentException e) {\r
251                         // TODO Auto-generated catch block\r
252                         e.printStackTrace();\r
253                 } catch (InvocationTargetException e) {\r
254                         // TODO Auto-generated catch block\r
255                         e.printStackTrace();\r
256                 }\r
257 \r
258                 String trueValue = "True";\r
259                 assertTrue(trueValue.equalsIgnoreCase("True"));\r
260 \r
261         }\r
262 \r
263         @Test\r
264         public void testAddTransactionDetailsToMessage() {\r
265                 Class clazz;\r
266                 try {\r
267                         clazz = Class.forName("EventsServiceImpl");\r
268                         Object obj = clazz.newInstance();\r
269                         Method method = clazz.getDeclaredMethod("addTransactionDetailsToMessage", null);\r
270                         method.setAccessible(true);\r
271                         method.invoke(obj, new MessageTest(), "testTopic", null, "11:00:00", 1234, 100l, true);\r
272 \r
273                 } catch (ClassNotFoundException e1) {\r
274                         // TODO Auto-generated catch block\r
275                         e1.printStackTrace();\r
276                 } catch (InstantiationException e) {\r
277                         // TODO Auto-generated catch block\r
278                         e.printStackTrace();\r
279                 } catch (IllegalAccessException e) {\r
280                         // TODO Auto-generated catch block\r
281                         e.printStackTrace();\r
282                 } catch (NoSuchMethodException e) {\r
283                         // TODO Auto-generated catch block\r
284                         e.printStackTrace();\r
285                 } catch (SecurityException e) {\r
286                         // TODO Auto-generated catch block\r
287                         e.printStackTrace();\r
288                 } catch (IllegalArgumentException e) {\r
289                         // TODO Auto-generated catch block\r
290                         e.printStackTrace();\r
291                 } catch (InvocationTargetException e) {\r
292                         // TODO Auto-generated catch block\r
293                         e.printStackTrace();\r
294                 }\r
295 \r
296                 String trueValue = "True";\r
297                 assertTrue(trueValue.equalsIgnoreCase("True"));\r
298 \r
299         }\r
300 \r
301         @Test\r
302         public void testIsTransEnabled() {\r
303                 Class clazz;\r
304                 try {\r
305                         clazz = Class.forName("EventsServiceImpl");\r
306                         Object obj = clazz.newInstance();\r
307                         Method method = clazz.getDeclaredMethod("isTransEnabled", null);\r
308                         method.setAccessible(true);\r
309                         method.invoke(obj, null);\r
310 \r
311                 } catch (ClassNotFoundException e1) {\r
312                         // TODO Auto-generated catch block\r
313                         e1.printStackTrace();\r
314                 } catch (InstantiationException e) {\r
315                         // TODO Auto-generated catch block\r
316                         e.printStackTrace();\r
317                 } catch (IllegalAccessException e) {\r
318                         // TODO Auto-generated catch block\r
319                         e.printStackTrace();\r
320                 } catch (NoSuchMethodException e) {\r
321                         // TODO Auto-generated catch block\r
322                         e.printStackTrace();\r
323                 } catch (SecurityException e) {\r
324                         // TODO Auto-generated catch block\r
325                         e.printStackTrace();\r
326                 } catch (IllegalArgumentException e) {\r
327                         // TODO Auto-generated catch block\r
328                         e.printStackTrace();\r
329                 } catch (InvocationTargetException e) {\r
330                         // TODO Auto-generated catch block\r
331                         e.printStackTrace();\r
332                 }\r
333 \r
334                 String trueValue = "True";\r
335                 assertTrue(trueValue.equalsIgnoreCase("True"));\r
336 \r
337         }\r
338 \r
339         @Test\r
340         public void testGenerateLogDetails() {\r
341                 Class clazz;\r
342                 try {\r
343                         clazz = Class.forName("EventsServiceImpl");\r
344                         Object obj = clazz.newInstance();\r
345                         Method method = clazz.getDeclaredMethod("generateLogDetails", null);\r
346                         method.setAccessible(true);\r
347                         method.invoke(obj, "testTopic", null, "11:00:00", 1234, 100l, true);\r
348 \r
349                 } catch (ClassNotFoundException e1) {\r
350                         // TODO Auto-generated catch block\r
351                         e1.printStackTrace();\r
352                 } catch (InstantiationException e) {\r
353                         // TODO Auto-generated catch block\r
354                         e.printStackTrace();\r
355                 } catch (IllegalAccessException e) {\r
356                         // TODO Auto-generated catch block\r
357                         e.printStackTrace();\r
358                 } catch (NoSuchMethodException e) {\r
359                         // TODO Auto-generated catch block\r
360                         e.printStackTrace();\r
361                 } catch (SecurityException e) {\r
362                         // TODO Auto-generated catch block\r
363                         e.printStackTrace();\r
364                 } catch (IllegalArgumentException e) {\r
365                         // TODO Auto-generated catch block\r
366                         e.printStackTrace();\r
367                 } catch (InvocationTargetException e) {\r
368                         // TODO Auto-generated catch block\r
369                         e.printStackTrace();\r
370                 }\r
371 \r
372                 String trueValue = "True";\r
373                 assertTrue(trueValue.equalsIgnoreCase("True"));\r
374 \r
375         }\r
376         \r
377 \r
378         @Test\r
379         public void testInfo() {\r
380 \r
381                 String foreNameString = "EventsServiceImpl" + "$" + "LogWrap";\r
382                 Object parent = new EventsServiceImpl();\r
383 \r
384                 Class<?> innerClass;\r
385                 try {\r
386                         innerClass = Class.forName(foreNameString);\r
387                         Constructor<?> constructor = innerClass.getDeclaredConstructor(EventsServiceImpl.class);\r
388                         constructor.setAccessible(true);\r
389                         Object child = constructor.newInstance(parent);\r
390 \r
391                         // invoking method on inner class object\r
392                         Method method = innerClass.getDeclaredMethod("info", null);\r
393                         method.setAccessible(true);// in case of unaccessible method\r
394                         method.invoke(child, "msg");\r
395                 } catch (ClassNotFoundException e) {\r
396                         // TODO Auto-generated catch block\r
397                         e.printStackTrace();\r
398                 } catch (NoSuchMethodException e) {\r
399                         // TODO Auto-generated catch block\r
400                         e.printStackTrace();\r
401                 } catch (SecurityException e) {\r
402                         // TODO Auto-generated catch block\r
403                         e.printStackTrace();\r
404                 } catch (InstantiationException e) {\r
405                         // TODO Auto-generated catch block\r
406                         e.printStackTrace();\r
407                 } catch (IllegalAccessException e) {\r
408                         // TODO Auto-generated catch block\r
409                         e.printStackTrace();\r
410                 } catch (IllegalArgumentException e) {\r
411                         // TODO Auto-generated catch block\r
412                         e.printStackTrace();\r
413                 } catch (InvocationTargetException e) {\r
414                         // TODO Auto-generated catch block\r
415                         e.printStackTrace();\r
416                 }\r
417 \r
418                 assertTrue(true);\r
419 \r
420         }\r
421         \r
422         @Test\r
423         public void testWarn() {\r
424 \r
425                 String foreNameString = "EventsServiceImpl" + "$" + "LogWrap";\r
426                 Object parent = new EventsServiceImpl();\r
427 \r
428                 Class<?> innerClass;\r
429                 try {\r
430                         innerClass = Class.forName(foreNameString);\r
431                         Constructor<?> constructor = innerClass.getDeclaredConstructor(EventsServiceImpl.class);\r
432                         constructor.setAccessible(true);\r
433                         Object child = constructor.newInstance(parent);\r
434 \r
435                         // invoking method on inner class object\r
436                         Method method = innerClass.getDeclaredMethod("warn", null);\r
437                         method.setAccessible(true);// in case of unaccessible method\r
438                         method.invoke(child, "msg", null);\r
439                 } catch (ClassNotFoundException e) {\r
440                         // TODO Auto-generated catch block\r
441                         e.printStackTrace();\r
442                 } catch (NoSuchMethodException e) {\r
443                         // TODO Auto-generated catch block\r
444                         e.printStackTrace();\r
445                 } catch (SecurityException e) {\r
446                         // TODO Auto-generated catch block\r
447                         e.printStackTrace();\r
448                 } catch (InstantiationException e) {\r
449                         // TODO Auto-generated catch block\r
450                         e.printStackTrace();\r
451                 } catch (IllegalAccessException e) {\r
452                         // TODO Auto-generated catch block\r
453                         e.printStackTrace();\r
454                 } catch (IllegalArgumentException e) {\r
455                         // TODO Auto-generated catch block\r
456                         e.printStackTrace();\r
457                 } catch (InvocationTargetException e) {\r
458                         // TODO Auto-generated catch block\r
459                         e.printStackTrace();\r
460                 }\r
461 \r
462                 assertTrue(true);\r
463 \r
464         }\r
465 */\r
466 }\r