Skip to content

Commit

Permalink
logging and reporting issue fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MohabMohie committed Aug 19, 2019
1 parent 3e7aa1a commit 6578c50
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 52 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.mohies</groupId>
<artifactId>SHAFT_ENGINE</artifactId>
<version>2.6.20190818</version>
<version>2.6.20190819</version>
<name>SHAFT_ENGINE</name>
<description>Selenium Hybrid Automation Framework for Testing [SHAFT]</description>

Expand Down
110 changes: 59 additions & 51 deletions src/main/java/com/shaft/api/RestActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.StringReader;
import java.io.StringWriter;
Expand Down Expand Up @@ -99,34 +100,22 @@ private static void passAction(String actionName, String testData, Response resp
if (testData != null) {
message = message + " With the following test data [" + testData + "].";
}

Boolean discreetLogging;
Boolean initialLoggingState = ReportManager.isDiscreteLogging();
if (isDiscrete) {
discreetLogging = isDiscrete;
} else {
discreetLogging = ReportManager.isDiscreteLogging();
}

if (discreetLogging) {
reportResponseBody(response, isDiscrete);
ReportManager.logDiscrete(message);
} else {
ReportManager.log(message);
}

if (response != null) {
reportResponseBody(response, discreetLogging);
reportResponseBody(response, initialLoggingState);
if (!initialLoggingState) {
ReportManager.log(message);
} else {
ReportManager.logDiscrete(message);
}
}

}

private static void passAction(String actionName, String testData, Boolean isDiscrete) {
if (isDiscrete) {
ReportManager.setDiscreteLogging(true);
}
passAction(actionName, testData, null, isDiscrete);
if (isDiscrete) {
ReportManager.setDiscreteLogging(false);
}
}

private static void failAction(String actionName, String testData, Response response) {
Expand Down Expand Up @@ -247,42 +236,61 @@ private RequestSpecification prepareRequestSpecs(List<List<Object>> formParamete
}

private void reportRequestBody(Object body) {
if (ReportManager.isDiscreteLogging()) {
ReportManager.logDiscrete("API Request - REST Body:\n" + body.toString());
} else {
if (body.toString() != null && !body.toString().equals("")) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(baos);

oos.writeObject(body);
oos.flush();
oos.close();

ReportManager.attachAsStep("API Request", "REST Body",
new ByteArrayInputStream(baos.toByteArray()));
} catch (IOException e) {
ReportManager.attachAsStep("API Request", "REST Body", body.toString());
}
if (body.toString() != null && !body.toString().equals("")) {
if (ReportManager.isDiscreteLogging()) {
ReportManager.logDiscrete("API Request - REST Body:\n" + parseBodyToJson(body));
} else {
ReportManager.attachAsStep("API Request", "REST Body", parseBodyToJson(body));
}
}
}

private static void reportResponseBody(Response response, Boolean isDiscrete) {
if (isDiscrete) {
ReportManager.logDiscrete("API Response - REST Body:\n" + response.getBody().asString());
} else {
if (response.getBody().asString() != null && !response.getBody().asString().equals("")) {
JSONParser parser = new JSONParser();
try {
org.json.simple.JSONObject actualJsonObject = (org.json.simple.JSONObject) parser
.parse(response.asString());
ReportManager.attachAsStep("API Response", "REST Body", new GsonBuilder().setPrettyPrinting()
.create().toJson(new JsonParser().parse(actualJsonObject.toJSONString())));
} catch (Exception e) {
// response is not parsable to JSON
ReportManager.attachAsStep("API Response", "REST Body", response.getBody().asInputStream());
if (response != null) {
if (isDiscrete) {
ReportManager.logDiscrete("API Response - REST Body:\n" + parseBodyToJson(response));
} else {
ReportManager.attachAsStep("API Response", "REST Body", parseBodyToJson(response));
}
}
}

private static InputStream parseBodyToJson(Response response) {
return parseBodyToJson(response.getBody());
}

private static InputStream parseBodyToJson(Object body) {
JSONParser parser = new JSONParser();
try {
org.json.simple.JSONObject actualJsonObject;
if (body.getClass().getName().toLowerCase().contains("restassured")) {
// if it's a string response body
actualJsonObject = (org.json.simple.JSONObject) parser
.parse(((io.restassured.response.ResponseBody<?>) body).asString());
} else if (body.getClass().getName().toLowerCase().contains("jsonobject")) {
actualJsonObject = (org.json.simple.JSONObject) parser
.parse(((JsonObject) body).toString().replace("\\n", "").replace("\\t", "").replace(" ", ""));
} else {
actualJsonObject = (org.json.simple.JSONObject) parser.parse(body.toString());
}
return new ByteArrayInputStream((new GsonBuilder().setPrettyPrinting().create()
.toJson(new JsonParser().parse(actualJsonObject.toJSONString()))).getBytes());
} catch (Exception e) {
// response is not parsable to JSON
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(body);
oos.flush();
oos.close();
return new ByteArrayInputStream(baos.toByteArray());
} catch (IOException ioe) {
if (body.getClass().getName().toLowerCase().contains("restassured")) {
// if it's a string response body
return ((io.restassured.response.ResponseBody<?>) body).asInputStream();
} else {
return new ByteArrayInputStream((body.toString()).getBytes());
}
}
}
Expand Down

0 comments on commit 6578c50

Please sign in to comment.