Skip to content

Commit

Permalink
Restructure report generator
Browse files Browse the repository at this point in the history
Issue: TNG#54
Signed-off-by: Johannes Thorn <2544827+johthor@users.noreply.github.com>
  • Loading branch information
johthor committed Feb 11, 2024
1 parent af5badc commit f1a3f43
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -56,6 +59,7 @@ public class AsciiDocReportGenerator extends AbstractReportGenerator {

private File targetDir;
private File featuresDir;
private File tagsDir;


@Override
Expand All @@ -77,6 +81,14 @@ public void generate() {

writeIndexFileForPendingScenarios();

final HierarchyCalculator hierarchyCalculator = new HierarchyCalculator(allTags, taggedScenarioFeatures);

final Map<String, Map<String, List<String>>> groupedTags = hierarchyCalculator.computeGroupedTag();

groupedTags.forEach(this::writeIndexFileForTaggedScenarios);

writeIndexFileForAllTags(groupedTags);

writeTotalStatisticsFile();

writeIndexFileForFullReport(config.getTitle());
Expand Down Expand Up @@ -150,6 +162,76 @@ private void writeIndexFileForPendingScenarios() {
writeAsciiDocBlocksToFile(targetDir, "pendingScenarios", asciiDocBlocks);
}

private void writeIndexFileForTaggedScenarios(final String tagType, final Map<String, List<String>> taggedScenarios) {
final Optional<Tag> firstTag = taggedScenarios.keySet().stream()
.findFirst()
.map(allTags::get);

if (firstTag.isEmpty()) {
return;
}

final int numTaggedScenarios = taggedScenarios.keySet().stream().mapToInt(taggedScenarioCounts::get).sum();

final List<String> asciiDocBlocks = taggedScenarios.keySet().size() == 1
? singleValuedTag(taggedScenarios, firstTag.get(), numTaggedScenarios)
: multiValuedTag(taggedScenarios, firstTag.get(), numTaggedScenarios);

writeAsciiDocBlocksToFile(tagsDir, tagType, asciiDocBlocks);
}

private List<String> multiValuedTag(final Map<String, List<String>> taggedScenarios, final Tag tag, final int numTaggedScenarios) {
final AsciiDocSnippetGenerator snippetGenerator = new AsciiDocSnippetGenerator(
tag.getName(), "tagged scenarios", numTaggedScenarios);

final List<String> asciiDocBlocks = snippetGenerator.generateIntroSnippet(tag.getDescription());
taggedScenarios.forEach((tagId, features) -> {
final List<String> snippet = snippetGenerator.generateTagSnippet(
allTags.get(tagId), taggedScenarioCounts.get(tagId), features, 0);
asciiDocBlocks.addAll(snippet);
});
return asciiDocBlocks;
}

private List<String> singleValuedTag(final Map<String, List<String>> taggedScenarios, final Tag tag, final int numTaggedScenarios) {
final AsciiDocSnippetGenerator snippetGenerator = new AsciiDocSnippetGenerator(
tag.toString(), "tagged scenarios", numTaggedScenarios);

final List<String> asciiDocBlocks = snippetGenerator.generateIntroSnippet(tag.getDescription());
taggedScenarios.forEach((tagId, features) -> {
final Tag valueTag = allTags.get(tagId);
final String tagName = TagMapper.toAsciiDocTagName(valueTag);
asciiDocBlocks.add("=== Scenarios");
final List<String> snippet = snippetGenerator.generateIndexSnippet("../" + FEATURE_PATH, features, tagName, 0);
asciiDocBlocks.addAll(snippet);
});
return asciiDocBlocks;
}


private void writeIndexFileForAllTags(final Map<String, Map<String, List<String>>> strings) {

final List<String> tagFiles = strings.entrySet().stream()
.sorted((o1, o2) -> {
final Tag tag1 = allTags.get(o1.getValue().keySet().stream().findFirst().orElse(""));
final Tag tag2 = allTags.get(o2.getValue().keySet().stream().findFirst().orElse(""));
return Objects.compare(tag1, tag2, Comparator.comparing(Tag::getName));

})
.map(entry -> entry.getKey().replace(' ', '_'))
.collect(Collectors.toList());
final Integer total = taggedScenarioCounts.values().stream().reduce(Integer::sum).orElse(999);
final AsciiDocSnippetGenerator snippetGenerator = new AsciiDocSnippetGenerator(
"Tags", "all tags are beautiful", total
);

final List<String> asciiDocBlocks = snippetGenerator.generateIntroSnippet("");
asciiDocBlocks.addAll(snippetGenerator.generateIndexSnippet("tags", tagFiles, "", 1));
writeAsciiDocBlocksToFile(targetDir, "allTags",
asciiDocBlocks);

}

private void writeTotalStatisticsFile() {
final ListMultimap<String, ReportStatistics> featureStatistics = completeReportModel.getAllReportModels()
.stream()
Expand Down Expand Up @@ -180,7 +262,6 @@ private void writeIndexFileForFullReport(final String reportTitle) {
}

private boolean prepareDirectories(final File targetDir) {
File tagsDir;
this.targetDir = targetDir;
if (this.targetDir == null) {
log.error("Target directory was not configured");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.tngtech.jgiven.report.asciidoc;

import com.tngtech.jgiven.report.model.Tag;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class HierarchyCalculator {
private final Map<String, List<String>> taggedScenarioFiles;
private final Map<String, Tag> allTags;

public HierarchyCalculator(final Map<String, Tag> allTags, final Map<String, List<String>> taggedScenarioFiles) {
this.taggedScenarioFiles = taggedScenarioFiles;
this.allTags = allTags;
}

Map<String, Map<String, List<String>>> computeGroupedTag() {
return taggedScenarioFiles.entrySet().stream()
.filter(entry -> allTags.get(entry.getKey()).getShownInNavigation())
.collect(Collectors.groupingBy(this::fullType, Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}

private String fullType(final Map.Entry<String, List<String>> entry) {
final Tag tag = allTags.get(entry.getKey());

return tag.getFullType();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ include::allScenarios.asciidoc[]
include::failedScenarios.asciidoc[]

include::pendingScenarios.asciidoc[]

include::allTags.asciidoc[]

0 comments on commit f1a3f43

Please sign in to comment.