@Test public void checkCompatibilityUsingProtoFiles() throws Exception { ResourceLoader resourceLoader = new ResourceLoader( "/io/confluent/kafka/schemaregistry/protobuf/diff/"); ProtoFileElement original = resourceLoader.readObj("TestProto.proto"); ProtoFileElement update = resourceLoader.readObj("TestProto2.proto"); List<Difference> changes = SchemaDiff.compare( new ProtobufSchema(original, Collections.emptyList(), Collections.emptyMap()), new ProtobufSchema(update, Collections.emptyList(), Collections.emptyMap()) ); assertTrue(changes.contains(new Difference( Difference.Type.FIELD_NAME_CHANGED, "#/TestMessage/2" ))); assertTrue(changes.contains(new Difference( Difference.Type.FIELD_SCALAR_KIND_CHANGED, "#/TestMessage/2" ))); }
public static List<Difference> compare( final ProtobufSchema original, final ProtobufSchema update ) { Map<String, SchemaReference> originalReferences = original.references().stream() .collect(Collectors.toMap(SchemaReference::getName, r -> r)); Map<String, SchemaReference> updateReferences = update.references().stream() .collect(Collectors.toMap(SchemaReference::getName, r -> r)); Map<String, ProtoFileElement> originalDependencies = original.dependencies(); Map<String, ProtoFileElement> updateDependencies = update.dependencies(); final Context ctx = new Context(COMPATIBLE_CHANGES); collectContextInfoForRefs(ctx, originalReferences, originalDependencies, true); collectContextInfoForRefs(ctx, updateReferences, updateDependencies, false); compare(ctx, original.rawSchema(), update.rawSchema()); return ctx.getDifferences(); }
SchemaDiff { public static List<Difference> compare( final ProtobufSchema original, final ProtobufSchema update ) { Map<String, SchemaReference> originalReferences = original.references().stream() .collect(Collectors.toMap(SchemaReference::getName, r -> r)); Map<String, SchemaReference> updateReferences = update.references().stream() .collect(Collectors.toMap(SchemaReference::getName, r -> r)); Map<String, ProtoFileElement> originalDependencies = original.dependencies(); Map<String, ProtoFileElement> updateDependencies = update.dependencies(); final Context ctx = new Context(COMPATIBLE_CHANGES); collectContextInfoForRefs(ctx, originalReferences, originalDependencies, true); collectContextInfoForRefs(ctx, updateReferences, updateDependencies, false); compare(ctx, original.rawSchema(), update.rawSchema()); return ctx.getDifferences(); } }
SchemaDiff { public static List<Difference> compare( final ProtobufSchema original, final ProtobufSchema update ) { Map<String, SchemaReference> originalReferences = original.references().stream() .collect(Collectors.toMap(SchemaReference::getName, r -> r)); Map<String, SchemaReference> updateReferences = update.references().stream() .collect(Collectors.toMap(SchemaReference::getName, r -> r)); Map<String, ProtoFileElement> originalDependencies = original.dependencies(); Map<String, ProtoFileElement> updateDependencies = update.dependencies(); final Context ctx = new Context(COMPATIBLE_CHANGES); collectContextInfoForRefs(ctx, originalReferences, originalDependencies, true); collectContextInfoForRefs(ctx, updateReferences, updateDependencies, false); compare(ctx, original.rawSchema(), update.rawSchema()); return ctx.getDifferences(); }  }
SchemaDiff { public static List<Difference> compare( final ProtobufSchema original, final ProtobufSchema update ) { Map<String, SchemaReference> originalReferences = original.references().stream() .collect(Collectors.toMap(SchemaReference::getName, r -> r)); Map<String, SchemaReference> updateReferences = update.references().stream() .collect(Collectors.toMap(SchemaReference::getName, r -> r)); Map<String, ProtoFileElement> originalDependencies = original.dependencies(); Map<String, ProtoFileElement> updateDependencies = update.dependencies(); final Context ctx = new Context(COMPATIBLE_CHANGES); collectContextInfoForRefs(ctx, originalReferences, originalDependencies, true); collectContextInfoForRefs(ctx, updateReferences, updateDependencies, false); compare(ctx, original.rawSchema(), update.rawSchema()); return ctx.getDifferences(); }  static List<Difference> compare(
      final ProtobufSchema original,
      final ProtobufSchema update
  ); static Optional<FieldElement> findField(String name, List<FieldElement> options); static void compareTypeElements(
      final Context ctx, final List<TypeElement> original, final List<TypeElement> update
  ); }
SchemaDiff { public static List<Difference> compare( final ProtobufSchema original, final ProtobufSchema update ) { Map<String, SchemaReference> originalReferences = original.references().stream() .collect(Collectors.toMap(SchemaReference::getName, r -> r)); Map<String, SchemaReference> updateReferences = update.references().stream() .collect(Collectors.toMap(SchemaReference::getName, r -> r)); Map<String, ProtoFileElement> originalDependencies = original.dependencies(); Map<String, ProtoFileElement> updateDependencies = update.dependencies(); final Context ctx = new Context(COMPATIBLE_CHANGES); collectContextInfoForRefs(ctx, originalReferences, originalDependencies, true); collectContextInfoForRefs(ctx, updateReferences, updateDependencies, false); compare(ctx, original.rawSchema(), update.rawSchema()); return ctx.getDifferences(); }  static List<Difference> compare(
      final ProtobufSchema original,
      final ProtobufSchema update
  ); static Optional<FieldElement> findField(String name, List<FieldElement> options); static void compareTypeElements(
      final Context ctx, final List<TypeElement> original, final List<TypeElement> update
  ); static final Set<Difference.Type> COMPATIBLE_CHANGES; }