Skip to content

Commit a809591

Browse files
committed
closes #161, closes #131
1 parent c5b8b05 commit a809591

File tree

12 files changed

+431
-22
lines changed

12 files changed

+431
-22
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ spark.withConfig(
177177
## What's not working (yet)
178178

179179
* ~~OfflineMode~~
180-
* Append, CreateDomainFromJsonArchive
180+
* ~~Append, CreateDomainFromJsonArchive~~
181181
* ~~Loading external configuration~~
182182
* You tell me..
183183

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.aventstack.extentreports;
22

3+
import java.io.File;
4+
import java.io.IOException;
35
import java.util.Calendar;
46
import java.util.List;
57

8+
import com.aventstack.extentreports.append.RawEntityConverter;
69
import com.aventstack.extentreports.model.Author;
710
import com.aventstack.extentreports.model.Category;
811
import com.aventstack.extentreports.model.Device;
@@ -98,4 +101,9 @@ protected void onSystemInfoAdded(SystemEnvInfo env) {
98101
protected void tryesolveMediaPathUsingKnownPaths(String[] knownPath) {
99102
this.mediaResolverPath = knownPath;
100103
}
104+
105+
protected void convertRawEntities(ExtentReports extent, File f) throws IOException {
106+
RawEntityConverter converter = new RawEntityConverter(extent);
107+
converter.convertAndApply(f);
108+
}
101109
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,16 @@ public ExtentTest createTest(Class<? extends IGherkinFormatterModel> type, Strin
186186
*
187187
* @param gherkinKeyword
188188
* Name of the {@link GherkinKeyword}
189-
* @param testName
189+
* @param name
190190
* Name of test
191191
* @param description
192192
* A short description of the test
193193
*
194194
* @return {@link ExtentTest} object
195195
*/
196-
public ExtentTest createTest(GherkinKeyword gherkinKeyword, String testName, String description) {
196+
public ExtentTest createTest(GherkinKeyword gherkinKeyword, String name, String description) {
197197
Class<? extends IGherkinFormatterModel> clazz = gherkinKeyword.getKeyword().getClass();
198-
return createTest(clazz, testName, description);
198+
return createTest(clazz, name, description);
199199
}
200200

201201
/**
@@ -377,6 +377,7 @@ public ExtentReports tryResolveMediaPath(String[] path) {
377377
* Exception thrown if the jsonFile is not found
378378
*/
379379
public void createDomainFromJsonArchive(File jsonFile) throws IOException {
380+
convertRawEntities(this, jsonFile);
380381
}
381382

382383
/**

src/main/java/com/aventstack/extentreports/json/JsonDeserializer.java renamed to src/main/java/com/aventstack/extentreports/append/JsonDeserializer.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.aventstack.extentreports.json;
1+
package com.aventstack.extentreports.append;
22

33
import java.io.File;
44
import java.io.IOException;
@@ -7,13 +7,23 @@
77
import java.util.ArrayList;
88
import java.util.List;
99

10+
import com.aventstack.extentreports.model.Media;
1011
import com.aventstack.extentreports.model.Test;
1112
import com.google.gson.Gson;
13+
import com.google.gson.GsonBuilder;
1214
import com.google.gson.reflect.TypeToken;
1315

14-
class JsonDeserializer {
15-
public List<Test> deserialize(File f) throws IOException {
16-
Gson gson = new Gson();
16+
public class JsonDeserializer {
17+
private File f;
18+
19+
public JsonDeserializer(File f) {
20+
this.f = f;
21+
}
22+
23+
public List<Test> deserialize() throws IOException {
24+
Gson gson = new GsonBuilder()
25+
.registerTypeAdapter(Media.class, new ScreenCaptureTypeAdapter())
26+
.create();
1727
String json = new String(Files.readAllBytes(f.toPath()));
1828
Type t = new TypeToken<ArrayList<Test>>() {
1929
}.getType();
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.aventstack.extentreports.append;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.List;
6+
7+
import com.aventstack.extentreports.ExtentReports;
8+
import com.aventstack.extentreports.ExtentTest;
9+
import com.aventstack.extentreports.GherkinKeyword;
10+
import com.aventstack.extentreports.MediaEntityBuilder;
11+
import com.aventstack.extentreports.model.Log;
12+
import com.aventstack.extentreports.model.Media;
13+
import com.aventstack.extentreports.model.ScreenCapture;
14+
import com.aventstack.extentreports.model.Test;
15+
16+
public class RawEntityConverter {
17+
private final ExtentReports extent;
18+
19+
public RawEntityConverter(ExtentReports extent) {
20+
this.extent = extent;
21+
}
22+
23+
public void convertAndApply(File jsonFile) throws IOException {
24+
if (!jsonFile.exists()) {
25+
return;
26+
}
27+
extent.setReportUsesManualConfiguration(true);
28+
List<Test> tests = new JsonDeserializer(jsonFile).deserialize();
29+
for (Test test : tests) {
30+
try {
31+
if (test.getBddType() == null) {
32+
createDomain(test, extent.createTest(test.getName(), test.getDescription()));
33+
} else {
34+
ExtentTest extentTest = extent.createTest(new GherkinKeyword(test.getBddType().toString()),
35+
test.getName(), test.getDescription());
36+
createDomain(test, extentTest);
37+
}
38+
} catch (ClassNotFoundException e) {
39+
e.printStackTrace();
40+
}
41+
}
42+
}
43+
44+
public void createDomain(Test test, ExtentTest extentTest) throws ClassNotFoundException {
45+
extentTest.getModel().setStartTime(test.getStartTime());
46+
extentTest.getModel().setEndTime(test.getEndTime());
47+
addMedia(test, extentTest);
48+
49+
// create events
50+
for (Log log : test.getLogs()) {
51+
if (!log.getMedia().isEmpty())
52+
addMedia(log, extentTest);
53+
else if (!log.getExceptions().isEmpty() && !log.getMedia().isEmpty())
54+
addMedia(log, extentTest, log.getExceptions().get(0).getException());
55+
else if (!log.getExceptions().isEmpty())
56+
log.getExceptions().forEach(x -> extentTest.log(log.getStatus(), x.getException()));
57+
else
58+
extentTest.log(log.getStatus(), log.getDetails());
59+
}
60+
61+
// assign attributes
62+
test.getAuthorSet().stream().map(x -> x.getName()).forEach(extentTest::assignAuthor);
63+
test.getCategorySet().stream().map(x -> x.getName()).forEach(extentTest::assignCategory);
64+
test.getDeviceSet().stream().map(x -> x.getName()).forEach(extentTest::assignDevice);
65+
66+
// handle nodes
67+
for (Test node : test.getChildren()) {
68+
ExtentTest extentNode = null;
69+
if (node.getBddType() == null)
70+
extentNode = extentTest.createNode(node.getName(), node.getDescription());
71+
else
72+
extentNode = extentTest.createNode(new GherkinKeyword(node.getBddType().toString()), node.getName(),
73+
node.getDescription());
74+
addMedia(node, extentNode);
75+
createDomain(node, extentNode);
76+
}
77+
}
78+
79+
private void addMedia(Log log, ExtentTest extentTest, Throwable ex) {
80+
for (Media m : log.getMedia()) {
81+
if (m.getPath() != null) {
82+
extentTest.log(log.getStatus(), ex,
83+
MediaEntityBuilder.createScreenCaptureFromPath(m.getPath()).build());
84+
} else if (((ScreenCapture) m).getBase64() != null) {
85+
extentTest.log(log.getStatus(), ex,
86+
MediaEntityBuilder.createScreenCaptureFromBase64String(((ScreenCapture) m).getBase64())
87+
.build());
88+
}
89+
}
90+
}
91+
92+
private void addMedia(Log log, ExtentTest extentTest) {
93+
for (Media m : log.getMedia()) {
94+
if (m.getPath() != null) {
95+
extentTest.log(log.getStatus(), log.getDetails(),
96+
MediaEntityBuilder.createScreenCaptureFromPath(m.getPath()).build());
97+
} else if (((ScreenCapture) m).getBase64() != null) {
98+
extentTest.log(log.getStatus(), log.getDetails(),
99+
MediaEntityBuilder.createScreenCaptureFromBase64String(((ScreenCapture) m).getBase64())
100+
.build());
101+
}
102+
}
103+
}
104+
105+
private void addMedia(Test test, ExtentTest extentTest) {
106+
if (test.getMedia() != null) {
107+
for (Media m : test.getMedia()) {
108+
if (m.getPath() != null) {
109+
extentTest.addScreenCaptureFromPath(m.getPath());
110+
} else if (m instanceof ScreenCapture) {
111+
extentTest.addScreenCaptureFromBase64String(((ScreenCapture) m).getBase64());
112+
}
113+
}
114+
}
115+
}
116+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.aventstack.extentreports.append;
2+
3+
import java.io.IOException;
4+
5+
import com.aventstack.extentreports.model.Media;
6+
import com.aventstack.extentreports.model.ScreenCapture;
7+
import com.google.gson.TypeAdapter;
8+
import com.google.gson.stream.JsonReader;
9+
import com.google.gson.stream.JsonToken;
10+
import com.google.gson.stream.JsonWriter;
11+
12+
public class ScreenCaptureTypeAdapter extends TypeAdapter<Media> {
13+
14+
@Override
15+
public void write(JsonWriter out, Media value) throws IOException {
16+
}
17+
18+
@Override
19+
public Media read(JsonReader reader) throws IOException {
20+
ScreenCapture sc = ScreenCapture.builder().build();
21+
reader.beginObject();
22+
String fieldName = null;
23+
int cycle = 0;
24+
while (reader.hasNext()) {
25+
JsonToken token = reader.peek();
26+
if (token.equals(JsonToken.NAME)) {
27+
fieldName = reader.nextName();
28+
}
29+
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("path")) {
30+
token = reader.peek();
31+
sc.setPath(reader.nextString());
32+
}
33+
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("resolvedPath")) {
34+
token = reader.peek();
35+
sc.setPath(reader.nextString());
36+
}
37+
if ("string".equalsIgnoreCase(token.name()) && fieldName.equalsIgnoreCase("base64")) {
38+
token = reader.peek();
39+
sc.setBase64(reader.nextString());
40+
}
41+
if (cycle++ > 10)
42+
return sc;
43+
}
44+
reader.endObject();
45+
return sc;
46+
}
47+
48+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@Getter
1010
@Setter
1111
@AllArgsConstructor
12-
public abstract class Media implements Serializable, BaseEntity {
12+
public class Media implements Serializable, BaseEntity {
1313
private static final long serialVersionUID = 5428859443090457608L;
1414

1515
private String path;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public class Report implements Serializable, BaseEntity {
3030
private Date endTime = Calendar.getInstance().getTime();
3131
@Builder.Default
3232
private Status status = Status.PASS;
33-
private final ReportStats stats = new ReportStats();
33+
private final transient ReportStats stats = new ReportStats();
3434
private final List<Test> testList = Collections.synchronizedList(new ArrayList<>());
35-
private final NamedAttributeContextManager<Author> authorCtx = new NamedAttributeContextManager<>();
36-
private final NamedAttributeContextManager<Category> categoryCtx = new NamedAttributeContextManager<>();
37-
private final NamedAttributeContextManager<Device> deviceCtx = new NamedAttributeContextManager<>();
38-
private final NamedAttributeContextManager<ExceptionInfo> exceptionInfoCtx = new NamedAttributeContextManager<>();
35+
private final transient NamedAttributeContextManager<Author> authorCtx = new NamedAttributeContextManager<>();
36+
private final transient NamedAttributeContextManager<Category> categoryCtx = new NamedAttributeContextManager<>();
37+
private final transient NamedAttributeContextManager<Device> deviceCtx = new NamedAttributeContextManager<>();
38+
private final transient NamedAttributeContextManager<ExceptionInfo> exceptionInfoCtx = new NamedAttributeContextManager<>();
3939
private final List<String> logs = Collections.synchronizedList(new ArrayList<>());
4040
private final List<SystemEnvInfo> systemEnvInfo = new ArrayList<>();
4141
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public final class Test implements RunResult, Serializable, BaseEntity {
2828
private static final long serialVersionUID = -4896520724677957845L;
2929
private static final AtomicInteger atomicInt = new AtomicInteger(0);
3030

31-
private final Integer id = atomicInt.incrementAndGet();
31+
private final transient Integer id = atomicInt.incrementAndGet();
3232
@Builder.Default
3333
private Date startTime = Calendar.getInstance().getTime();
3434
@Builder.Default
@@ -41,7 +41,7 @@ public final class Test implements RunResult, Serializable, BaseEntity {
4141
private boolean isLeaf = true;
4242
private String name;
4343
private String description;
44-
private Test parent;
44+
private transient Test parent;
4545
private Class<? extends IGherkinFormatterModel> bddType;
4646
private final List<Test> children = Collections.synchronizedList(new ArrayList<>());
4747
private final List<Log> logs = Collections.synchronizedList(new ArrayList<>());

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@
2424
import lombok.Getter;
2525

2626
@Getter
27-
@SuppressWarnings("rawtypes")
2827
public class ExtentSparkReporter extends AbstractFileReporter
2928
implements
30-
ReportObserver,
29+
ReportObserver<ReportEntity>,
3130
ReporterConfigurable,
3231
ViewsConfigurable<ExtentSparkReporter>,
33-
ReporterFilterable {
32+
ReporterFilterable<ExtentSparkReporter> {
3433
private static final Logger logger = Logger.getLogger(ExtentSparkReporter.class.getName());
3534
private static final String TEMPLATE_LOCATION = "templates/";
3635
private static final String ENCODING = "UTF-8";
@@ -78,19 +77,22 @@ public ExtentSparkReporter withConfig(ExtentSparkReporterConfig conf) {
7877

7978
@Override
8079
public void loadJSONConfig(File jsonFile) throws IOException {
81-
final JsonConfigLoader loader = new JsonConfigLoader<ExtentSparkReporterConfig>(conf, jsonFile);
80+
final JsonConfigLoader<ExtentSparkReporterConfig> loader = new JsonConfigLoader<ExtentSparkReporterConfig>(conf,
81+
jsonFile);
8282
loader.apply();
8383
}
8484

8585
@Override
8686
public void loadJSONConfig(String jsonString) throws IOException {
87-
final JsonConfigLoader loader = new JsonConfigLoader<ExtentSparkReporterConfig>(conf, jsonString);
87+
final JsonConfigLoader<ExtentSparkReporterConfig> loader = new JsonConfigLoader<ExtentSparkReporterConfig>(conf,
88+
jsonString);
8889
loader.apply();
8990
}
9091

9192
@Override
9293
public void loadXMLConfig(File xmlFile) throws IOException {
93-
final XmlConfigLoader loader = new XmlConfigLoader<ExtentSparkReporterConfig>(conf, xmlFile);
94+
final XmlConfigLoader<ExtentSparkReporterConfig> loader = new XmlConfigLoader<ExtentSparkReporterConfig>(conf,
95+
xmlFile);
9496
loader.apply();
9597
}
9698

0 commit comments

Comments
 (0)