Search in sources :

Example 1 with SchemaEvolutionIncompatibility

use of org.zalando.nakadi.validation.schema.SchemaEvolutionIncompatibility in project nakadi by zalando.

the class SchemaEvolutionService method checkEvolutionIncompatibilities.

private void checkEvolutionIncompatibilities(final EventType from, final EventTypeBase to) throws InvalidEventTypeException {
    final List<SchemaEvolutionIncompatibility> incompatibilities = schemaEvolutionConstraints.stream().map(c -> c.validate(from, to)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    if (incompatibilities.isEmpty()) {
        return;
    }
    // There is a special case of incompatibility - change of category from NONE to BUSINESS, with adding metadata.
    // Let's check that it is the case.
    // This solution is not scalable, but without huge refactoring it is the simplest one.
    final boolean categoryChanged = incompatibilities.stream().anyMatch(v -> v instanceof SchemaEvolutionIncompatibility.CategoryIncompatibility);
    if (categoryChanged) {
        final boolean metadataChanged = incompatibilities.stream().anyMatch(v -> v instanceof SchemaEvolutionIncompatibility.MetadataIncompatibility);
        final int expectedIncompatibilities = 1 + (metadataChanged ? 1 : 0);
        if (incompatibilities.size() == expectedIncompatibilities) {
            // Now let's check, that it is really change from NONE to BUSINESS and some other conditions.
            if (from.getCategory() == EventCategory.UNDEFINED && to.getCategory() == EventCategory.BUSINESS) {
                if (to.getEnrichmentStrategies().contains(EnrichmentStrategyDescriptor.METADATA_ENRICHMENT) && to.getCompatibilityMode() != CompatibilityMode.COMPATIBLE) {
                    // Finally, we are allowing this step.
                    return;
                }
            }
        }
        throw new InvalidEventTypeException("Category change is allowed only from 'undefined' to 'business'. " + "'enrichment_strategies' should be properly set as well");
    }
    throw new InvalidEventTypeException(incompatibilities.stream().map(SchemaEvolutionIncompatibility::getReason).collect(Collectors.joining("; ")));
}
Also used : EventCategory(org.zalando.nakadi.domain.EventCategory) DateTimeZone(org.joda.time.DateTimeZone) PATCH(org.zalando.nakadi.domain.Version.Level.PATCH) SchemaEvolutionIncompatibility(org.zalando.nakadi.validation.schema.SchemaEvolutionIncompatibility) EnrichmentStrategyDescriptor(org.zalando.nakadi.domain.EnrichmentStrategyDescriptor) ForbiddenAttributeIncompatibility(org.zalando.nakadi.validation.schema.ForbiddenAttributeIncompatibility) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) JSONObject(org.json.JSONObject) SchemaDiff(org.zalando.nakadi.validation.schema.diff.SchemaDiff) Map(java.util.Map) SchemaLoader(org.everit.json.schema.loader.SchemaLoader) ADDITIONAL_ITEMS_CHANGED(org.zalando.nakadi.domain.SchemaChange.Type.ADDITIONAL_ITEMS_CHANGED) ADDITIONAL_PROPERTIES_CHANGED(org.zalando.nakadi.domain.SchemaChange.Type.ADDITIONAL_PROPERTIES_CHANGED) EventType(org.zalando.nakadi.domain.EventType) ValidationException(org.everit.json.schema.ValidationException) REQUIRED_ARRAY_EXTENDED(org.zalando.nakadi.domain.SchemaChange.Type.REQUIRED_ARRAY_EXTENDED) DateTime(org.joda.time.DateTime) MAJOR(org.zalando.nakadi.domain.Version.Level.MAJOR) NO_CHANGES(org.zalando.nakadi.domain.Version.Level.NO_CHANGES) SchemaEvolutionConstraint(org.zalando.nakadi.validation.schema.SchemaEvolutionConstraint) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) Version(org.zalando.nakadi.domain.Version) List(java.util.List) TITLE_CHANGED(org.zalando.nakadi.domain.SchemaChange.Type.TITLE_CHANGED) SchemaChange(org.zalando.nakadi.domain.SchemaChange) EventTypeBase(org.zalando.nakadi.domain.EventTypeBase) CompatibilityMode(org.zalando.nakadi.domain.CompatibilityMode) Schema(org.everit.json.schema.Schema) Optional(java.util.Optional) DESCRIPTION_CHANGED(org.zalando.nakadi.domain.SchemaChange.Type.DESCRIPTION_CHANGED) PROPERTIES_ADDED(org.zalando.nakadi.domain.SchemaChange.Type.PROPERTIES_ADDED) InvalidEventTypeException(org.zalando.nakadi.exceptions.InvalidEventTypeException) InvalidEventTypeException(org.zalando.nakadi.exceptions.InvalidEventTypeException) Optional(java.util.Optional) SchemaEvolutionIncompatibility(org.zalando.nakadi.validation.schema.SchemaEvolutionIncompatibility) SchemaEvolutionConstraint(org.zalando.nakadi.validation.schema.SchemaEvolutionConstraint)

Example 2 with SchemaEvolutionIncompatibility

use of org.zalando.nakadi.validation.schema.SchemaEvolutionIncompatibility in project nakadi by zalando.

the class SchemaEvolutionServiceTest method checkEvolutionConstraints.

@Test(expected = InvalidEventTypeException.class)
public void checkEvolutionConstraints() throws Exception {
    final EventTypeTestBuilder builder = EventTypeTestBuilder.builder();
    final EventType oldEventType = builder.build();
    final EventType newEventType = builder.build();
    final Optional<SchemaEvolutionIncompatibility> incompatibility = Optional.of(new SchemaEvolutionIncompatibility("incompatible"));
    Mockito.doReturn(incompatibility).when(evolutionConstraint).validate(oldEventType, newEventType);
    service.evolve(oldEventType, newEventType);
}
Also used : EventType(org.zalando.nakadi.domain.EventType) EventTypeTestBuilder(org.zalando.nakadi.utils.EventTypeTestBuilder) SchemaEvolutionIncompatibility(org.zalando.nakadi.validation.schema.SchemaEvolutionIncompatibility) Test(org.junit.Test)

Aggregations

EventType (org.zalando.nakadi.domain.EventType)2 SchemaEvolutionIncompatibility (org.zalando.nakadi.validation.schema.SchemaEvolutionIncompatibility)2 Lists (com.google.common.collect.Lists)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 BinaryOperator (java.util.function.BinaryOperator)1 Collectors (java.util.stream.Collectors)1 Schema (org.everit.json.schema.Schema)1 ValidationException (org.everit.json.schema.ValidationException)1 SchemaLoader (org.everit.json.schema.loader.SchemaLoader)1 DateTime (org.joda.time.DateTime)1 DateTimeZone (org.joda.time.DateTimeZone)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1 CompatibilityMode (org.zalando.nakadi.domain.CompatibilityMode)1 EnrichmentStrategyDescriptor (org.zalando.nakadi.domain.EnrichmentStrategyDescriptor)1 EventCategory (org.zalando.nakadi.domain.EventCategory)1 EventTypeBase (org.zalando.nakadi.domain.EventTypeBase)1