Replace artifact folder ONBOARDED_PACKAGE in CSAR
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / run / StartTest2backup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.sdc.ci.tests.run;
22
23
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.core.LoggerContext;
26 import org.junit.runner.JUnitCore;
27 import org.junit.runner.Result;
28 import org.junit.runner.notification.Failure;
29 import org.openecomp.sdc.ci.tests.api.AttSdcTest;
30 import org.openecomp.sdc.ci.tests.config.Config;
31 import org.openecomp.sdc.ci.tests.utils.Utils;
32 import org.openecomp.sdc.ci.tests.utils.general.FileUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import java.io.File;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.lang.annotation.Annotation;
40 import java.lang.reflect.Method;
41 import java.net.URISyntaxException;
42 import java.net.URL;
43 import java.text.SimpleDateFormat;
44 import java.util.ArrayList;
45 import java.util.Calendar;
46 import java.util.Enumeration;
47 import java.util.List;
48 import java.util.concurrent.atomic.AtomicBoolean;
49 import java.util.jar.JarEntry;
50 import java.util.jar.JarFile;
51
52 public class StartTest2backup {
53
54         private List<Class<? extends AttSdcTest>> testClasses = new ArrayList<Class<? extends AttSdcTest>>();
55         public static long timeOfTest = 0;
56
57         public static boolean debug = false;
58
59         public static AtomicBoolean loggerInitialized = new AtomicBoolean(false);
60
61         protected static Logger logger = null;
62
63         public static void main(String[] args) {
64
65                 String debugEnabled = System.getProperty("debug");
66                 if (debugEnabled != null && debugEnabled.equalsIgnoreCase("true")) {
67                         debug = true;
68                 }
69                 System.out.println("Debug mode is " + (debug ? "enabled" : "disabled"));
70
71                 enableLogger();
72
73                 Config config = null;
74                 try {
75                         config = Utils.getConfig();
76                 } catch (FileNotFoundException e) {
77                         e.printStackTrace();
78                 }
79
80                 if (config == null) {
81                         logger.error("Failed to configuration file of ci tests.");
82                         System.exit(1);
83                 }
84
85                 List<String> packagesToRun = config.getPackages();
86                 if (packagesToRun == null || true == packagesToRun.isEmpty()) {
87                         logger.error("No package was configured to be executed.");
88                         System.exit(2);
89                 }
90                 StartTest2backup tests = new StartTest2backup();
91
92                 boolean stopOnClassFailure = false;
93                 String stopOnClassFailureStr = System.getProperty("stopOnClassFailure");
94                 if (stopOnClassFailureStr != null && stopOnClassFailureStr.equalsIgnoreCase("true")) {
95                         stopOnClassFailure = true;
96                 } else {
97                         Boolean stopOnClassFailureObj = config.isStopOnClassFailure();
98                         if (stopOnClassFailureObj != null) {
99                                 stopOnClassFailure = stopOnClassFailureObj.booleanValue();
100                         }
101                 }
102
103                 tests.start(packagesToRun, stopOnClassFailure);
104         }
105
106         public StartTest2backup() {
107                 logger = LoggerFactory.getLogger(StartTest2backup.class.getName());
108         }
109
110         public static void enableLogger() {
111
112                 if (false == loggerInitialized.get()) {
113
114                         loggerInitialized.set(true);
115
116                         String log4jPropsFile = System.getProperty("log4j.configuration");
117                         if (System.getProperty("os.name").contains("Windows")) {
118                                 String logProps = "src/main/resources/ci/conf/log4j2.properties";
119                                 if (log4jPropsFile == null) {
120                                         System.setProperty("targetlog", "target/");
121                                         log4jPropsFile = logProps;
122                                 }
123
124                         }
125                         
126                         LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
127                         File file = new File(log4jPropsFile);
128                         context.setConfigLocation(file.toURI());
129                 }
130         }
131
132         public void start(List<String> packages, boolean exitOnFailure) {
133
134                 boolean success = true;
135                 StringBuilder results = new StringBuilder();
136                 Result result;
137
138                 if (packages == null) {
139                         return;
140                 }
141
142                 for (String packageName : packages) {
143                         // List<Class> classesForPackage =
144                         // getClassesForPackage("org.openecomp.sdc.ci.tests.execute");
145                         List<Class> classesForPackage = getClassesForPackage(packageName);
146                         if (classesForPackage != null && false == classesForPackage.isEmpty()) {
147                                 for (Class testUnit : classesForPackage) {
148                                         testClasses.add(testUnit);
149                                 }
150                         }
151                 }
152
153                 System.out.println(testClasses);
154
155                 // tsetClasses.add(LogValidatorTest.class);
156                 // tsetClasses.add(AttNorthboundTest.class);
157
158                 results.append(
159                                 "<Html><head><style>th{background-color: gray;color: white;height: 30px;}td {color: black;height: 30px;}.fail {background-color: #FF5555;width: 100px;text-align: center;}.success {background-color: #00FF00;width: 100px;text-align: center;}.name {width: 200px;background-color: #F0F0F0;}.message {width: 300px;background-color: #F0F0F0;}</style></head><body>");
160
161                 Calendar calendar = Calendar.getInstance();
162                 timeOfTest = calendar.getTimeInMillis();
163                 SimpleDateFormat date_format = new SimpleDateFormat("MMM dd yyyy HH:mm:ss");
164                 results.append("<br/><h2> This report generated on " + date_format.format(calendar.getTime()) + "</h2><br/>");
165
166                 results.append("<table>");
167                 addTableHead(results);
168
169                 int size = testClasses.size();
170                 int index = 0;
171
172                 int totalRunTests = 0;
173                 int totalFailureTests = 0;
174                 int totalIgnoreTests = 0;
175                 int numOfFailureClasses = 0;
176                 for (Class<? extends AttSdcTest> testClass : testClasses) {
177
178                         index++;
179
180                         StringBuilder builder = new StringBuilder();
181                         String str = "***************************************************************************";
182                         builder.append(str + "\n");
183                         String current = "class " + index + "/" + size + " failure(" + numOfFailureClasses + ") + RUNS("
184                                         + totalRunTests + ")" + " FAILURES(" + totalFailureTests + ") IGNORED(" + totalIgnoreTests + ")";
185                         int interval = ((str.length() - current.length() - 2) / 2);
186                         String substring = str.substring(0, interval);
187                         builder.append(substring + " " + current + " " + substring + "\n");
188                         builder.append(str + "\n");
189
190                         System.out.println(builder.toString());
191
192                         logger.debug(builder.toString());
193                         logger.debug("Going to run test class {}", testClass.getName());
194
195                         result = JUnitCore.runClasses(testClass);
196                         if (result.wasSuccessful() == false) {
197                                 numOfFailureClasses++;
198                         }
199                         logger.debug("Test class {} finished {}",testClass.getName(),(result.wasSuccessful() ? "OK." : " WITH ERROR."));
200                         List<Failure> failures = result.getFailures();
201                         if (failures != null) {
202                                 for (Failure failure : failures) {
203                                         logger.error("Test class {} failure test {}-{}",testClass.getName(),failure.getTestHeader(),failure.getTrace());
204                                 }
205                         }
206                         int runsPerClass = result.getRunCount();
207                         int failuresPerClass = result.getFailureCount();
208                         int ignoredPerClass = result.getIgnoreCount();
209
210                         totalRunTests += runsPerClass;
211                         totalFailureTests += failuresPerClass;
212                         totalIgnoreTests += ignoredPerClass;
213
214                         logger.debug("class {} Failed tests {} %",testClass.getName(),(failuresPerClass * 1.0) / runsPerClass * 100);
215                         logger.debug("class {} Ignored tests {} %",testClass.getName(),(ignoredPerClass * 1.0) / runsPerClass * 100);
216
217                         // List<Failure> failures = result.getFailures();
218                         // if (failures != null) {
219                         // for (Failure failure : failures) {
220                         // System.err.println("9999999999" + failure.getTestHeader());
221                         // }
222                         // }
223
224                         addUnitTestResult(results, testClass, result);
225                         success &= result.wasSuccessful();
226
227                         if (numOfFailureClasses > 0) {
228                                 // if (exitOnFailure) {
229                                 if (exitOnFailure) {
230                                         break;
231                                 }
232                         }
233                 }
234
235                 results.append("</table>");
236                 results.append("<br/><h2> Tests Summary: </h2><br/>");
237                 results.append("Total Runs  : " + totalRunTests + "<br/>");
238                 results.append("Total Failure  : " + totalFailureTests + "<br/>");
239                 results.append("Total: " + totalFailureTests + "/" + totalRunTests + "<br/>");
240                 results.append("</html>");
241
242                 FileUtils.writeToFile(Config.instance().getOutputFolder() + File.separator + Config.instance().getReportName(),
243                                 results.toString());
244
245                 if (!success) {
246                         System.out.println("FAILURE");
247                         logger.error("Failure tests : {} %",((totalFailureTests + totalIgnoreTests) * 1.0) / (totalRunTests + totalIgnoreTests));
248                         logger.error("Ignored tests : {} %",(totalIgnoreTests * 1.0) / (totalRunTests + totalIgnoreTests));
249                         System.exit(1);
250                 }
251
252                 System.out.println("SUCCESS");
253         }
254
255         private List<Class> getClassesForPackage(String pkgname) {
256
257                 List<Class> classes = new ArrayList<Class>();
258
259                 // Get a File object for the package
260                 File directory = null;
261                 String fullPath;
262                 String relPath = pkgname.replace('.', '/');
263
264                 // System.out.println("ClassDiscovery: Package: " + pkgname +
265                 // " becomes Path:" + relPath);
266
267                 URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
268
269                 // System.out.println("ClassDiscovery: Resource = " + resource);
270                 if (resource == null) {
271                         throw new RuntimeException("No resource for " + relPath);
272                 }
273                 fullPath = resource.getFile();
274                 // System.out.println("ClassDiscovery: FullPath = " + resource);
275
276                 if (debug) {
277                         System.out.println("fullPath is " + fullPath);
278                 }
279
280                 try {
281                         directory = new File(resource.toURI());
282                 } catch (URISyntaxException e) {
283                         throw new RuntimeException(
284                                         pkgname + " (" + resource
285                                                         + ") does not appear to be a valid URL / URI.  Strange, since we got it from the system...",
286                                         e);
287                 } catch (IllegalArgumentException e) {
288                         directory = null;
289                 }
290                 // System.out.println("ClassDiscovery: Directory = " + directory);
291
292                 if (directory != null && directory.exists()) {
293
294                         // Get the list of the files contained in the package
295                         String[] files = directory.list();
296                         for (int i = 0; i < files.length; i++) {
297
298                                 // we are only interested in .class files
299                                 if (files[i].endsWith(".class") && false == files[i].contains("$")) {
300
301                                         // removes the .class extension
302                                         String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
303
304                                         // System.out.println("ClassDiscovery: className = " +
305                                         // className);
306
307                                         if (debug) {
308                                                 System.out.println("ClassDiscovery: className = " + className);
309                                         }
310
311                                         try {
312                                                 Class clas = Class.forName(className);
313                                                 boolean isAddToRun = false;
314                                                 Method[] methods = clas.getMethods();
315                                                 for (Method method : methods) {
316                                                         Annotation[] anns = method.getAnnotations();
317                                                         for (Annotation an : anns) {
318                                                                 if (an.annotationType().getSimpleName().equalsIgnoreCase("Test")) {
319                                                                         isAddToRun = true;
320                                                                         break;
321                                                                 }
322                                                         }
323                                                 }
324                                                 if (isAddToRun)
325                                                         classes.add(clas);
326                                         } catch (ClassNotFoundException e) {
327                                                 throw new RuntimeException("ClassNotFoundException loading " + className);
328                                         }
329                                 }
330                         }
331                 } else {
332                         try {
333                                 String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
334
335                                 if (debug) {
336                                         System.out.println("jarPath is " + jarPath);
337                                 }
338
339                                 JarFile jarFile = new JarFile(jarPath);
340                                 Enumeration<JarEntry> entries = jarFile.entries();
341                                 while (entries.hasMoreElements()) {
342                                         JarEntry entry = entries.nextElement();
343                                         String entryName = entry.getName();
344                                         if (entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
345
346                                                 // System.out.println("ClassDiscovery: JarEntry: " +
347                                                 // entryName);
348                                                 String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
349
350                                                 // System.out.println("ClassDiscovery: className = " +
351                                                 // className);
352
353                                                 if (false == className.contains("$")) {
354
355                                                         if (debug) {
356                                                                 System.out.println("ClassDiscovery: className = " + className);
357                                                         }
358
359                                                         try {
360                                                                 Class clas = Class.forName(className);
361                                                                 boolean isAddToRun = false;
362                                                                 Method[] methods = clas.getMethods();
363                                                                 for (Method method : methods) {
364                                                                         Annotation[] anns = method.getAnnotations();
365                                                                         for (Annotation an : anns) {
366                                                                                 if (an.annotationType().getSimpleName().equalsIgnoreCase("Test")) {
367                                                                                         isAddToRun = true;
368                                                                                         break;
369                                                                                 }
370                                                                         }
371                                                                 }
372                                                                 if (isAddToRun)
373                                                                         classes.add(clas);
374                                                         } catch (ClassNotFoundException e) {
375                                                                 throw new RuntimeException("ClassNotFoundException loading " + className);
376                                                         }
377                                                 }
378                                         }
379                                 }
380                                 jarFile.close();
381
382                         } catch (IOException e) {
383                                 throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
384                         }
385                 }
386                 return classes;
387         }
388
389         private void addTableHead(StringBuilder results) {
390                 results.append("<tr>");
391                 results.append("<th>").append("Unit Test").append("</th>");
392                 results.append("<th>").append("Result").append("</th>");
393                 results.append("</tr>");
394         }
395
396         private void addUnitTestResult(StringBuilder results, Class<? extends AttSdcTest> testClass,
397                         Result unitTestResult) {
398
399                 boolean isSuccess = unitTestResult.wasSuccessful();
400
401                 String result = (isSuccess) ? "success" : "fail";
402                 String fileName = FileUtils.getFileName(testClass.getName());
403                 results.append("<tr>");
404                 // results.append("<td>").append(FileUtils.getFileName(testClass.getName())).append("</td>");
405                 results.append("<td class=\"name\">")
406                                 .append("<a href=\"" + fileName + timeOfTest + ".html\">" + fileName + "</a>").append("</td>");
407                 results.append("<td class=\"" + result + "\">").append(result).append("</td>");
408                 results.append("</tr>");
409         }
410
411 }