DataHighwaySchemaValidator { public static void validate(Schema schema, UnionRule unionRule) throws SchemaValidationException { validateIsRecord(schema); validateDoesNotContainReservedColumnName(schema); validateObfuscationAnnotations(schema); validateLogicalTypes(schema); SchemaValidator.validate(schema); if (DISALLOW_NON_NULLABLE_UNIONS == unionRule) { validateNonNullableUnions(schema); } } private  DataHighwaySchemaValidator(); static boolean isValid(Schema schema, UnionRule unionRule); static void validate(Schema schema, UnionRule unionRule);  }
@Test(expected = SchemaValidationException.class) public void rootTypeIsNotRecord() throws Exception { Schema schema = SchemaBuilder.builder().bytesType(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test(expected = SchemaValidationException.class) public void schemaContainsFieldWithReservedColumnName() throws Exception { Schema schema = SchemaBuilder .record("r") .fields() .name(PARTITION_COLUMN_NAME) .type() .stringType() .noDefault() .endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test(expected = SchemaValidationException.class) public void schemaContainsAliasWithReservedColumnName() throws Exception { Schema schema = SchemaBuilder .record("r") .fields() .name("f") .aliases(PARTITION_COLUMN_NAME) .type() .stringType() .noDefault() .endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test public void schemaDoesNotContainReservedColumnName() throws Exception { Schema schema = SchemaBuilder.record("r").fields().name("f").type().stringType().noDefault().endRecord(); try { DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); } catch (SchemaValidationException e) { fail(); } }
@Test public void nullableUnion() throws SchemaValidationException { Schema union = SchemaBuilder.unionOf().intType().and().nullType().endUnion(); Schema schema = SchemaBuilder.record("r").fields().name("f").type(union).noDefault().endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test public void optionalUnion() throws SchemaValidationException { Schema union = SchemaBuilder.unionOf().nullType().and().intType().endUnion(); Schema schema = SchemaBuilder.record("r").fields().name("f").type(union).noDefault().endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test(expected = IllegalArgumentException.class) public void singleTypeInUnion() throws SchemaValidationException { Schema union = SchemaBuilder.unionOf().intType().endUnion(); Schema schema = SchemaBuilder.record("r").fields().name("f").type(union).noDefault().endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test public void singleTypeInUnionAllow() throws SchemaValidationException { Schema union = SchemaBuilder.unionOf().intType().endUnion(); Schema schema = SchemaBuilder.record("r").fields().name("f").type(union).noDefault().endRecord(); DataHighwaySchemaValidator.validate(schema, ALLOW_NON_NULLABLE_UNIONS); }
@Test(expected = IllegalArgumentException.class) public void twoTypesInUnion() throws SchemaValidationException { Schema union = SchemaBuilder.unionOf().intType().and().longType().endUnion(); Schema schema = SchemaBuilder.record("r").fields().name("f").type(union).noDefault().endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test(expected = IllegalArgumentException.class) public void threeTypesInUnion() throws SchemaValidationException { Schema union = SchemaBuilder.unionOf().intType().and().longType().and().nullType().endUnion(); Schema schema = SchemaBuilder.record("r").fields().name("f").type(union).noDefault().endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test public void validUnionInMap() throws SchemaValidationException { Schema union = SchemaBuilder.unionOf().intType().and().nullType().endUnion(); Schema map = SchemaBuilder.map().values(union); Schema schema = SchemaBuilder.record("r").fields().name("f").type(map).noDefault().endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test(expected = IllegalArgumentException.class) public void invalidUnionInMap() throws SchemaValidationException { Schema union = SchemaBuilder.unionOf().intType().and().longType().endUnion(); Schema map = SchemaBuilder.map().values(union); Schema schema = SchemaBuilder.record("r").fields().name("f").type(map).noDefault().endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test public void validUnionInArray() throws SchemaValidationException { Schema union = SchemaBuilder.unionOf().intType().and().nullType().endUnion(); Schema array = SchemaBuilder.array().items(union); Schema schema = SchemaBuilder.record("r").fields().name("f").type(array).noDefault().endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test(expected = IllegalArgumentException.class) public void invalidUnionInArray() throws SchemaValidationException { Schema union = SchemaBuilder.unionOf().intType().and().longType().endUnion(); Schema array = SchemaBuilder.array().items(union); Schema schema = SchemaBuilder.record("r").fields().name("f").type(array).noDefault().endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test public void validPiiProperty() throws Exception { Schema schema = SchemaBuilder .record("r") .fields() .name("f") .prop(SENSITIVITY, PII) .type(SchemaBuilder.builder().stringType()) .noDefault() .endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test(expected = InvalidPiiAnnotationException.class) public void invalidPiiProperty() throws Exception { Schema schema = SchemaBuilder .record("r") .fields() .name("f") .prop(SENSITIVITY, PII) .type(SchemaBuilder.builder().map().values().stringType()) .noDefault() .endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }
@Test(expected = IllegalArgumentException.class) public void invalidLogicalType() throws Exception { Schema schema = SchemaBuilder .record("r") .fields() .name("f") .prop(LogicalType.LOGICAL_TYPE_PROP, "date") .type(SchemaBuilder.builder().map().values().stringType()) .noDefault() .endRecord(); DataHighwaySchemaValidator.validate(schema, DISALLOW_NON_NULLABLE_UNIONS); }