diff --git a/testng/src/test/java/com/baeldung/reports/CustomisedReports.java b/testng/src/test/java/com/baeldung/reports/CustomisedReports.java index 70810ab0dc..d4f1319deb 100644 --- a/testng/src/test/java/com/baeldung/reports/CustomisedReports.java +++ b/testng/src/test/java/com/baeldung/reports/CustomisedReports.java @@ -10,56 +10,85 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; import java.util.stream.Stream; +import static java.util.stream.Collectors.toList; + public class CustomisedReports implements IReporter { private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class); + private static final String ROW_TEMPLATE = "%s%s%s%s%s"; + public void generateReport(List xmlSuites, List suites, String outputDirectory) { String reportTemplate = initReportTemplate(); - String resultRow = "%s%s%s%s%s"; - StringBuilder rows = new StringBuilder(); - suites.forEach(suite -> { - Map suiteResults = suite.getResults(); - suiteResults.forEach((testName, suiteResult) -> { - ITestContext testContext = suiteResult.getTestContext(); + final List rows = suites + .stream() + .flatMap(suiteToResults()) + .collect(Collectors.toList()); - Stream failedTests = testContext.getFailedTests().getAllResults().stream(); - Stream passedTests = testContext.getPassedTests().getAllResults().stream(); - Stream skippedTests = testContext.getSkippedTests().getAllResults().stream(); - - String suiteName = suite.getName(); - - Stream allTestResults = Stream.concat(Stream.concat(failedTests, passedTests), skippedTests); - generateReportRows(resultRow, rows, testName, suiteName, allTestResults); - }); - }); reportTemplate = reportTemplate.replaceFirst("", rows.toString() + ""); saveReportTemplate(outputDirectory, reportTemplate); } - private void generateReportRows(String resultRow, StringBuilder rows, String testName, String suiteName, Stream allTestResults) { - allTestResults - .forEach(testResult -> { - String testReportRow = ""; - if (testResult.getStatus() == ITestResult.FAILURE) { - testReportRow = String.format(resultRow, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA"); - } - if (testResult.getStatus() == ITestResult.SUCCESS) { - testReportRow = String.format(resultRow, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis())); + private Function> suiteToResults() { + return suite -> suite.getResults().entrySet() + .stream() + .flatMap(resultsToRows(suite)); + } + private Function, Stream> resultsToRows(ISuite suite) { + return e -> { + ITestContext testContext = e.getValue().getTestContext(); + + Set failedTests = testContext + .getFailedTests() + .getAllResults(); + Set passedTests = testContext + .getPassedTests() + .getAllResults(); + Set skippedTests = testContext + .getSkippedTests() + .getAllResults(); + + String suiteName = suite.getName(); + + return Stream + .of(failedTests, passedTests, skippedTests) + .flatMap(results -> generateReportRows(e.getKey(), suiteName, results).stream()); + }; + } + + private List generateReportRows(String testName, String suiteName, Set allTestResults) { + return allTestResults.stream() + .map(testResultToResultRow(testName, suiteName)) + .collect(toList()); + } + + private Function testResultToResultRow(String testName, String suiteName) { + return testResult -> { + switch (testResult.getStatus()) { + case ITestResult.FAILURE: + return String.format(ROW_TEMPLATE, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA"); + + case ITestResult.SUCCESS: + return String.format(ROW_TEMPLATE, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis())); + + case ITestResult.SKIP: + return String.format(ROW_TEMPLATE, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA"); + + default: + return ""; } - if (testResult.getStatus() == ITestResult.SKIP) { - testReportRow = String.format(resultRow, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA"); - } - rows.append(testReportRow); - }); + }; } private String initReportTemplate() { String template = null; - byte[] reportTemplate = null; + byte[] reportTemplate; try { reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html")); template = new String(reportTemplate, "UTF-8");