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