2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.xacml.parser;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
27 import java.io.BufferedReader;
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;
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;
47 public class ParseLogTest {
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;
57 public void setUp() throws Exception {
58 System.setProperty("com.sun.management.jmxremote.port", "9998");
59 im = Mockito.mock(IntegrityMonitor.class);
62 Mockito.doNothing().when(im).startTransaction();
63 } catch (StandbyStatusException | AdministrativeStateException e) {
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);
72 testFile1 = classLoader.getResource("LineTest.txt").getFile();
73 testFile2 = classLoader.getResource("LineTest2.txt").getFile();
78 public void tearDown() {
80 logger.debug("tearDown: enter");
82 File file = new File("nonExistFile.txt");
84 logger.debug("tearDown: exit");
88 public void testCountLines() throws IOException {
90 logger.debug("testCountLines: enter");
92 int returnValue = ParseLog.countLines(testFile1);
93 logger.debug("testCountLines: returnValue: " + returnValue);
94 assertEquals(12, returnValue);
96 logger.debug("testCountLines: exit");
100 public void testParseRemoteSystem() {
102 logger.debug("testParseRemoteSystem: enter");
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);
109 logger.debug("testParseRemoteSystem: exit");
113 public void testGetPropertiesValue() {
115 logger.debug("testGetPropertiesValue: enter");
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");
130 Properties returnConfig = ParseLog.getPropertiesValue(configFile);
131 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
132 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
134 logger.debug("testGetPropertiesValue: exit");
138 public void testGetPropertiesFail() {
140 logger.debug("testGetPropertiesFail: enter");
142 Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
143 logger.debug("testGetPropertiesFail: returnValue: " + returnValue);
144 assertEquals(null, returnValue);
146 logger.debug("testGetPropertiesFail: exit");
150 public void testParseDate(){
152 logger.debug("testParseDate: enter");
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);
160 logger.debug("testParseDate: exit");
164 public void testParseDateFail(){
166 logger.debug("testParseDateFail: enter");
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);
173 logger.debug("testParseDateFail: exit");
177 public void testPullLastLineRead(){
179 logger.debug("testPullLastLineRead: enter");
180 File file = new File(testFile1);
181 String returnValue = null;
183 returnValue = ParseLog.PullLastLineRead(file, "pap-rest.log");
184 logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue);
185 } catch (IOException e) {
188 assertEquals("52", returnValue);
191 returnValue = ParseLog.PullLastLineRead(file, "debug.log");
192 logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue);
193 } catch (IOException e) {
196 assertEquals("17", returnValue);
199 returnValue = ParseLog.PullLastLineRead(file, "error.log");
200 logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue);
201 } catch (IOException e) {
204 assertEquals("22", returnValue);
206 logger.debug("testPullLastLineRead: exit");
210 public void testPullLastLineReadNoFile(){
212 logger.debug("testPullLastLineReadNoFile: enter");
214 File file = new File("nonExistFile.txt");
216 assertEquals(null, ParseLog.PullLastLineRead(file, "pap-rest"));
217 } catch (IOException e) {
221 logger.debug("testPullLastLineReadNoFile: exit");
225 public void testPullLastLineReadFail(){
227 logger.debug("testPullLastLineReadFail: enter");
229 File file = new File(testFile2);
231 assertEquals(null, ParseLog.PullLastLineRead(file, "pap-rest"));
232 } catch (IOException e) {
236 logger.debug("testPullLastLineReadFail: exit");
240 public void testPullOutLogValues(){
242 logger.debug("testPullOutLogValues: enter");
245 FileInputStream fstream;
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());
253 } catch (IOException e) {
257 logger.debug("testPullOutLogValues: exit");
261 public void testGetPaths(){
263 logger.debug("testGetPaths: enter");
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);
272 logPaths = "C:\\pap-log\\pap-rest.log";
273 retrunObject = ParseLog.getPaths(logPaths);
274 assertEquals(1, retrunObject.length);
278 retrunObject = ParseLog.getPaths(logPaths);
279 assertTrue(retrunObject == null);
281 } catch (Exception e) {
285 logger.debug("testGetPaths: exit");
289 public void testPullOutLogValuesSecond(){
291 logger.debug("testPullOutLogValuesSecond: enter");
294 FileInputStream fstream;
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());
303 } catch (IOException e) {
307 logger.debug("testPullOutLogValuesSecond: exit");
311 public void testPullOutLogValuesThird(){
313 logger.debug("testPullOutLogValuesThird: enter");
316 FileInputStream fstream;
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();
325 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
326 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
328 } catch (IOException e) {
332 logger.debug("testPullOutLogValuesThird: exit");
336 public void testPullOutLogValuesFourth(){
338 logger.debug("testPullOutLogValuesFourth: enter");
340 FileInputStream fstream;
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();
349 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
350 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
352 } catch (IOException e) {
356 logger.debug("testPullOutLogValuesFourth: exit");
360 public void testPullOutLogValuesFith(){
362 logger.debug("testPullOutLogValuesFith: enter");
364 FileInputStream fstream;
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();
373 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
374 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
376 } catch (IOException e) {
380 logger.debug("testPullOutLogValuesFith: exit");
384 public void testPullOutLogValuesSixth(){
386 logger.debug("testPullOutLogValuesSixth: enter");
388 FileInputStream fstream;
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();
397 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
398 assertEquals(LOGTYPE.SEVERE, retrunObject.getLogType());
400 } catch (IOException e) {
404 logger.debug("testPullOutLogValuesSixth: exit");
408 public void testPullOutLogValuesSeven(){
410 logger.debug("testPullOutLogValuesSeven: enter");
412 FileInputStream fstream;
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();
421 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
422 assertEquals(LOGTYPE.ERROR, retrunObject.getLogType());
424 } catch (IOException e) {
428 logger.debug("testPullOutLogValuesSeven: exit");
432 public void testPullOutLogValuesEight(){
434 logger.debug("testPullOutLogValuesEight: enter");
436 FileInputStream fstream;
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();
445 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
446 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
448 } catch (IOException e) {
452 logger.debug("testPullOutLogValuesEight: exit");
456 public void testPullOutLogValuesNull(){
458 logger.debug("testPullOutLogValuesNull: enter");
460 LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
461 assertEquals(null, retrunObject);
463 logger.debug("testPullOutLogValuesNull: exit");
467 public void testLogEntryObject(){
469 logger.debug("testLogEntryObject: enter");
471 Date date = new Date();
474 LogEntryObject logObject = new LogEntryObject();
475 logObject.setSystem("vm02");
476 logObject.setSystemType("pap");
477 logObject.setDate(date);
478 logObject.setRemote("remote");
480 assertEquals("vm02", logObject.getSystem());
481 assertEquals("pap", logObject.getSystemType());
482 assertEquals(date, logObject.getDate());
483 assertEquals("remote", logObject.getRemote());
485 logger.debug("testLogEntryObject: exit");
489 public void testProcess(){
491 logger.debug("testProcess: enter");
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 ";
495 im = Mockito.mock(IntegrityMonitor.class);
497 Mockito.doNothing().when(im).startTransaction();
498 } catch (StandbyStatusException | AdministrativeStateException e) {
501 Mockito.doNothing().when(im).endTransaction();
502 ParseLog.process(line, "pap", LOGTYPE.INFO);
504 logger.debug("testProcess: exit");