Skip to content

Commit c0164a3

Browse files
Merge pull request eugenp#1814 from eugenp/report-generation-ref
Idiomatic reporting refactor
2 parents d67d03b + fd3f569 commit c0164a3

1 file changed

Lines changed: 63 additions & 35 deletions

File tree

‎testng/src/test/java/com/baeldung/reports/CustomisedReports.java‎

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,84 @@
1010
import java.nio.file.Paths;
1111
import java.util.List;
1212
import java.util.Map;
13+
import java.util.Set;
14+
import java.util.function.Function;
15+
import java.util.stream.Collectors;
1316
import java.util.stream.Stream;
1417

18+
import static java.util.stream.Collectors.toList;
19+
1520
public class CustomisedReports implements IReporter {
1621
private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class);
1722

23+
private static final String ROW_TEMPLATE = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
24+
1825
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
1926
String reportTemplate = initReportTemplate();
20-
String resultRow = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
21-
StringBuilder rows = new StringBuilder();
22-
suites.forEach(suite -> {
23-
Map<String, ISuiteResult> suiteResults = suite.getResults();
24-
suiteResults.forEach((testName, suiteResult) -> {
25-
26-
ITestContext testContext = suiteResult.getTestContext();
27-
28-
Stream<ITestResult> failedTests = testContext.getFailedTests().getAllResults().stream();
29-
Stream<ITestResult> passedTests = testContext.getPassedTests().getAllResults().stream();
30-
Stream<ITestResult> skippedTests = testContext.getSkippedTests().getAllResults().stream();
31-
32-
String suiteName = suite.getName();
33-
34-
Stream<ITestResult> allTestResults = Stream.concat(Stream.concat(failedTests, passedTests), skippedTests);
35-
generateReportRows(resultRow, rows, testName, suiteName, allTestResults);
36-
});
37-
});
38-
reportTemplate = reportTemplate.replaceFirst("</tbody>", rows.toString() + "</tbody>");
39-
saveReportTemplate(outputDirectory, reportTemplate);
27+
28+
final String body = suites
29+
.stream()
30+
.flatMap(suiteToResults())
31+
.collect(Collectors.joining());
32+
33+
saveReportTemplate(outputDirectory, reportTemplate.replaceFirst("</tbody>", String.format("%s</tbody>", body)));
4034
}
4135

42-
private void generateReportRows(String resultRow, StringBuilder rows, String testName, String suiteName, Stream<ITestResult> allTestResults) {
43-
allTestResults
44-
.forEach(testResult -> {
45-
String testReportRow = "";
46-
if (testResult.getStatus() == ITestResult.FAILURE) {
47-
testReportRow = String.format(resultRow, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA");
48-
}
49-
if (testResult.getStatus() == ITestResult.SUCCESS) {
50-
testReportRow = String.format(resultRow, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis()));
36+
private Function<ISuite, Stream<? extends String>> suiteToResults() {
37+
return suite -> suite.getResults().entrySet()
38+
.stream()
39+
.flatMap(resultsToRows(suite));
40+
}
5141

42+
private Function<Map.Entry<String, ISuiteResult>, Stream<? extends String>> resultsToRows(ISuite suite) {
43+
return e -> {
44+
ITestContext testContext = e.getValue().getTestContext();
45+
46+
Set<ITestResult> failedTests = testContext
47+
.getFailedTests()
48+
.getAllResults();
49+
Set<ITestResult> passedTests = testContext
50+
.getPassedTests()
51+
.getAllResults();
52+
Set<ITestResult> skippedTests = testContext
53+
.getSkippedTests()
54+
.getAllResults();
55+
56+
String suiteName = suite.getName();
57+
58+
return Stream
59+
.of(failedTests, passedTests, skippedTests)
60+
.flatMap(results -> generateReportRows(e.getKey(), suiteName, results).stream());
61+
};
62+
}
63+
64+
private List<String> generateReportRows(String testName, String suiteName, Set<ITestResult> allTestResults) {
65+
return allTestResults.stream()
66+
.map(testResultToResultRow(testName, suiteName))
67+
.collect(toList());
68+
}
69+
70+
private Function<ITestResult, String> testResultToResultRow(String testName, String suiteName) {
71+
return testResult -> {
72+
switch (testResult.getStatus()) {
73+
case ITestResult.FAILURE:
74+
return String.format(ROW_TEMPLATE, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA");
75+
76+
case ITestResult.SUCCESS:
77+
return String.format(ROW_TEMPLATE, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis()));
78+
79+
case ITestResult.SKIP:
80+
return String.format(ROW_TEMPLATE, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA");
81+
82+
default:
83+
return "";
5284
}
53-
if (testResult.getStatus() == ITestResult.SKIP) {
54-
testReportRow = String.format(resultRow, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA");
55-
}
56-
rows.append(testReportRow);
57-
});
85+
};
5886
}
5987

6088
private String initReportTemplate() {
6189
String template = null;
62-
byte[] reportTemplate = null;
90+
byte[] reportTemplate;
6391
try {
6492
reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html"));
6593
template = new String(reportTemplate, "UTF-8");

0 commit comments

Comments
 (0)