Search in sources :

Example 51 with DataSchema

use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.

the class TestSchemaSampleDataGenerator method testTyperefSchema.

@Test
public void testTyperefSchema() {
    final RecordDataSchema schema = (RecordDataSchema) DataTemplateUtil.getSchema(TyperefTest.class);
    final DataMap value = SchemaSampleDataGenerator.buildRecordData(schema, _spec);
    for (RecordDataSchema.Field field : schema.getFields()) {
        final DataSchema fieldSchema = field.getType();
        if (!(fieldSchema instanceof TyperefDataSchema)) {
            continue;
        }
        final TyperefDataSchema fieldTyperefSchema = (TyperefDataSchema) field.getType();
        final Object fieldValue = value.get(field.getName());
        final Object rebuildValue = SchemaSampleDataGenerator.buildData(fieldTyperefSchema.getDereferencedDataSchema(), _spec);
        Assert.assertSame(fieldValue.getClass(), rebuildValue.getClass());
    }
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Example 52 with DataSchema

use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.

the class TestSchemaSampleDataGenerator method testUnionSchema.

@Test
public void testUnionSchema() {
    final UnionDataSchema schema = (UnionDataSchema) DataTemplateUtil.getSchema(UnionTest.UnionWithNull.class);
    final Set<String> memberKeys = new HashSet<String>();
    for (DataSchema memberSchema : schema.getTypes()) {
        memberKeys.add(memberSchema.getUnionMemberKey());
    }
    final String nullMemberKey = DataSchemaConstants.NULL_DATA_SCHEMA.getUnionMemberKey();
    for (int i = 0; i < memberKeys.size() * 2; ++i) {
        final DataMap value = (DataMap) SchemaSampleDataGenerator.buildData(schema, _spec);
        if (value == null) {
            Assert.assertTrue(memberKeys.contains(nullMemberKey));
            continue;
        }
        final String key = value.keySet().iterator().next();
        Assert.assertTrue(memberKeys.contains(key));
    }
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) ByteString(com.linkedin.data.ByteString) HashSet(java.util.HashSet) DataMap(com.linkedin.data.DataMap) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Example 53 with DataSchema

use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.

the class TestCustomAnyRecord method testCustomAnyRecordSchema.

@Test
public void testCustomAnyRecordSchema() {
    RecordDataSchema schemaFromInstance = (new AnyRecord()).schema();
    DataSchema schemaFromClass = DataTemplateUtil.getSchema(AnyRecord.class);
    assertSame(schemaFromClass, schemaFromInstance);
    CustomAnyRecord custom = new CustomAnyRecord();
    RecordDataSchema customSchemaFromInstance = custom.schema();
    DataSchema customSchemaFromClass = DataTemplateUtil.getSchema(CustomAnyRecord.class);
    assertSame(customSchemaFromClass, customSchemaFromInstance);
    assertEquals(customSchemaFromClass, schemaFromClass);
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Test(org.testng.annotations.Test)

Example 54 with DataSchema

use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.

the class TestEnum method testEnum.

private static <T extends Enum<T>> void testEnum(Class<T> enumClass) {
    try {
        assertTrue(Enum.class.isAssignableFrom(enumClass));
        // has embedded EnumDataSchema
        DataSchema schema = DataTemplateUtil.getSchema(enumClass);
        assertNotNull(schema);
        assertTrue(schema instanceof EnumDataSchema);
        // get symbols
        EnumDataSchema enumSchema = (EnumDataSchema) schema;
        Set<String> schemaSymbols = new HashSet<String>(enumSchema.getSymbols());
        assertNotNull(schemaSymbols);
        for (String symbol : schemaSymbols) {
            // IllegalArgumentException thrown if not valid symbol
            Enum.valueOf(enumClass, symbol);
        }
        // IllegalArgumentException thrown if not valid symbol
        Enum.valueOf(enumClass, "$UNKNOWN");
    } catch (Exception exc) {
        fail("Unexpected exception", exc);
    }
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) HashSet(java.util.HashSet)

Example 55 with DataSchema

use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.

the class TestLongStringLiteral method testSchema.

@Test
public void testSchema() {
    DataSchema schema = DataTemplateUtil.getSchema(LongStringLiteral.class);
    String schemaText = schema.toString();
    assertTrue(schemaText.length() > 65536);
    RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
    RecordDataSchema.Field field = recordDataSchema.getField("text");
    DataList defaultValue = (DataList) field.getDefault();
    assertEquals(defaultValue.size(), 400);
    for (Object s : defaultValue) {
        assertEquals(s, LOREM);
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) DataList(com.linkedin.data.DataList) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Test(org.testng.annotations.Test)

Aggregations

DataSchema (com.linkedin.data.schema.DataSchema)131 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)82 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)53 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)48 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)44 DataMap (com.linkedin.data.DataMap)43 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)41 MapDataSchema (com.linkedin.data.schema.MapDataSchema)40 Test (org.testng.annotations.Test)37 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)36 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)24 ByteString (com.linkedin.data.ByteString)15 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)15 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)14 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)14 ArrayList (java.util.ArrayList)12 DataList (com.linkedin.data.DataList)11 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)9 SchemaParser (com.linkedin.data.schema.SchemaParser)9 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)9