Search in sources :

Example 76 with RecordDataSchema

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());
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) TestUtil.dataMapFromString(com.linkedin.data.TestUtil.dataMapFromString) ByteString(com.linkedin.data.ByteString) TestUtil.dataSchemaFromString(com.linkedin.data.TestUtil.dataSchemaFromString) DataMap(com.linkedin.data.DataMap)

Example 77 with RecordDataSchema

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());
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap)

Example 78 with RecordDataSchema

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);
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 79 with RecordDataSchema

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++;
    }
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 80 with RecordDataSchema

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");
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Test(org.testng.annotations.Test)

Aggregations

RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)106 DataMap (com.linkedin.data.DataMap)43 Test (org.testng.annotations.Test)43 DataSchema (com.linkedin.data.schema.DataSchema)40 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)32 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)28 MapDataSchema (com.linkedin.data.schema.MapDataSchema)26 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)25 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)20 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)19 ArrayList (java.util.ArrayList)18 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)14 Name (com.linkedin.data.schema.Name)14 Map (java.util.Map)10 Schema (org.apache.avro.Schema)10 DataList (com.linkedin.data.DataList)9 GenericRecord (org.apache.avro.generic.GenericRecord)9 ValidateDataAgainstSchema (com.linkedin.data.schema.validation.ValidateDataAgainstSchema)8 HashMap (java.util.HashMap)8 FieldDef (com.linkedin.data.template.FieldDef)7