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.*;
25 import java.io.BufferedReader;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.text.SimpleDateFormat;
31 import java.util.Collections;
32 import java.util.Date;
33 import java.util.Properties;
35 import org.openecomp.policy.common.im.AdministrativeStateException;
36 import org.openecomp.policy.common.im.IntegrityMonitor;
37 import org.openecomp.policy.common.im.StandbyStatusException;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mockito;
43 import org.openecomp.xacml.parser.LogEntryObject;
44 import org.openecomp.xacml.parser.ParseLog;
45 import org.openecomp.xacml.parser.LogEntryObject.LOGTYPE;
48 public class ParseLogTest {
50 private ParseLog logParser = new ParseLog();
51 private Properties config = new Properties();
52 private String configFile = "test_config.properties";
53 private static Properties myProp;
54 private static Properties systemProps;
55 private static String resourceName;
56 private IntegrityMonitor im;
60 public void setUp() throws Exception {
61 System.setProperty("com.sun.management.jmxremote.port", "9998");
62 im = Mockito.mock(IntegrityMonitor.class);
64 // Need PowerMockito for mocking static method getInstance(...)
65 // PowerMockito.mockStatic(IntegrityMonitor.class);
68 Mockito.doNothing().when(im).startTransaction();
69 } catch (StandbyStatusException | AdministrativeStateException e) {
72 Mockito.doNothing().when(im).endTransaction();
77 public void tearDown() {
78 File file = new File("nonExistFile.txt");
80 // systemProps.remove("com.sun.management.jmxremote.port");
84 public void testMain() {
88 } catch (Exception e) {
94 public void testCountLines() throws IOException {
95 String fileName = "LineTest.txt";
96 int returnValue = logParser.countLines(fileName);
98 assertEquals(9, returnValue);
102 public void testParseRemoteSystem() {
103 String line = "||org.openecomp.policy.pap.xacml.rest.XACMLPapServlet$Heartbeat.run(XACMLPapServlet.java:2801)||Heartbeat 'https://localhost:8081/pdp/' status='UP_TO_DATE'";
104 String returnValue = ParseLog.parseRemoteSystem(line);
105 assertEquals("localhost:8081", returnValue);
109 public void testGetPropertiesValue() {
110 config = new Properties();
111 config.put("RESOURCE_NAME", "logparser_pap01");
112 config.put("JDBC_DRIVER" ,"com.mysql.jdbc.Driver");
113 config.put("JDBC_URL", "jdbc:mysql://localhost:3306/");
114 config.put("JDBC_USER", "root");
115 config.put("JDBC_PASSWORD", "password");
116 config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi");
117 config.put("SERVER", "password");
118 config.put("JDBC_PASSWORD", "https://localhost:9091/pap/");
119 config.put("LOGTYPE", "PAP");
120 config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\pap-rest.log");
121 config.put("PARSERLOGPATH", "IntegrityMonitor.log");
123 Properties returnConfig = logParser.getPropertiesValue(configFile);
126 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));
130 public void testGetPropertiesFail() {
131 Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
133 assertEquals(null, returnValue);
137 public void testParseDate(){
138 String line = "2016-02-23 08:07:30";
139 Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", false);
141 assertEquals("Tue Feb 23 08:07:30 CST 2016", returnValue.toString());
145 public void testParseDateFail(){
146 String line = "2016-02-23 08:07:30";
147 Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", true);
149 assertEquals(null, returnValue);
153 public void testPullLastLineRead(){
155 File file = new File("LineTest.txt");
156 String returnValue = null;
158 returnValue = ParseLog.PullLastLineRead(file).trim();
159 } catch (IOException e) {
162 assertEquals("12", returnValue);
167 public void testPullLastLineReadNoFile(){
169 File file = new File("nonExistFile.txt");
171 assertEquals(null, ParseLog.PullLastLineRead(file));
172 } catch (IOException e) {
177 public void testPullLastLineReadFail(){
179 File file = new File("LineTest2.txt");
181 assertEquals(null, ParseLog.PullLastLineRead(file));
182 } catch (IOException e) {
188 public void testPullOutLogValues(){
191 FileInputStream fstream;
193 fstream = new FileInputStream("LineTest.txt");
194 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
195 String strLine = br.readLine();
196 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "ERROR");
197 assertEquals("ERROR_VALUE", retrunObject.getDescription());
199 } catch (IOException e) {
205 public void testPullOutLogValuesSecond(){
208 FileInputStream fstream;
210 fstream = new FileInputStream("LineTest.txt");
211 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
212 String strLine = br.readLine();
213 strLine = br.readLine();
214 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "INFO");
215 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
217 } catch (IOException e) {
223 public void testPullOutLogValuesThird(){
226 FileInputStream fstream;
229 fstream = new FileInputStream("LineTest.txt");
230 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
231 String strLine = br.readLine();
232 for (int i =0; i < number; i++){
233 strLine = br.readLine();
235 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
236 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
238 } catch (IOException e) {
244 public void testPullOutLogValuesFourth(){
246 FileInputStream fstream;
249 fstream = new FileInputStream("LineTest.txt");
250 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
251 String strLine = br.readLine();
252 for (int i =0; i < number; i++){
253 strLine = br.readLine();
255 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
256 assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
258 } catch (IOException e) {
264 public void testPullOutLogValuesFith(){
266 FileInputStream fstream;
269 fstream = new FileInputStream("LineTest.txt");
270 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
271 String strLine = br.readLine();
272 for (int i =0; i < number; i++){
273 strLine = br.readLine();
275 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
276 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
278 } catch (IOException e) {
284 public void testPullOutLogValuesSixth(){
286 FileInputStream fstream;
289 fstream = new FileInputStream("LineTest.txt");
290 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
291 String strLine = br.readLine();
292 for (int i =0; i < number; i++){
293 strLine = br.readLine();
295 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
296 assertEquals(LOGTYPE.SEVERE, retrunObject.getLogType());
298 } catch (IOException e) {
304 public void testPullOutLogValuesSeven(){
306 FileInputStream fstream;
309 fstream = new FileInputStream("LineTest.txt");
310 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
311 String strLine = br.readLine();
312 for (int i =0; i < number; i++){
313 strLine = br.readLine();
315 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
316 assertEquals(LOGTYPE.ERROR, retrunObject.getLogType());
318 } catch (IOException e) {
324 public void testPullOutLogValuesEight(){
326 FileInputStream fstream;
329 fstream = new FileInputStream("LineTest.txt");
330 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
331 String strLine = br.readLine();
332 for (int i =0; i < number; i++){
333 strLine = br.readLine();
335 LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
336 assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
338 } catch (IOException e) {
344 public void testPullOutLogValuesNull(){
346 FileInputStream fstream;
347 LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
348 assertEquals(null, retrunObject);
352 public void testLogEntryObject(){
353 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
354 Date date = new Date();
357 LogEntryObject logObject = new LogEntryObject();
358 logObject.setSystem("vm02");
359 logObject.setSystemType("pap");
360 logObject.setDate(date);
361 logObject.setRemote("remote");
363 assertEquals("vm02", logObject.getSystem());
364 assertEquals("pap", logObject.getSystemType());
365 assertEquals(date, logObject.getDate());
366 assertEquals("remote", logObject.getRemote());
370 public void testProcess(){
371 String line = "2015-04-01 09:13:44.947 DEBUG 17482 --- [nio-8480-exec-7] c.a.l.ecomp.policy.std.StdPolicyConfig : config Retrieved ";
373 im = Mockito.mock(IntegrityMonitor.class);
375 Mockito.doNothing().when(im).startTransaction();
376 } catch (StandbyStatusException | AdministrativeStateException e) {
379 Mockito.doNothing().when(im).endTransaction();
380 ParseLog.process(line, "pap");