Search in sources :

Example 16 with DataSchema

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

the class TestDataSchemaResolver method testClassNameDataSchemaResolver.

@Test
public void testClassNameDataSchemaResolver() {
    @SuppressWarnings("deprecation") final ClassNameDataSchemaResolver resolver = new ClassNameDataSchemaResolver();
    final PegasusSchemaParser parser = new SchemaParser(resolver);
    final Class<? extends RecordTemplate> testClass = ClassNameFooRecord.class;
    final String nonExistSchemaName = "Non-Existing Schema";
    final DataSchema existSchema = parser.lookupName(testClass.getName());
    assertNotNull(existSchema);
    assertTrue(existSchema instanceof RecordDataSchema);
    assertEquals(((RecordDataSchema) existSchema).getFullName(), testClass.getCanonicalName());
    assertFalse(resolver.isBadLocation(new ClassNameDataSchemaLocation(testClass.getName())));
    final DataSchema nonExistSchema = parser.lookupName(nonExistSchemaName);
    assertNull(nonExistSchema);
    assertTrue(parser.errorMessage().contains(nonExistSchemaName));
    assertTrue(resolver.isBadLocation(new ClassNameDataSchemaLocation(nonExistSchemaName)));
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 17 with DataSchema

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

the class TestSchemaFilter method testInputs.

private static void testInputs(Object[][] inputs, boolean isAvroUnionMode) throws IOException {
    for (Object[] row : inputs) {
        String schemaText = (String) row[0];
        Predicate predicate = (Predicate) row[1];
        String expected = (String) row[2];
        NamedDataSchema schema = dataSchemaFromString(schemaText, isAvroUnionMode);
        DataSchema filteredSchema = null;
        SchemaParser parser = new SchemaParser();
        parser.getValidationOptions().setAvroUnionMode(isAvroUnionMode);
        filteredSchema = Filters.removeByPredicate(schema, predicate, parser);
        if (filteredSchema != null) {
            // Schema string match
            String expectedSchemaText = expected;
            DataSchema expectedSchema = dataSchemaFromString(expectedSchemaText, isAvroUnionMode);
            assertEquals(filteredSchema.toString(), expectedSchema.toString());
            assertEquals(filteredSchema, expectedSchema);
        } else {
            String parserMessage = parser.errorMessage();
            assertTrue(parserMessage.contains(expected), "\nContains :" + expected + "\nActual   :" + parserMessage);
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) SchemaParser(com.linkedin.data.schema.SchemaParser) Predicate(com.linkedin.data.it.Predicate)

Example 18 with DataSchema

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

the class TestDataElement method testDataElement.

@Test(dataProvider = "dataElementFactories")
public void testDataElement(DataElementFactory factory) throws IOException {
    RecordDataSchema fooSchema = (RecordDataSchema) TestUtil.dataSchemaFromString(fooSchemaText);
    ArrayDataSchema arraySchema = (ArrayDataSchema) fooSchema.getField("array").getType();
    String fooText = "{\n" + "  \"int\" : 34,\n" + "  \"string\" : \"abc\",\n" + "  \"array\" : [\n" + "    { \"int\" : 56 },\n" + "    { \"string\" : \"xyz\" },\n" + "    { \"array\" : [\n" + "      { \"int\" : 78 }\n" + "    ] }\n" + "  ]\n" + "}\n";
    DataMap foo = TestUtil.dataMapFromString(fooText);
    DataElement root = factory.create(foo, DataElement.ROOT_NAME, fooSchema, null);
    DataElement int1 = factory.create(foo.get("int"), "int", fooSchema.getField("int").getType(), root);
    DataElement string1 = factory.create(foo.get("string"), "string", fooSchema.getField("string").getType(), root);
    DataElement array1 = factory.create(foo.get("array"), "array", fooSchema.getField("array").getType(), root);
    DataElement foo20 = factory.create(array1.getChild(0), 0, arraySchema.getItems(), array1);
    DataElement foo21 = factory.create(array1.getChild(1), 1, arraySchema.getItems(), array1);
    DataElement foo22 = factory.create(array1.getChild(2), 2, arraySchema.getItems(), array1);
    DataElement int20 = factory.create(foo20.getChild("int"), "int", fooSchema.getField("int").getType(), foo20);
    DataElement string21 = factory.create(foo21.getChild("string"), "string", fooSchema.getField("string").getType(), foo21);
    DataElement array22 = factory.create(foo22.getChild("array"), "array", fooSchema.getField("array").getType(), foo22);
    DataElement foo30 = factory.create(array22.getChild(0), 0, arraySchema.getItems(), array22);
    DataElement int30 = factory.create(foo30.getChild("int"), "int", fooSchema.getField("int").getType(), foo30);
    // test path
    Object[][] testPathInput = { { root, foo, fooSchema, new Object[] {} }, { int1, foo.get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "int" } }, { string1, foo.get("string"), DataSchemaConstants.STRING_DATA_SCHEMA, new Object[] { "string" } }, { array1, foo.get("array"), arraySchema, new Object[] { "array" } }, { foo20, ((DataList) foo.get("array")).get(0), fooSchema, new Object[] { "array", 0 } }, { foo21, ((DataList) foo.get("array")).get(1), fooSchema, new Object[] { "array", 1 } }, { foo22, ((DataList) foo.get("array")).get(2), fooSchema, new Object[] { "array", 2 } }, { int20, ((DataMap) ((DataList) foo.get("array")).get(0)).get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "array", 0, "int" } }, { string21, ((DataMap) ((DataList) foo.get("array")).get(1)).get("string"), DataSchemaConstants.STRING_DATA_SCHEMA, new Object[] { "array", 1, "string" } }, { array22, ((DataMap) ((DataList) foo.get("array")).get(2)).get("array"), arraySchema, new Object[] { "array", 2, "array" } }, { foo30, ((DataList) ((DataMap) ((DataList) foo.get("array")).get(2)).get("array")).get(0), fooSchema, new Object[] { "array", 2, "array", 0 } }, { int30, ((DataMap) ((DataList) ((DataMap) ((DataList) foo.get("array")).get(2)).get("array")).get(0)).get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "array", 2, "array", 0, "int" } } };
    ArrayList<Object> pathAsList = new ArrayList<Object>();
    for (Object[] row : testPathInput) {
        DataElement element = (DataElement) row[0];
        // test value
        Object expectedValue = row[1];
        assertSame(expectedValue, element.getValue());
        // test schema
        DataSchema expectedSchema = (DataSchema) row[2];
        assertSame(expectedSchema, element.getSchema());
        // test name
        Object[] expectedPath = (Object[]) row[3];
        Object expectedName = expectedPath.length == 0 ? DataElement.ROOT_NAME : expectedPath[expectedPath.length - 1];
        assertEquals(expectedName, element.getName());
        // test path
        Object[] path = element.path();
        element.pathAsList(pathAsList);
        StringBuilder builder = new StringBuilder();
        StringBuilder builder2 = new StringBuilder();
        assertEquals(expectedPath.length, path.length);
        assertEquals(expectedPath.length, pathAsList.size());
        for (int i = 0; i < expectedPath.length; i++) {
            assertEquals(path[i], expectedPath[i]);
            assertEquals(pathAsList.get(i), expectedPath[i]);
            builder.append('*').append(expectedPath[i]);
            builder2.append(DataElement.SEPARATOR).append(expectedPath[i]);
        }
        assertEquals(builder.toString(), element.pathAsString('*'));
        assertEquals(builder2.toString(), element.pathAsString());
        // test copyChain
        DataElement copy = element.copyChain();
        assertElementChainEquals(copy, element.copyChain(), null);
        // test DataElementUtil.element
        DataElement elementFromUtil = DataElementUtil.element(root, path);
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root, pathAsList);
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root, element.pathAsString());
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root, element.pathAsString('*'), '*');
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), path);
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), pathAsList);
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), element.pathAsString());
        assertElementChainEquals(elementFromUtil, element, root);
        elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), element.pathAsString('*'), '*');
        assertElementChainEquals(elementFromUtil, element, root);
    }
}
Also used : ArrayList(java.util.ArrayList) TestUtil.dataMapFromString(com.linkedin.data.TestUtil.dataMapFromString) DataMap(com.linkedin.data.DataMap) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataList(com.linkedin.data.DataList) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Test(org.testng.annotations.Test)

