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.openecomp.xacml.parser;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
26 import java.io.BufferedReader;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.util.Date;
32 import java.util.Properties;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.openecomp.policy.common.im.AdministrativeStateException;
39 import org.openecomp.policy.common.im.IntegrityMonitor;
40 import org.openecomp.policy.common.im.StandbyStatusException;
41 import org.openecomp.xacml.parser.LogEntryObject.LOGTYPE;
44 public class ParseLogTest {
46 private Properties config = new Properties();
47 private String configFile = "test_config.properties";
48 private IntegrityMonitor im;
52 public void setUp() throws Exception {
53 System.setProperty("com.sun.management.jmxremote.port", "9998");
54 im = Mockito.mock(IntegrityMonitor.class);
57 Mockito.doNothing().when(im).startTransaction();
58 } catch (StandbyStatusException | AdministrativeStateException e) {
61 Mockito.doNothing().when(im).endTransaction();
66 public void tearDown() {
67 File file = new File("nonExistFile.txt");
72 public void testMain() {
75 } catch (Exception e) {
81 public void testCountLines() throws IOException {
82 String fileName = "LineTest.txt";
83 int returnValue = ParseLog.countLines(fileName);
85 assertEquals(9, returnValue);
89 public void testParseRemoteSystem() {
90 String line = "||org.openecomp.policy.pap.xacml.rest.XACMLPapServlet$Heartbeat.run(XACMLPapServlet.java:2801)||Heartbeat 'https://localhost:8081/pdp/' status='UP_TO_DATE'";
91 String returnValue = ParseLog.parseRemoteSystem(line);
92 assertEquals("localhost:8081", returnValue);
96 public void testGetPropertiesValue() {
97 config = new Properties();
98 config.put("RESOURCE_NAME", "logparser_pap01");
99 config.put("JDBC_DRIVER" ,"com.mysql.jdbc.Driver");
100 config.put("JDBC_URL", "jdbc:mysql://localhost:3306/");
101 config.put("JDBC_USER", "root");
102 config.put("JDBC_PASSWORD", "password");
103 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
104 config.put("SERVER", "password");
105 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
106 config.put("LOGTYPE", "PAP");
107 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\pap-rest.log");
108 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
110 Properties returnConfig = ParseLog.getPropertiesValue(configFile);
113 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
117 public void testGetPropertiesFail() {
118 Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
120 assertEquals(null, returnValue);
124 public void testParseDate(){
125 String line = "2016-02-23 08:07:30";
126 Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", false);
127 line = returnValue.toString().substring(0, returnValue.toString().lastIndexOf(":30")+3);
128 assertEquals("Tue Feb 23 08:07:30", line);
132 public void testParseDateFail(){
133 String line = "2016-02-23 08:07:30";
134 Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", true);
136 assertEquals(null, returnValue);
140 public void testPullLastLineRead(){
142 File file = new File("LineTest.txt");
143 String returnValue = null;
145 returnValue = ParseLog.PullLastLineRead(file).trim();
146 } catch (IOException e) {
149 assertEquals("12", returnValue);
154 public void testPullLastLineReadNoFile(){
156 File file = new File("nonExistFile.txt");
158 assertEquals(null, ParseLog.PullLastLineRead(file));
159 } catch (IOException e) {
164 public void testPullLastLineReadFail(){
166 File file = new File("LineTest2.txt");
168 assertEquals(null, ParseLog.PullLastLineRead(file));
169 } catch (IOException e) {
175 public void testPullOutLogValues(){
178 FileInputStream fstream;
180 fstream = new FileInputStream("LineTest.txt");
181 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
182 String strLine = br.readLine();
183 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "ERROR");
184 assertEquals("ERROR_VALUE", retrunObject.getDescription());
186 } catch (IOException e) {
192 public void testPullOutLogValuesSecond(){
195 FileInputStream fstream;
197 fstream = new FileInputStream("LineTest.txt");
198 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
199 String strLine = br.readLine();
200 strLine = br.readLine();
201 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "INFO");
202 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
204 } catch (IOException e) {
210 public void testPullOutLogValuesThird(){
213 FileInputStream fstream;
216 fstream = new FileInputStream("LineTest.txt");
217 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
218 String strLine = br.readLine();
219 for (int i =0; i < number; i++){
220 strLine = br.readLine();
222 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
223 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
225 } catch (IOException e) {
231 public void testPullOutLogValuesFourth(){
233 FileInputStream fstream;
236 fstream = new FileInputStream("LineTest.txt");
237 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
238 String strLine = br.readLine();
239 for (int i =0; i < number; i++){
240 strLine = br.readLine();
242 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
243 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
245 } catch (IOException e) {
251 public void testPullOutLogValuesFith(){
253 FileInputStream fstream;
256 fstream = new FileInputStream("LineTest.txt");
257 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
258 String strLine = br.readLine();
259 for (int i =0; i < number; i++){
260 strLine = br.readLine();
262 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
263 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
265 } catch (IOException e) {
271 public void testPullOutLogValuesSixth(){
273 FileInputStream fstream;
276 fstream = new FileInputStream("LineTest.txt");
277 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
278 String strLine = br.readLine();
279 for (int i =0; i < number; i++){
280 strLine = br.readLine();
282 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
283 assertEquals(LOGTYPE.SEVERE, retrunObject.getLogType());
285 } catch (IOException e) {
291 public void testPullOutLogValuesSeven(){
293 FileInputStream fstream;
296 fstream = new FileInputStream("LineTest.txt");
297 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
298 String strLine = br.readLine();
299 for (int i =0; i < number; i++){
300 strLine = br.readLine();
302 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
303 assertEquals(LOGTYPE.ERROR, retrunObject.getLogType());
305 } catch (IOException e) {
311 public void testPullOutLogValuesEight(){
313 FileInputStream fstream;
316 fstream = new FileInputStream("LineTest.txt");
317 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
318 String strLine = br.readLine();
319 for (int i =0; i < number; i++){
320 strLine = br.readLine();
322 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
323 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
325 } catch (IOException e) {
331 public void testPullOutLogValuesNull(){
333 LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
334 assertEquals(null, retrunObject);
338 public void testLogEntryObject(){
339 Date date = new Date();
342 LogEntryObject logObject = new LogEntryObject();
343 logObject.setSystem("vm02");
344 logObject.setSystemType("pap");
345 logObject.setDate(date);
346 logObject.setRemote("remote");
348 assertEquals("vm02", logObject.getSystem());
349 assertEquals("pap", logObject.getSystemType());
350 assertEquals(date, logObject.getDate());
351 assertEquals("remote", logObject.getRemote());
355 public void testProcess(){
356 String line = "2015-04-01 09:13:44.947 DEBUG 17482 --- [nio-8480-exec-7] c.a.l.ecomp.policy.std.StdPolicyConfig : config Retrieved ";
358 im = Mockito.mock(IntegrityMonitor.class);
360 Mockito.doNothing().when(im).startTransaction();
361 } catch (StandbyStatusException | AdministrativeStateException e) {
364 Mockito.doNothing().when(im).endTransaction();
365 ParseLog.process(line, "pap");