From bf4fdb80939b8204a5bef42a3f5d71d503fd5179 Mon Sep 17 00:00:00 2001
From: Anurag Deshpande
Date: Thu, 9 Sep 2021 13:25:28 -0600
Subject: [PATCH 1/4] 1. Extracting buildLog method to encapsulate most common
implementation in the class. But also to make log() method accept a Log
object as a parameter. This will allow building Log object independent of the
cycle and allows more control for adding additional properties onto the Log
object. Refactoring existing methods accordingly
2. Exposing log(Log log) method to allow adding custom-built log object to the existing flow
BREAKING CHANGE: None
Closes 315
---
.../aventstack/extentreports/ExtentTest.java | 62 ++++++++++++++-----
.../extentreports/ExtentTestLogTest.java | 4 +-
2 files changed, 49 insertions(+), 17 deletions(-)
diff --git a/src/main/java/com/aventstack/extentreports/ExtentTest.java b/src/main/java/com/aventstack/extentreports/ExtentTest.java
index 665d414..9e02ce0 100644
--- a/src/main/java/com/aventstack/extentreports/ExtentTest.java
+++ b/src/main/java/com/aventstack/extentreports/ExtentTest.java
@@ -2,7 +2,9 @@
import java.io.Serializable;
import java.util.Arrays;
+import java.util.Date;
+import com.aventstack.extentreports.gherkin.model.Feature;
import com.aventstack.extentreports.gherkin.model.IGherkinFormatterModel;
import com.aventstack.extentreports.markuputils.Markup;
import com.aventstack.extentreports.markuputils.MarkupHelper;
@@ -53,7 +55,7 @@ public class ExtentTest implements RunResult, Serializable {
* An instance of {@link ExtentReports} to which this {@link ExtentTest}
* belongs
*/
- private transient ExtentReports extent;
+ private final transient ExtentReports extent;
/**
* Internal model
@@ -342,13 +344,9 @@ public ExtentTest generateLog(Status status, Markup markup) {
*
*
*
- * test.log(Status.FAIL, "details", MediaEntityBuilder.createScreenCaptureFromPath("screen.png").build());
+ * test.log(buildLog(Status.FAIL, "details"), MediaEntityBuilder.createScreenCaptureFromPath("screen.png").build());
*
*
- * @param status
- * {@link Status}
- * @param details
- * Details
* @param t
* A {@link Throwable} exception to be logged, enabling the
* Exception view of certain HTML reporters
@@ -357,12 +355,7 @@ public ExtentTest generateLog(Status status, Markup markup) {
*
* @return An {@link ExtentTest} object
*/
- public ExtentTest log(Status status, String details, Throwable t, Media media) {
- Assert.notNull(status, "Status must not be null");
- Log log = Log.builder()
- .status(status)
- .details(details == null ? "" : details)
- .build();
+ public ExtentTest log(Log log, Throwable t, Media media) {
ExceptionInfo exceptionInfo = ExceptionInfoService.createExceptionInfo(t);
log.setException(exceptionInfo);
if (exceptionInfo != null)
@@ -376,6 +369,25 @@ public ExtentTest log(Status status, String details, Throwable t, Media media) {
return this;
}
+ /**
+ * @param status {@link Status} status of the log
+ * @param details {@link String} details to be added to the log
+ * @param overrideLogDate if passed, the log will be overridden to this timestamp. Else it will be defaulted to the current time stamp
+ * @return {@link Log} Log object with the status, details and timestamp set
+ */
+ private Log buildLog(Status status, String details, Date overrideLogDate){
+ Assert.notNull(status, "Status must not be null");
+ Log log = Log.builder()
+ .status(status)
+ .details(details == null ? "" : details)
+ .build();
+ if(overrideLogDate != null){
+ log.setTimestamp(overrideLogDate);
+ }
+
+ return log;
+ }
+
/**
* Logs an event with {@link Status}, details and a media object:
* {@link ScreenCapture}
@@ -398,7 +410,11 @@ public ExtentTest log(Status status, String details, Throwable t, Media media) {
* @return An {@link ExtentTest} object
*/
public ExtentTest log(Status status, String details, Media media) {
- return log(status, details, null, media);
+ return log(buildLog(status, details, null), null, media);
+ }
+
+ public ExtentTest log(Log log){
+ return log(log, null, null);
}
/**
@@ -421,7 +437,7 @@ public ExtentTest log(Status status, String details, Media media) {
* @return An {@link ExtentTest} object
*/
public ExtentTest log(Status status, Media media) {
- return log(status, null, null, media);
+ return log(buildLog(status, null, null), null, media);
}
/**
@@ -438,6 +454,20 @@ public ExtentTest log(Status status, String details) {
return log(status, details, null);
}
+ /**
+ * Logs an event with {@link Status} and details
+ *
+ * @param status
+ * {@link Status}
+ * @param details
+ * Details
+ *
+ * @return An {@link ExtentTest} object
+ */
+// public ExtentTest log(Status status, String details, Date timestamp) {
+// return log(status, details, null);
+// }
+
/**
* Logs an event with {@link Status} and custom {@link Markup} such as:
*
@@ -482,7 +512,7 @@ public ExtentTest log(Status status, Markup markup) {
* @return An {@link ExtentTest} object
*/
public ExtentTest log(Status status, Throwable t, Media media) {
- return log(status, null, t, media);
+ return log(buildLog(status, null, null), t, media);
}
/**
@@ -1095,4 +1125,4 @@ public ExtentTest addScreenCaptureFromBase64String(String base64, String title)
public ExtentTest addScreenCaptureFromBase64String(String base64) {
return addScreenCaptureFromBase64String(base64, null);
}
-}
\ No newline at end of file
+}
diff --git a/src/test/java/com/aventstack/extentreports/ExtentTestLogTest.java b/src/test/java/com/aventstack/extentreports/ExtentTestLogTest.java
index d08215d..e03e7a3 100644
--- a/src/test/java/com/aventstack/extentreports/ExtentTestLogTest.java
+++ b/src/test/java/com/aventstack/extentreports/ExtentTestLogTest.java
@@ -2,6 +2,7 @@
import java.io.IOException;
+import com.aventstack.extentreports.model.Log;
import org.testng.Assert;
import org.testng.annotations.Test;
@@ -22,7 +23,8 @@ private Exception ex() {
@Test(expectedExceptions = IllegalArgumentException.class)
public void logWithStatusNull() {
- test().log(null, null, null, null);
+ Log log = Log.builder().status(null).details("").build();
+ test().log(log, null, null);
}
@Test
From af570907688e701def0e7c4308582e63afc37cd9 Mon Sep 17 00:00:00 2001
From: Anurag Deshpande
Date: Thu, 9 Sep 2021 13:25:57 -0600
Subject: [PATCH 2/4] Refactor to clean up code and optimize imports
---
.../append/RawEntityConverter.java | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/src/main/java/com/aventstack/extentreports/append/RawEntityConverter.java b/src/main/java/com/aventstack/extentreports/append/RawEntityConverter.java
index 7598b90..359c16c 100644
--- a/src/main/java/com/aventstack/extentreports/append/RawEntityConverter.java
+++ b/src/main/java/com/aventstack/extentreports/append/RawEntityConverter.java
@@ -8,10 +8,7 @@
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.GherkinKeyword;
import com.aventstack.extentreports.MediaEntityBuilder;
-import com.aventstack.extentreports.model.Log;
-import com.aventstack.extentreports.model.Media;
-import com.aventstack.extentreports.model.ScreenCapture;
-import com.aventstack.extentreports.model.Test;
+import com.aventstack.extentreports.model.*;
public class RawEntityConverter {
private final ExtentReports extent;
@@ -53,14 +50,16 @@ else if (log.hasException())
extentTest.log(log.getStatus(), log.getException().getException());
else if (log.hasMedia())
addMedia(log, extentTest, null);
- else
- extentTest.log(log.getStatus(), log.getDetails());
+ else {
+ Log logToAdd = Log.builder().status(log.getStatus()).details(log.getDetails()).timestamp(log.getTimestamp()).build();
+ extentTest.log(logToAdd);
+ }
}
// 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()) {
From e876bbee16558b8ce24dcd92d72bd028f83c10d9 Mon Sep 17 00:00:00 2001
From: Anurag Deshpande
Date: Thu, 9 Sep 2021 13:30:25 -0600
Subject: [PATCH 3/4] putting all overloaded methods together
---
.../aventstack/extentreports/ExtentTest.java | 37 ++++++++++---------
1 file changed, 19 insertions(+), 18 deletions(-)
diff --git a/src/main/java/com/aventstack/extentreports/ExtentTest.java b/src/main/java/com/aventstack/extentreports/ExtentTest.java
index 9e02ce0..905f1e8 100644
--- a/src/main/java/com/aventstack/extentreports/ExtentTest.java
+++ b/src/main/java/com/aventstack/extentreports/ExtentTest.java
@@ -369,24 +369,6 @@ public ExtentTest log(Log log, Throwable t, Media media) {
return this;
}
- /**
- * @param status {@link Status} status of the log
- * @param details {@link String} details to be added to the log
- * @param overrideLogDate if passed, the log will be overridden to this timestamp. Else it will be defaulted to the current time stamp
- * @return {@link Log} Log object with the status, details and timestamp set
- */
- private Log buildLog(Status status, String details, Date overrideLogDate){
- Assert.notNull(status, "Status must not be null");
- Log log = Log.builder()
- .status(status)
- .details(details == null ? "" : details)
- .build();
- if(overrideLogDate != null){
- log.setTimestamp(overrideLogDate);
- }
-
- return log;
- }
/**
* Logs an event with {@link Status}, details and a media object:
@@ -529,6 +511,25 @@ public ExtentTest log(Status status, Throwable t) {
return log(status, t, null);
}
+ /**
+ * @param status {@link Status} status of the log
+ * @param details {@link String} details to be added to the log
+ * @param overrideLogDate if passed, the log will be overridden to this timestamp. Else it will be defaulted to the current time stamp
+ * @return {@link Log} Log object with the status, details and timestamp set
+ */
+ private Log buildLog(Status status, String details, Date overrideLogDate){
+ Assert.notNull(status, "Status must not be null");
+ Log log = Log.builder()
+ .status(status)
+ .details(details == null ? "" : details)
+ .build();
+ if(overrideLogDate != null){
+ log.setTimestamp(overrideLogDate);
+ }
+
+ return log;
+ }
+
/**
* Logs an Status.INFO
event with details and a media object:
* {@link ScreenCapture}
From 7943a57a8f44816f3a88d3f70a17d0a2d16152bd Mon Sep 17 00:00:00 2001
From: Anurag Deshpande
Date: Thu, 9 Sep 2021 13:40:34 -0600
Subject: [PATCH 4/4] fixing the test
---
src/main/java/com/aventstack/extentreports/ExtentTest.java | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/main/java/com/aventstack/extentreports/ExtentTest.java b/src/main/java/com/aventstack/extentreports/ExtentTest.java
index 905f1e8..b405896 100644
--- a/src/main/java/com/aventstack/extentreports/ExtentTest.java
+++ b/src/main/java/com/aventstack/extentreports/ExtentTest.java
@@ -356,6 +356,7 @@ public ExtentTest generateLog(Status status, Markup markup) {
* @return An {@link ExtentTest} object
*/
public ExtentTest log(Log log, Throwable t, Media media) {
+ Assert.notNull(log.getStatus(), "Status must not be null");
ExceptionInfo exceptionInfo = ExceptionInfoService.createExceptionInfo(t);
log.setException(exceptionInfo);
if (exceptionInfo != null)