From 50870bac15683a0ece4b3ca5eb180b835e0a72fb Mon Sep 17 00:00:00 2001 From: "ruither.borba" Date: Fri, 26 Jul 2024 16:49:46 -0300 Subject: [PATCH] ensuring test ng is optional for schema validation --- .../support/SchemaValidatorUtil.java | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/NST/src/main/java/com/ebay/nst/schema/validation/support/SchemaValidatorUtil.java b/NST/src/main/java/com/ebay/nst/schema/validation/support/SchemaValidatorUtil.java index 6e4e0cb..f95d327 100644 --- a/NST/src/main/java/com/ebay/nst/schema/validation/support/SchemaValidatorUtil.java +++ b/NST/src/main/java/com/ebay/nst/schema/validation/support/SchemaValidatorUtil.java @@ -3,8 +3,6 @@ import java.io.IOException; import java.util.Objects; -import org.testng.Reporter; - import com.ebay.utility.timestamp.TimeStamp; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; @@ -22,6 +20,19 @@ public class SchemaValidatorUtil { + private static final boolean isTestNGAvailable; + + static { + boolean testNGAvailable; + try { + Class.forName("org.testng.Reporter"); + testNGAvailable = true; + } catch (ClassNotFoundException e) { + testNGAvailable = false; + } + isTestNGAvailable = testNGAvailable; + } + public void validate(JsonNode schemaNode, String responseBody) { final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); @@ -29,14 +40,14 @@ public void validate(JsonNode schemaNode, String responseBody) { try { final JsonSchema jsonSchema = factory.getJsonSchema(schemaNode); final JsonNode responseNode = JsonLoader.fromString(responseBody); - Reporter.log(String.format("Start json validation at %s", TimeStamp.getCurrentTime()), true); - ProcessingReport processingReport = jsonSchema.validateUnchecked(responseNode, true); - Reporter.log(String.format("Finish json validation at %s", TimeStamp.getCurrentTime()), true); + log(String.format("Start json validation at %s", TimeStamp.getCurrentTime())); + ProcessingReport processingReport = jsonSchema.validateUnchecked(responseNode); + log(String.format("Finish json validation at %s", TimeStamp.getCurrentTime())); if (processingReport.isSuccess()) { - Reporter.log("Validate response against schema: Success", true); + log("Validate response against schema: Success"); } else { - Reporter.log("Validate response against schema: Failed", true); + log("Validate response against schema: Failed"); StringBuilder sb = new StringBuilder(); for (ProcessingMessage processingMessage : processingReport) { @@ -82,11 +93,11 @@ public void validate(JsonNode schemaNode, String responseBody) { } } catch (IOException e) { e.printStackTrace(); - Reporter.log(String.format("IOException from schemaResourcePath. %s", e.getMessage()), true); + log(String.format("IOException from schemaResourcePath. %s", e.getMessage())); throw new SchemaValidationException(e.getMessage()); } catch (ProcessingException e) { e.printStackTrace(); - Reporter.log(String.format("ProcessingException from getJsonSchema. %s", e.getMessage()), true); + log(String.format("ProcessingException from getJsonSchema. %s", e.getMessage())); throw new SchemaValidationException(e.getMessage()); } } @@ -274,4 +285,12 @@ protected String processPolymorphicObjectErrors(ObjectNode processingMessageJson return errorMessage.toString(); } + + private void log(String message) { + if (isTestNGAvailable) { + org.testng.Reporter.log(message, true); + } else { + System.out.println(message); + } + } }