Search in sources :

Example 11 with ProcessingReport

use of com.github.fge.jsonschema.core.report.ProcessingReport in project useful-java-links by Vedenin.

the class JsonSchemaValidatorHelloWorld method main.

public static void main(final String... args) throws IOException, ProcessingException {
    final JsonNode fstabSchema = Utils.loadResource("/fstab.json");
    final JsonNode good = Utils.loadResource("/fstab-good.json");
    final JsonNode bad = Utils.loadResource("/fstab-bad.json");
    final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    final JsonSchema schema = factory.getJsonSchema(fstabSchema);
    ProcessingReport report;
    report = schema.validate(good);
    System.out.println(report);
    report = schema.validate(bad);
    System.out.println(report);
    report = schema.validate(bad2);
    System.out.println(report);
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) JsonSchemaFactory(com.github.fge.jsonschema.main.JsonSchemaFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 12 with ProcessingReport

use of com.github.fge.jsonschema.core.report.ProcessingReport in project metron by apache.

the class AbstractParserConfigTest method validateJsonData.

protected boolean validateJsonData(final String jsonSchema, final String jsonData) throws IOException, ProcessingException {
    final JsonNode d = JsonLoader.fromString(jsonData);
    final JsonNode s = JsonLoader.fromString(jsonSchema);
    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonValidator v = factory.getValidator();
    ProcessingReport report = v.validate(s, d);
    return report.toString().contains("success");
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonSchemaFactory(com.github.fge.jsonschema.main.JsonSchemaFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonValidator(com.github.fge.jsonschema.main.JsonValidator)

Example 13 with ProcessingReport

use of com.github.fge.jsonschema.core.report.ProcessingReport in project metron by apache.

the class CEFParserTest method validateJsonData.

protected boolean validateJsonData(final String jsonSchema, final String jsonData) throws Exception {
    final JsonNode d = JsonLoader.fromString(jsonData);
    final JsonNode s = JsonLoader.fromString(jsonSchema);
    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonValidator v = factory.getValidator();
    ProcessingReport report = v.validate(s, d);
    return report.toString().contains("success");
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonSchemaFactory(com.github.fge.jsonschema.main.JsonSchemaFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonValidator(com.github.fge.jsonschema.main.JsonValidator)

Example 14 with ProcessingReport

use of com.github.fge.jsonschema.core.report.ProcessingReport in project oc-explorer by devgateway.

the class OcdsSchemaValidatorService method validate.

/**
 * Validates the incoming {@link JsonNode} against OCDS schema
 *
 * @param jsonNode
 * @return
 */
public ProcessingReportWithNode validate(final JsonNode jsonNode) {
    try {
        ProcessingReport processingReport = schema.validate(jsonNode);
        ProcessingReportWithNode processingReportWithNode = null;
        try {
            processingReportWithNode = new ProcessingReportWithNode(processingReport, processingReport.isSuccess() ? null : jacksonObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode));
        } catch (JsonProcessingException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return processingReportWithNode;
    } catch (ProcessingException e) {
        logger.error(e.getMessage());
        e.printStackTrace();
    }
    return null;
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 15 with ProcessingReport

use of com.github.fge.jsonschema.core.report.ProcessingReport in project IPK-BrAPI-Validator by plantbreeding.

the class TestItemRunner method schemaMatch.

/**
 * Check if response matches schema
 *
 * @param p Path to the schema to be tested
 * @return TestItemReport
 */
private TestExecReport schemaMatch(String p) {
    LOGGER.info("Testing Schema");
    TestExecReport tr = new TestExecReport("Json matches schema: " + p, false);
    tr.setType("schema mismatch");
    tr.setSchema(p);
    try {
        String jsonString = vr.extract().response().asString();
        SchemaValidator schemaValidator = new SchemaValidator();
        ProcessingReport r = schemaValidator.validate(p, jsonString);
        if (r.isSuccess()) {
            LOGGER.info("Schema Test Passed");
            tr.addMessage("Response structure matches schema.");
            tr.setPassed(true);
        } else {
            LOGGER.info("Schema Test Failed");
            tr.addMessage("Response structure doesn't match schema.");
            r.forEach(message -> tr.addError(message.asJson()));
        }
        return tr;
    } catch (ConnectionClosedException | JsonParseException e1) {
        LOGGER.info("Invalid response");
        LOGGER.info("== cause ==");
        LOGGER.info(e1.getMessage());
        tr.addMessage("Server response is not valid JSON.");
        return tr;
    } catch (AssertionError | IOException | ProcessingException e1) {
        LOGGER.info("Doesn't match schema");
        LOGGER.info("== cause ==");
        LOGGER.info(e1.getMessage());
        tr.addMessage(e1.getMessage());
        return tr;
    }
}
Also used : TestExecReport(de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestExecReport) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) ConnectionClosedException(org.apache.http.ConnectionClosedException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException)

Aggregations

ProcessingReport (com.github.fge.jsonschema.core.report.ProcessingReport)24 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)11 JsonSchema (com.github.fge.jsonschema.main.JsonSchema)8 JsonSchemaFactory (com.github.fge.jsonschema.main.JsonSchemaFactory)7 IOException (java.io.IOException)7 ProcessingMessage (com.github.fge.jsonschema.core.report.ProcessingMessage)5 JsonValidator (com.github.fge.jsonschema.main.JsonValidator)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 Extension (io.syndesis.common.model.extension.Extension)3 Test (org.junit.Test)3 TestExecReport (de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestExecReport)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 ConnectionClosedException (org.apache.http.ConnectionClosedException)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Violation (io.syndesis.common.model.Violation)1