CategoriesPlugin extends CompositeAggregator implements Reader { @SuppressWarnings("PMD.DefaultPackage") static void addCategoriesForResults(final List<LaunchResults> launchesResults) { launchesResults.forEach(launch -> { final List<Category> categories = launch.getExtra(CATEGORIES, Collections::emptyList); launch.getResults().forEach(result -> { final List<Category> resultCategories = result.getExtraBlock(CATEGORIES, new ArrayList<>()); categories.forEach(category -> { if (matches(result, category)) { resultCategories.add(category); } }); if (resultCategories.isEmpty() && Status.FAILED.equals(result.getStatus())) { result.getExtraBlock(CATEGORIES, new ArrayList<Category>()).add(FAILED_TESTS); } if (resultCategories.isEmpty() && Status.BROKEN.equals(result.getStatus())) { result.getExtraBlock(CATEGORIES, new ArrayList<Category>()).add(BROKEN_TESTS); } }); }); } CategoriesPlugin(); @Override void readResults(final Configuration configuration,
                            final ResultsVisitor visitor,
                            final Path directory); static boolean matches(final TestResult result, final Category category); @Override void aggregate(final Configuration configuration,
                          final List<LaunchResults> launchesResults,
                          final Path outputDirectory); static final String CATEGORIES; static final Category FAILED_TESTS; static final Category BROKEN_TESTS; static final String JSON_FILE_NAME; static final String CSV_FILE_NAME; }
@Test void shouldDefaultCategoriesToResults() { final TestResult first = new TestResult() .setName("first") .setStatus(Status.FAILED) .setStatusMessage("A"); final TestResult second = new TestResult() .setName("second") .setStatus(Status.BROKEN) .setStatusMessage("B"); CategoriesPlugin.addCategoriesForResults(createSingleLaunchResults(first, second)); assertThat(first.getExtraBlock(CATEGORIES, new ArrayList<Category>())) .hasSize(1) .extracting(Category::getName) .containsExactlyInAnyOrder(FAILED_TESTS.getName()); assertThat(second.getExtraBlock(CATEGORIES, new ArrayList<Category>())) .hasSize(1) .extracting(Category::getName) .containsExactlyInAnyOrder(BROKEN_TESTS.getName()); }
@Test void shouldSetCustomCategoriesToResults() { final String categoryName = "Some category"; Category category = new Category() .setName(categoryName) .setMessageRegex(".*") .setMatchedStatuses(singletonList(Status.BROKEN)); Map<String, Object> meta = new HashMap<>(); meta.put("categories", singletonList(category)); final TestResult first = new TestResult() .setName("first") .setStatus(Status.FAILED) .setStatusMessage("B"); final TestResult second = new TestResult() .setName("second") .setStatus(Status.BROKEN) .setStatusMessage("B"); CategoriesPlugin.addCategoriesForResults(createSingleLaunchResults(meta, first, second)); assertThat(first.getExtraBlock(CATEGORIES, new ArrayList<Category>())) .hasSize(1) .extracting(Category::getName) .containsExactlyInAnyOrder(FAILED_TESTS.getName()); assertThat(second.getExtraBlock(CATEGORIES, new ArrayList<Category>())) .hasSize(1) .extracting(Category::getName) .containsExactlyInAnyOrder(categoryName); }