Skip to content

Commit

Permalink
Merge pull request #56 from delki8/optional-testng-reporter
Browse files Browse the repository at this point in the history
ensuring test ng is optional for schema validation
  • Loading branch information
yarg0007 authored Jul 30, 2024
2 parents 3723516 + 50870ba commit b0081db
Showing 1 changed file with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,21 +20,34 @@

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();

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) {
Expand Down Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit b0081db

Please sign in to comment.