[POLICY-122] Policy GUI Fixes
[policy/engine.git] / LogParser / src / test / java / org / openecomp / 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.openecomp.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.openecomp.policy.common.im.AdministrativeStateException;
40 import org.openecomp.policy.common.im.IntegrityMonitor;
41 import org.openecomp.policy.common.im.StandbyStatusException;
42 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
43 import org.openecomp.policy.common.logging.flexlogger.Logger;
44 import org.openecomp.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.openecomp.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  testParseDateFail(){
165                 
166                 logger.debug("testParseDateFail: enter");
167                 
168                 String line = "2016-02-23 08:07:30";
169                 Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", true);
170                 logger.debug("testParseDateFail: returnValue: " + returnValue);
171                 assertEquals(null, returnValue);
172                 
173                 logger.debug("testParseDateFail: exit");
174         }
175
176         @Test
177         public void testPullLastLineRead(){
178                 
179                 logger.debug("testPullLastLineRead: enter");
180                 File file = new File(testFile1);
181                 String returnValue = null;
182                 try {
183                         returnValue = ParseLog.PullLastLineRead(file, "pap-rest.log");
184                         logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue);
185                 } catch (IOException e) {
186                         fail();
187                 }               
188                 assertEquals("52", returnValue);
189                 
190                 try {
191                         returnValue = ParseLog.PullLastLineRead(file, "debug.log");
192                         logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue);
193                 } catch (IOException e) {
194                         fail();
195                 }               
196                 assertEquals("17", returnValue);
197                 
198                 try {
199                         returnValue = ParseLog.PullLastLineRead(file, "error.log");
200                         logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue);
201                 } catch (IOException e) {
202                         fail();
203                 }               
204                 assertEquals("22", returnValue);
205
206                 logger.debug("testPullLastLineRead: exit");
207         }
208
209         @Test
210         public void testPullLastLineReadNoFile(){
211                 
212                 logger.debug("testPullLastLineReadNoFile: enter");
213                 
214                 File file = new File("nonExistFile.txt");
215                 try {
216                         assertEquals(null, ParseLog.PullLastLineRead(file, "pap-rest"));
217                 } catch (IOException e) {
218                         fail();
219                 }
220                 
221                 logger.debug("testPullLastLineReadNoFile: exit");
222         }
223
224         @Test
225         public void testPullLastLineReadFail(){
226                 
227                 logger.debug("testPullLastLineReadFail: enter");
228                 
229                 File file = new File(testFile2);
230                 try {
231                         assertEquals(null, ParseLog.PullLastLineRead(file, "pap-rest"));
232                 } catch (IOException e) {
233                         fail();
234                 }
235                 
236                 logger.debug("testPullLastLineReadFail: exit");
237         }
238
239         @Test
240         public void testPullOutLogValues(){
241                 
242                 logger.debug("testPullOutLogValues: enter");
243                 //ERROR_VALUE
244                 // Open the file
245                 FileInputStream fstream;
246                 try {
247                         fstream = new FileInputStream(testFile1);
248                         BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
249                         String strLine = br.readLine();
250                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "ERROR");
251                         assertEquals("ERROR_VALUE", retrunObject.getDescription());
252                         br.close();
253                 } catch (IOException e) {
254                         fail();
255                 }       
256
257                 logger.debug("testPullOutLogValues: exit");
258         }
259         
260         @Test
261         public void testGetPaths(){
262                 
263                 logger.debug("testGetPaths: enter");
264                 
265                 try {
266                         // valid test
267                         String logPaths = "C:\\pap-log\\pap-rest.log;C:\\pap-log\\debug.log;C:\\pap-log\\error.log";
268                         String [] retrunObject = ParseLog.getPaths(logPaths);
269                         assertEquals(3, retrunObject.length);
270                         
271                         // valid test
272                         logPaths = "C:\\pap-log\\pap-rest.log";
273                         retrunObject = ParseLog.getPaths(logPaths);
274                         assertEquals(1, retrunObject.length);
275                         
276                         // invalid test
277                         logPaths = "";
278                         retrunObject = ParseLog.getPaths(logPaths);
279                         assertTrue(retrunObject == null);
280
281                 } catch (Exception e) {
282                         fail();
283                 }       
284
285                 logger.debug("testGetPaths: exit");
286         }       
287
288         @Test
289         public void testPullOutLogValuesSecond(){
290                 
291                 logger.debug("testPullOutLogValuesSecond: enter");
292                 //ERROR_VALUE
293                 // Open the file
294                 FileInputStream fstream;
295                 try {
296                         fstream = new FileInputStream(testFile1);
297                         BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
298                         String strLine = br.readLine();
299                         strLine = br.readLine();
300                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "INFO");
301                         assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
302                         br.close();
303                 } catch (IOException e) {
304                         fail();
305                 }       
306                 
307                 logger.debug("testPullOutLogValuesSecond: exit");
308         }
309
310         @Test
311         public void testPullOutLogValuesThird(){
312                 
313                 logger.debug("testPullOutLogValuesThird: enter");
314                 //ERROR_VALUE
315                 // Open the file
316                 FileInputStream fstream;
317                 try {
318                         int number = 3;
319                         fstream = new FileInputStream(testFile1);
320                         BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
321                         String strLine = br.readLine();
322                         for (int i =0; i < number; i++){
323                                 strLine = br.readLine();
324                         }
325                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
326                         assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
327                         br.close();
328                 } catch (IOException e) {
329                         fail();
330                 }       
331                 
332                 logger.debug("testPullOutLogValuesThird: exit");
333         }
334
335         @Test
336         public void testPullOutLogValuesFourth(){
337                 
338                 logger.debug("testPullOutLogValuesFourth: enter");
339                 // Open the file
340                 FileInputStream fstream;
341                 try {
342                         int number = 4;
343                         fstream = new FileInputStream(testFile1);
344                         BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
345                         String strLine = br.readLine();
346                         for (int i =0; i < number; i++){
347                                 strLine = br.readLine();
348                         }
349                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
350                         assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
351                         br.close();
352                 } catch (IOException e) {
353                         fail();
354                 }       
355                 
356                 logger.debug("testPullOutLogValuesFourth: exit");
357         }
358
359         @Test
360         public void testPullOutLogValuesFith(){
361                 
362                 logger.debug("testPullOutLogValuesFith: enter");
363                 // Open the file
364                 FileInputStream fstream;
365                 try {
366                         int number = 5;
367                         fstream = new FileInputStream(testFile1);
368                         BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
369                         String strLine = br.readLine();
370                         for (int i =0; i < number; i++){
371                                 strLine = br.readLine();
372                         }
373                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
374                         assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
375                         br.close();
376                 } catch (IOException e) {
377                         fail();
378                 }       
379                 
380                 logger.debug("testPullOutLogValuesFith: exit");
381         }
382
383         @Test
384         public void testPullOutLogValuesSixth(){
385                 
386                 logger.debug("testPullOutLogValuesSixth: enter");
387                 // Open the file
388                 FileInputStream fstream;
389                 try {
390                         int number = 6;
391                         fstream = new FileInputStream(testFile1);
392                         BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
393                         String strLine = br.readLine();
394                         for (int i =0; i < number; i++){
395                                 strLine = br.readLine();
396                         }
397                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
398                         assertEquals(LOGTYPE.SEVERE, retrunObject.getLogType());
399                         br.close();
400                 } catch (IOException e) {
401                         fail();
402                 }       
403                 
404                 logger.debug("testPullOutLogValuesSixth: exit");
405         }
406
407         @Test
408         public void testPullOutLogValuesSeven(){
409                 
410                 logger.debug("testPullOutLogValuesSeven: enter");
411                 // Open the file
412                 FileInputStream fstream;
413                 try {
414                         int number = 7;
415                         fstream = new FileInputStream(testFile1);
416                         BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
417                         String strLine = br.readLine();
418                         for (int i =0; i < number; i++){
419                                 strLine = br.readLine();
420                         }
421                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
422                         assertEquals(LOGTYPE.ERROR, retrunObject.getLogType());
423                         br.close();
424                 } catch (IOException e) {
425                         fail();
426                 }       
427                 
428                 logger.debug("testPullOutLogValuesSeven: exit");
429         }
430
431         @Test
432         public void testPullOutLogValuesEight(){
433                 
434                 logger.debug("testPullOutLogValuesEight: enter");
435                 // Open the file
436                 FileInputStream fstream;
437                 try {
438                         int number = 8;
439                         fstream = new FileInputStream(testFile1);
440                         BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
441                         String strLine = br.readLine();
442                         for (int i =0; i < number; i++){
443                                 strLine = br.readLine();
444                         }
445                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
446                         assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
447                         br.close();
448                 } catch (IOException e) {
449                         fail();
450                 }       
451                 
452                 logger.debug("testPullOutLogValuesEight: exit");
453         }
454
455         @Test
456         public void testPullOutLogValuesNull(){
457                 
458                 logger.debug("testPullOutLogValuesNull: enter");
459                 // Open the file
460                 LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
461                 assertEquals(null, retrunObject);
462                 
463                 logger.debug("testPullOutLogValuesNull: exit");
464         }
465
466         @Test
467         public void testLogEntryObject(){
468                 
469                 logger.debug("testLogEntryObject: enter");
470                 
471                 Date date = new Date();
472          
473                 // Open the file
474                 LogEntryObject logObject = new LogEntryObject();
475                 logObject.setSystem("vm02");
476                 logObject.setSystemType("pap");
477                 logObject.setDate(date);
478                 logObject.setRemote("remote");
479
480                 assertEquals("vm02", logObject.getSystem());
481                 assertEquals("pap", logObject.getSystemType());
482                 assertEquals(date, logObject.getDate());
483                 assertEquals("remote", logObject.getRemote());
484                 
485                 logger.debug("testLogEntryObject: exit");
486         }
487
488         @Test
489         public void testProcess(){
490                 
491                 logger.debug("testProcess: enter");
492                 
493                 String line = "2015-04-01 09:13:44.947  DEBUG 17482 --- [nio-8480-exec-7] c.a.l.ecomp.policy.std.StdPolicyConfig   : config Retrieved ";
494         
495                 im = Mockito.mock(IntegrityMonitor.class);
496                 try {
497                         Mockito.doNothing().when(im).startTransaction();
498                 } catch (StandbyStatusException | AdministrativeStateException e) {
499                         fail();
500                 }
501                 Mockito.doNothing().when(im).endTransaction();
502                 ParseLog.process(line, "pap", LOGTYPE.INFO);
503                 
504                 logger.debug("testProcess: exit");
505         }
506 }