Idiomatic refactor
This commit is contained in:
		
							parent
							
								
									4e8a466b45
								
							
						
					
					
						commit
						6856ff42ad
					
				| @ -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 = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>"; | ||||
| 
 | ||||
|     public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { | ||||
|         String reportTemplate = initReportTemplate(); | ||||
|         String resultRow = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>"; | ||||
|         StringBuilder rows = new StringBuilder(); | ||||
|         suites.forEach(suite -> { | ||||
|             Map<String, ISuiteResult> suiteResults = suite.getResults(); | ||||
|             suiteResults.forEach((testName, suiteResult) -> { | ||||
| 
 | ||||
|                 ITestContext testContext = suiteResult.getTestContext(); | ||||
|         final List<String> rows = suites | ||||
|           .stream() | ||||
|           .flatMap(suiteToResults()) | ||||
|           .collect(Collectors.toList()); | ||||
| 
 | ||||
|                 Stream<ITestResult> failedTests = testContext.getFailedTests().getAllResults().stream(); | ||||
|                 Stream<ITestResult> passedTests = testContext.getPassedTests().getAllResults().stream(); | ||||
|                 Stream<ITestResult> skippedTests = testContext.getSkippedTests().getAllResults().stream(); | ||||
| 
 | ||||
|                 String suiteName = suite.getName(); | ||||
| 
 | ||||
|                 Stream<ITestResult> allTestResults = Stream.concat(Stream.concat(failedTests, passedTests), skippedTests); | ||||
|                 generateReportRows(resultRow, rows, testName, suiteName, allTestResults); | ||||
|             }); | ||||
|         }); | ||||
|         reportTemplate = reportTemplate.replaceFirst("</tbody>", rows.toString() + "</tbody>"); | ||||
|         saveReportTemplate(outputDirectory, reportTemplate); | ||||
|     } | ||||
| 
 | ||||
|     private void generateReportRows(String resultRow, StringBuilder rows, String testName, String suiteName, Stream<ITestResult> 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<ISuite, Stream<? extends String>> suiteToResults() { | ||||
|         return suite -> suite.getResults().entrySet() | ||||
|           .stream() | ||||
|           .flatMap(resultsToRows(suite)); | ||||
|     } | ||||
| 
 | ||||
|     private Function<Map.Entry<String, ISuiteResult>, Stream<? extends String>> resultsToRows(ISuite suite) { | ||||
|         return e -> { | ||||
|             ITestContext testContext = e.getValue().getTestContext(); | ||||
| 
 | ||||
|             Set<ITestResult> failedTests = testContext | ||||
|               .getFailedTests() | ||||
|               .getAllResults(); | ||||
|             Set<ITestResult> passedTests = testContext | ||||
|               .getPassedTests() | ||||
|               .getAllResults(); | ||||
|             Set<ITestResult> skippedTests = testContext | ||||
|               .getSkippedTests() | ||||
|               .getAllResults(); | ||||
| 
 | ||||
|             String suiteName = suite.getName(); | ||||
| 
 | ||||
|             return Stream | ||||
|               .of(failedTests, passedTests, skippedTests) | ||||
|               .flatMap(results -> generateReportRows(e.getKey(), suiteName, results).stream()); | ||||
|         }; | ||||
|     } | ||||
| 
 | ||||
|     private List<String> generateReportRows(String testName, String suiteName, Set<ITestResult> allTestResults) { | ||||
|         return allTestResults.stream() | ||||
|           .map(testResultToResultRow(testName, suiteName)) | ||||
|           .collect(toList()); | ||||
|     } | ||||
| 
 | ||||
|     private Function<ITestResult, String> 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"); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user