Search in sources :

Example 1 with UnprocessableSchemaException

use of io.apicurio.registry.rules.UnprocessableSchemaException in project apicurio-registry by Apicurio.

the class AvroCompatibilityChecker method testCompatibility.

/**
 * @see CompatibilityChecker#testCompatibility(io.apicurio.registry.rules.compatibility.CompatibilityLevel, java.util.List, java.lang.String)
 */
@Override
public CompatibilityExecutionResult testCompatibility(CompatibilityLevel compatibilityLevel, List<String> existingSchemaStrings, String proposedSchemaString) {
    requireNonNull(compatibilityLevel, "compatibilityLevel MUST NOT be null");
    requireNonNull(existingSchemaStrings, "existingSchemaStrings MUST NOT be null");
    requireNonNull(proposedSchemaString, "proposedSchemaString MUST NOT be null");
    SchemaValidator schemaValidator = validatorFor(compatibilityLevel);
    if (schemaValidator == null) {
        return CompatibilityExecutionResult.compatible();
    }
    try {
        List<Schema> existingSchemas = existingSchemaStrings.stream().map(s -> new Schema.Parser().parse(s)).collect(Collectors.toList());
        // the most recent must come first, i.e. reverse-chronological.
        Collections.reverse(existingSchemas);
        Schema toValidate = new Schema.Parser().parse(proposedSchemaString);
        schemaValidator.validate(toValidate, existingSchemas);
        return CompatibilityExecutionResult.compatible();
    } catch (SchemaValidationException e) {
        return CompatibilityExecutionResult.incompatible(e);
    } catch (SchemaParseException | AvroTypeException e) {
        throw new UnprocessableSchemaException(e.getMessage());
    }
}
Also used : List(java.util.List) Schema(org.apache.avro.Schema) SchemaValidator(org.apache.avro.SchemaValidator) AvroTypeException(org.apache.avro.AvroTypeException) SchemaValidationException(org.apache.avro.SchemaValidationException) Objects.requireNonNull(java.util.Objects.requireNonNull) SchemaValidatorBuilder(org.apache.avro.SchemaValidatorBuilder) UnprocessableSchemaException(io.apicurio.registry.rules.UnprocessableSchemaException) SchemaParseException(org.apache.avro.SchemaParseException) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) SchemaValidationException(org.apache.avro.SchemaValidationException) SchemaParseException(org.apache.avro.SchemaParseException) Schema(org.apache.avro.Schema) SchemaValidator(org.apache.avro.SchemaValidator) UnprocessableSchemaException(io.apicurio.registry.rules.UnprocessableSchemaException) AvroTypeException(org.apache.avro.AvroTypeException)

Aggregations

UnprocessableSchemaException (io.apicurio.registry.rules.UnprocessableSchemaException)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Collectors (java.util.stream.Collectors)1 AvroTypeException (org.apache.avro.AvroTypeException)1 Schema (org.apache.avro.Schema)1 SchemaParseException (org.apache.avro.SchemaParseException)1 SchemaValidationException (org.apache.avro.SchemaValidationException)1 SchemaValidator (org.apache.avro.SchemaValidator)1 SchemaValidatorBuilder (org.apache.avro.SchemaValidatorBuilder)1