Policy TestSuite Enabled
[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.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import java.io.BufferedReader;
27 import java.io.File;
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;
33
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;
42
43
44 public class ParseLogTest {
45
46         private Properties config = new Properties();
47         private String configFile = "test_config.properties";
48         private IntegrityMonitor im;
49
50         
51         @Before
52         public void setUp() throws Exception {
53                 System.setProperty("com.sun.management.jmxremote.port", "9998");
54                 im = Mockito.mock(IntegrityMonitor.class);
55                 
56                 try {
57                         Mockito.doNothing().when(im).startTransaction();
58                 } catch (StandbyStatusException | AdministrativeStateException e) {
59                         fail();
60                 }
61                 Mockito.doNothing().when(im).endTransaction();
62
63         }
64
65         @After
66         public void tearDown() {
67                 File file = new File("nonExistFile.txt");
68                 file.delete();
69         }
70
71         //@Test
72         public void testMain() {
73                 try {   
74                         ParseLog.main(null);
75                 } catch (Exception e) {
76                         fail();
77                 }
78         }
79
80         @Test
81         public void testCountLines() throws IOException {
82                 String fileName = "LineTest.txt";
83                 int returnValue = ParseLog.countLines(fileName);
84                 
85                 assertEquals(9, returnValue);
86         }
87         
88         @Test
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);
93         }
94
95         @Test
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");
109                 
110                 Properties returnConfig = ParseLog.getPropertiesValue(configFile);
111
112                 
113                 assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME"));   
114         }
115         
116         @Test
117         public void testGetPropertiesFail() {   
118                 Properties returnValue = ParseLog.getPropertiesValue("nonExistFile");
119                 
120                 assertEquals(null, returnValue);        
121         }
122
123         @Test
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);
129         }
130         
131         @Test
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);
135                 
136                 assertEquals(null, returnValue);
137         }
138         
139         @Test
140         public void testPullLastLineRead(){
141                 
142                 File file = new File("LineTest.txt");
143                 String returnValue = null;
144                 try {
145                         returnValue = ParseLog.PullLastLineRead(file).trim();
146                 } catch (IOException e) {
147                         fail();
148                 }               
149                 assertEquals("12", returnValue);
150
151         }
152         
153         @Test
154         public void testPullLastLineReadNoFile(){
155                 
156                 File file = new File("nonExistFile.txt");
157                 try {
158                         assertEquals(null, ParseLog.PullLastLineRead(file));
159                 } catch (IOException e) {
160                         fail();
161                 }
162         }
163         @Test
164         public void testPullLastLineReadFail(){
165                 
166                 File file = new File("LineTest2.txt");
167                 try {
168                         assertEquals(null, ParseLog.PullLastLineRead(file));
169                 } catch (IOException e) {
170                         fail();
171                 }
172         }
173
174         @Test
175         public void testPullOutLogValues(){
176                 //ERROR_VALUE
177                 // Open the file
178                 FileInputStream fstream;
179                 try {
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());
185                         br.close();
186                 } catch (IOException e) {
187                         fail();
188                 }       
189 //              assert(true);
190         }
191         @Test
192         public void testPullOutLogValuesSecond(){
193                 //ERROR_VALUE
194                 // Open the file
195                 FileInputStream fstream;
196                 try {
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());
203                         br.close();
204                 } catch (IOException e) {
205                         fail();
206                 }       
207         }
208         
209         @Test
210         public void testPullOutLogValuesThird(){
211                 //ERROR_VALUE
212                 // Open the file
213                 FileInputStream fstream;
214                 try {
215                         int number = 3;
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();
221                         }
222                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
223                         assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
224                         br.close();
225                 } catch (IOException e) {
226                         fail();
227                 }       
228         }
229
230         @Test
231         public void testPullOutLogValuesFourth(){
232                 // Open the file
233                 FileInputStream fstream;
234                 try {
235                         int number = 4;
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();
241                         }
242                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP");
243                         assertEquals(LOGTYPE.INFO, retrunObject.getLogType());
244                         br.close();
245                 } catch (IOException e) {
246                         fail();
247                 }       
248         }
249         
250         @Test
251         public void testPullOutLogValuesFith(){
252                 // Open the file
253                 FileInputStream fstream;
254                 try {
255                         int number = 5;
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();
261                         }
262                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
263                         assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
264                         br.close();
265                 } catch (IOException e) {
266                         fail();
267                 }       
268         }
269         
270         @Test
271         public void testPullOutLogValuesSixth(){
272                 // Open the file
273                 FileInputStream fstream;
274                 try {
275                         int number = 6;
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();
281                         }
282                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP");
283                         assertEquals(LOGTYPE.SEVERE, retrunObject.getLogType());
284                         br.close();
285                 } catch (IOException e) {
286                         fail();
287                 }       
288         }
289
290         @Test
291         public void testPullOutLogValuesSeven(){
292                 // Open the file
293                 FileInputStream fstream;
294                 try {
295                         int number = 7;
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();
301                         }
302                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console");
303                         assertEquals(LOGTYPE.ERROR, retrunObject.getLogType());
304                         br.close();
305                 } catch (IOException e) {
306                         fail();
307                 }       
308         }
309         
310         @Test
311         public void testPullOutLogValuesEight(){
312                 // Open the file
313                 FileInputStream fstream;
314                 try {
315                         int number = 8;
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();
321                         }
322                         LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap");
323                         assertEquals(LOGTYPE.WARN, retrunObject.getLogType());
324                         br.close();
325                 } catch (IOException e) {
326                         fail();
327                 }       
328         }
329         
330         @Test
331         public void testPullOutLogValuesNull(){
332                 // Open the file
333                 LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console");
334                 assertEquals(null, retrunObject);
335         }
336         
337         @Test
338         public void testLogEntryObject(){
339                 Date date = new Date();
340          
341                 // Open the file
342                 LogEntryObject logObject = new LogEntryObject();
343                 logObject.setSystem("vm02");
344                 logObject.setSystemType("pap");
345                 logObject.setDate(date);
346                 logObject.setRemote("remote");
347
348                 assertEquals("vm02", logObject.getSystem());
349                 assertEquals("pap", logObject.getSystemType());
350                 assertEquals(date, logObject.getDate());
351                 assertEquals("remote", logObject.getRemote());
352         }
353
354         @Test
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 ";
357         
358                 im = Mockito.mock(IntegrityMonitor.class);
359                 try {
360                         Mockito.doNothing().when(im).startTransaction();
361                 } catch (StandbyStatusException | AdministrativeStateException e) {
362                         fail();
363                 }
364                 Mockito.doNothing().when(im).endTransaction();
365                 ParseLog.process(line, "pap");
366         }
367 }