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