313750eeba5cc37e1100cc1b13f499deeaa62e0b
[policy/engine.git] / LogParser / src / test / java / org / onap / xacml / parser / ParseLogTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * LogParser
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.xacml.parser;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.util.Date;
33 import java.util.Properties;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mockito;
42 import org.onap.policy.common.im.AdministrativeStateException;
43 import org.onap.policy.common.im.IntegrityMonitor;
44 import org.onap.policy.common.im.IntegrityMonitorException;
45 import org.onap.policy.common.im.StandbyStatusException;
46 import org.onap.policy.common.logging.flexlogger.FlexLogger;
47 import org.onap.policy.common.logging.flexlogger.Logger;
48 import org.onap.xacml.parser.LogEntryObject.LogType;
49 import org.powermock.api.mockito.PowerMockito;
50 import org.powermock.core.classloader.annotations.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
52
53 @PrepareForTest({ ParseLogTest.class, IntegrityMonitor.class })
54 @RunWith(PowerMockRunner.class)
55 public class ParseLogTest {
56
57     private static Logger logger = FlexLogger.getLogger(ParseLogTest.class);
58     private Properties config = new Properties();
59     private String configFile;
60     private String configFileDebug;
61     private String configFileError;
62     private String testFile1;
63     private String testFile2;
64     private IntegrityMonitor im;
65
66     /**
67      * Setup for the test case execution.
68      *
69      * @throws Exception if any error occurs
70      */
71     @Before
72     public void setUp() throws Exception {
73         System.setProperty("com.sun.management.jmxremote.port", "9998");
74         im = Mockito.mock(IntegrityMonitor.class);
75         final String regex = "^\\/[a-zA-Z]\\:\\/";
76
77         try {
78             Mockito.doNothing().when(im).startTransaction();
79         } catch (StandbyStatusException | AdministrativeStateException e) {
80             fail();
81         }
82         Mockito.doNothing().when(im).endTransaction();
83         final ClassLoader classLoader = getClass().getClassLoader();
84         configFile = classLoader.getResource("test_config.properties").getFile();
85         configFileDebug = classLoader.getResource("test_config_debug.properties").getFile();
86         configFileError = classLoader.getResource("test_config_error.properties").getFile();
87         final Pattern pattern = Pattern.compile(regex);
88         final Matcher matcher = pattern.matcher(configFile);
89         final Matcher matcherDebug = pattern.matcher(configFileDebug);
90         final Matcher matcherError = pattern.matcher(configFileError);
91
92         if (matcher.find()) {
93             configFile = configFile.substring(1);
94         }
95         if (matcherDebug.find()) {
96             configFileDebug = configFileDebug.substring(1);
97         }
98         if (matcherError.find()) {
99             configFileError = configFileError.substring(1);
100         }
101         testFile1 = classLoader.getResource("LineTest.txt").getFile();
102         testFile2 = classLoader.getResource("LineTest2.txt").getFile();
103
104     }
105
106     /**
107      * Cleaning off after test case execution.
108      */
109     @After
110     public void tearDown() {
111
112         logger.debug("tearDown: enter");
113
114         final File file = new File("nonExistFile.txt");
115         file.delete();
116         logger.debug("tearDown: exit");
117     }
118
119     @Test
120     public void testCountLines() throws IOException {
121
122         logger.debug("testCountLines: enter");
123
124         final int returnValue = ParseLog.countLines(testFile1);
125         logger.debug("testCountLines: returnValue: " + returnValue);
126         assertEquals(12, returnValue);
127
128         logger.debug("testCountLines: exit");
129     }
130
131     @Test
132     public void testParseRemoteSystem() {
133
134         logger.debug("testParseRemoteSystem: enter");
135
136         final String line =
137                 "||org.onap.policy.pap.xacml.rest.XACMLPapServlet$Heartbeat.run(XACMLPapServlet.java:2801)||Heartbeat 'https://localhost:8081/pdp/' status='UP_TO_DATE'";
138         final String returnValue = ParseLog.parseRemoteSystem(line);
139         logger.debug("testParseRemoteSystem: returnValue: " + returnValue);
140         assertEquals("localhost:8081", returnValue);
141
142         logger.debug("testParseRemoteSystem: exit");
143     }
144
145     @Test
146     public void testGetPropertiesValue() {
147
148         logger.debug("testGetPropertiesValue: enter");
149
150         config = new Properties();
151         config.put("RESOURCE_NAME", "logparser_pap01");
152         config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
153         config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
154         config.put("JDBC_USER", "root");
155         config.put("JDBC_PASSWORD", "password");
156         config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
157         config.put("SERVER", "password");
158         config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
159         config.put("LOGTYPE", "PAP");
160         config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\pap-rest.log");
161         config.put("PARSERLOGPATH", "IntegrityMonitor.log");
162
163         final Properties returnConfig = ParseLog.getPropertiesValue(configFile);
164         logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
165         assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
166
167         logger.debug("testGetPropertiesValue: exit");
168     }
169
170     @Test
171     public void testGetPropertiesValue_1() {
172
173         logger.debug("testGetPropertiesValue: enter");
174
175         config = new Properties();
176         config.put("RESOURCE_NAME", "logparser_pap01");
177         config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
178         config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
179         config.put("JDBC_USER", "root");
180         config.put("JDBC_PASSWORD", "password");
181         config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
182         config.put("SERVER", "password");
183         config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
184         config.put("LOGTYPE", "PAP");
185         config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\debug\\pap-rest.log");
186         config.put("PARSERLOGPATH", "IntegrityMonitor.log");
187
188         final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug);
189         logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
190         assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
191
192         logger.debug("testGetPropertiesValue_1: exit");
193     }
194
195     @Test
196     public void testGetPropertiesValue_2() {
197
198         logger.debug("testGetPropertiesValue: enter");
199
200         config = new Properties();
201         config.put("RESOURCE_NAME", "logparser_pap01");
202         config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
203         config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
204         config.put("JDBC_USER", "root");
205         config.put("JDBC_PASSWORD", "password");
206         config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
207         config.put("SERVER", "password");
208         config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
209         config.put("LOGTYPE", "PAP");
210         config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\error\\pap-rest.log");
211         config.put("PARSERLOGPATH", "IntegrityMonitor.log");
212
213         final Properties returnConfig = ParseLog.getPropertiesValue(configFileError);
214         logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
215         assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
216
217         logger.debug("testGetPropertiesValue_2: exit");
218     }
219
220     @Test
221     public void testGetPropertiesFail() {
222
223         logger.debug("testGetPropertiesFail: enter");
224
225         final Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
226         logger.debug("testGetPropertiesFail: returnValue: " + returnValue);
227         assertEquals(null, returnValue);
228
229         logger.debug("testGetPropertiesFail: exit");
230     }
231
232     @Test
233     public void testParseDate() {
234
235         logger.debug("testParseDate: enter");
236
237         String line = "2016-02-23 08:07:30";
238         final Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", false);
239         logger.debug("testParseDate: returnValue: " + returnValue);
240         line = returnValue.toString().substring(0, returnValue.toString().lastIndexOf(":30") + 3);
241         assertEquals("Tue Feb 23 08:07:30", line);
242
243         logger.debug("testParseDate: exit");
244     }
245
246     @Test
247     public void testPullLastLineRead() {
248
249         logger.debug("testPullLastLineRead: enter");
250         final File file = new File(testFile1);
251         String returnValue = null;
252         try {
253             returnValue = ParseLog.pullLastLineRead(file, "pap-rest.log");
254             logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue);
255         } catch (final IOException e) {
256             fail();
257         }
258         assertEquals("52", returnValue);
259
260         try {
261             returnValue = ParseLog.pullLastLineRead(file, "debug.log");
262             logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue);
263         } catch (final IOException e) {
264             fail();
265         }
266         assertEquals("17", returnValue);
267
268         try {
269             returnValue = ParseLog.pullLastLineRead(file, "error.log");
270             logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue);
271         } catch (final IOException e) {
272             fail();
273         }
274         assertEquals("22", returnValue);
275
276         logger.debug("testPullLastLineRead: exit");
277     }
278
279     @Test
280     public void testPullLastLineReadNoFile() {
281
282         logger.debug("testPullLastLineReadNoFile: enter");
283
284         final File file = new File("nonExistFile.txt");
285         try {
286             assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
287         } catch (final IOException e) {
288             fail();
289         }
290
291         logger.debug("testPullLastLineReadNoFile: exit");
292     }
293
294     @Test
295     public void testPullLastLineReadFail() {
296
297         logger.debug("testPullLastLineReadFail: enter");
298
299         final File file = new File(testFile2);
300         try {
301             assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
302         } catch (final IOException e) {
303             fail();
304         }
305
306         logger.debug("testPullLastLineReadFail: exit");
307     }
308
309     @Test
310     public void testPullOutLogValues() {
311
312         logger.debug("testPullOutLogValues: enter");
313         // ERROR_VALUE
314         // Open the file
315         FileInputStream fstream;
316         try {
317             fstream = new FileInputStream(testFile1);
318             final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
319             final String strLine = br.readLine();
320             final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "ERROR");
321             assertEquals("ERROR_VALUE", retrunObject.getDescription());
322             br.close();
323         } catch (final IOException e) {
324             fail();
325         }
326
327         logger.debug("testPullOutLogValues: exit");
328     }
329
330     @Test
331     public void testGetPaths() {
332
333         logger.debug("testGetPaths: enter");
334
335         try {
336             // valid test
337             String logPaths = "C:\\pap-log\\pap-rest.log;C:\\pap-log\\debug.log;C:\\pap-log\\error.log";
338             String[] retrunObject = ParseLog.getPaths(logPaths);
339             assertEquals(3, retrunObject.length);
340
341             // valid test
342             logPaths = "C:\\pap-log\\pap-rest.log";
343             retrunObject = ParseLog.getPaths(logPaths);
344             assertEquals(1, retrunObject.length);
345
346             // invalid test
347             logPaths = "";
348             retrunObject = ParseLog.getPaths(logPaths);
349             assertTrue(retrunObject == null);
350
351         } catch (final Exception e) {
352             fail();
353         }
354
355         logger.debug("testGetPaths: exit");
356     }
357
358     @Test
359     public void testPullOutLogValuesSecond() {
360
361         logger.debug("testPullOutLogValuesSecond: enter");
362         // ERROR_VALUE
363         // Open the file
364         FileInputStream fstream;
365         try {
366             fstream = new FileInputStream(testFile1);
367             final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
368             String strLine = br.readLine();
369             strLine = br.readLine();
370             final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "INFO");
371             assertEquals(LogType.INFO, retrunObject.getLogType());
372             br.close();
373         } catch (final IOException e) {
374             fail();
375         }
376
377         logger.debug("testPullOutLogValuesSecond: exit");
378     }
379
380     @Test
381     public void testPullOutLogValuesThird() {
382
383         logger.debug("testPullOutLogValuesThird: enter");
384         // ERROR_VALUE
385         // Open the file
386         FileInputStream fstream;
387         try {
388             final int number = 3;
389             fstream = new FileInputStream(testFile1);
390             final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
391             String strLine = br.readLine();
392             for (int i = 0; i < number; i++) {
393                 strLine = br.readLine();
394             }
395             final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
396             assertEquals(LogType.INFO, retrunObject.getLogType());
397             br.close();
398         } catch (final IOException e) {
399             fail();
400         }
401
402         logger.debug("testPullOutLogValuesThird: exit");
403     }
404
405     @Test
406     public void testPullOutLogValuesFourth() {
407
408         logger.debug("testPullOutLogValuesFourth: enter");
409         // Open the file
410         FileInputStream fstream;
411         try {
412             final int number = 4;
413             fstream = new FileInputStream(testFile1);
414             final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
415             String strLine = br.readLine();
416             for (int i = 0; i < number; i++) {
417                 strLine = br.readLine();
418             }
419             final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
420             assertEquals(LogType.INFO, retrunObject.getLogType());
421             br.close();
422         } catch (final IOException e) {
423             fail();
424         }
425
426         logger.debug("testPullOutLogValuesFourth: exit");
427     }
428
429     @Test
430     public void testPullOutLogValuesFith() {
431
432         logger.debug("testPullOutLogValuesFith: enter");
433         // Open the file
434         FileInputStream fstream;
435         try {
436             final int number = 5;
437             fstream = new FileInputStream(testFile1);
438             final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
439             String strLine = br.readLine();
440             for (int i = 0; i < number; i++) {
441                 strLine = br.readLine();
442             }
443             final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
444             assertEquals(LogType.WARN, retrunObject.getLogType());
445             br.close();
446         } catch (final IOException e) {
447             fail();
448         }
449
450         logger.debug("testPullOutLogValuesFith: exit");
451     }
452
453     @Test
454     public void testPullOutLogValuesSixth() {
455
456         logger.debug("testPullOutLogValuesSixth: enter");
457         // Open the file
458         FileInputStream fstream;
459         try {
460             final int number = 6;
461             fstream = new FileInputStream(testFile1);
462             final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
463             String strLine = br.readLine();
464             for (int i = 0; i < number; i++) {
465                 strLine = br.readLine();
466             }
467             final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
468             assertEquals(LogType.SEVERE, retrunObject.getLogType());
469             br.close();
470         } catch (final IOException e) {
471             fail();
472         }
473
474         logger.debug("testPullOutLogValuesSixth: exit");
475     }
476
477     @Test
478     public void testPullOutLogValuesSeven() {
479
480         logger.debug("testPullOutLogValuesSeven: enter");
481         // Open the file
482         FileInputStream fstream;
483         try {
484             final int number = 7;
485             fstream = new FileInputStream(testFile1);
486             final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
487             String strLine = br.readLine();
488             for (int i = 0; i < number; i++) {
489                 strLine = br.readLine();
490             }
491             final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
492             assertEquals(LogType.ERROR, retrunObject.getLogType());
493             br.close();
494         } catch (final IOException e) {
495             fail();
496         }
497
498         logger.debug("testPullOutLogValuesSeven: exit");
499     }
500
501     @Test
502     public void testPullOutLogValuesEight() {
503
504         logger.debug("testPullOutLogValuesEight: enter");
505         // Open the file
506         FileInputStream fstream;
507         try {
508             final int number = 8;
509             fstream = new FileInputStream(testFile1);
510             final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
511             String strLine = br.readLine();
512             for (int i = 0; i < number; i++) {
513                 strLine = br.readLine();
514             }
515             final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
516             assertEquals(LogType.WARN, retrunObject.getLogType());
517             br.close();
518         } catch (final IOException e) {
519             fail();
520         }
521
522         logger.debug("testPullOutLogValuesEight: exit");
523     }
524
525     @Test
526     public void testPullOutLogValuesNull() {
527
528         logger.debug("testPullOutLogValuesNull: enter");
529         // Open the file
530         final LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
531         assertEquals(null, retrunObject);
532
533         logger.debug("testPullOutLogValuesNull: exit");
534     }
535
536     @Test
537     public void testLogEntryObject() {
538
539         logger.debug("testLogEntryObject: enter");
540
541         final Date date = new Date();
542
543         // Open the file
544         final LogEntryObject logObject = new LogEntryObject();
545         logObject.setSystem("vm02");
546         logObject.setSystemType("pap");
547         logObject.setDate(date);
548         logObject.setRemote("remote");
549
550         assertEquals("vm02", logObject.getSystem());
551         assertEquals("pap", logObject.getSystemType());
552         assertEquals(date, logObject.getDate());
553         assertEquals("remote", logObject.getRemote());
554
555         logger.debug("testLogEntryObject: exit");
556     }
557
558     @Test
559     public void testProcess() {
560
561         logger.debug("testProcess: enter");
562
563         final String line = "2015-04-01 09:13:44.947" + "  DEBUG 17482 --- [nio-8480-exec-7] "
564                 + "c.a.l.onap.policy.std.StdPolicyConfig" + "   : config Retrieved ";
565
566         im = Mockito.mock(IntegrityMonitor.class);
567         try {
568             Mockito.doNothing().when(im).startTransaction();
569         } catch (final IntegrityMonitorException e) {
570             fail();
571         }
572         Mockito.doNothing().when(im).endTransaction();
573         ParseLog.process(line, "pap", LogType.INFO);
574
575         logger.debug("testProcess: exit");
576     }
577
578     @Test
579     public void testMain() {
580         try {
581             ParseLog.main(new String[] {});
582         } catch (final Exception e) {
583             logger.debug("exception occured while executing the test: exit");
584         }
585     }
586
587     @Test
588     public void testMainDebug() {
589         try {
590             final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug);
591             PowerMockito.mockStatic(IntegrityMonitor.class);
592             Mockito.when(IntegrityMonitor.getInstance("test", returnConfig)).thenReturn(im);
593             ParseLog.main(new String[] {});
594             Thread.sleep(30000);
595         } catch (final Exception e) {
596             logger.debug("exception occured while executing the test: exit");
597         }
598     }
599
600     @Test
601     public void testMainError() {
602         try {
603             final Properties returnConfig = ParseLog.getPropertiesValue(configFileError);
604             PowerMockito.mockStatic(IntegrityMonitor.class);
605             Mockito.when(IntegrityMonitor.getInstance("test", returnConfig)).thenReturn(im);
606             ParseLog.main(new String[] {});
607             Thread.sleep(30000);
608         } catch (final Exception e) {
609             logger.debug("exception occured while executing the test: exit");
610         }
611     }
612 }