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