Skip to content

Commit 5f8f532

Browse files
committed
#173, #176 refactoring
Combined usage: ExtentSparkReporter spark = new ExtentSparkReporter("spark.html") .filter() .statusFilter() .as(new Status[] { Status.SKIP, Status.FAIL }) .apply() .viewConfigurer() .viewOrder() .as(new ViewName[] { ViewName.DASHBOARD }) .apply();
1 parent a4c2ca6 commit 5f8f532

File tree

13 files changed

+122
-89
lines changed

13 files changed

+122
-89
lines changed

Readme.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ It is now possible to create separate reports for each status (or a group of the
4545
ExtentReports extent = new ExtentReports();
4646
// will only contain failures
4747
ExtentSparkReporter sparkFail = new ExtentSparkReporter("target/spark/fail.html")
48-
.with()
49-
.statusFilter().as(new Status[] {Status.FAIL})
50-
.<ExtentSparkReporter>build();
48+
.filter()
49+
.statusFilter()
50+
.as(new Status[] { Status.FAIL })
51+
.apply()
5152
// will contain all tests
5253
ExtentSparkReporter sparkAll = new ExtentSparkReporter("spark/all.html");
5354
extent.attachReporter(sparkFail, sparkAll);
@@ -60,9 +61,10 @@ to be the primary view, followed by Tests, you can use the snippet below:
6061
```java
6162
ExtentReports extent = new ExtentReports();
6263
ExtentSparkReporter spark = new ExtentSparkReporter("spark/spark.html")
63-
.with()
64-
.viewOrder().as(new ViewName[] { ViewName.DASHBOARD, ViewName.TEST })
65-
.<ExtentSparkReporter>build();
64+
.viewConfigurer()
65+
.viewOrder()
66+
.as(new ViewName[] { ViewName.DASHBOARD, ViewName.TEST })
67+
.apply();
6668
```
6769

6870
The above will limit the report to 2 views, with DASHBOARD view the primary one, followed by TEST.

src/main/java/com/aventstack/extentreports/model/service/ReportStatsService.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.aventstack.extentreports.model.service;
22

3+
import java.util.ArrayList;
34
import java.util.Arrays;
45
import java.util.List;
56
import java.util.Map;
@@ -17,13 +18,14 @@ public class ReportStatsService {
1718

1819
public static void refreshReportStats(final ReportStats stats,
1920
final List<Test> testList) {
21+
resetReportStats(stats);
2022
if (testList == null || testList.isEmpty())
2123
return;
2224

23-
resetReportStats(stats);
2425
refreshReportStats(stats, testList, stats.getParent(), stats.getParentPercentage());
2526

26-
// level 1, for BDD, this would also include Scenario and excludes ScenarioOutline
27+
// level 1, for BDD, this would also include Scenario and excludes
28+
// ScenarioOutline
2729
List<Test> children = testList.stream()
2830
.flatMap(x -> x.getChildren().stream())
2931
.filter(x -> x.getBddType() != ScenarioOutline.class)
@@ -63,26 +65,29 @@ public static void refreshReportStats(final ReportStats stats,
6365
private static void refreshReportStats(final ReportStats stats,
6466
final List<? extends RunResult> list, final Map<Status, Long> countMap,
6567
final Map<Status, Float> percentageMap) {
66-
if (list == null || list.isEmpty())
68+
if (list == null)
6769
return;
6870
Map<Status, Long> map = list.stream().collect(
6971
Collectors.groupingBy(RunResult::getStatus, Collectors.counting()));
7072
Arrays.asList(Status.values()).forEach(x -> map.putIfAbsent(x, 0L));
73+
countMap.putAll(map);
74+
if (list.isEmpty()) {
75+
percentageMap.putAll(
76+
map.entrySet().stream()
77+
.collect(Collectors.toMap(e -> e.getKey(), e -> Float.valueOf(e.getValue()))));
78+
return;
79+
}
7180
Map<Status, Float> pctMap = map.entrySet().stream()
7281
.collect(Collectors.toMap(e -> e.getKey(),
7382
e -> Float.valueOf(e.getValue() * 100 / list.size())));
74-
countMap.putAll(map);
7583
percentageMap.putAll(pctMap);
7684
}
7785

7886
public static void resetReportStats(final ReportStats stats) {
79-
stats.getChild().clear();
80-
stats.getChildPercentage().clear();
81-
stats.getGrandchild().clear();
82-
stats.getGrandchildPercentage().clear();
83-
stats.getLog().clear();
84-
stats.getLogPercentage().clear();
85-
stats.getParent().clear();
86-
stats.getParentPercentage().clear();
87+
List<RunResult> list = new ArrayList<>();
88+
refreshReportStats(stats, list, stats.getParent(), stats.getParentPercentage());
89+
refreshReportStats(stats, list, stats.getChild(), stats.getChildPercentage());
90+
refreshReportStats(stats, list, stats.getGrandchild(), stats.getGrandchildPercentage());
91+
refreshReportStats(stats, list, stats.getLog(), stats.getLogPercentage());
8792
}
8893
}

src/main/java/com/aventstack/extentreports/reporter/ExtentSparkReporter.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,28 @@
1313
import com.aventstack.extentreports.model.Report;
1414
import com.aventstack.extentreports.observer.ReportObserver;
1515
import com.aventstack.extentreports.observer.entity.ReportEntity;
16+
import com.aventstack.extentreports.reporter.configuration.EntityFilters;
1617
import com.aventstack.extentreports.reporter.configuration.ExtentSparkReporterConfig;
17-
import com.aventstack.extentreports.reporter.configuration.FileReporterConfigurer;
18-
import com.aventstack.extentreports.reporter.configuration.InteractiveReporterConfig;
18+
import com.aventstack.extentreports.reporter.configuration.ViewConfigurer;
1919
import com.aventstack.extentreports.reporter.configuration.ViewName;
20+
import com.aventstack.extentreports.reporter.configuration.ViewsConfigurable;
2021
import com.google.gson.Gson;
2122

2223
import freemarker.template.Template;
2324
import freemarker.template.TemplateException;
2425
import io.reactivex.rxjava3.core.Observer;
2526
import io.reactivex.rxjava3.disposables.Disposable;
27+
import lombok.AccessLevel;
2628
import lombok.Getter;
2729

2830
@Getter
2931
@SuppressWarnings("rawtypes")
30-
public class ExtentSparkReporter extends AbstractFileReporter implements ReportObserver, ReporterConfigurable {
32+
public class ExtentSparkReporter extends AbstractFileReporter
33+
implements
34+
ReportObserver,
35+
ReporterConfigurable,
36+
ViewsConfigurable<ExtentSparkReporter>,
37+
ReporterFilterable {
3138
private static final Logger logger = Logger.getLogger(ExtentSparkReporter.class.getName());
3239
private static final String TEMPLATE_LOCATION = "templates/";
3340
private static final String ENCODING = "UTF-8";
@@ -38,7 +45,11 @@ public class ExtentSparkReporter extends AbstractFileReporter implements ReportO
3845
ViewName.CATEGORY, ViewName.DASHBOARD, ViewName.EXCEPTION, ViewName.TEST, ViewName.LOG
3946
});
4047

41-
private FileReporterConfigurer<ExtentSparkReporter> configurer = new FileReporterConfigurer<>(this);
48+
@Getter(value = AccessLevel.NONE)
49+
private final ViewConfigurer<ExtentSparkReporter> viewConfigurer = new ViewConfigurer<>(this);
50+
@Getter(value = AccessLevel.NONE)
51+
private final EntityFilters<ExtentSparkReporter> filter = new EntityFilters<>(this);
52+
4253
private ExtentSparkReporterConfig conf = ExtentSparkReporterConfig.builder().build();
4354
private Disposable disposable;
4455
private List<ViewName> viewNames = SUPPORTED_VIEWS;
@@ -51,11 +62,17 @@ public ExtentSparkReporter(File f) {
5162
super(f);
5263
}
5364

54-
public FileReporterConfigurer with() {
55-
return configurer;
65+
@Override
66+
public EntityFilters<ExtentSparkReporter> filter() {
67+
return filter;
68+
}
69+
70+
@Override
71+
public ViewConfigurer<ExtentSparkReporter> viewConfigurer() {
72+
return viewConfigurer;
5673
}
5774

58-
public InteractiveReporterConfig config() {
75+
public ExtentSparkReporterConfig config() {
5976
return conf;
6077
}
6178

@@ -122,10 +139,10 @@ private void start(Disposable d) {
122139
}
123140

124141
private void flush(ReportEntity value) {
125-
final Report report = filterAndGet(value.getReport(), configurer.getStatusFilter().getStatus());
142+
final Report report = filterAndGet(value.getReport(), filter.statusFilter().getStatus());
126143
try {
127144
getTemplateModel().put("this", this);
128-
getTemplateModel().put("viewOrder", configurer.getViewOrder().getViewOrder());
145+
getTemplateModel().put("viewOrder", viewConfigurer.viewOrder().getViewOrder());
129146
getTemplateModel().put("report", report);
130147
createFreemarkerConfig(TEMPLATE_LOCATION, ENCODING);
131148
final String filePath = getFile().isDirectory()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.aventstack.extentreports.reporter;
2+
3+
import com.aventstack.extentreports.reporter.configuration.EntityFilters;
4+
5+
@FunctionalInterface
6+
public interface ReporterFilterable<T extends AbstractReporter> {
7+
EntityFilters<T> filter();
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.aventstack.extentreports.reporter.configuration;
2+
3+
import com.aventstack.extentreports.reporter.AbstractReporter;
4+
5+
import lombok.Getter;
6+
import lombok.experimental.Accessors;
7+
8+
@Getter
9+
@Accessors(fluent = true)
10+
public class EntityFilters<T extends AbstractReporter> {
11+
private final T reporter;
12+
private final StatusFilter<T> statusFilter;
13+
14+
public EntityFilters(T reporter) {
15+
this.reporter = reporter;
16+
statusFilter = new StatusFilter<T>(this);
17+
}
18+
19+
public T apply() {
20+
return reporter;
21+
}
22+
}

src/main/java/com/aventstack/extentreports/reporter/configuration/FileReporterConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
@Getter
99
@Setter
10-
@SuperBuilder
10+
@SuperBuilder(builderMethodName = "with")
1111
public abstract class FileReporterConfig extends AbstractConfiguration {
1212
@Builder.Default
1313
private String encoding = "UTF-8";

src/main/java/com/aventstack/extentreports/reporter/configuration/FileReporterConfigurer.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main/java/com/aventstack/extentreports/reporter/configuration/StatusFilter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@
1111
import lombok.Getter;
1212

1313
@Getter
14-
public class StatusFilter {
14+
public class StatusFilter<T extends AbstractReporter> {
1515
private Set<Status> status = new HashSet<>(Arrays.asList(Status.values()));
16-
private FileReporterConfigurer<?> configurer;
16+
private EntityFilters<?> configurer;
1717

18-
public <T extends AbstractReporter> StatusFilter(FileReporterConfigurer<T> configurer) {
18+
public StatusFilter(EntityFilters<T> configurer) {
1919
this.configurer = configurer;
2020
}
2121

2222
@SuppressWarnings("unchecked")
23-
public <T extends AbstractReporter> FileReporterConfigurer<T> as(Set<Status> status) {
23+
public EntityFilters<T> as(Set<Status> status) {
2424
this.status = status;
25-
return (FileReporterConfigurer<T>) configurer.configure(this);
25+
return (EntityFilters<T>) configurer;
2626
}
2727

28-
public <T extends AbstractReporter> FileReporterConfigurer<T> as(List<Status> status) {
28+
public EntityFilters<T> as(List<Status> status) {
2929
return as(new HashSet<>(status));
3030
}
3131

32-
public <T extends AbstractReporter> FileReporterConfigurer<T> as(Status[] status) {
32+
public EntityFilters<T> as(Status[] status) {
3333
return as(Arrays.asList(status));
3434
}
3535
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.aventstack.extentreports.reporter.configuration;
2+
3+
import lombok.Getter;
4+
import lombok.experimental.Accessors;
5+
6+
@Getter
7+
@Accessors(fluent = true)
8+
public class ViewConfigurer<T extends ViewsConfigurable<T>> {
9+
private final T reporter;
10+
private final ViewOrder<T> viewOrder;
11+
12+
public ViewConfigurer(T reporter) {
13+
this.reporter = reporter;
14+
viewOrder = new ViewOrder<T>(this);
15+
}
16+
17+
public T apply() {
18+
return reporter;
19+
}
20+
}

src/main/java/com/aventstack/extentreports/reporter/configuration/ViewOrder.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import java.util.Arrays;
44
import java.util.List;
55

6-
import com.aventstack.extentreports.reporter.AbstractReporter;
7-
86
import lombok.Getter;
97
import lombok.NoArgsConstructor;
108

119
@Getter
1210
@NoArgsConstructor
13-
public class ViewOrder {
11+
public class ViewOrder<T extends ViewsConfigurable<T>> {
1412
private static final List<ViewName> DEFAULT_ORDER = Arrays.asList(new ViewName[]{
1513
ViewName.TEST,
1614
ViewName.EXCEPTION,
@@ -22,19 +20,19 @@ public class ViewOrder {
2220
});
2321

2422
private List<ViewName> viewOrder = DEFAULT_ORDER;
25-
private FileReporterConfigurer<?> configurer;
23+
private ViewConfigurer<?> configurer;
2624

27-
public <T extends AbstractReporter> ViewOrder(FileReporterConfigurer<T> configurer) {
25+
public ViewOrder(ViewConfigurer<T> configurer) {
2826
this.configurer = configurer;
2927
}
3028

3129
@SuppressWarnings("unchecked")
32-
public <T extends AbstractReporter> FileReporterConfigurer<T> as(List<ViewName> order) {
30+
public ViewConfigurer<T> as(List<ViewName> order) {
3331
this.viewOrder = order;
34-
return (FileReporterConfigurer<T>) configurer.configure(this);
32+
return (ViewConfigurer<T>) configurer;
3533
}
3634

37-
public <T extends AbstractReporter> FileReporterConfigurer<T> as(ViewName[] viewOrder) {
35+
public ViewConfigurer<T> as(ViewName[] viewOrder) {
3836
return as(Arrays.asList(viewOrder));
3937
}
4038
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.aventstack.extentreports.reporter.configuration;
2+
3+
public interface ViewsConfigurable<T> {
4+
ViewConfigurer<?> viewConfigurer();
5+
}

src/main/java/com/aventstack/extentreports/reporter/configuration/ViewsOrderable.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/resources/com/aventstack/extentreports/templates/spark/partials/dashboard.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,12 @@
205205
<script>
206206
var statusGroup = {
207207
parentCount: ${ report.stats.parent?size?c },
208+
<#if report.stats.parent?size != 0>
208209
passParent: ${ report.stats.parent?api.get(Status.PASS)?c },
209210
failParent: ${ report.stats.parent?api.get(Status.FAIL)?c },
210211
warningParent: ${ report.stats.parent?api.get(Status.WARNING)?c },
211212
skipParent: ${ report.stats.parent?api.get(Status.SKIP)?c },
213+
</#if>
212214
childCount: ${ report.stats.child?size?c },
213215
<#if report.stats.child?size != 0>
214216
passChild: ${ report.stats.child?api.get(Status.PASS)?c },

0 commit comments

Comments
 (0)