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 testFile1;
56 private String testFile2;
57 private IntegrityMonitor im;
60 public void setUp() throws Exception {
61 System.setProperty("com.sun.management.jmxremote.port", "9998");
62 im = Mockito.mock(IntegrityMonitor.class);
63 String regex = "^\\/[a-zA-Z]\\:\\/";
66 Mockito.doNothing().when(im).startTransaction();
67 } catch (StandbyStatusException | AdministrativeStateException e) {
70 Mockito.doNothing().when(im).endTransaction();
71 ClassLoader classLoader = getClass().getClassLoader();
72 configFile = classLoader.getResource("test_config.properties").getFile();
73 Pattern pattern = Pattern.compile(regex);
74 Matcher matcher = pattern.matcher(configFile);
77 configFile = configFile.substring(1);
79 testFile1 = classLoader.getResource("LineTest.txt").getFile();
80 testFile2 = classLoader.getResource("LineTest2.txt").getFile();
85 public void tearDown() {
87 logger.debug("tearDown: enter");
89 File file = new File("nonExistFile.txt");
91 logger.debug("tearDown: exit");
95 public void testCountLines() throws IOException {
97 logger.debug("testCountLines: enter");
99 int returnValue = ParseLog.countLines(testFile1);
100 logger.debug("testCountLines: returnValue: " + returnValue);
101 assertEquals(12, returnValue);
103 logger.debug("testCountLines: exit");
107 public void testParseRemoteSystem() {
109 logger.debug("testParseRemoteSystem: enter");
111 String line = "||org.onap.policy.pap.xacml.rest.XACMLPapServlet$Heartbeat.run(XACMLPapServlet.java:2801)||Heartbeat 'https://localhost:8081/pdp/' status='UP_TO_DATE'";
112 String returnValue = ParseLog.parseRemoteSystem(line);
113 logger.debug("testParseRemoteSystem: returnValue: " + returnValue);
114 assertEquals("localhost:8081", returnValue);
116 logger.debug("testParseRemoteSystem: exit");
120 public void testGetPropertiesValue() {
122 logger.debug("testGetPropertiesValue: enter");
124 config = new Properties();
125 config.put("RESOURCE_NAME", "logparser_pap01");
126 config.put("JDBC_DRIVER" ,"org.mariadb.jdbc.Driver");
127 config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/");
128 config.put("JDBC_USER", "root");
129 config.put("JDBC_PASSWORD", "password");
130 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
131 config.put("SERVER", "password");
132 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
133 config.put("LOGTYPE", "PAP");
134 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\pap-rest.log");
135 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
137 Properties returnConfig = ParseLog.getPropertiesValue(configFile);
138 logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig);
139 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
141 logger.debug("testGetPropertiesValue: exit");
145 public void testGetPropertiesFail() {
147 logger.debug("testGetPropertiesFail: enter");
149 Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
150 logger.debug("testGetPropertiesFail: returnValue: " + returnValue);
151 assertEquals(null, returnValue);
153 logger.debug("testGetPropertiesFail: exit");
157 public void testParseDate(){
159 logger.debug("testParseDate: enter");
161 String line = "2016-02-23 08:07:30";
162 Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", false);
163 logger.debug("testParseDate: returnValue: " + returnValue);
164 line = returnValue.toString().substring(0, returnValue.toString().lastIndexOf(":30")+3);
165 assertEquals("Tue Feb 23 08:07:30", line);
167 logger.debug("testParseDate: exit");
171 public void testPullLastLineRead(){
173 logger.debug("testPullLastLineRead: enter");
174 File file = new File(testFile1);
175 String returnValue = null;
177 returnValue = ParseLog.pullLastLineRead(file, "pap-rest.log");
178 logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue);
179 } catch (IOException e) {
182 assertEquals("52", returnValue);
185 returnValue = ParseLog.pullLastLineRead(file, "debug.log");
186 logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue);
187 } catch (IOException e) {
190 assertEquals("17", returnValue);
193 returnValue = ParseLog.pullLastLineRead(file, "error.log");
194 logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue);
195 } catch (IOException e) {
198 assertEquals("22", returnValue);
200 logger.debug("testPullLastLineRead: exit");
204 public void testPullLastLineReadNoFile(){
206 logger.debug("testPullLastLineReadNoFile: enter");
208 File file = new File("nonExistFile.txt");
210 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
211 } catch (IOException e) {
215 logger.debug("testPullLastLineReadNoFile: exit");
219 public void testPullLastLineReadFail(){
221 logger.debug("testPullLastLineReadFail: enter");
223 File file = new File(testFile2);
225 assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest"));
226 } catch (IOException e) {
230 logger.debug("testPullLastLineReadFail: exit");
234 public void testPullOutLogValues(){
236 logger.debug("testPullOutLogValues: enter");
239 FileInputStream fstream;
241 fstream = new FileInputStream(testFile1);
242 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
243 String strLine = br.readLine();
244 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "ERROR");
245 assertEquals("ERROR_VALUE", retrunObject.getDescription());
247 } catch (IOException e) {
251 logger.debug("testPullOutLogValues: exit");
255 public void testGetPaths(){
257 logger.debug("testGetPaths: enter");
261 String logPaths = "C:\\pap-log\\pap-rest.log;C:\\pap-log\\debug.log;C:\\pap-log\\error.log";
262 String [] retrunObject = ParseLog.getPaths(logPaths);
263 assertEquals(3, retrunObject.length);
266 logPaths = "C:\\pap-log\\pap-rest.log";
267 retrunObject = ParseLog.getPaths(logPaths);
268 assertEquals(1, retrunObject.length);
272 retrunObject = ParseLog.getPaths(logPaths);
273 assertTrue(retrunObject == null);
275 } catch (Exception e) {
279 logger.debug("testGetPaths: exit");
283 public void testPullOutLogValuesSecond(){
285 logger.debug("testPullOutLogValuesSecond: enter");
288 FileInputStream fstream;
290 fstream = new FileInputStream(testFile1);
291 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
292 String strLine = br.readLine();
293 strLine = br.readLine();
294 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "INFO");
295 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
297 } catch (IOException e) {
301 logger.debug("testPullOutLogValuesSecond: exit");
305 public void testPullOutLogValuesThird(){
307 logger.debug("testPullOutLogValuesThird: enter");
310 FileInputStream fstream;
313 fstream = new FileInputStream(testFile1);
314 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
315 String strLine = br.readLine();
316 for (int i =0; i < number; i++){
317 strLine = br.readLine();
319 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
320 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
322 } catch (IOException e) {
326 logger.debug("testPullOutLogValuesThird: exit");
330 public void testPullOutLogValuesFourth(){
332 logger.debug("testPullOutLogValuesFourth: enter");
334 FileInputStream fstream;
337 fstream = new FileInputStream(testFile1);
338 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
339 String strLine = br.readLine();
340 for (int i =0; i < number; i++){
341 strLine = br.readLine();
343 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
344 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
346 } catch (IOException e) {
350 logger.debug("testPullOutLogValuesFourth: exit");
354 public void testPullOutLogValuesFith(){
356 logger.debug("testPullOutLogValuesFith: enter");
358 FileInputStream fstream;
361 fstream = new FileInputStream(testFile1);
362 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
363 String strLine = br.readLine();
364 for (int i =0; i < number; i++){
365 strLine = br.readLine();
367 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
368 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
370 } catch (IOException e) {
374 logger.debug("testPullOutLogValuesFith: exit");
378 public void testPullOutLogValuesSixth(){
380 logger.debug("testPullOutLogValuesSixth: enter");
382 FileInputStream fstream;
385 fstream = new FileInputStream(testFile1);
386 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
387 String strLine = br.readLine();
388 for (int i =0; i < number; i++){
389 strLine = br.readLine();
391 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
392 assertEquals(LOGTYPE.SEVERE, retrunObject.getLogType());
394 } catch (IOException e) {
398 logger.debug("testPullOutLogValuesSixth: exit");
402 public void testPullOutLogValuesSeven(){
404 logger.debug("testPullOutLogValuesSeven: enter");
406 FileInputStream fstream;
409 fstream = new FileInputStream(testFile1);
410 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
411 String strLine = br.readLine();
412 for (int i =0; i < number; i++){
413 strLine = br.readLine();
415 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
416 assertEquals(LOGTYPE.ERROR, retrunObject.getLogType());
418 } catch (IOException e) {
422 logger.debug("testPullOutLogValuesSeven: exit");
426 public void testPullOutLogValuesEight(){
428 logger.debug("testPullOutLogValuesEight: enter");
430 FileInputStream fstream;
433 fstream = new FileInputStream(testFile1);
434 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
435 String strLine = br.readLine();
436 for (int i =0; i < number; i++){
437 strLine = br.readLine();
439 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
440 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
442 } catch (IOException e) {
446 logger.debug("testPullOutLogValuesEight: exit");
450 public void testPullOutLogValuesNull(){
452 logger.debug("testPullOutLogValuesNull: enter");
454 LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
455 assertEquals(null, retrunObject);
457 logger.debug("testPullOutLogValuesNull: exit");
461 public void testLogEntryObject(){
463 logger.debug("testLogEntryObject: enter");
465 Date date = new Date();
468 LogEntryObject logObject = new LogEntryObject();
469 logObject.setSystem("vm02");
470 logObject.setSystemType("pap");
471 logObject.setDate(date);
472 logObject.setRemote("remote");
474 assertEquals("vm02", logObject.getSystem());
475 assertEquals("pap", logObject.getSystemType());
476 assertEquals(date, logObject.getDate());
477 assertEquals("remote", logObject.getRemote());
479 logger.debug("testLogEntryObject: exit");
483 public void testProcess(){
485 logger.debug("testProcess: enter");
487 String line = "2015-04-01 09:13:44.947 DEBUG 17482 --- [nio-8480-exec-7] c.a.l.onap.policy.std.StdPolicyConfig : config Retrieved ";
489 im = Mockito.mock(IntegrityMonitor.class);
491 Mockito.doNothing().when(im).startTransaction();
492 } catch (IntegrityMonitorException e) {
495 Mockito.doNothing().when(im).endTransaction();
496 ParseLog.process(line, "pap", LOGTYPE.INFO);
498 logger.debug("testProcess: exit");