|
10 | 10 | import java.nio.file.Paths; |
11 | 11 | import java.util.List; |
12 | 12 | import java.util.Map; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.function.Function; |
| 15 | +import java.util.stream.Collectors; |
13 | 16 | import java.util.stream.Stream; |
14 | 17 |
|
| 18 | +import static java.util.stream.Collectors.toList; |
| 19 | + |
15 | 20 | public class CustomisedReports implements IReporter { |
16 | 21 | private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class); |
17 | 22 |
|
| 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 | + |
18 | 25 | public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { |
19 | 26 | 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))); |
40 | 34 | } |
41 | 35 |
|
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 | + } |
51 | 41 |
|
| 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 ""; |
52 | 84 | } |
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 | + }; |
58 | 86 | } |
59 | 87 |
|
60 | 88 | private String initReportTemplate() { |
61 | 89 | String template = null; |
62 | | - byte[] reportTemplate = null; |
| 90 | + byte[] reportTemplate; |
63 | 91 | try { |
64 | 92 | reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html")); |
65 | 93 | template = new String(reportTemplate, "UTF-8"); |
|
0 commit comments