2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * Setup for the test case execution.
69 * @throws Exception if any error occurs
72 public void setUp() throws Exception {
73 System.setProperty("com.sun.management.jmxremote.port", "9998");
74 im = Mockito.mock(IntegrityMonitor.class);
75 final String regex = "^\\/[a-zA-Z]\\:\\/";
78 Mockito.doNothing().when(im).startTransaction();
79 } catch (StandbyStatusException | AdministrativeStateException e) {
82 Mockito.doNothing().when(im).endTransaction();
83 final ClassLoader classLoader = getClass().getClassLoader();
84 configFile = classLoader.getResource("test_config.properties").getFile();
85 configFileDebug = classLoader.getResource("test_config_debug.properties").getFile();
86 configFileError = classLoader.getResource("test_config_error.properties").getFile();
87 final Pattern pattern = Pattern.compile(regex);
88 final Matcher matcher = pattern.matcher(configFile);
89 final Matcher matcherDebug = pattern.matcher(configFileDebug);
90 final Matcher matcherError = pattern.matcher(configFileError);
93 configFile = configFile.substring(1);
95 if (matcherDebug.find()) {
96 configFileDebug = configFileDebug.substring(1);
98 if (matcherError.find()) {
99 configFileError = configFileError.substring(1);
101 testFile1 = classLoader.getResource("LineTest.txt").getFile();
102 testFile2 = classLoader.getResource("LineTest2.txt").getFile();
107 * Cleaning off after test case execution.
110 public void tearDown() {
112 logger.debug("tearDown: enter");
114 final File file = new File("nonExistFile.txt");
116 logger.debug("tearDown: exit");
120 public void testCountLines() throws IOException {
122 logger.debug("testCountLines: enter");
124 final int returnValue = ParseLog.countLines(testFile1);
125 logger.debug("testCountLines: returnValue: " + returnValue);
126 assertEquals(12, returnValue);
128 logger.debug("testCountLines: exit");
132 public void testParseRemoteSystem() {
134 logger.debug("testParseRemoteSystem: enter");
137 "||org.onap.policy.pap.xacml.rest.XACMLPapServlet$Heartbeat.run(XACMLPapServlet.java:2801)||Heartbeat 'https://localhost:8081/pdp/' status='UP_TO_DATE'";
138 final String returnValue = ParseLog.parseRemoteSystem(line);
139 logger.debug("testParseRemoteSystem: returnValue: " + returnValue);
140 assertEquals("localhost:8081", returnValue);
142 logger.debug("testParseRemoteSystem: exit");
146 public void testGetPropertiesValue() {
148 logger.debug("testGetPropertiesValue: enter");
150 config = new Properties();
151 config.put("RESOURCE_NAME", "logparser_pap01");
152 config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
153 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
154 config.put("JDBC_USER", "root");
155 config.put("JDBC_PASSWORD", "password");
156 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
157 config.put("SERVER", "password");
158 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
159 config.put("LOGTYPE", "PAP");
160 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\pap-rest.log");
161 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
163 final Properties returnConfig = ParseLog.getPropertiesValue(configFile);
164 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
165 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
167 logger.debug("testGetPropertiesValue: exit");
171 public void testGetPropertiesValue_1() {
173 logger.debug("testGetPropertiesValue: enter");
175 config = new Properties();
176 config.put("RESOURCE_NAME", "logparser_pap01");
177 config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
178 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
179 config.put("JDBC_USER", "root");
180 config.put("JDBC_PASSWORD", "password");
181 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
182 config.put("SERVER", "password");
183 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
184 config.put("LOGTYPE", "PAP");
185 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\debug\\pap-rest.log");
186 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
188 final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug);
189 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
190 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
192 logger.debug("testGetPropertiesValue_1: exit");
196 public void testGetPropertiesValue_2() {
198 logger.debug("testGetPropertiesValue: enter");
200 config = new Properties();
201 config.put("RESOURCE_NAME", "logparser_pap01");
202 config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
203 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
204 config.put("JDBC_USER", "root");
205 config.put("JDBC_PASSWORD", "password");
206 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
207 config.put("SERVER", "password");
208 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
209 config.put("LOGTYPE", "PAP");
210 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\error\\pap-rest.log");
211 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
213 final Properties returnConfig = ParseLog.getPropertiesValue(configFileError);
214 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
215 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
217 logger.debug("testGetPropertiesValue_2: exit");
221 public void testGetPropertiesFail() {
223 logger.debug("testGetPropertiesFail: enter");
225 final Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
226 logger.debug("testGetPropertiesFail: returnValue: " + returnValue);
227 assertEquals(null, returnValue);
229 logger.debug("testGetPropertiesFail: exit");
233 public void testParseDate() {
235 logger.debug("testParseDate: enter");
237 String line = "2016-02-23 08:07:30";
238 final Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", false);
239 logger.debug("testParseDate: returnValue: " + returnValue);
240 line = returnValue.toString().substring(0, returnValue.toString().lastIndexOf(":30") + 3);
241 assertEquals("Tue Feb 23 08:07:30", line);
243 logger.debug("testParseDate: exit");
247 public void testPullLastLineRead() {
249 logger.debug("testPullLastLineRead: enter");
250 final File file = new File(testFile1);
251 String returnValue = null;
253 returnValue = ParseLog.pullLastLineRead(file, "pap-rest.log");
254 logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue);
255 } catch (final IOException e) {
258 assertEquals("52", returnValue);
261 returnValue = ParseLog.pullLastLineRead(file, "debug.log");
262 logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue);
263 } catch (final IOException e) {
266 assertEquals("17", returnValue);
269 returnValue = ParseLog.pullLastLineRead(file, "error.log");
270 logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue);
271 } catch (final IOException e) {
274 assertEquals("22", returnValue);
276 logger.debug("testPullLastLineRead: exit");
280 public void testPullLastLineReadNoFile() {
282 logger.debug("testPullLastLineReadNoFile: enter");
284 final File file = new File("nonExistFile.txt");
286 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
287 } catch (final IOException e) {
291 logger.debug("testPullLastLineReadNoFile: exit");
295 public void testPullLastLineReadFail() {
297 logger.debug("testPullLastLineReadFail: enter");
299 final File file = new File(testFile2);
301 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
302 } catch (final IOException e) {
306 logger.debug("testPullLastLineReadFail: exit");
310 public void testPullOutLogValues() {
312 logger.debug("testPullOutLogValues: enter");
315 FileInputStream fstream;
317 fstream = new FileInputStream(testFile1);
318 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
319 final String strLine = br.readLine();
320 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "ERROR");
321 assertEquals("ERROR_VALUE", retrunObject.getDescription());
323 } catch (final IOException e) {
327 logger.debug("testPullOutLogValues: exit");
331 public void testGetPaths() {
333 logger.debug("testGetPaths: enter");
337 String logPaths = "C:\\pap-log\\pap-rest.log;C:\\pap-log\\debug.log;C:\\pap-log\\error.log";
338 String[] retrunObject = ParseLog.getPaths(logPaths);
339 assertEquals(3, retrunObject.length);
342 logPaths = "C:\\pap-log\\pap-rest.log";
343 retrunObject = ParseLog.getPaths(logPaths);
344 assertEquals(1, retrunObject.length);
348 retrunObject = ParseLog.getPaths(logPaths);
349 assertTrue(retrunObject == null);
351 } catch (final Exception e) {
355 logger.debug("testGetPaths: exit");
359 public void testPullOutLogValuesSecond() {
361 logger.debug("testPullOutLogValuesSecond: enter");
364 FileInputStream fstream;
366 fstream = new FileInputStream(testFile1);
367 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
368 String strLine = br.readLine();
369 strLine = br.readLine();
370 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "INFO");
371 assertEquals(LogType.INFO, retrunObject.getLogType());
373 } catch (final IOException e) {
377 logger.debug("testPullOutLogValuesSecond: exit");
381 public void testPullOutLogValuesThird() {
383 logger.debug("testPullOutLogValuesThird: enter");
386 FileInputStream fstream;
388 final int number = 3;
389 fstream = new FileInputStream(testFile1);
390 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
391 String strLine = br.readLine();
392 for (int i = 0; i < number; i++) {
393 strLine = br.readLine();
395 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
396 assertEquals(LogType.INFO, retrunObject.getLogType());
398 } catch (final IOException e) {
402 logger.debug("testPullOutLogValuesThird: exit");
406 public void testPullOutLogValuesFourth() {
408 logger.debug("testPullOutLogValuesFourth: enter");
410 FileInputStream fstream;
412 final int number = 4;
413 fstream = new FileInputStream(testFile1);
414 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
415 String strLine = br.readLine();
416 for (int i = 0; i < number; i++) {
417 strLine = br.readLine();
419 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
420 assertEquals(LogType.INFO, retrunObject.getLogType());
422 } catch (final IOException e) {
426 logger.debug("testPullOutLogValuesFourth: exit");
430 public void testPullOutLogValuesFith() {
432 logger.debug("testPullOutLogValuesFith: enter");
434 FileInputStream fstream;
436 final int number = 5;
437 fstream = new FileInputStream(testFile1);
438 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
439 String strLine = br.readLine();
440 for (int i = 0; i < number; i++) {
441 strLine = br.readLine();
443 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
444 assertEquals(LogType.WARN, retrunObject.getLogType());
446 } catch (final IOException e) {
450 logger.debug("testPullOutLogValuesFith: exit");
454 public void testPullOutLogValuesSixth() {
456 logger.debug("testPullOutLogValuesSixth: enter");
458 FileInputStream fstream;
460 final int number = 6;
461 fstream = new FileInputStream(testFile1);
462 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
463 String strLine = br.readLine();
464 for (int i = 0; i < number; i++) {
465 strLine = br.readLine();
467 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
468 assertEquals(LogType.SEVERE, retrunObject.getLogType());
470 } catch (final IOException e) {
474 logger.debug("testPullOutLogValuesSixth: exit");
478 public void testPullOutLogValuesSeven() {
480 logger.debug("testPullOutLogValuesSeven: enter");
482 FileInputStream fstream;
484 final int number = 7;
485 fstream = new FileInputStream(testFile1);
486 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
487 String strLine = br.readLine();
488 for (int i = 0; i < number; i++) {
489 strLine = br.readLine();
491 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
492 assertEquals(LogType.ERROR, retrunObject.getLogType());
494 } catch (final IOException e) {
498 logger.debug("testPullOutLogValuesSeven: exit");
502 public void testPullOutLogValuesEight() {
504 logger.debug("testPullOutLogValuesEight: enter");
506 FileInputStream fstream;
508 final int number = 8;
509 fstream = new FileInputStream(testFile1);
510 final BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
511 String strLine = br.readLine();
512 for (int i = 0; i < number; i++) {
513 strLine = br.readLine();
515 final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
516 assertEquals(LogType.WARN, retrunObject.getLogType());
518 } catch (final IOException e) {
522 logger.debug("testPullOutLogValuesEight: exit");
526 public void testPullOutLogValuesNull() {
528 logger.debug("testPullOutLogValuesNull: enter");
530 final LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
531 assertEquals(null, retrunObject);
533 logger.debug("testPullOutLogValuesNull: exit");
537 public void testLogEntryObject() {
539 logger.debug("testLogEntryObject: enter");
541 final Date date = new Date();
544 final LogEntryObject logObject = new LogEntryObject();
545 logObject.setSystem("vm02");
546 logObject.setSystemType("pap");
547 logObject.setDate(date);
548 logObject.setRemote("remote");
550 assertEquals("vm02", logObject.getSystem());
551 assertEquals("pap", logObject.getSystemType());
552 assertEquals(date, logObject.getDate());
553 assertEquals("remote", logObject.getRemote());
555 logger.debug("testLogEntryObject: exit");
559 public void testProcess() {
561 logger.debug("testProcess: enter");
563 final String line = "2015-04-01 09:13:44.947" + " DEBUG 17482 --- [nio-8480-exec-7] "
564 + "c.a.l.onap.policy.std.StdPolicyConfig" + " : config Retrieved ";
566 im = Mockito.mock(IntegrityMonitor.class);
568 Mockito.doNothing().when(im).startTransaction();
569 } catch (final IntegrityMonitorException e) {
572 Mockito.doNothing().when(im).endTransaction();
573 ParseLog.process(line, "pap", LogType.INFO);
575 logger.debug("testProcess: exit");
579 public void testMain() {
581 ParseLog.main(new String[] {});
582 } catch (final Exception e) {
583 logger.debug("exception occured while executing the test: exit");
588 public void testMainDebug() {
590 final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug);
591 PowerMockito.mockStatic(IntegrityMonitor.class);
592 Mockito.when(IntegrityMonitor.getInstance("test", returnConfig)).thenReturn(im);
593 ParseLog.main(new String[] {});
595 } catch (final Exception e) {
596 logger.debug("exception occured while executing the test: exit");
601 public void testMainError() {
603 final Properties returnConfig = ParseLog.getPropertiesValue(configFileError);
604 PowerMockito.mockStatic(IntegrityMonitor.class);
605 Mockito.when(IntegrityMonitor.getInstance("test", returnConfig)).thenReturn(im);
606 ParseLog.main(new String[] {});
608 } catch (final Exception e) {
609 logger.debug("exception occured while executing the test: exit");