Skip to content

Commit c51e790

Browse files
committed
refactor methods out of services into relevant entities
1 parent 07d9407 commit c51e790

5 files changed

Lines changed: 40 additions & 40 deletions

File tree

src/main/java/com/aventstack/extentreports/ExtentReports.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.aventstack.extentreports.model.Media;
1111
import com.aventstack.extentreports.model.ReportStats;
1212
import com.aventstack.extentreports.model.SystemEnvInfo;
13-
import com.aventstack.extentreports.model.service.TestService;
1413
import com.aventstack.extentreports.observer.ExtentObserver;
1514

1615
/**
@@ -265,7 +264,7 @@ public void removeTest(ExtentTest test) {
265264
* The test name
266265
*/
267266
public void removeTest(String name) {
268-
TestService.findTest(getReport().getTestList(), name)
267+
getReport().findTest(getReport().getTestList(), name)
269268
.ifPresent(this::onTestRemoved);
270269
}
271270

src/main/java/com/aventstack/extentreports/model/Report.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Collections;
77
import java.util.Date;
88
import java.util.List;
9+
import java.util.Optional;
910
import java.util.stream.Collectors;
1011

1112
import com.aventstack.extentreports.Status;
@@ -73,7 +74,25 @@ public final boolean anyTestHasStatus(Status status) {
7374
return testList.stream()
7475
.anyMatch(x -> x.getStatus() == status);
7576
}
76-
77+
78+
public Optional<Test> findTest(List<Test> list, String name) {
79+
Optional<Test> test = list.stream().filter(x -> x.getName().equals(name)).findFirst();
80+
if (!test.isPresent())
81+
for (Test t : list)
82+
return findTest(t.getChildren(), name);
83+
return test;
84+
}
85+
86+
public List<ExceptionInfo> aggregateExceptions(List<Test> testList) {
87+
List<ExceptionInfo> list = new ArrayList<>();
88+
for (Test test : testList) {
89+
list.addAll(test.aggregateExceptions());
90+
if (!test.getChildren().isEmpty())
91+
aggregateExceptions(test.getChildren());
92+
}
93+
return list;
94+
}
95+
7796
public final long timeTaken() {
7897
return endTime.getTime() - startTime.getTime();
7998
}

src/main/java/com/aventstack/extentreports/model/Test.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Set;
1414
import java.util.concurrent.ConcurrentHashMap;
1515
import java.util.concurrent.atomic.AtomicInteger;
16+
import java.util.stream.Collectors;
1617

1718
import com.aventstack.extentreports.Status;
1819
import com.aventstack.extentreports.gherkin.model.IGherkinFormatterModel;
@@ -168,6 +169,13 @@ public final boolean hasScreenCapture() {
168169
public final long timeTaken() {
169170
return endTime.getTime() - startTime.getTime();
170171
}
172+
173+
public List<ExceptionInfo> aggregateExceptions() {
174+
return logs.stream()
175+
.filter(x -> x.getException() != null)
176+
.map(x -> x.getException())
177+
.collect(Collectors.toList());
178+
}
171179

172180
public final Test getAncestor() {
173181
Test test = this;
Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,13 @@
11
package com.aventstack.extentreports.model.service;
22

3-
import java.util.ArrayList;
43
import java.util.Calendar;
54
import java.util.List;
6-
import java.util.Optional;
7-
import java.util.stream.Collectors;
85

96
import com.aventstack.extentreports.gherkin.model.IGherkinFormatterModel;
10-
import com.aventstack.extentreports.model.ExceptionInfo;
117
import com.aventstack.extentreports.model.Log;
128
import com.aventstack.extentreports.model.Test;
139

1410
public class TestService {
15-
public static boolean deleteTest(List<Test> list, Test test) {
16-
boolean removed = list.removeIf(x -> x.getId() == test.getId());
17-
if (!removed)
18-
list.forEach(x -> deleteTest(x.getChildren(), test));
19-
return removed;
20-
}
21-
22-
public static Optional<Test> findTest(List<Test> list, String name) {
23-
Optional<Test> test = list.stream().filter(x -> x.getName().equals(name)).findFirst();
24-
if (!test.isPresent())
25-
for (Test t : list)
26-
return findTest(t.getChildren(), name);
27-
return test;
28-
}
29-
3011
public static Boolean testHasScreenCapture(Test test, Boolean deep) {
3112
if (deep) {
3213
Boolean hasScreenCapture = !test.getMedia().isEmpty()
@@ -38,23 +19,6 @@ public static Boolean testHasScreenCapture(Test test, Boolean deep) {
3819
return test.hasScreenCapture();
3920
}
4021

41-
public static List<ExceptionInfo> aggregateExceptions(List<Test> testList) {
42-
List<ExceptionInfo> list = new ArrayList<>();
43-
for (Test test : testList) {
44-
list.addAll(aggregateExceptions(test));
45-
if (!test.getChildren().isEmpty())
46-
aggregateExceptions(test.getChildren());
47-
}
48-
return list;
49-
}
50-
51-
public static List<ExceptionInfo> aggregateExceptions(Test test) {
52-
return test.getLogs().stream()
53-
.filter(x -> x.getException() != null)
54-
.map(x -> x.getException())
55-
.collect(Collectors.toList());
56-
}
57-
5822
public static Test createTest(Class<? extends IGherkinFormatterModel> type, String name, String description) {
5923
if (name == null || name.isEmpty())
6024
throw new IllegalArgumentException("Test name cannot be null or empty");
@@ -72,4 +36,11 @@ public static Test createTest(String name, String description) {
7236
public static Test createTest(String name) {
7337
return createTest(name, null);
7438
}
39+
40+
public static boolean deleteTest(List<Test> list, Test test) {
41+
boolean removed = list.removeIf(x -> x.getId() == test.getId());
42+
if (!removed)
43+
list.forEach(x -> deleteTest(x.getChildren(), test));
44+
return removed;
45+
}
7546
}

src/test/java/com/aventstack/extentreports/reporter/JsonFormatterBDDTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.aventstack.extentreports.reporter;
22

33
import java.io.IOException;
4+
import java.io.UnsupportedEncodingException;
45

56
import org.testng.annotations.Test;
67

78
import com.aventstack.extentreports.ExtentReports;
9+
import com.aventstack.extentreports.gherkin.GherkinDialectManager;
810
import com.aventstack.extentreports.gherkin.model.Feature;
911
import com.aventstack.extentreports.gherkin.model.Given;
1012
import com.aventstack.extentreports.gherkin.model.Scenario;
@@ -13,8 +15,9 @@ public class JsonFormatterBDDTest {
1315
private static final String JSON_PATH = "target/extent.json";
1416

1517
@Test
16-
public void writeBdd() {
18+
public void writeBdd() throws UnsupportedEncodingException {
1719
ExtentReports extent = new ExtentReports();
20+
extent.setGherkinDialect(GherkinDialectManager.getDefaultLanguage());
1821
JsonFormatter json = new JsonFormatter(JSON_PATH);
1922
extent.attachReporter(json);
2023
extent.createTest(Feature.class, "FeatureName")

0 commit comments

Comments
 (0)