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.onap.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;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
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;
49 public class ParseLogTest {
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;
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]\\:\\/";
65 Mockito.doNothing().when(im).startTransaction();
66 } catch (StandbyStatusException | AdministrativeStateException e) {
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);
76 configFile = configFile.substring(1);
78 testFile1 = classLoader.getResource("LineTest.txt").getFile();
79 testFile2 = classLoader.getResource("LineTest2.txt").getFile();
84 public void tearDown() {
86 logger.debug("tearDown: enter");
88 File file = new File("nonExistFile.txt");
90 logger.debug("tearDown: exit");
94 public void testCountLines() throws IOException {
96 logger.debug("testCountLines: enter");
98 int returnValue = ParseLog.countLines(testFile1);
99 logger.debug("testCountLines: returnValue: " + returnValue);
100 assertEquals(12, returnValue);
102 logger.debug("testCountLines: exit");
106 public void testParseRemoteSystem() {
108 logger.debug("testParseRemoteSystem: enter");
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);
115 logger.debug("testParseRemoteSystem: exit");
119 public void testGetPropertiesValue() {
121 logger.debug("testGetPropertiesValue: enter");
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");
136 Properties returnConfig = ParseLog.getPropertiesValue(configFile);
137 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
138 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
140 logger.debug("testGetPropertiesValue: exit");
144 public void testGetPropertiesFail() {
146 logger.debug("testGetPropertiesFail: enter");
148 Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
149 logger.debug("testGetPropertiesFail: returnValue: " + returnValue);
150 assertEquals(null, returnValue);
152 logger.debug("testGetPropertiesFail: exit");
156 public void testParseDate(){
158 logger.debug("testParseDate: enter");
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);
166 logger.debug("testParseDate: exit");
170 public void testPullLastLineRead(){
172 logger.debug("testPullLastLineRead: enter");
173 File file = new File(testFile1);
174 String returnValue = null;
176 returnValue = ParseLog.pullLastLineRead(file, "pap-rest.log");
177 logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue);
178 } catch (IOException e) {
181 assertEquals("52", returnValue);
184 returnValue = ParseLog.pullLastLineRead(file, "debug.log");
185 logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue);
186 } catch (IOException e) {
189 assertEquals("17", returnValue);
192 returnValue = ParseLog.pullLastLineRead(file, "error.log");
193 logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue);
194 } catch (IOException e) {
197 assertEquals("22", returnValue);
199 logger.debug("testPullLastLineRead: exit");
203 public void testPullLastLineReadNoFile(){
205 logger.debug("testPullLastLineReadNoFile: enter");
207 File file = new File("nonExistFile.txt");
209 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
210 } catch (IOException e) {
214 logger.debug("testPullLastLineReadNoFile: exit");
218 public void testPullLastLineReadFail(){
220 logger.debug("testPullLastLineReadFail: enter");
222 File file = new File(testFile2);
224 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
225 } catch (IOException e) {
229 logger.debug("testPullLastLineReadFail: exit");
233 public void testPullOutLogValues(){
235 logger.debug("testPullOutLogValues: enter");
238 FileInputStream fstream;
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());
246 } catch (IOException e) {
250 logger.debug("testPullOutLogValues: exit");
254 public void testGetPaths(){
256 logger.debug("testGetPaths: enter");
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);
265 logPaths = "C:\\pap-log\\pap-rest.log";
266 retrunObject = ParseLog.getPaths(logPaths);
267 assertEquals(1, retrunObject.length);
271 retrunObject = ParseLog.getPaths(logPaths);
272 assertTrue(retrunObject == null);
274 } catch (Exception e) {
278 logger.debug("testGetPaths: exit");
282 public void testPullOutLogValuesSecond(){
284 logger.debug("testPullOutLogValuesSecond: enter");
287 FileInputStream fstream;
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());
296 } catch (IOException e) {
300 logger.debug("testPullOutLogValuesSecond: exit");
304 public void testPullOutLogValuesThird(){
306 logger.debug("testPullOutLogValuesThird: enter");
309 FileInputStream fstream;
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();
318 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
319 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
321 } catch (IOException e) {
325 logger.debug("testPullOutLogValuesThird: exit");
329 public void testPullOutLogValuesFourth(){
331 logger.debug("testPullOutLogValuesFourth: enter");
333 FileInputStream fstream;
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();
342 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
343 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
345 } catch (IOException e) {
349 logger.debug("testPullOutLogValuesFourth: exit");
353 public void testPullOutLogValuesFith(){
355 logger.debug("testPullOutLogValuesFith: enter");
357 FileInputStream fstream;
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();
366 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
367 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
369 } catch (IOException e) {
373 logger.debug("testPullOutLogValuesFith: exit");
377 public void testPullOutLogValuesSixth(){
379 logger.debug("testPullOutLogValuesSixth: enter");
381 FileInputStream fstream;
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();
390 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
391 assertEquals(LOGTYPE.SEVERE, retrunObject.getLogType());
393 } catch (IOException e) {
397 logger.debug("testPullOutLogValuesSixth: exit");
401 public void testPullOutLogValuesSeven(){
403 logger.debug("testPullOutLogValuesSeven: enter");
405 FileInputStream fstream;
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();
414 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
415 assertEquals(LOGTYPE.ERROR, retrunObject.getLogType());
417 } catch (IOException e) {
421 logger.debug("testPullOutLogValuesSeven: exit");
425 public void testPullOutLogValuesEight(){
427 logger.debug("testPullOutLogValuesEight: enter");
429 FileInputStream fstream;
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();
438 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
439 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
441 } catch (IOException e) {
445 logger.debug("testPullOutLogValuesEight: exit");
449 public void testPullOutLogValuesNull(){
451 logger.debug("testPullOutLogValuesNull: enter");
453 LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
454 assertEquals(null, retrunObject);
456 logger.debug("testPullOutLogValuesNull: exit");
460 public void testLogEntryObject(){
462 logger.debug("testLogEntryObject: enter");
464 Date date = new Date();
467 LogEntryObject logObject = new LogEntryObject();
468 logObject.setSystem("vm02");
469 logObject.setSystemType("pap");
470 logObject.setDate(date);
471 logObject.setRemote("remote");
473 assertEquals("vm02", logObject.getSystem());
474 assertEquals("pap", logObject.getSystemType());
475 assertEquals(date, logObject.getDate());
476 assertEquals("remote", logObject.getRemote());
478 logger.debug("testLogEntryObject: exit");
482 public void testProcess(){
484 logger.debug("testProcess: enter");
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 ";
488 im = Mockito.mock(IntegrityMonitor.class);
490 Mockito.doNothing().when(im).startTransaction();
491 } catch (StandbyStatusException | AdministrativeStateException e) {
494 Mockito.doNothing().when(im).endTransaction();
495 ParseLog.process(line, "pap", LOGTYPE.INFO);
497 logger.debug("testProcess: exit");