Skip to content

Commit 9f26b9e

Browse files
committed
updated logger and handled blank and empty responses
1 parent 47218ad commit 9f26b9e

File tree

4 files changed

+45
-62
lines changed

4 files changed

+45
-62
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.mfaisalkhatri.api.logger;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.microsoft.playwright.APIResponse;
6+
import org.apache.commons.lang3.StringUtils;
7+
import org.apache.logging.log4j.LogManager;
8+
9+
10+
public class Logger {
11+
12+
private final APIResponse response;
13+
private final org.apache.logging.log4j.Logger log;
14+
15+
public Logger (final APIResponse response) {
16+
this.response = response;
17+
this.log = LogManager.getLogger (getClass ());
18+
}
19+
20+
public void logResponseDetails () {
21+
String responseBody = this.response.text ();
22+
this.log.info ("Logging Response Details....\n responseHeaders: {}, \nstatusCode: {},",
23+
this.response.headers (), this.response.status ());
24+
this.log.info ("\n Response body: {}", prettyPrintJson (responseBody));
25+
this.log.info ("End of Logs!");
26+
}
27+
28+
private String prettyPrintJson (final String text) {
29+
if (StringUtils.isNotBlank (text) && StringUtils.isNotEmpty (text)) {
30+
try {
31+
final ObjectMapper objectMapper = new ObjectMapper ();
32+
final Object jsonObject = objectMapper.readValue (text, Object.class);
33+
return objectMapper.writerWithDefaultPrettyPrinter ()
34+
.writeValueAsString (jsonObject);
35+
} catch (final JsonProcessingException e) {
36+
this.log.error ("Failed to pretty print JSON: {}", e.getMessage (), e);
37+
}
38+
}
39+
return "No response body found!";
40+
41+
}
42+
}
43+

src/test/java/io/github/mfaisalkhatri/api/restfulbooker/tests/BaseTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import com.microsoft.playwright.APIResponse;
77
import io.github.mfaisalkhatri.api.manager.RequestManager;
8-
import io.github.mfaisalkhatri.api.restfulecommerce.Logger;
8+
import io.github.mfaisalkhatri.api.logger.Logger;
99
import org.testng.annotations.AfterTest;
1010
import org.testng.annotations.BeforeTest;
1111

src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/BaseTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.microsoft.playwright.APIRequestContext;
88
import com.microsoft.playwright.APIResponse;
99
import com.microsoft.playwright.Playwright;
10+
import io.github.mfaisalkhatri.api.logger.Logger;
1011
import org.testng.annotations.AfterClass;
1112
import org.testng.annotations.BeforeClass;
1213

src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/Logger.java

-61
This file was deleted.

0 commit comments

Comments
 (0)