Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensuring test ng is optional for schema validation #56

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
Loading