Initial OpenECOMP policy/engine commit
[policy/engine.git] / LogParser / src / test / java / org / openecomp / xacml / parser / ParseLogTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * LogParser
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.xacml.parser;
22
23 import static org.junit.Assert.*;
24
25 import java.io.BufferedReader;
26 import java.io.File;
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;
34
35 import org.openecomp.policy.common.im.AdministrativeStateException;
36 import org.openecomp.policy.common.im.IntegrityMonitor;
37 import org.openecomp.policy.common.im.StandbyStatusException;
38
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;
46
47
48 public class ParseLogTest {
49
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;
57
58         
59         @Before
60         public void setUp() throws Exception {
61                 System.setProperty("com.sun.management.jmxremote.port", "9998");
62                 im = Mockito.mock(IntegrityMonitor.class);
63                 
64                 // Need PowerMockito for mocking static method getInstance(...)
65         //      PowerMockito.mockStatic(IntegrityMonitor.class);
66                 
67                 try {
68                         Mockito.doNothing().when(im).startTransaction();
69                 } catch (StandbyStatusException | AdministrativeStateException e) {
70                         fail();
71                 }
72                 Mockito.doNothing().when(im).endTransaction();
73
74         }
75
76         @After
77         public void tearDown() {
78                 File file = new File("nonExistFile.txt");
79                 file.delete();
80 //              systemProps.remove("com.sun.management.jmxremote.port");
81         }
82
83         @Test
84         public void testMain() {
85                 try {   
86                         
87                         logParser.main(null);
88                 } catch (Exception e) {
89                         //fail();
90                 }
91         }
92
93         @Test
94         public void testCountLines() throws IOException {
95                 String fileName = "LineTest.txt";
96                 int returnValue = logParser.countLines(fileName);
97                 
98                 assertEquals(9, returnValue);
99         }
100         
101         @Test
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);
106         }
107
108         @Test
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");
122                 
123                 Properties returnConfig = logParser.getPropertiesValue(configFile);
124
125                 
126                 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));   
127         }
128         
129         @Test
130         public void testGetPropertiesFail() {   
131                 Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
132                 
133                 assertEquals(null, returnValue);        
134         }
135
136         @Test
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);
140                 
141                 assertEquals("Tue Feb 23 08:07:30 CST 2016", returnValue.toString());
142         }
143         
144         @Test
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);
148                 
149                 assertEquals(null, returnValue);
150         }
151         
152         @Test
153         public void testPullLastLineRead(){
154                 
155                 File file = new File("LineTest.txt");
156                 String returnValue = null;
157                 try {
158                         returnValue = ParseLog.PullLastLineRead(file).trim();
159                 } catch (IOException e) {
160                         fail();
161                 }               
162                 assertEquals("12", returnValue);
163
164         }
165         
166         @Test
167         public void testPullLastLineReadNoFile(){
168                 
169                 File file = new File("nonExistFile.txt");
170                 try {
171                         assertEquals(null, ParseLog.PullLastLineRead(file));
172                 } catch (IOException e) {
173                         fail();
174                 }
175         }
176         @Test
177         public void testPullLastLineReadFail(){
178                 
179                 File file = new File("LineTest2.txt");
180                 try {
181                         assertEquals(null, ParseLog.PullLastLineRead(file));
182                 } catch (IOException e) {
183                         fail();
184                 }
185         }
186
187         @Test
188         public void testPullOutLogValues(){
189                 //ERROR_VALUE
190                 // Open the file
191                 FileInputStream fstream;
192                 try {
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());
198                         br.close();
199                 } catch (IOException e) {
200                         fail();
201                 }       
202 //              assert(true);
203         }
204         @Test
205         public void testPullOutLogValuesSecond(){
206                 //ERROR_VALUE
207                 // Open the file
208                 FileInputStream fstream;
209                 try {
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());
216                         br.close();
217                 } catch (IOException e) {
218                         fail();
219                 }       
220         }
221         
222         @Test
223         public void testPullOutLogValuesThird(){
224                 //ERROR_VALUE
225                 // Open the file
226                 FileInputStream fstream;
227                 try {
228                         int number = 3;
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();
234                         }
235                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
236                         assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
237                         br.close();
238                 } catch (IOException e) {
239                         fail();
240                 }       
241         }
242
243         @Test
244         public void testPullOutLogValuesFourth(){
245                 // Open the file
246                 FileInputStream fstream;
247                 try {
248                         int number = 4;
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();
254                         }
255                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
256                         assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
257                         br.close();
258                 } catch (IOException e) {
259                         fail();
260                 }       
261         }
262         
263         @Test
264         public void testPullOutLogValuesFith(){
265                 // Open the file
266                 FileInputStream fstream;
267                 try {
268                         int number = 5;
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();
274                         }
275                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
276                         assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
277                         br.close();
278                 } catch (IOException e) {
279                         fail();
280                 }       
281         }
282         
283         @Test
284         public void testPullOutLogValuesSixth(){
285                 // Open the file
286                 FileInputStream fstream;
287                 try {
288                         int number = 6;
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();
294                         }
295                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
296                         assertEquals(LOGTYPE.SEVERE, retrunObject.getLogType());
297                         br.close();
298                 } catch (IOException e) {
299                         fail();
300                 }       
301         }
302
303         @Test
304         public void testPullOutLogValuesSeven(){
305                 // Open the file
306                 FileInputStream fstream;
307                 try {
308                         int number = 7;
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();
314                         }
315                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
316                         assertEquals(LOGTYPE.ERROR, retrunObject.getLogType());
317                         br.close();
318                 } catch (IOException e) {
319                         fail();
320                 }       
321         }
322         
323         @Test
324         public void testPullOutLogValuesEight(){
325                 // Open the file
326                 FileInputStream fstream;
327                 try {
328                         int number = 8;
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();
334                         }
335                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
336                         assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
337                         br.close();
338                 } catch (IOException e) {
339                         fail();
340                 }       
341         }
342         
343         @Test
344         public void testPullOutLogValuesNull(){
345                 // Open the file
346                 FileInputStream fstream;
347                 LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
348                 assertEquals(null, retrunObject);
349         }
350         
351         @Test
352         public void testLogEntryObject(){
353                 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
354                 Date date = new Date();
355          
356                 // Open the file
357                 LogEntryObject logObject = new LogEntryObject();
358                 logObject.setSystem("vm02");
359                 logObject.setSystemType("pap");
360                 logObject.setDate(date);
361                 logObject.setRemote("remote");
362
363                 assertEquals("vm02", logObject.getSystem());
364                 assertEquals("pap", logObject.getSystemType());
365                 assertEquals(date, logObject.getDate());
366                 assertEquals("remote", logObject.getRemote());
367         }
368
369         @Test
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 ";
372         
373                 im = Mockito.mock(IntegrityMonitor.class);
374                 try {
375                         Mockito.doNothing().when(im).startTransaction();
376                 } catch (StandbyStatusException | AdministrativeStateException e) {
377                         fail();
378                 }
379                 Mockito.doNothing().when(im).endTransaction();
380                 ParseLog.process(line, "pap");
381         }
382 }