Initial OpenECOMP SDC commit
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / run / ExtentReporterNG.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 import java.io.File;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.testng.IReporter;
30 import org.testng.IResultMap;
31 import org.testng.ISuite;
32 import org.testng.ISuiteResult;
33 import org.testng.ITestContext;
34 import org.testng.ITestResult;
35 import org.testng.xml.XmlSuite;
36
37 import com.relevantcodes.extentreports.ExtentReports;
38 import com.relevantcodes.extentreports.ExtentTest;
39 import com.relevantcodes.extentreports.LogStatus;
40
41 public class ExtentReporterNG implements IReporter {
42         private ExtentReports extent;
43
44         @Override
45         public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
46                 extent = new ExtentReports(outputDirectory + File.separator + "ExtentReportsTestNG.html", true);
47
48                 for (ISuite suite : suites) {
49                         Map<String, ISuiteResult> result = suite.getResults();
50
51                         for (ISuiteResult r : result.values()) {
52                                 ITestContext context = r.getTestContext();
53
54                                 buildTestNodes(context.getPassedTests(), LogStatus.PASS);
55                                 buildTestNodes(context.getFailedTests(), LogStatus.FAIL);
56                                 buildTestNodes(context.getSkippedTests(), LogStatus.SKIP);
57                         }
58                 }
59
60                 extent.flush();
61                 extent.close();
62         }
63
64         private void buildTestNodes(IResultMap tests, LogStatus status) {
65                 ExtentTest test;
66
67                 if (tests.size() > 0) {
68                         for (ITestResult result : tests.getAllResults()) {
69                                 test = extent.startTest(result.getMethod().getMethodName());
70
71                                 test.getTest().setStartedTime(getTime(result.getStartMillis()));
72                                 test.getTest().setEndedTime(getTime(result.getEndMillis()));
73
74                                 for (String group : result.getMethod().getGroups())
75                                         test.assignCategory(group);
76
77                                 String message = "Test " + status.toString().toLowerCase() + "ed";
78
79                                 if (result.getThrowable() != null)
80                                         message = result.getThrowable().getMessage();
81
82                                 test.log(status, message);
83
84                                 extent.endTest(test);
85                         }
86                 }
87         }
88
89         private Date getTime(long millis) {
90                 Calendar calendar = Calendar.getInstance();
91                 calendar.setTimeInMillis(millis);
92                 return calendar.getTime();
93         }
94 }