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