Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / test / java / org / onap / sdc / toscaparser / api / GetValidationIssues.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.onap.sdc.toscaparser.api;
22
23 import com.opencsv.CSVWriter;
24
25 import java.io.File;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Scanner;
32
33 //Generate excel file, include all validation issues errors in jtosca
34 //the error java code, the line number and file name for each error.
35 public class GetValidationIssues {
36
37     public static CSVWriter fileWriter = null;
38     public static List<String[]> data = new ArrayList<>();
39
40     public static void main(String[] args) {
41         System.out.println("GetAllValidationIssues - path to project files Directory is " + Arrays.toString(args));
42         File jtoscaFiles = new File(args[0] + "\\jtosca\\src\\main\\java\\org\\onap\\sdc\\toscaparser\\api");
43
44         try {
45             printFiles(jtoscaFiles);
46             fileWriter = new CSVWriter(new FileWriter(args[1] + "\\JToscaValidationIssues_" + System.currentTimeMillis() + ".csv"), '\t');
47             fileWriter.writeNext(new String[]{"Error Message", "Class Name", "Line No."}, false);
48             fileWriter.writeAll(data, false);
49         } catch (IOException e) {
50             e.printStackTrace();
51         } finally {
52             try {
53                 fileWriter.flush();
54                 fileWriter.close();
55             } catch (IOException e) {
56                 System.out.println("Error while flushing/closing fileWriter !!!");
57                 e.printStackTrace();
58             }
59         }
60     }
61
62     private static void printFiles(File dir) {
63         if (dir != null && dir.exists()) {
64             for (File file : dir.listFiles()) {
65                 if (file.isDirectory())
66                     printFiles(file);
67                 else {
68                     Scanner scanner = null;
69                     try {
70                         scanner = new Scanner(file);
71
72                         int lineNum = 0;
73                         while (scanner.hasNextLine()) {
74                             String line = scanner.nextLine();
75                             lineNum++;
76                             if (line.startsWith("/*python"))
77                                 break;
78
79                             if (!line.trim().startsWith("//") && !line.trim().startsWith("#") && line.contains("ThreadLocalsHolder.getCollector().appendValidationIssue")) {
80                                 String errMsg = line.trim();
81                                 if (!errMsg.contains(";")) {
82                                     String nextLine = null;
83                                     while (scanner.hasNextLine() && (nextLine == null || !nextLine.contains(";"))) {
84                                         nextLine = scanner.nextLine();
85                                         errMsg += nextLine.trim();
86                                     }
87                                 }
88
89                                 data.add(new String[]{errMsg, file.getName(), String.valueOf(lineNum)});
90                             }
91                         }
92                     } catch (IOException e) {
93                         e.printStackTrace();
94                     }
95                 }
96             }
97         }
98     }
99 }
100