2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018, 2020 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.PowerMockIgnore;
51 import org.powermock.core.classloader.annotations.PrepareForTest;
52 import org.powermock.modules.junit4.PowerMockRunner;
54 @RunWith(PowerMockRunner.class)
55 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "jdk.internal.reflect.*", "javax.xml.*", "org.xml.*"})
56 @PrepareForTest({ ParseLogTest.class, IntegrityMonitor.class })
57 public class ParseLogTest {
59 private static Logger logger = FlexLogger.getLogger(ParseLogTest.class);
60 private Properties config = new Properties();
61 private String configFile;
62 private String configFileDebug;
63 private String configFileError;
64 private String testFile1;
65 private String testFile2;
66 private IntegrityMonitor im;
69 * Setup for the test case execution.
71 * @throws Exception if any error occurs
74 public void setUp() throws Exception {
75 System.setProperty("com.sun.management.jmxremote.port", "9998");
76 im = Mockito.mock(IntegrityMonitor.class);
77 final String regex = "^\\/[a-zA-Z]\\:\\/";
80 Mockito.doNothing().when(im).startTransaction();
81 } catch (StandbyStatusException | AdministrativeStateException e) {
84 Mockito.doNothing().when(im).endTransaction();
85 final ClassLoader classLoader = getClass().getClassLoader();
86 configFile = classLoader.getResource("test_config.properties").getFile();
87 configFileDebug = classLoader.getResource("test_config_debug.properties").getFile();
88 configFileError = classLoader.getResource("test_config_error.properties").getFile();
89 final Pattern pattern = Pattern.compile(regex);
90 final Matcher matcher = pattern.matcher(configFile);
91 final Matcher matcherDebug = pattern.matcher(configFileDebug);
92 final Matcher matcherError = pattern.matcher(configFileError);
95 configFile = configFile.substring(1);
97 if (matcherDebug.find()) {
98 configFileDebug = configFileDebug.substring(1);
100 if (matcherError.find()) {
101 configFileError = configFileError.substring(1);
103 testFile1 = classLoader.getResource("LineTest.txt").getFile();
104 testFile2 = classLoader.getResource("LineTest2.txt").getFile();
109 * Cleaning off after test case execution.
112 public void tearDown() {
114 logger.debug("tearDown: enter");
116 final File file = new File("nonExistFile.txt");
118 logger.debug("tearDown: exit");
122 public void testCountLines() throws IOException {
124 logger.debug("testCountLines: enter");
126 final int returnValue = ParseLog.countLines(testFile1);
127 logger.debug("testCountLines: returnValue: " + returnValue);
128 assertEquals(12, returnValue);
130 logger.debug("testCountLines: exit");
134 public void testParseRemoteSystem() {
136 logger.debug("testParseRemoteSystem: enter");
139 "||org.onap.policy.pap.xacml.rest.XACMLPapServlet$Heartbeat.run(XACMLPapServlet.java:2801)||Heartbeat 'https://localhost:8081/pdp/' status='UP_TO_DATE'";
140 final String returnValue = ParseLog.parseRemoteSystem(line);
141 logger.debug("testParseRemoteSystem: returnValue: " + returnValue);
142 assertEquals("localhost:8081", returnValue);
144 logger.debug("testParseRemoteSystem: exit");
148 public void testGetPropertiesValue() {
150 logger.debug("testGetPropertiesValue: enter");
152 config = new Properties();
153 config.put("RESOURCE_NAME", "logparser_pap01");
154 config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
155 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
156 config.put("JDBC_USER", "root");
157 config.put("JDBC_PASSWORD", "password");
158 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
159 config.put("SERVER", "password");
160 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
161 config.put("LOGTYPE", "PAP");
162 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\pap-rest.log");
163 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
165 final Properties returnConfig = ParseLog.getPropertiesValue(configFile);
166 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
167 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
169 logger.debug("testGetPropertiesValue: exit");
173 public void testGetPropertiesValue_1() {
175 logger.debug("testGetPropertiesValue: enter");
177 config = new Properties();
178 config.put("RESOURCE_NAME", "logparser_pap01");
179 config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
180 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
181 config.put("JDBC_USER", "root");
182 config.put("JDBC_PASSWORD", "password");
183 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
184 config.put("SERVER", "password");
185 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
186 config.put("LOGTYPE", "PAP");
187 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\debug\\pap-rest.log");
188 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
190 final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug);
191 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
192 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
194 logger.debug("testGetPropertiesValue_1: exit");
198 public void testGetPropertiesValue_2() {
200 logger.debug("testGetPropertiesValue: enter");
202 config = new Properties();
203 config.put("RESOURCE_NAME", "logparser_pap01");
204 config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
205 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
206 config.put("JDBC_USER", "root");
207 config.put("JDBC_PASSWORD", "password");
208 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
209 config.put("SERVER", "password");
210 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
211 config.put("LOGTYPE", "PAP");
212 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\error\\pap-rest.log");
213 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
215 final Properties returnConfig = ParseLog.getPropertiesValue(configFileError);
216 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
217 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
219 logger.debug("testGetPropertiesValue_2: exit");
223 public void testGetPropertiesFail() {
225 logger.debug("testGetPropertiesFail: enter");
227 final Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
228 logger.debug("testGetPropertiesFail: returnValue: " + returnValue);
229 assertEquals(null, returnValue);
231 logger.debug("testGetPropertiesFail: exit");
235 public void testParseDate() {
237 logger.debug("testParseDate: enter");
239 String line = "2016-02-23 08:07:30";
240 final Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", false);
241 logger.debug("testParseDate: returnValue: " + returnValue);
242 line = returnValue.toString().substring(0, returnValue.toString().lastIndexOf(":30") + 3);
243 assertEquals("Tue Feb 23 08:07:30", line);
245 logger.debug("testParseDate: exit");
249 public void testPullLastLineRead() {
251 logger.debug("testPullLastLineRead: enter");
252 final File file = new File(testFile1);
253 String returnValue = null;
255 returnValue = ParseLog.pullLastLineRead(file, "pap-rest.log");
256 logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue);
257 } catch (final IOException e) {
260 assertEquals("52", returnValue);
263 returnValue = ParseLog.pullLastLineRead(file, "debug.log");
264 logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue);
265 } catch (final IOException e) {
268 assertEquals("17", returnValue);
271 returnValue = ParseLog.pullLastLineRead(file, "error.log");
272 logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue);
273 } catch (final IOException e) {
276 assertEquals("22", returnValue);
278 logger.debug("testPullLastLineRead: exit");
282 public void testPullLastLineReadNoFile() {
284 logger.debug("testPullLastLineReadNoFile: enter");
286 final File file = new File("nonExistFile.txt");
288 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
289 } catch (final IOException e) {
293 logger.debug("testPullLastLineReadNoFile: exit");
297 public void testPullLastLineReadFail() {
299 logger.debug("testPullLastLineReadFail: enter");
301 final File file = new File(testFile2);
303 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
304 } catch (final IOException e) {
308 logger.debug("testPullLastLineReadFail: exit");
312 public void testPullOutLogValues() {
314 logger.debug("testPullOutLogValues: enter");
317 FileInputStream fstream;
319 fstream = new FileInputStream(testFile1);
320 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
321 final String strLine = br.readLine();
322 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "ERROR");
323 assertEquals("ERROR_VALUE", retrunObject.getDescription());
325 } catch (final IOException e) {
329 logger.debug("testPullOutLogValues: exit");
333 public void testGetPaths() {
335 logger.debug("testGetPaths: enter");
339 String logPaths = "C:\\pap-log\\pap-rest.log;C:\\pap-log\\debug.log;C:\\pap-log\\error.log";
340 String[] retrunObject = ParseLog.getPaths(logPaths);
341 assertEquals(3, retrunObject.length);
344 logPaths = "C:\\pap-log\\pap-rest.log";
345 retrunObject = ParseLog.getPaths(logPaths);
346 assertEquals(1, retrunObject.length);
350 retrunObject = ParseLog.getPaths(logPaths);
351 assertTrue(retrunObject == null);
353 } catch (final Exception e) {
357 logger.debug("testGetPaths: exit");
361 public void testPullOutLogValuesSecond() {
363 logger.debug("testPullOutLogValuesSecond: enter");
366 FileInputStream fstream;
368 fstream = new FileInputStream(testFile1);
369 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
370 String strLine = br.readLine();
371 strLine = br.readLine();
372 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "INFO");
373 assertEquals(LogType.INFO, retrunObject.getLogType());
375 } catch (final IOException e) {
379 logger.debug("testPullOutLogValuesSecond: exit");
383 public void testPullOutLogValuesThird() {
385 logger.debug("testPullOutLogValuesThird: enter");
388 FileInputStream fstream;
390 final int number = 3;
391 fstream = new FileInputStream(testFile1);
392 final 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 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
398 assertEquals(LogType.INFO, retrunObject.getLogType());
400 } catch (final IOException e) {
404 logger.debug("testPullOutLogValuesThird: exit");
408 public void testPullOutLogValuesFourth() {
410 logger.debug("testPullOutLogValuesFourth: enter");
412 FileInputStream fstream;
414 final int number = 4;
415 fstream = new FileInputStream(testFile1);
416 final 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 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
422 assertEquals(LogType.INFO, retrunObject.getLogType());
424 } catch (final IOException e) {
428 logger.debug("testPullOutLogValuesFourth: exit");
432 public void testPullOutLogValuesFith() {
434 logger.debug("testPullOutLogValuesFith: enter");
436 FileInputStream fstream;
438 final int number = 5;
439 fstream = new FileInputStream(testFile1);
440 final 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 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
446 assertEquals(LogType.WARN, retrunObject.getLogType());
448 } catch (final IOException e) {
452 logger.debug("testPullOutLogValuesFith: exit");
456 public void testPullOutLogValuesSixth() {
458 logger.debug("testPullOutLogValuesSixth: enter");
460 FileInputStream fstream;
462 final int number = 6;
463 fstream = new FileInputStream(testFile1);
464 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
465 String strLine = br.readLine();
466 for (int i = 0; i < number; i++) {
467 strLine = br.readLine();
469 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
470 assertEquals(LogType.SEVERE, retrunObject.getLogType());
472 } catch (final IOException e) {
476 logger.debug("testPullOutLogValuesSixth: exit");
480 public void testPullOutLogValuesSeven() {
482 logger.debug("testPullOutLogValuesSeven: enter");
484 FileInputStream fstream;
486 final int number = 7;
487 fstream = new FileInputStream(testFile1);
488 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
489 String strLine = br.readLine();
490 for (int i = 0; i < number; i++) {
491 strLine = br.readLine();
493 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
494 assertEquals(LogType.ERROR, retrunObject.getLogType());
496 } catch (final IOException e) {
500 logger.debug("testPullOutLogValuesSeven: exit");
504 public void testPullOutLogValuesEight() {
506 logger.debug("testPullOutLogValuesEight: enter");
508 FileInputStream fstream;
510 final int number = 8;
511 fstream = new FileInputStream(testFile1);
512 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
513 String strLine = br.readLine();
514 for (int i = 0; i < number; i++) {
515 strLine = br.readLine();
517 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
518 assertEquals(LogType.WARN, retrunObject.getLogType());
520 } catch (final IOException e) {
524 logger.debug("testPullOutLogValuesEight: exit");
528 public void testPullOutLogValuesNull() {
530 logger.debug("testPullOutLogValuesNull: enter");
532 final LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
533 assertEquals(null, retrunObject);
535 logger.debug("testPullOutLogValuesNull: exit");
539 public void testLogEntryObject() {
541 logger.debug("testLogEntryObject: enter");
543 final Date date = new Date();
546 final LogEntryObject logObject = new LogEntryObject();
547 logObject.setSystem("vm02");
548 logObject.setSystemType("pap");
549 logObject.setDate(date);
550 logObject.setRemote("remote");
552 assertEquals("vm02", logObject.getSystem());
553 assertEquals("pap", logObject.getSystemType());
554 assertEquals(date, logObject.getDate());
555 assertEquals("remote", logObject.getRemote());
557 logger.debug("testLogEntryObject: exit");
561 public void testProcess() {
563 logger.debug("testProcess: enter");
565 final String line = "2015-04-01 09:13:44.947" + " DEBUG 17482 --- [nio-8480-exec-7] "
566 + "c.a.l.onap.policy.std.StdPolicyConfig" + " : config Retrieved ";
568 im = Mockito.mock(IntegrityMonitor.class);
570 Mockito.doNothing().when(im).startTransaction();
571 } catch (final IntegrityMonitorException e) {
574 Mockito.doNothing().when(im).endTransaction();
575 ParseLog.process(line, "pap", LogType.INFO);
577 logger.debug("testProcess: exit");
581 public void testMain() {
583 ParseLog.main(new String[] {});
584 } catch (final Exception e) {
585 logger.debug("exception occured while executing the test: exit");
590 public void testMainDebug() {
592 final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug);
593 PowerMockito.mockStatic(IntegrityMonitor.class);
594 Mockito.when(IntegrityMonitor.getInstance("test", returnConfig)).thenReturn(im);
595 ParseLog.main(new String[] {});
597 } catch (final Exception e) {
598 logger.debug("exception occured while executing the test: exit");
603 public void testMainError() {
605 final Properties returnConfig = ParseLog.getPropertiesValue(configFileError);
606 PowerMockito.mockStatic(IntegrityMonitor.class);
607 Mockito.when(IntegrityMonitor.getInstance("test", returnConfig)).thenReturn(im);
608 ParseLog.main(new String[] {});
610 } catch (final Exception e) {
611 logger.debug("exception occured while executing the test: exit");