Skip to content

Commit

Permalink
fixes #331 - Gson error when merging reports due to Throwable#detailM…
Browse files Browse the repository at this point in the history
…essage
  • Loading branch information
anshooarora committed Sep 18, 2023
1 parent 36c7418 commit 8c67fcc
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ protected void onDeviceAdded(Device x, Test test) {
@Override
protected void onFlush() {
getReport().refresh();
if (!usingNaturalConf)
if (!usingNaturalConf) {
getReport().applyOverrideConf();
}
super.onFlush();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public JsonDeserializer(final File f) {

public List<Test> deserialize() throws IOException {
Gson gson = GsonExtentTypeAdapterBuilder.builder()
.withBddTypeAdapterFactory()
.withGsonTypeAdapterFactory()
.withScreenCaptureTypeAdapter()
.build();
String json = new String(Files.readAllBytes(f.toPath()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.GherkinKeyword;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.model.ExceptionInfo;
import com.aventstack.extentreports.model.Log;
import com.aventstack.extentreports.model.Media;
import com.aventstack.extentreports.model.NamedAttribute;
import com.aventstack.extentreports.model.ScreenCapture;
import com.aventstack.extentreports.model.Test;

public class RawEntityConverter {

private final ExtentReports extent;

public RawEntityConverter(final ExtentReports extent) {
Expand All @@ -26,43 +29,42 @@ public void convertAndApply(final File jsonFile) throws IOException {
}

extent.setReportUsesManualConfiguration(true);
List<Test> tests = new JsonDeserializer(jsonFile).deserialize();
for (Test test : tests) {
final List<Test> tests = new JsonDeserializer(jsonFile).deserialize();

for (final Test test : tests) {
ExtentTest extentTest;
try {
if (test.getBddType() == null) {
createDomain(test, extent.createTest(test.getName(), test.getDescription()));
if (test.isBDD()) {
final GherkinKeyword gk = new GherkinKeyword(test.getBddType().getSimpleName());
extentTest = extent.createTest(gk, test.getName(), test.getDescription());
} else {
ExtentTest extentTest = extent.createTest(new GherkinKeyword(test.getBddType().getSimpleName()),
test.getName(), test.getDescription());
createDomain(test, extentTest);
extentTest = extent.createTest(test.getName(), test.getDescription());
}
createDomain(test, extentTest);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

public void createDomain(Test test, ExtentTest extentTest) throws ClassNotFoundException {
public void createDomain(final Test test, final ExtentTest extentTest) throws ClassNotFoundException {
extentTest.getModel().setStartTime(test.getStartTime());
extentTest.getModel().setEndTime(test.getEndTime());
addMedia(test, extentTest);
constructTestMedia(test, extentTest);

// create events
for (Log log : test.getLogs()) {
if (log.hasException() && log.hasMedia())
addMedia(log, extentTest, log.getException().getException());
else if (log.hasException())
extentTest.log(log.getStatus(), log.getException().getException());
else if (log.hasMedia())
addMedia(log, extentTest, null);
else
if (log.hasException() || log.hasMedia()) {
constructLog(log, extentTest, log.getException());
} else {
extentTest.log(log.getStatus(), log.getDetails());
}
}

// assign attributes
test.getAuthorSet().stream().map(x -> x.getName()).forEach(extentTest::assignAuthor);
test.getCategorySet().stream().map(x -> x.getName()).forEach(extentTest::assignCategory);
test.getDeviceSet().stream().map(x -> x.getName()).forEach(extentTest::assignDevice);
test.getAuthorSet().stream().map(NamedAttribute::getName).forEach(extentTest::assignAuthor);
test.getCategorySet().stream().map(NamedAttribute::getName).forEach(extentTest::assignCategory);
test.getDeviceSet().stream().map(NamedAttribute::getName).forEach(extentTest::assignDevice);

// handle nodes
for (Test node : test.getChildren()) {
Expand All @@ -73,23 +75,38 @@ else if (log.hasMedia())
GherkinKeyword gk = new GherkinKeyword(node.getBddType().getSimpleName());
extentNode = extentTest.createNode(gk, node.getName(), node.getDescription());
}
addMedia(node, extentNode);
constructTestMedia(node, extentNode);
createDomain(node, extentNode);
}
}

private void addMedia(Log log, ExtentTest extentTest, Throwable ex) {
Media m = log.getMedia();
if (m.getPath() != null) {
extentTest.log(log.getStatus(), ex,
MediaEntityBuilder.createScreenCaptureFromPath(m.getPath()).build());
} else if (((ScreenCapture) m).getBase64() != null) {
extentTest.log(log.getStatus(), ex,
MediaEntityBuilder.createScreenCaptureFromBase64String(((ScreenCapture) m).getBase64()).build());
private void constructLog(final Log log, final ExtentTest extentTest, final ExceptionInfo ex) {
final Media m = log.getMedia();

if (m != null) {
if (m.getPath() != null) {
extentTest.log(log.getStatus(),
MediaEntityBuilder.createScreenCaptureFromPath(m.getPath()).build());
}
if (((ScreenCapture) m).getBase64() != null) {
extentTest.log(log.getStatus(),
MediaEntityBuilder.createScreenCaptureFromBase64String(((ScreenCapture) m).getBase64()).build());
}
}

if (ex != null) {
if (!extentTest.getModel().hasLog()) {
extentTest.log(log.getStatus(), log.getDetails());
}

final List<Log> logs = extentTest.getModel().getLogs();
final Log lastLog = logs.get(logs.size() - 1);
lastLog.setException(ex);
extentTest.getModel().getExceptions().add(ex);
}
}

private void addMedia(Test test, ExtentTest extentTest) {
private void constructTestMedia(final Test test, final ExtentTest extentTest) {
if (test.getMedia() != null) {
for (Media m : test.getMedia()) {
if (m.getPath() != null) {
Expand All @@ -100,4 +117,5 @@ private void addMedia(Test test, ExtentTest extentTest) {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
import com.google.gson.stream.JsonWriter;

public class BddTypeAdapter extends TypeAdapter<Class<? extends IGherkinFormatterModel>> {

private static final Logger LOG = Logger.getLogger(BddTypeAdapter.class.getName());

@SuppressWarnings("unchecked")
@Override
public Class<? extends IGherkinFormatterModel> read(final JsonReader reader) throws IOException {
int cycle = 0;
while (reader.hasNext()) {
JsonToken token = reader.peek();
final JsonToken token = reader.peek();
if ("string".equalsIgnoreCase(token.name())) {
token = reader.peek();
String s = reader.nextString();
final String s = reader.nextString();
if (s != null && !s.isEmpty()) {
try {
return (Class<? extends IGherkinFormatterModel>) Class.forName(s);
Expand All @@ -30,8 +29,6 @@ public Class<? extends IGherkinFormatterModel> read(final JsonReader reader) thr
}
}
}
if (cycle++ > 10)
return null;
}
return null;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import com.google.gson.GsonBuilder;

public class GsonExtentTypeAdapterBuilder {

public static Builder builder() {
return new Builder();
}

public static class Builder {

GsonBuilder builder = new GsonBuilder();

public Builder withBddTypeAdapterFactory() {
builder.registerTypeAdapterFactory(new BddTypeAdapterFactory());
public Builder withGsonTypeAdapterFactory() {
builder.registerTypeAdapterFactory(new GsonTypeAdapterFactory());
return this;
}

Expand All @@ -26,5 +28,7 @@ public Builder withScreenCaptureTypeAdapter() {
public Gson build() {
return builder.create();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.aventstack.extentreports.gson;

import com.aventstack.extentreports.gherkin.model.IGherkinFormatterModel;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;

public class GsonTypeAdapterFactory implements TypeAdapterFactory {

@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (Class.class.isAssignableFrom(type.getRawType()) &&
type.getType().getTypeName().indexOf(IGherkinFormatterModel.class.getTypeName()) >= 0) {
return (TypeAdapter<T>) new BddTypeAdapter();
}

return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.aventstack.extentreports.gson;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

public class ThrowableTypeAdapter extends TypeAdapter<Throwable> {

@Override
public Throwable read(final JsonReader reader) {
return new UnsupportedOperationException();
}

@Override
public void write(final JsonWriter out, final Throwable value) throws IOException {
if (value == null) {
out.nullValue();
return;
}

out.beginObject();
out.name("type");
out.value(value.getClass().getSimpleName());

out.name("message");
out.value(value.getMessage());

final Throwable cause = value.getCause();
if (cause != null) {
out.name("cause");
write(out, cause);
}

out.endObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
@Setter
@ToString
public class ExceptionInfo extends NamedAttribute implements Serializable, BaseEntity {

private static final long serialVersionUID = -8152865623044194249L;
private Throwable exception;

private transient Throwable exception;
private String stackTrace;

@Builder
public ExceptionInfo(Throwable exception, String name, String stackTrace) {
super(name);
this.exception = exception;
this.stackTrace = stackTrace;
}

}
3 changes: 2 additions & 1 deletion src/main/java/com/aventstack/extentreports/model/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public void addChild(Test child) {

private void end(Status evtStatus) {
setStatus(Status.max(status, evtStatus));
if (useNaturalConf)
if (useNaturalConf) {
propagateTime();
}
}

private void propagateTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public void onComplete() {

private void flush(ReportEntity value) {
Gson gson = GsonExtentTypeAdapterBuilder.builder()
.withBddTypeAdapterFactory()
.withGsonTypeAdapterFactory()
.build();
final String filePath = getFileNameAsExt(FILE_NAME, new String[]{".json"});
try (FileWriter writer = new FileWriter(new File(filePath))) {
try (FileWriter writer = new FileWriter(filePath)) {
List<Test> list = value.getReport().getTestList();
gson.toJson(list, writer);
} catch (IOException e) {
Expand All @@ -61,19 +61,19 @@ private void flush(ReportEntity value) {
}

@Override
public void loadJSONConfig(File jsonFile) throws IOException {
public void loadJSONConfig(File jsonFile) {
}

@Override
public void loadJSONConfig(String jsonString) throws IOException {
public void loadJSONConfig(String jsonString) {

}

@Override
public void loadXMLConfig(File xmlFile) throws IOException {
public void loadXMLConfig(File xmlFile) {
}

@Override
public void loadXMLConfig(String xmlFile) throws IOException {
public void loadXMLConfig(String xmlFile) {
}
}
Loading

0 comments on commit 8c67fcc

Please sign in to comment.