Search in sources :

Example 1 with TestUtil.dataMapFromString

use of com.linkedin.data.TestUtil.dataMapFromString 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)

Aggregations

DataList (com.linkedin.data.DataList)1 DataMap (com.linkedin.data.DataMap)1 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)1 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 DataSchema (com.linkedin.data.schema.DataSchema)1 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1