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