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