PiiSchemaEvolutionValidator { public static void validate(Schema newSchema, Optional<Schema> currentSchema) { currentSchema.ifPresent(current -> { Fields fields = SchemaTraverser.traverse(current, new FieldsVisitor()); SchemaTraverser.traverse(newSchema, new ValidatingVisitor(fields)); }); }  static void validate(Schema newSchema, Optional<Schema> currentSchema);  }
@Test(expected = InvalidPiiAnnotationException.class) public void addPiiToExistingFieldFails() throws Exception { Schema newSchema = SchemaBuilder .record("r") .fields() .name("f") .prop("sensitivity", "PII") .type(SchemaBuilder.builder().stringType()) .noDefault() .endRecord(); Schema currentSchema = SchemaBuilder .record("r") .fields() .name("f") .type(SchemaBuilder.builder().stringType()) .noDefault() .endRecord(); PiiSchemaEvolutionValidator.validate(newSchema, Optional.of(currentSchema)); }
@Test public void existingPiiNoChangeIsOk() throws Exception { Schema newSchema = SchemaBuilder .record("r") .fields() .name("f") .prop("sensitivity", "PII") .type(SchemaBuilder.builder().stringType()) .noDefault() .endRecord(); Schema currentSchema = SchemaBuilder .record("r") .fields() .name("f") .prop("sensitivity", "PII") .type(SchemaBuilder.builder().stringType()) .noDefault() .endRecord(); PiiSchemaEvolutionValidator.validate(newSchema, Optional.of(currentSchema)); }
@Test public void newSchemaIsOk() throws Exception { Schema newSchema = SchemaBuilder .record("r") .fields() .name("f") .prop("sensitivity", "PII") .type(SchemaBuilder.builder().stringType()) .noDefault() .endRecord(); PiiSchemaEvolutionValidator.validate(newSchema, Optional.empty()); }
@Test public void newPiiFieldIsOk() throws Exception { Schema newSchema = SchemaBuilder .record("r") .fields() .name("f") .prop("sensitivity", "PII") .type(SchemaBuilder.builder().stringType()) .noDefault() .endRecord(); Schema currentSchema = SchemaBuilder.record("r").fields().endRecord(); PiiSchemaEvolutionValidator.validate(newSchema, Optional.of(currentSchema)); }