use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class TestValidation method testCoercionValidation.
public void testCoercionValidation(String schemaText, String key, Object[][] inputs, Object[] badObjects, CoercionMode coercionMode) throws IOException {
Assert.assertTrue(coercionMode != CoercionMode.OFF);
ValidationOptions options = normalCoercionValidationOption();
options.setCoercionMode(coercionMode);
RecordDataSchema schema = (RecordDataSchema) dataSchemaFromString(schemaText);
Assert.assertTrue(schema != null);
DataMap map = new DataMap();
for (Object[] row : inputs) {
map.put(key, row[0]);
ValidationResult result = validate(map, schema, options);
Assert.assertTrue(result.isValid(), result.getMessages().toString());
if (result.hasFix()) {
DataMap fixedMap = (DataMap) result.getFixed();
Assert.assertSame(fixedMap.getClass(), DataMap.class);
Object fixed = fixedMap.get(key);
Assert.assertTrue(fixed != null);
Class<?> fixedClass = fixed.getClass();
Class<?> goodClass = row[0].getClass();
switch(schema.getField(key).getType().getDereferencedType()) {
case BYTES:
case FIXED:
// String to ByteString conversion check
Assert.assertNotSame(goodClass, fixedClass);
Assert.assertSame(goodClass, String.class);
Assert.assertSame(fixedClass, ByteString.class);
Assert.assertEquals(((ByteString) fixed).asAvroString(), row[0]);
break;
case INT:
// convert numbers to Integer
Assert.assertNotSame(goodClass, fixedClass);
assertAllowedClass(coercionMode, goodClass);
Assert.assertSame(fixedClass, Integer.class);
break;
case LONG:
// convert numbers to Long
Assert.assertNotSame(goodClass, fixedClass);
assertAllowedClass(coercionMode, goodClass);
Assert.assertSame(fixedClass, Long.class);
break;
case FLOAT:
// convert numbers to Float
Assert.assertNotSame(goodClass, fixedClass);
// Validate the input class, except for non-numeric values like "NaN" where String is allowed
if (!isNonNumericFloatString(row[0])) {
assertAllowedClass(coercionMode, goodClass);
}
Assert.assertSame(fixedClass, Float.class);
break;
case DOUBLE:
// convert numbers to Double
Assert.assertNotSame(goodClass, fixedClass);
// Validate the input class, except for non-numeric values like "NaN" where String is allowed
if (!isNonNumericFloatString(row[0])) {
assertAllowedClass(coercionMode, goodClass);
}
Assert.assertSame(fixedClass, Double.class);
break;
case BOOLEAN:
if (coercionMode == CoercionMode.STRING_TO_PRIMITIVE) {
Assert.assertNotSame(goodClass, fixedClass);
Assert.assertTrue(goodClass == String.class);
Assert.assertSame(fixedClass, Boolean.class);
}
break;
case RECORD:
case ARRAY:
case MAP:
case UNION:
Assert.assertSame(goodClass, fixedClass);
break;
default:
throw new IllegalStateException("unknown conversion");
}
Assert.assertEquals(fixed, row[1]);
} else {
Assert.assertSame(map, result.getFixed());
}
}
for (Object bad : badObjects) {
map.put(key, bad);
ValidationResult result = validate(map, schema, options);
Assert.assertFalse(result.isValid());
Assert.assertSame(map, result.getFixed());
}
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class TestValidation method testCoercionValidation.
public void testCoercionValidation(String schemaText, String key, Object[] goodObjects, Object[] badObjects, ValidationOptions options) throws IOException {
RecordDataSchema schema = (RecordDataSchema) dataSchemaFromString(schemaText);
Assert.assertTrue(schema != null);
DataMap map = new DataMap();
for (Object good : goodObjects) {
map.put(key, good);
ValidationResult result = validate(map, schema, options);
Assert.assertTrue(result.isValid());
Assert.assertFalse(result.hasFix());
Assert.assertSame(map, result.getFixed());
}
for (Object bad : badObjects) {
map.put(key, bad);
ValidationResult result = validate(map, schema, options);
Assert.assertFalse(result.isValid());
Assert.assertSame(map, result.getFixed());
}
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class TestPdlSchemaParser method testComplexTypeWithProperties.
@Test
public void testComplexTypeWithProperties() {
RecordDataSchema mainRecordSchema = (RecordDataSchema) parsePdlSchema("ComplexTypeWithProperties.pdl");
RecordDataSchema.Field arrayField = mainRecordSchema.getField("arrayField");
Assert.assertNotNull(arrayField);
Assert.assertFalse(arrayField.getType().getProperties().isEmpty());
DataMap expectedProperty = new DataMap();
DataMap validate = new DataMap();
validate.put("minSize", 1);
expectedProperty.put("validate", validate);
Assert.assertTrue(arrayField.getProperties().isEmpty());
Assert.assertEquals(arrayField.getType().getProperties(), expectedProperty);
RecordDataSchema.Field mapField = mainRecordSchema.getField("mapField");
Assert.assertNotNull(mapField);
Assert.assertTrue(mapField.getProperties().isEmpty());
Assert.assertFalse(mapField.getType().getProperties().isEmpty());
Assert.assertEquals(mapField.getType().getProperties(), expectedProperty);
RecordDataSchema.Field unionField = mainRecordSchema.getField("unionField");
Assert.assertNotNull(unionField);
Assert.assertTrue(unionField.getProperties().isEmpty());
Assert.assertFalse(unionField.getType().getProperties().isEmpty());
validate.clear();
validate.put("minValue", 0);
Assert.assertEquals(unionField.getType().getProperties(), expectedProperty);
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class TestPdlSchemaParser method testUnionDataSchemaWithAliases.
@Test
public void testUnionDataSchemaWithAliases() {
List<String> membersInDeclaredOrder = new ArrayList<>(Arrays.asList("null", "member", "article", "school", "organization", "company", "jobs", "courses", "fingerprint", "audio", "video"));
Set<String> inlinedMembers = new HashSet<>(Arrays.asList("organization", "jobs", "courses", "fingerprint", "audio"));
RecordDataSchema mainRecordSchema = (RecordDataSchema) parsePdlSchema("unionWithAliases.pdl");
RecordDataSchema.Field resourceField = mainRecordSchema.getField("resource");
UnionDataSchema resourceSchema = (UnionDataSchema) resourceField.getType();
Assert.assertTrue(resourceSchema.areMembersAliased());
Assert.assertEquals(resourceSchema.getMembers().size(), membersInDeclaredOrder.size());
int index = 0;
for (UnionDataSchema.Member member : resourceSchema.getMembers()) {
Assert.assertFalse(member.hasError());
boolean isNonNullMember = (member.getType().getDereferencedType() != DataSchema.Type.NULL);
// Only non-null members should be aliased
Assert.assertEquals(member.hasAlias(), isNonNullMember);
String memberKey = member.getUnionMemberKey();
DataSchema type = member.getType();
// Verify the member's getUnionMemberKey() is same as the member alias (for non null members)
Assert.assertEquals(memberKey, isNonNullMember ? member.getAlias() : type.getUnionMemberKey());
// Verify the order is maintained as declared in the union definition
Assert.assertEquals(memberKey, membersInDeclaredOrder.get(index));
// Verify the inlined member definition is captured correctly
Assert.assertEquals(member.isDeclaredInline(), inlinedMembers.contains(memberKey));
// Verify the type, doc and other properties
Assert.assertEquals(type, resourceSchema.getTypeByMemberKey(memberKey));
Assert.assertEquals(member.getDoc(), isNonNullMember ? memberKey + " doc" : "");
Assert.assertEquals(member.getProperties().containsKey("inlined"), member.isDeclaredInline());
index++;
}
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class TestPdlSchemaParser method testNamedDataSchemaWithAliases.
/**
* Ensures that a {@link NamedDataSchema} can have aliases defined, and that those aliases can be used to reference
* the schema.
*/
@Test
public void testNamedDataSchemaWithAliases() {
RecordDataSchema mainRecordSchema = (RecordDataSchema) parsePdlSchema("namedWithAliases.pdl");
// Test that all the aliases have the correct full name
assertAliasesEqual(mainRecordSchema.getField("recordField"), "com.linkedin.data.schema.grammar.RecordAlias", "com.linkedin.data.schema.grammar.RecordAlias2");
assertAliasesEqual(mainRecordSchema.getField("typerefField"), "com.linkedin.data.schema.grammar.TyperefAlias");
assertAliasesEqual(mainRecordSchema.getField("fixedField"), "com.linkedin.data.schema.grammar.FixedAlias");
assertAliasesEqual(mainRecordSchema.getField("enumField"), "com.linkedin.data.schema.grammar.EnumAlias", "org.example.OverriddenEnumAlias");
// Test that the aliases are bound to the correct schemas
RecordDataSchema.Field refsField = mainRecordSchema.getField("references");
Assert.assertNotNull(refsField);
RecordDataSchema refsRecord = (RecordDataSchema) refsField.getType();
assertFieldTypesEqual(refsRecord, mainRecordSchema, "recordField");
assertFieldTypesEqual(refsRecord, mainRecordSchema, "typerefField");
assertFieldTypesEqual(refsRecord, mainRecordSchema, "fixedField");
assertFieldTypesEqual(refsRecord, mainRecordSchema, "enumField");
}
Aggregations