diff --git a/src/test/java/io/github/mfaisalkhatri/api/logger/Logger.java b/src/test/java/io/github/mfaisalkhatri/api/logger/Logger.java new file mode 100644 index 0000000..6b35053 --- /dev/null +++ b/src/test/java/io/github/mfaisalkhatri/api/logger/Logger.java @@ -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!"; + + } +} + diff --git a/src/test/java/io/github/mfaisalkhatri/api/restfulbooker/tests/BaseTest.java b/src/test/java/io/github/mfaisalkhatri/api/restfulbooker/tests/BaseTest.java index 3378810..eb3fb01 100644 --- a/src/test/java/io/github/mfaisalkhatri/api/restfulbooker/tests/BaseTest.java +++ b/src/test/java/io/github/mfaisalkhatri/api/restfulbooker/tests/BaseTest.java @@ -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; diff --git a/src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/BaseTest.java b/src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/BaseTest.java index 8cdb887..94a8dca 100644 --- a/src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/BaseTest.java +++ b/src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/BaseTest.java @@ -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; diff --git a/src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/Logger.java b/src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/Logger.java deleted file mode 100644 index 3ab5681..0000000 --- a/src/test/java/io/github/mfaisalkhatri/api/restfulecommerce/Logger.java +++ /dev/null @@ -1,61 +0,0 @@ -package io.github.mfaisalkhatri.api.restfulecommerce; - -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 (); - if (StringUtils.isNotBlank (responseBody)) { - this.log.info ("Logging Response Details....\n responseHeaders: {}, \nstatusCode: {}, \nresponseBody: {}", - this.response.headers (), this.response.status (), prettyPrintJson (this.response.text ())); - } - this.log.info ("End of Logs!"); - } - - private String prettyPrintJson (final String text) { - if (StringUtils.isNotBlank (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 text; - } - } - return "No response body generated!"; - } - - // private String prettyPrintJson (final String text) { - // String prettyPrintJson = ""; - // if (text != null && !text.isBlank() && !text.isEmpty()) { - // try { - // - // final ObjectMapper objectMapper = new ObjectMapper (); - // final Object jsonObject = objectMapper.readValue (text, Object.class); - // prettyPrintJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject); - // return prettyPrintJson; - // } catch (final JsonProcessingException e) { - // this.log.error ("Error Printing Pretty Json : {}", e.getMessage ()); - // } - // } - // this.log.info ("No response body generated!"); - // return null; - // } -} -