Skip to content

Commit

Permalink
updated logger and handled blank and empty responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mfaisalkhatri committed Dec 26, 2024
1 parent 47218ad commit 9f26b9e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 62 deletions.
43 changes: 43 additions & 0 deletions src/test/java/io/github/mfaisalkhatri/api/logger/Logger.java
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!";

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import com.microsoft.playwright.APIResponse;
import io.github.mfaisalkhatri.api.manager.RequestManager;
import io.github.mfaisalkhatri.api.restfulecommerce.Logger;
import io.github.mfaisalkhatri.api.logger.Logger;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.microsoft.playwright.APIRequestContext;
import com.microsoft.playwright.APIResponse;
import com.microsoft.playwright.Playwright;
import io.github.mfaisalkhatri.api.logger.Logger;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

Expand Down

This file was deleted.

0 comments on commit 9f26b9e

Please sign in to comment.