Example 19 with DataSchema

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

the class TestDataIterator method testDataSchemaTypeEquals.

@Test(dataProvider = "orders")
public void testDataSchemaTypeEquals(IterationOrder order) throws IOException {
    String input = "{\n" + "  \"a\" : 222,\n" + "  \"b\" : \"foo\",\n" + "  \"c\" : {\n" + "    \"a\" : 333,\n" + "    \"e\" : {\n" + "      \"b\" : \"bar\",\n" + "      \"d\" : [ 0, 1, 2 ]\n" + "    }\n" + "  },\n" + "  \"d\" : [ 10, 11, 12],\n" + "  \"f\" : {\n" + "    \"k1\" : \"v1\",\n" + "    \"k2\" : \"v2\"\n" + "  },\n" + "  \"u\" : {\n" + "    \"int\" : 123\n" + "  }\n" + "}";
    String schemaText = "{\n" + "  \"name\" : \"foo\",\n" + "  \"type\" : \"record\",\n" + "  \"fields\" : [\n" + "    { \"name\" : \"a\", \"type\" : \"int\" },\n" + "    { \"name\" : \"b\", \"type\" : \"string\" },\n" + "    { \"name\" : \"c\", \"type\" : \"foo\" },\n" + "    { \"name\" : \"d\", \"type\" : { \"type\" : \"array\", \"items\" : \"int\" } },\n" + "    { \"name\" : \"e\", \"type\" : \"foo\" },\n" + "    { \"name\" : \"f\", \"type\" : { \"type\" : \"map\", \"values\" : \"string\" } },\n" + "    { \"name\" : \"u\", \"type\" : [ \"int\", \"double\", \"string\" ] }\n" + "  ]\n" + "}\n";
    DataSchema schema = DataTemplateUtil.parseSchema(schemaText);
    Object[][] tests = { { DataSchema.Type.INT, Arrays.asList(222, 333, 0, 1, 2, 10, 11, 12, 123) }, { DataSchema.Type.STRING, Arrays.asList("foo", "bar", "v1", "v2") } };
    Object o = jsonToObject(input);
    for (Object[] row : tests) {
        Builder builder = Builder.create(o, schema, order).filterBy(dataSchemaTypeEquals((DataSchema.Type) row[0]));
        @SuppressWarnings("unchecked") List<Object> expected = (List<Object>) row[1];
        assertEqualsByValue(builder, expected);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) DataList(com.linkedin.data.DataList) ArrayList(java.util.ArrayList) List(java.util.List) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Example 20 with DataSchema

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

the class UnionTemplate method selectCustomType.

/**
   * Set a new value into the union whose type needs to be coerced by {@link DirectCoercer}.
   *
   * @param memberSchema provides the {@link DataSchema} of the new value.
   * @param memberClass provides the expected class of the value.
   * @param dataClass provides the class stored in the underlying {@link DataMap}.
   * @param key provides the key that identifies the type of the value.
   * @param value provides the value to set.
   * @param <T> type of the value.
   * @throws ClassCastException if the input value does not match the
   *                            expected class and the value cannot be coerced to the
   *                            expected class.
   * @throws NullUnionUnsupportedOperationException if the union is a null union.
   */
protected <T> void selectCustomType(DataSchema memberSchema, Class<T> memberClass, Class<?> dataClass, String key, T value) throws ClassCastException, NullUnionUnsupportedOperationException {
    checkNotNull();
    DataSchema memberType = _schema.getType(key);
    // something is wrong with the generated code if this occurs.
    assert (memberType != null);
    Object object = DataTemplateUtil.coerceInput(value, memberClass, dataClass);
    _map.clear();
    _map.put(key, object);
    _customTypeCache = value;
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema)

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