-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated logger and handled blank and empty responses
- Loading branch information
1 parent
47218ad
commit 9f26b9e
Showing
4 changed files
with
45 additions
and
62 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/test/java/io/github/mfaisalkhatri/api/logger/Logger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.github.mfaisalkhatri.api.logger; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.microsoft.playwright.APIResponse; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
|
||
|
||
public class Logger { | ||
|
||
private final APIResponse response; | ||
private final org.apache.logging.log4j.Logger log; | ||
|
||
public Logger (final APIResponse response) { | ||
this.response = response; | ||
this.log = LogManager.getLogger (getClass ()); | ||
} | ||
|
||
public void logResponseDetails () { | ||
String responseBody = this.response.text (); | ||
this.log.info ("Logging Response Details....\n responseHeaders: {}, \nstatusCode: {},", | ||
this.response.headers (), this.response.status ()); | ||
this.log.info ("\n Response body: {}", prettyPrintJson (responseBody)); | ||
this.log.info ("End of Logs!"); | ||
} | ||
|
||
private String prettyPrintJson (final String text) { | ||
if (StringUtils.isNotBlank (text) && StringUtils.isNotEmpty (text)) { | ||
try { | ||
final ObjectMapper objectMapper = new ObjectMapper (); | ||
final Object jsonObject = objectMapper.readValue (text, Object.class); | ||
return objectMapper.writerWithDefaultPrettyPrinter () | ||
.writeValueAsString (jsonObject); | ||
} catch (final JsonProcessingException e) { | ||
this.log.error ("Failed to pretty print JSON: {}", e.getMessage (), e); | ||
} | ||
} | ||
return "No response body found!"; | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 0 additions & 61 deletions
61
src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/Logger.java
This file was deleted.
Oops, something went wrong.