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.junit.runner.RunWith;
41 import org.mockito.Mockito;
42 import org.onap.policy.common.im.AdministrativeStateException;
43 import org.onap.policy.common.im.IntegrityMonitor;
44 import org.onap.policy.common.im.IntegrityMonitorException;
45 import org.onap.policy.common.im.StandbyStatusException;
46 import org.onap.policy.common.logging.flexlogger.FlexLogger;
47 import org.onap.policy.common.logging.flexlogger.Logger;
48 import org.onap.xacml.parser.LogEntryObject.LOGTYPE;
49 import org.powermock.api.mockito.PowerMockito;
50 import org.powermock.core.classloader.annotations.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
53 @PrepareForTest({ParseLogTest.class, IntegrityMonitor.class})
54 @RunWith(PowerMockRunner.class)
55 public class ParseLogTest {
57 private static Logger logger = FlexLogger.getLogger(ParseLogTest.class);
58 private Properties config = new Properties();
59 private String configFile;
60 private String configFileDebug;
61 private String configFileError;
62 private String testFile1;
63 private String testFile2;
64 private IntegrityMonitor im;
67 public void setUp() throws Exception {
68 System.setProperty("com.sun.management.jmxremote.port", "9998");
69 im = Mockito.mock(IntegrityMonitor.class);
70 String regex = "^\\/[a-zA-Z]\\:\\/";
73 Mockito.doNothing().when(im).startTransaction();
74 } catch (StandbyStatusException | AdministrativeStateException e) {
77 Mockito.doNothing().when(im).endTransaction();
78 ClassLoader classLoader = getClass().getClassLoader();
79 configFile = classLoader.getResource("test_config.properties").getFile();
80 configFileDebug = classLoader.getResource("test_config_debug.properties").getFile();
81 configFileError = classLoader.getResource("test_config_error.properties").getFile();
82 Pattern pattern = Pattern.compile(regex);
83 Matcher matcher = pattern.matcher(configFile);
84 Matcher matcherDebug = pattern.matcher(configFileDebug);
85 Matcher matcherError = pattern.matcher(configFileError);
88 configFile = configFile.substring(1);
90 if (matcherDebug.find()) {
91 configFileDebug = configFileDebug.substring(1);
93 if (matcherError.find()) {
94 configFileError = configFileError.substring(1);
96 testFile1 = classLoader.getResource("LineTest.txt").getFile();
97 testFile2 = classLoader.getResource("LineTest2.txt").getFile();
102 public void tearDown() {
104 logger.debug("tearDown: enter");
106 File file = new File("nonExistFile.txt");
108 logger.debug("tearDown: exit");
112 public void testCountLines() throws IOException {
114 logger.debug("testCountLines: enter");
116 int returnValue = ParseLog.countLines(testFile1);
117 logger.debug("testCountLines: returnValue: " + returnValue);
118 assertEquals(12, returnValue);
120 logger.debug("testCountLines: exit");
124 public void testParseRemoteSystem() {
126 logger.debug("testParseRemoteSystem: enter");
128 String line = "||org.onap.policy.pap.xacml.rest.XACMLPapServlet$Heartbeat.run(XACMLPapServlet.java:2801)||Heartbeat 'https://localhost:8081/pdp/' status='UP_TO_DATE'";
129 String returnValue = ParseLog.parseRemoteSystem(line);
130 logger.debug("testParseRemoteSystem: returnValue: " + returnValue);
131 assertEquals("localhost:8081", returnValue);
133 logger.debug("testParseRemoteSystem: exit");
137 public void testGetPropertiesValue() {
139 logger.debug("testGetPropertiesValue: enter");
141 config = new Properties();
142 config.put("RESOURCE_NAME", "logparser_pap01");
143 config.put("JDBC_DRIVER" ,"org.mariadb.jdbc.Driver");
144 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
145 config.put("JDBC_USER", "root");
146 config.put("JDBC_PASSWORD", "password");
147 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
148 config.put("SERVER", "password");
149 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
150 config.put("LOGTYPE", "PAP");
151 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\pap-rest.log");
152 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
154 Properties returnConfig = ParseLog.getPropertiesValue(configFile);
155 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
156 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
158 logger.debug("testGetPropertiesValue: exit");
162 public void testGetPropertiesValue_1() {
164 logger.debug("testGetPropertiesValue: enter");
166 config = new Properties();
167 config.put("RESOURCE_NAME", "logparser_pap01");
168 config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
169 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
170 config.put("JDBC_USER", "root");
171 config.put("JDBC_PASSWORD", "password");
172 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
173 config.put("SERVER", "password");
174 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
175 config.put("LOGTYPE", "PAP");
176 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\debug\\pap-rest.log");
177 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
179 final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug);
180 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
181 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
183 logger.debug("testGetPropertiesValue_1: exit");
187 public void testGetPropertiesValue_2() {
189 logger.debug("testGetPropertiesValue: enter");
191 config = new Properties();
192 config.put("RESOURCE_NAME", "logparser_pap01");
193 config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
194 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
195 config.put("JDBC_USER", "root");
196 config.put("JDBC_PASSWORD", "password");
197 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
198 config.put("SERVER", "password");
199 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
200 config.put("LOGTYPE", "PAP");
201 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\error\\pap-rest.log");
202 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
204 final Properties returnConfig = ParseLog.getPropertiesValue(configFileError);
205 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
206 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
208 logger.debug("testGetPropertiesValue_2: exit");
212 public void testGetPropertiesFail() {
214 logger.debug("testGetPropertiesFail: enter");
216 Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
217 logger.debug("testGetPropertiesFail: returnValue: " + returnValue);
218 assertEquals(null, returnValue);
220 logger.debug("testGetPropertiesFail: exit");
224 public void testParseDate(){
226 logger.debug("testParseDate: enter");
228 String line = "2016-02-23 08:07:30";
229 Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", false);
230 logger.debug("testParseDate: returnValue: " + returnValue);
231 line = returnValue.toString().substring(0, returnValue.toString().lastIndexOf(":30")+3);
232 assertEquals("Tue Feb 23 08:07:30", line);
234 logger.debug("testParseDate: exit");
238 public void testPullLastLineRead(){
240 logger.debug("testPullLastLineRead: enter");
241 File file = new File(testFile1);
242 String returnValue = null;
244 returnValue = ParseLog.pullLastLineRead(file, "pap-rest.log");
245 logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue);
246 } catch (IOException e) {
249 assertEquals("52", returnValue);
252 returnValue = ParseLog.pullLastLineRead(file, "debug.log");
253 logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue);
254 } catch (IOException e) {
257 assertEquals("17", returnValue);
260 returnValue = ParseLog.pullLastLineRead(file, "error.log");
261 logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue);
262 } catch (IOException e) {
265 assertEquals("22", returnValue);
267 logger.debug("testPullLastLineRead: exit");
271 public void testPullLastLineReadNoFile(){
273 logger.debug("testPullLastLineReadNoFile: enter");
275 File file = new File("nonExistFile.txt");
277 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
278 } catch (IOException e) {
282 logger.debug("testPullLastLineReadNoFile: exit");
286 public void testPullLastLineReadFail(){
288 logger.debug("testPullLastLineReadFail: enter");
290 File file = new File(testFile2);
292 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
293 } catch (IOException e) {
297 logger.debug("testPullLastLineReadFail: exit");
301 public void testPullOutLogValues(){
303 logger.debug("testPullOutLogValues: enter");
306 FileInputStream fstream;
308 fstream = new FileInputStream(testFile1);
309 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
310 String strLine = br.readLine();
311 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "ERROR");
312 assertEquals("ERROR_VALUE", retrunObject.getDescription());
314 } catch (IOException e) {
318 logger.debug("testPullOutLogValues: exit");
322 public void testGetPaths(){
324 logger.debug("testGetPaths: enter");
328 String logPaths = "C:\\pap-log\\pap-rest.log;C:\\pap-log\\debug.log;C:\\pap-log\\error.log";
329 String [] retrunObject = ParseLog.getPaths(logPaths);
330 assertEquals(3, retrunObject.length);
333 logPaths = "C:\\pap-log\\pap-rest.log";
334 retrunObject = ParseLog.getPaths(logPaths);
335 assertEquals(1, retrunObject.length);
339 retrunObject = ParseLog.getPaths(logPaths);
340 assertTrue(retrunObject == null);
342 } catch (Exception e) {
346 logger.debug("testGetPaths: exit");
350 public void testPullOutLogValuesSecond(){
352 logger.debug("testPullOutLogValuesSecond: enter");
355 FileInputStream fstream;
357 fstream = new FileInputStream(testFile1);
358 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
359 String strLine = br.readLine();
360 strLine = br.readLine();
361 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "INFO");
362 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
364 } catch (IOException e) {
368 logger.debug("testPullOutLogValuesSecond: exit");
372 public void testPullOutLogValuesThird(){
374 logger.debug("testPullOutLogValuesThird: enter");
377 FileInputStream fstream;
380 fstream = new FileInputStream(testFile1);
381 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
382 String strLine = br.readLine();
383 for (int i =0; i < number; i++){
384 strLine = br.readLine();
386 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
387 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
389 } catch (IOException e) {
393 logger.debug("testPullOutLogValuesThird: exit");
397 public void testPullOutLogValuesFourth(){
399 logger.debug("testPullOutLogValuesFourth: enter");
401 FileInputStream fstream;
404 fstream = new FileInputStream(testFile1);
405 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
406 String strLine = br.readLine();
407 for (int i =0; i < number; i++){
408 strLine = br.readLine();
410 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
411 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
413 } catch (IOException e) {
417 logger.debug("testPullOutLogValuesFourth: exit");
421 public void testPullOutLogValuesFith(){
423 logger.debug("testPullOutLogValuesFith: enter");
425 FileInputStream fstream;
428 fstream = new FileInputStream(testFile1);
429 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
430 String strLine = br.readLine();
431 for (int i =0; i < number; i++){
432 strLine = br.readLine();
434 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
435 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
437 } catch (IOException e) {
441 logger.debug("testPullOutLogValuesFith: exit");
445 public void testPullOutLogValuesSixth(){
447 logger.debug("testPullOutLogValuesSixth: enter");
449 FileInputStream fstream;
452 fstream = new FileInputStream(testFile1);
453 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
454 String strLine = br.readLine();
455 for (int i =0; i < number; i++){
456 strLine = br.readLine();
458 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
459 assertEquals(LOGTYPE.SEVERE, retrunObject.getLogType());
461 } catch (IOException e) {
465 logger.debug("testPullOutLogValuesSixth: exit");
469 public void testPullOutLogValuesSeven(){
471 logger.debug("testPullOutLogValuesSeven: enter");
473 FileInputStream fstream;
476 fstream = new FileInputStream(testFile1);
477 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
478 String strLine = br.readLine();
479 for (int i =0; i < number; i++){
480 strLine = br.readLine();
482 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
483 assertEquals(LOGTYPE.ERROR, retrunObject.getLogType());
485 } catch (IOException e) {
489 logger.debug("testPullOutLogValuesSeven: exit");
493 public void testPullOutLogValuesEight(){
495 logger.debug("testPullOutLogValuesEight: enter");
497 FileInputStream fstream;
500 fstream = new FileInputStream(testFile1);
501 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
502 String strLine = br.readLine();
503 for (int i =0; i < number; i++){
504 strLine = br.readLine();
506 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
507 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
509 } catch (IOException e) {
513 logger.debug("testPullOutLogValuesEight: exit");
517 public void testPullOutLogValuesNull(){
519 logger.debug("testPullOutLogValuesNull: enter");
521 LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
522 assertEquals(null, retrunObject);
524 logger.debug("testPullOutLogValuesNull: exit");
528 public void testLogEntryObject(){
530 logger.debug("testLogEntryObject: enter");
532 Date date = new Date();
535 LogEntryObject logObject = new LogEntryObject();
536 logObject.setSystem("vm02");
537 logObject.setSystemType("pap");
538 logObject.setDate(date);
539 logObject.setRemote("remote");
541 assertEquals("vm02", logObject.getSystem());
542 assertEquals("pap", logObject.getSystemType());
543 assertEquals(date, logObject.getDate());
544 assertEquals("remote", logObject.getRemote());
546 logger.debug("testLogEntryObject: exit");
550 public void testProcess(){
552 logger.debug("testProcess: enter");
554 String line = "2015-04-01 09:13:44.947 DEBUG 17482 --- [nio-8480-exec-7] c.a.l.onap.policy.std.StdPolicyConfig : config Retrieved ";
556 im = Mockito.mock(IntegrityMonitor.class);
558 Mockito.doNothing().when(im).startTransaction();
559 } catch (IntegrityMonitorException e) {
562 Mockito.doNothing().when(im).endTransaction();
563 ParseLog.process(line, "pap", LOGTYPE.INFO);
565 logger.debug("testProcess: exit");
569 public void testMain() {
571 ParseLog.main(new String[] {});
572 } catch (final Exception e) {
573 logger.debug("exception occured while executing the test: exit");
578 public void testMainDebug() {
580 final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug);
581 PowerMockito.mockStatic(IntegrityMonitor.class);
582 Mockito.when(IntegrityMonitor.getInstance("test", returnConfig)).thenReturn(im);
583 ParseLog.main(new String[] {});
585 } catch (final Exception e) {
586 logger.debug("exception occured while executing the test: exit");
591 public void testMainError() {
593 final Properties returnConfig = ParseLog.getPropertiesValue(configFileError);
594 PowerMockito.mockStatic(IntegrityMonitor.class);
595 Mockito.when(IntegrityMonitor.getInstance("test", returnConfig)).thenReturn(im);
596 ParseLog.main(new String[] {});
598 } catch (final Exception e) {
599 logger.debug("exception occured while executing the test: exit");