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.IntegrityMonitorException;
44 import org.onap.policy.common.im.StandbyStatusException;
45 import org.onap.policy.common.logging.flexlogger.FlexLogger;
46 import org.onap.policy.common.logging.flexlogger.Logger;
47 import org.onap.xacml.parser.LogEntryObject.LOGTYPE;
50 public class ParseLogTest {
52 private static Logger logger = FlexLogger.getLogger(ParseLogTest.class);
53 private Properties config = new Properties();
54 private String configFile;
55 private String configFileDebug;
56 private String configFileError;
57 private String testFile1;
58 private String testFile2;
59 private IntegrityMonitor im;
62 public void setUp() throws Exception {
63 System.setProperty("com.sun.management.jmxremote.port", "9998");
64 im = Mockito.mock(IntegrityMonitor.class);
65 String regex = "^\\/[a-zA-Z]\\:\\/";
68 Mockito.doNothing().when(im).startTransaction();
69 } catch (StandbyStatusException | AdministrativeStateException e) {
72 Mockito.doNothing().when(im).endTransaction();
73 ClassLoader classLoader = getClass().getClassLoader();
74 configFile = classLoader.getResource("test_config.properties").getFile();
75 configFileDebug = classLoader.getResource("test_config_debug.properties").getFile();
76 configFileError = classLoader.getResource("test_config_error.properties").getFile();
77 Pattern pattern = Pattern.compile(regex);
78 Matcher matcher = pattern.matcher(configFile);
79 Matcher matcherDebug = pattern.matcher(configFileDebug);
80 Matcher matcherError = pattern.matcher(configFileError);
83 configFile = configFile.substring(1);
85 if (matcherDebug.find()) {
86 configFileDebug = configFileDebug.substring(1);
88 if (matcherError.find()) {
89 configFileError = configFileError.substring(1);
91 testFile1 = classLoader.getResource("LineTest.txt").getFile();
92 testFile2 = classLoader.getResource("LineTest2.txt").getFile();
97 public void tearDown() {
99 logger.debug("tearDown: enter");
101 File file = new File("nonExistFile.txt");
103 logger.debug("tearDown: exit");
107 public void testCountLines() throws IOException {
109 logger.debug("testCountLines: enter");
111 int returnValue = ParseLog.countLines(testFile1);
112 logger.debug("testCountLines: returnValue: " + returnValue);
113 assertEquals(12, returnValue);
115 logger.debug("testCountLines: exit");
119 public void testParseRemoteSystem() {
121 logger.debug("testParseRemoteSystem: enter");
123 String line = "||org.onap.policy.pap.xacml.rest.XACMLPapServlet$Heartbeat.run(XACMLPapServlet.java:2801)||Heartbeat 'https://localhost:8081/pdp/' status='UP_TO_DATE'";
124 String returnValue = ParseLog.parseRemoteSystem(line);
125 logger.debug("testParseRemoteSystem: returnValue: " + returnValue);
126 assertEquals("localhost:8081", returnValue);
128 logger.debug("testParseRemoteSystem: exit");
132 public void testGetPropertiesValue() {
134 logger.debug("testGetPropertiesValue: enter");
136 config = new Properties();
137 config.put("RESOURCE_NAME", "logparser_pap01");
138 config.put("JDBC_DRIVER" ,"org.mariadb.jdbc.Driver");
139 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
140 config.put("JDBC_USER", "root");
141 config.put("JDBC_PASSWORD", "password");
142 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
143 config.put("SERVER", "password");
144 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
145 config.put("LOGTYPE", "PAP");
146 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\pap-rest.log");
147 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
149 Properties returnConfig = ParseLog.getPropertiesValue(configFile);
150 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
151 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
153 logger.debug("testGetPropertiesValue: exit");
157 public void testGetPropertiesValue_1() {
159 logger.debug("testGetPropertiesValue: enter");
161 config = new Properties();
162 config.put("RESOURCE_NAME", "logparser_pap01");
163 config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
164 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
165 config.put("JDBC_USER", "root");
166 config.put("JDBC_PASSWORD", "password");
167 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
168 config.put("SERVER", "password");
169 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
170 config.put("LOGTYPE", "PAP");
171 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\debug\\pap-rest.log");
172 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
174 final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug);
175 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
176 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
178 logger.debug("testGetPropertiesValue_1: exit");
182 public void testGetPropertiesValue_2() {
184 logger.debug("testGetPropertiesValue: enter");
186 config = new Properties();
187 config.put("RESOURCE_NAME", "logparser_pap01");
188 config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver");
189 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
190 config.put("JDBC_USER", "root");
191 config.put("JDBC_PASSWORD", "password");
192 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
193 config.put("SERVER", "password");
194 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
195 config.put("LOGTYPE", "PAP");
196 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\error\\pap-rest.log");
197 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
199 final Properties returnConfig = ParseLog.getPropertiesValue(configFileError);
200 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
201 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
203 logger.debug("testGetPropertiesValue_2: exit");
207 public void testGetPropertiesFail() {
209 logger.debug("testGetPropertiesFail: enter");
211 Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
212 logger.debug("testGetPropertiesFail: returnValue: " + returnValue);
213 assertEquals(null, returnValue);
215 logger.debug("testGetPropertiesFail: exit");
219 public void testParseDate(){
221 logger.debug("testParseDate: enter");
223 String line = "2016-02-23 08:07:30";
224 Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", false);
225 logger.debug("testParseDate: returnValue: " + returnValue);
226 line = returnValue.toString().substring(0, returnValue.toString().lastIndexOf(":30")+3);
227 assertEquals("Tue Feb 23 08:07:30", line);
229 logger.debug("testParseDate: exit");
233 public void testPullLastLineRead(){
235 logger.debug("testPullLastLineRead: enter");
236 File file = new File(testFile1);
237 String returnValue = null;
239 returnValue = ParseLog.pullLastLineRead(file, "pap-rest.log");
240 logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue);
241 } catch (IOException e) {
244 assertEquals("52", returnValue);
247 returnValue = ParseLog.pullLastLineRead(file, "debug.log");
248 logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue);
249 } catch (IOException e) {
252 assertEquals("17", returnValue);
255 returnValue = ParseLog.pullLastLineRead(file, "error.log");
256 logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue);
257 } catch (IOException e) {
260 assertEquals("22", returnValue);
262 logger.debug("testPullLastLineRead: exit");
266 public void testPullLastLineReadNoFile(){
268 logger.debug("testPullLastLineReadNoFile: enter");
270 File file = new File("nonExistFile.txt");
272 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
273 } catch (IOException e) {
277 logger.debug("testPullLastLineReadNoFile: exit");
281 public void testPullLastLineReadFail(){
283 logger.debug("testPullLastLineReadFail: enter");
285 File file = new File(testFile2);
287 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
288 } catch (IOException e) {
292 logger.debug("testPullLastLineReadFail: exit");
296 public void testPullOutLogValues(){
298 logger.debug("testPullOutLogValues: enter");
301 FileInputStream fstream;
303 fstream = new FileInputStream(testFile1);
304 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
305 String strLine = br.readLine();
306 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "ERROR");
307 assertEquals("ERROR_VALUE", retrunObject.getDescription());
309 } catch (IOException e) {
313 logger.debug("testPullOutLogValues: exit");
317 public void testGetPaths(){
319 logger.debug("testGetPaths: enter");
323 String logPaths = "C:\\pap-log\\pap-rest.log;C:\\pap-log\\debug.log;C:\\pap-log\\error.log";
324 String [] retrunObject = ParseLog.getPaths(logPaths);
325 assertEquals(3, retrunObject.length);
328 logPaths = "C:\\pap-log\\pap-rest.log";
329 retrunObject = ParseLog.getPaths(logPaths);
330 assertEquals(1, retrunObject.length);
334 retrunObject = ParseLog.getPaths(logPaths);
335 assertTrue(retrunObject == null);
337 } catch (Exception e) {
341 logger.debug("testGetPaths: exit");
345 public void testPullOutLogValuesSecond(){
347 logger.debug("testPullOutLogValuesSecond: enter");
350 FileInputStream fstream;
352 fstream = new FileInputStream(testFile1);
353 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
354 String strLine = br.readLine();
355 strLine = br.readLine();
356 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "INFO");
357 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
359 } catch (IOException e) {
363 logger.debug("testPullOutLogValuesSecond: exit");
367 public void testPullOutLogValuesThird(){
369 logger.debug("testPullOutLogValuesThird: enter");
372 FileInputStream fstream;
375 fstream = new FileInputStream(testFile1);
376 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
377 String strLine = br.readLine();
378 for (int i =0; i < number; i++){
379 strLine = br.readLine();
381 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
382 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
384 } catch (IOException e) {
388 logger.debug("testPullOutLogValuesThird: exit");
392 public void testPullOutLogValuesFourth(){
394 logger.debug("testPullOutLogValuesFourth: enter");
396 FileInputStream fstream;
399 fstream = new FileInputStream(testFile1);
400 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
401 String strLine = br.readLine();
402 for (int i =0; i < number; i++){
403 strLine = br.readLine();
405 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
406 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
408 } catch (IOException e) {
412 logger.debug("testPullOutLogValuesFourth: exit");
416 public void testPullOutLogValuesFith(){
418 logger.debug("testPullOutLogValuesFith: enter");
420 FileInputStream fstream;
423 fstream = new FileInputStream(testFile1);
424 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
425 String strLine = br.readLine();
426 for (int i =0; i < number; i++){
427 strLine = br.readLine();
429 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
430 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
432 } catch (IOException e) {
436 logger.debug("testPullOutLogValuesFith: exit");
440 public void testPullOutLogValuesSixth(){
442 logger.debug("testPullOutLogValuesSixth: enter");
444 FileInputStream fstream;
447 fstream = new FileInputStream(testFile1);
448 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
449 String strLine = br.readLine();
450 for (int i =0; i < number; i++){
451 strLine = br.readLine();
453 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
454 assertEquals(LOGTYPE.SEVERE, retrunObject.getLogType());
456 } catch (IOException e) {
460 logger.debug("testPullOutLogValuesSixth: exit");
464 public void testPullOutLogValuesSeven(){
466 logger.debug("testPullOutLogValuesSeven: enter");
468 FileInputStream fstream;
471 fstream = new FileInputStream(testFile1);
472 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
473 String strLine = br.readLine();
474 for (int i =0; i < number; i++){
475 strLine = br.readLine();
477 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
478 assertEquals(LOGTYPE.ERROR, retrunObject.getLogType());
480 } catch (IOException e) {
484 logger.debug("testPullOutLogValuesSeven: exit");
488 public void testPullOutLogValuesEight(){
490 logger.debug("testPullOutLogValuesEight: enter");
492 FileInputStream fstream;
495 fstream = new FileInputStream(testFile1);
496 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
497 String strLine = br.readLine();
498 for (int i =0; i < number; i++){
499 strLine = br.readLine();
501 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
502 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
504 } catch (IOException e) {
508 logger.debug("testPullOutLogValuesEight: exit");
512 public void testPullOutLogValuesNull(){
514 logger.debug("testPullOutLogValuesNull: enter");
516 LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
517 assertEquals(null, retrunObject);
519 logger.debug("testPullOutLogValuesNull: exit");
523 public void testLogEntryObject(){
525 logger.debug("testLogEntryObject: enter");
527 Date date = new Date();
530 LogEntryObject logObject = new LogEntryObject();
531 logObject.setSystem("vm02");
532 logObject.setSystemType("pap");
533 logObject.setDate(date);
534 logObject.setRemote("remote");
536 assertEquals("vm02", logObject.getSystem());
537 assertEquals("pap", logObject.getSystemType());
538 assertEquals(date, logObject.getDate());
539 assertEquals("remote", logObject.getRemote());
541 logger.debug("testLogEntryObject: exit");
545 public void testProcess(){
547 logger.debug("testProcess: enter");
549 String line = "2015-04-01 09:13:44.947 DEBUG 17482 --- [nio-8480-exec-7] c.a.l.onap.policy.std.StdPolicyConfig : config Retrieved ";
551 im = Mockito.mock(IntegrityMonitor.class);
553 Mockito.doNothing().when(im).startTransaction();
554 } catch (IntegrityMonitorException e) {
557 Mockito.doNothing().when(im).endTransaction();
558 ParseLog.process(line, "pap", LOGTYPE.INFO);
560 logger.debug("testProcess: exit");
564 public void testMain() {
566 ParseLog.main(new String[] {});
567 } catch (final Exception e) {
568 logger.debug("exception occured while executing the test: exit");