Search in sources :

Example 6 with JsonSchema

use of org.leadpony.justify.api.JsonSchema in project tcases by Cornutum.

the class MocoTestConfigReader method getMocoTestConfig.

/**
 * Returns a {@link MocoTestConfig} instance.
 */
public MocoTestConfig getMocoTestConfig() {
    JsonValidationService service = JsonValidationService.newInstance();
    JsonSchema schema = service.readSchema(getClass().getResourceAsStream("/schema/moco-test-schema.json"));
    ProblemHandler handler = ProblemHandler.throwing();
    try (JsonReader reader = service.createReader(stream_, schema, handler)) {
        JsonObject json;
        try {
            json = reader.readObject();
        } catch (Exception e) {
            throw new MocoTestConfigException("Invalid Moco test configuration", e);
        }
        return MocoTestConfigJson.asMocoTestConfig(json);
    }
}
Also used : ProblemHandler(org.leadpony.justify.api.ProblemHandler) JsonValidationService(org.leadpony.justify.api.JsonValidationService) JsonSchema(org.leadpony.justify.api.JsonSchema) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject)

Example 7 with JsonSchema

use of org.leadpony.justify.api.JsonSchema in project selenium_java by sergueik.

the class SchemaCatalog method findRootSchema.

private JsonSchema findRootSchema(URI id) {
    JsonSchema schema = cache.get(id);
    if (schema != null) {
        return schema;
    }
    Location resource = get(id);
    if (resource == null) {
        return null;
    }
    schema = readReferencedSchema(resource);
    if (schema != null) {
        cache.put(id, schema);
        return schema;
    } else {
        return null;
    }
}
Also used : JsonSchema(org.leadpony.justify.api.JsonSchema)

Example 8 with JsonSchema

use of org.leadpony.justify.api.JsonSchema in project selenium_java by sergueik.

the class Validate method readSchemaCatalogAt.

private void readSchemaCatalogAt(Location location) {
    console.print(READ_CATALOG, location);
    JsonSchema schema = readSchemaFromResource("catalog.schema.json");
    List<Problem> problems = new ArrayList<>();
    ProblemHandler handler = createProblemHandler(problems);
    try (JsonParser parser = service.createParser(openCatalog(location), schema, handler)) {
        parseCatalog(parser, location);
        if (!problems.isEmpty()) {
            console.withColor(Color.DANGER).print(CATALOG_INVALID, location, Problems.countLeast(problems));
            throw new CommandException(CATALOG_FAILED);
        }
    } catch (JsonParsingException e) {
        console.withColor(Color.DANGER).print(CATALOG_MALFORMED, e);
        throw new CommandException(CATALOG_FAILED);
    } catch (JsonException e) {
        throw new CommandException(e);
    }
}
Also used : JsonException(javax.json.JsonException) ProblemHandler(org.leadpony.justify.api.ProblemHandler) JsonSchema(org.leadpony.justify.api.JsonSchema) ArrayList(java.util.ArrayList) Problem(org.leadpony.justify.api.Problem) JsonParser(javax.json.stream.JsonParser) JsonParsingException(javax.json.stream.JsonParsingException)

Example 9 with JsonSchema

use of org.leadpony.justify.api.JsonSchema in project selenium_java by sergueik.

the class SchemaCatalog method resolveSchema.

@Override
public JsonSchema resolveSchema(URI id) {
    String fragment = id.getFragment();
    URI baseId = withoutFragment(id);
    JsonSchema schema = findRootSchema(baseId);
    if (schema == null) {
        return null;
    }
    if (fragment == null) {
        return schema;
    } else {
        return schema.getSubschemaAt(fragment);
    }
}
Also used : JsonSchema(org.leadpony.justify.api.JsonSchema) URI(java.net.URI)

Example 10 with JsonSchema

use of org.leadpony.justify.api.JsonSchema in project selenium_java by sergueik.

the class Validate method validateSchemaAt.

/**
 * Reads and validates a JSON schema.
 *
 * @param location the location of the JSON schema to be read and validated.
 * @return the schema read if successful, or {@code null} if errors occcured.
 */
private JsonSchema validateSchemaAt(Location location) {
    JsonSchemaReaderFactory factory = createSchemaReaderFactory();
    try (JsonSchemaReader reader = factory.createSchemaReader(openSchema(location))) {
        JsonSchema schema = reader.read();
        console.withColor(Color.SUCCESS).print(SCHEMA_VALID, location);
        return schema;
    } catch (JsonValidatingException e) {
        List<Problem> problems = e.getProblems();
        problemPrinter.handleProblems(problems);
        console.withColor(Color.DANGER).print(SCHEMA_INVALID, location, Problems.countLeast(problems));
        setStatus(Status.INVALID);
        return null;
    } catch (JsonParsingException e) {
        console.withColor(Color.DANGER).print(SCHEMA_MALFORMED, e);
        setStatus(Status.INVALID);
        return null;
    } catch (JsonException e) {
        throw new CommandException(e);
    }
}
Also used : JsonValidatingException(org.leadpony.justify.api.JsonValidatingException) JsonException(javax.json.JsonException) JsonSchemaReader(org.leadpony.justify.api.JsonSchemaReader) JsonSchema(org.leadpony.justify.api.JsonSchema) ArrayList(java.util.ArrayList) List(java.util.List) JsonSchemaReaderFactory(org.leadpony.justify.api.JsonSchemaReaderFactory) JsonParsingException(javax.json.stream.JsonParsingException)

Aggregations

JsonSchema (org.leadpony.justify.api.JsonSchema)17 ProblemHandler (org.leadpony.justify.api.ProblemHandler)13 JsonValidationService (org.leadpony.justify.api.JsonValidationService)10 ArrayList (java.util.ArrayList)7 JsonReader (javax.json.JsonReader)7 StringReader (java.io.StringReader)5 JsonObject (javax.json.JsonObject)5 JsonReader (jakarta.json.JsonReader)4 List (java.util.List)4 InputStream (java.io.InputStream)3 JsonSchemaReader (org.leadpony.justify.api.JsonSchemaReader)3 Problem (org.leadpony.justify.api.Problem)3 JsonArray (jakarta.json.JsonArray)2 JsonException (jakarta.json.JsonException)2 JsonObject (jakarta.json.JsonObject)2 JsonPatch (jakarta.json.JsonPatch)2 JsonProvider (jakarta.json.spi.JsonProvider)2 JsonParser (jakarta.json.stream.JsonParser)2 IOException (java.io.IOException)2 JsonParser (javax.json.stream.JsonParser)2