Search in sources :

Example 86 with DataList

use of com.linkedin.data.DataList in project rest.li by linkedin.

the class TestDataSchema method testFieldDefaultsAndUnionMemberKeys.

@Test
public void testFieldDefaultsAndUnionMemberKeys() throws IOException {
    String schemaText = "{ \"type\" : \"record\", \"name\" : \"foo\", \"fields\" : \n" + "[ { \"name\" : \"bar\", \"type\" : { \"name\" : \"barType\", \"type\" : \"record\", \"fields\" : [ \n" + "{ \"name\" : \"boolean\", \"type\" : \"boolean\", \"default\" : true }, \n" + "{ \"name\" : \"int\", \"type\" : \"int\", \"default\" : -1 }, \n" + "{ \"name\" : \"long\", \"type\" : \"long\", \"default\" : -2 }, \n" + "{ \"name\" : \"float\", \"type\" : \"float\", \"default\" : -3.0 }, \n" + "{ \"name\" : \"double\", \"type\" : \"double\", \"default\" : -4.0 }, \n" + "{ \"name\" : \"string\", \"type\" : \"string\", \"default\" : \"default_string\" }, \n" + "{ \"name\" : \"bytes\", \"type\" : \"bytes\", \"default\" : \"default_bytes\" }, \n" + "{ \"name\" : \"array\", \"type\" : { \"type\" : \"array\", \"items\" : \"int\" }, \"default\" : [ -1, -2, -3, -4 ] }, \n" + "{ \"name\" : \"enum\", \"type\" : { \"type\" : \"enum\", \"name\" : \"enumType\", \"symbols\" : [ \"apple\", \"orange\", \"banana\" ] }, \"default\" : \"apple\" }, \n" + "{ \"name\" : \"fixed\", \"type\" : { \"type\" : \"fixed\", \"name\" : \"fixedType\", \"size\" : 4 }, \"default\" : \"1234\" }, \n" + "{ \"name\" : \"map\", \"type\" : { \"type\" : \"map\", \"values\" : \"int\" }, \"default\" : { \"key1\" : -5 } }, \n" + "{ \"name\" : \"record\", \"type\" : { \"type\" : \"record\", \"name\" : \"recordType\", \"fields\" : [ { \"name\" : \"int\", \"type\" : \"int\" } ] }, \"default\" : { \"int\" : -6 } }, \n" + "{ \"name\" : \"union\", \"type\" : [ \"int\", \"recordType\", \"enumType\", \"fixedType\" ], \"default\" : { \"enumType\" : \"orange\"} }, \n" + "{ \"name\" : \"unionWithNull\", \"type\" : [ \"null\", \"enumType\", \"fixedType\" ], \"default\" : null } \n" + "] } } ] }";
    String key = "bar";
    // Test default values.
    Object[][] defaultInput = { { "boolean", true }, { "int", -1 }, { "long", -2L }, { "float", -3.0f }, { "double", -4.0 }, { "string", "default_string" }, { "bytes", ByteString.copyAvroString("default_bytes", false) }, { "array", new DataList(asList(-1, -2, -3, -4)) }, { "enum", "apple" }, { "fixed", ByteString.copyAvroString("1234", false) }, { "map", new DataMap(asMap("key1", -5)) }, { "record", new DataMap(asMap("int", -6)) }, { "union", new DataMap(asMap("enumType", "orange")) }, { "unionWithNull", Data.NULL } };
    DataSchema schema = dataSchemaFromString(schemaText);
    RecordDataSchema fooSchema = (RecordDataSchema) schema;
    RecordDataSchema.Field barField = fooSchema.getField(key);
    RecordDataSchema barSchema = (RecordDataSchema) barField.getType();
    for (Object[] pair : defaultInput) {
        String targetKey = (String) pair[0];
        RecordDataSchema.Field targetField = barSchema.getField(targetKey);
        assertEquals(pair[1], targetField.getDefault());
    }
    // Test default values.
    Object[][] unionMemberKeyInput = { { "boolean", "boolean" }, { "int", "int" }, { "long", "long" }, { "float", "float" }, { "double", "double" }, { "string", "string" }, { "bytes", "bytes" }, { "array", "array" }, { "enum", "enumType" }, { "fixed", "fixedType" }, { "map", "map" }, { "record", "recordType" }, { "union", "union" }, { "unionWithNull", "union" } };
    for (Object[] pair : unionMemberKeyInput) {
        String targetKey = (String) pair[0];
        RecordDataSchema.Field targetField = barSchema.getField(targetKey);
        assertEquals(pair[1], targetField.getType().getUnionMemberKey());
    }
}
Also used : DataList(com.linkedin.data.DataList) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 87 with DataList

use of com.linkedin.data.DataList in project rest.li by linkedin.

the class TestValidation method testValidationWithFixupAbsentWithDefault.

@Test
public void testValidationWithFixupAbsentWithDefault() throws IOException, CloneNotSupportedException {
    String schemaText = "{ \"type\" : \"record\", \"name\" : \"foo\", \"fields\" : \n" + "[ { \"name\" : \"bar\", \"type\" : { \"name\" : \"barType\", \"type\" : \"record\", \"fields\" : [ \n" + "{ \"name\" : \"boolean\", \"type\" : \"boolean\", \"default\" : true }, \n" + "{ \"name\" : \"int\", \"type\" : \"int\", \"default\" : 1 }, \n" + "{ \"name\" : \"long\", \"type\" : \"long\", \"default\" : 2 }, \n" + "{ \"name\" : \"float\", \"type\" : \"float\", \"default\" : 3.0 }, \n" + "{ \"name\" : \"double\", \"type\" : \"double\", \"default\" : 4.0 }, \n" + "{ \"name\" : \"string\", \"type\" : \"string\", \"default\" : \"cow\" }, \n" + "{ \"name\" : \"bytes\", \"type\" : \"bytes\", \"default\" : \"dog\" }, \n" + "{ \"name\" : \"array\", \"type\" : { \"type\" : \"array\", \"items\" : \"int\" }, \"default\" : [ -1, -2, -3 ] }, \n" + "{ \"name\" : \"enum\", \"type\" : { \"type\" : \"enum\", \"name\" : \"enumType\", \"symbols\" : [ \"apple\", \"orange\", \"banana\" ] }, \"default\" : \"apple\" }, \n" + "{ \"name\" : \"fixed\", \"type\" : { \"type\" : \"fixed\", \"name\" : \"fixedType\", \"size\" : 4 }, \"default\" : \"1234\" }, \n" + "{ \"name\" : \"map\", \"type\" : { \"type\" : \"map\", \"values\" : \"int\" }, \"default\" : { \"1\" : 1, \"2\" : 2 } }, \n" + "{ \"name\" : \"record\", \"type\" : { \"type\" : \"record\", \"name\" : \"recordType\", \"fields\" : [ { \"name\" : \"int\", \"type\" : \"int\" } ] }, \"default\" : { \"int\" : 1 } }, \n" + "{ \"name\" : \"union\", \"type\" : [ \"int\", \"recordType\", \"enumType\", \"fixedType\" ], \"default\" : { \"enumType\" : \"orange\" } }, \n" + "{ \"name\" : \"unionWithNull\", \"type\" : [ \"null\", \"enumType\", \"fixedType\" ], \"default\" : null }, \n" + "{ \"name\" : \"optionalInt\", \"type\" : \"int\", \"optional\" : true }, \n" + "{ \"name\" : \"optionalDefaultInt\", \"type\" : \"int\", \"optional\" : true, \"default\" : 42 } \n" + "] } } ] }";
    String key = "bar";
    DataSchema schema = dataSchemaFromString(schemaText);
    Assert.assertTrue(schema != null);
    Object[][][] input = { { { new ValidationOptions(RequiredMode.FIXUP_ABSENT_WITH_DEFAULT) }, { new DataMap(), new DataMap(asMap("boolean", true, "int", 1, "long", 2L, "float", 3.0f, "double", 4.0, "string", "cow", "bytes", ByteString.copyAvroString("dog", false), "array", new DataList(asList(-1, -2, -3)), "enum", "apple", "fixed", ByteString.copyAvroString("1234", false), "map", new DataMap(asMap("1", 1, "2", 2)), "record", new DataMap(asMap("int", 1)), "union", new DataMap(asMap("enumType", "orange")), "unionWithNull", Data.NULL)) } } };
    testValidationWithNormalCoercion(schema, key, input);
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataList(com.linkedin.data.DataList) TestUtil.dataMapFromString(com.linkedin.data.TestUtil.dataMapFromString) ByteString(com.linkedin.data.ByteString) TestUtil.dataSchemaFromString(com.linkedin.data.TestUtil.dataSchemaFromString) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 88 with DataList

use of com.linkedin.data.DataList in project rest.li by linkedin.

the class TestArrayTemplate method testArrayBadInput.

@SuppressWarnings("unchecked")
public <ArrayTemplate extends AbstractArrayTemplate<E>, E> void testArrayBadInput(Class<ArrayTemplate> templateClass, ArrayDataSchema schema, List<E> good, List<Object> badInput, List<Object> badOutput) {
    try {
        Exception exc = null;
        ArrayTemplate arrayTemplateBad = templateClass.newInstance();
        DataList badDataList = new DataList();
        ArrayTemplate badWrappedArrayTemplate = DataTemplateUtil.wrap(badDataList, schema, templateClass);
        List<E> badIn = (List<E>) badInput;
        // add(E element)
        for (E o : badIn) {
            try {
                exc = null;
                arrayTemplateBad.add(o);
            } catch (Exception e) {
                exc = e;
            }
            assertTrue(exc != null);
            assertTrue(o == null || exc instanceof ClassCastException);
            assertTrue(o != null || exc instanceof NullPointerException);
        }
        // add(int index, E element)
        for (Object o : badIn) {
            try {
                exc = null;
                arrayTemplateBad.add(0, (E) o);
            } catch (Exception e) {
                exc = e;
            }
            assertTrue(exc != null);
            assertTrue(o == null || exc instanceof ClassCastException);
            assertTrue(o != null || exc instanceof NullPointerException);
        }
        // addAll(Collection<E> c)
        try {
            exc = null;
            arrayTemplateBad.addAll(badIn);
        } catch (Exception e) {
            exc = e;
        }
        assertTrue(exc != null);
        assertTrue(exc instanceof ClassCastException);
        // set(int index, E element)
        arrayTemplateBad.addAll(good);
        assertTrue(arrayTemplateBad.size() > 1);
        for (Object o : badIn) {
            try {
                exc = null;
                arrayTemplateBad.set(0, (E) o);
            } catch (Exception e) {
                exc = e;
            }
            assertTrue(exc != null);
            assertTrue(o == null || exc instanceof ClassCastException);
            assertTrue(o != null || exc instanceof NullPointerException);
        }
        // listIterator add
        for (Object o : badIn) {
            try {
                exc = null;
                ListIterator<E> it = arrayTemplateBad.listIterator(0);
                it.add((E) o);
            } catch (Exception e) {
                exc = e;
            }
            assertTrue(exc != null);
            assertTrue(o == null || exc instanceof ClassCastException);
            assertTrue(o != null || exc instanceof NullPointerException);
        }
        // listIterator set
        for (Object o : badIn) {
            try {
                exc = null;
                ListIterator<E> it = arrayTemplateBad.listIterator(0);
                it.next();
                it.set((E) o);
            } catch (Exception e) {
                exc = e;
            }
            assertTrue(exc != null);
            assertTrue(o == null || exc instanceof ClassCastException);
            assertTrue(o != null || exc instanceof NullPointerException);
        }
        badDataList.clear();
        badDataList.addAll(badOutput);
        badWrappedArrayTemplate = DataTemplateUtil.wrap(badDataList, schema, templateClass);
        // Get returns bad
        for (int i = 0; i < badWrappedArrayTemplate.size(); ++i) {
            try {
                exc = null;
                badWrappedArrayTemplate.get(i);
            } catch (Exception e) {
                exc = e;
            }
            assertTrue(exc != null);
            assertTrue(exc instanceof TemplateOutputCastException);
        }
        // Set returns bad
        badDataList.clear();
        badDataList.addAll(badOutput);
        assertEquals(badWrappedArrayTemplate.size(), badOutput.size());
        for (int i = 0; i < badWrappedArrayTemplate.size(); ++i) {
            try {
                exc = null;
                badWrappedArrayTemplate.set(i, good.get(0));
            } catch (Exception e) {
                exc = e;
            }
            assertTrue(exc != null);
            assertTrue(exc instanceof TemplateOutputCastException);
        }
        // Remove returns bad
        badDataList.clear();
        badDataList.addAll(badOutput);
        assertEquals(badWrappedArrayTemplate.size(), badOutput.size());
        for (int i = 0; i < badWrappedArrayTemplate.size(); ++i) {
            try {
                exc = null;
                badWrappedArrayTemplate.remove(0);
            } catch (Exception e) {
                exc = e;
            }
            assertTrue(exc != null);
            assertTrue(exc instanceof TemplateOutputCastException);
        }
        // Iterator returns bad
        for (Object o : badOutput) {
            badDataList.clear();
            badDataList.add(o);
            try {
                exc = null;
                badWrappedArrayTemplate.iterator().next();
            } catch (Exception e) {
                exc = e;
            }
            assertTrue(exc != null);
            assertTrue(exc instanceof TemplateOutputCastException);
        }
        // ListIterator returns bad
        for (Object o : badOutput) {
            badDataList.clear();
            badDataList.add(o);
            try {
                exc = null;
                badWrappedArrayTemplate.listIterator().next();
            } catch (Exception e) {
                exc = e;
            }
            assertTrue(exc != null);
            assertTrue(exc instanceof TemplateOutputCastException);
        }
        for (Object o : badOutput) {
            badDataList.clear();
            badDataList.add(o);
            try {
                exc = null;
                badWrappedArrayTemplate.listIterator(badWrappedArrayTemplate.size()).previous();
            } catch (Exception e) {
                exc = e;
            }
            assertTrue(exc != null);
            assertTrue(exc instanceof TemplateOutputCastException);
        }
    } catch (IllegalAccessException exc) {
        fail("Unexpected exception", exc);
    } catch (InstantiationException exc) {
        fail("Unexpected exception", exc);
    } catch (TemplateOutputCastException exc) {
        fail("Unexpected exception", exc);
    }
}
Also used : DataList(com.linkedin.data.DataList) DataList(com.linkedin.data.DataList) TestUtil.asList(com.linkedin.data.TestUtil.asList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 89 with DataList

use of com.linkedin.data.DataList in project rest.li by linkedin.

the class TestMapTemplate method testBooleanMap.

@Test
public void testBooleanMap() {
    MapDataSchema schema = (MapDataSchema) DataTemplateUtil.parseSchema("{ \"type\" : \"map\", \"values\" : \"boolean\" }");
    Map<String, Boolean> input = asMap("true", true, "false", false);
    Map<String, Boolean> adds = asMap("thirteen", true, "seventeen", false, "nineteen", true);
    Map<String, Object> badInput = asMap("integer", 99, "long", 999L, "float", 88.0f, "double", 888.0, "string", "hello", "bytes", ByteString.empty(), "object", new Object(), "null", null, "array", new StringArray(), "record", new FooRecord());
    Map<String, Object> badOutput = asMap("integer", 99, "long", 999L, "float", 88.0f, "double", 888.0, "string", "hello", "bytes", ByteString.empty(), "map", new DataMap(), "list", new DataList());
    testMap(BooleanMap.class, schema, input, adds);
    testMapBadInput(BooleanMap.class, schema, input, badInput, badOutput);
}
Also used : DataList(com.linkedin.data.DataList) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 90 with DataList

use of com.linkedin.data.DataList in project rest.li by linkedin.

the class TestMapTemplate method testLongMap.

@Test
public void testLongMap() {
    MapDataSchema schema = (MapDataSchema) DataTemplateUtil.parseSchema("{ \"type\" : \"map\", \"values\" : \"long\" }");
    Map<String, Long> input = asMap("one", 1L, "three", 3L, "five", 5L, "seven", 7L, "eleven", 11L);
    Map<String, Long> adds = asMap("thirteen", 13L, "seventeen", 17L, "nineteen", 19L);
    Map<String, Object> badInput = asMap("boolean", true, "string", "hello", "bytes", ByteString.empty(), "object", new Object(), "null", null, "array", new StringArray(), "record", new FooRecord());
    Map<String, Object> badOutput = asMap("boolean", true, "string", "hello", "bytes", ByteString.empty(), "map", new DataMap(), "list", new DataList());
    testMap(LongMap.class, schema, input, adds);
    testMapBadInput(LongMap.class, schema, input, badInput, badOutput);
    Map<String, ? extends Number> castFrom = asMap("one", 1, "three", 3.0f, "five", 5.0, "seven", 7, "eleven", 11);
    testNumberMap(LongMap.class, schema, input, castFrom);
}
Also used : DataList(com.linkedin.data.DataList) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Aggregations

DataList (com.linkedin.data.DataList)160 DataMap (com.linkedin.data.DataMap)126 Test (org.testng.annotations.Test)102 ByteString (com.linkedin.data.ByteString)64 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)34 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)33 ArrayList (java.util.ArrayList)22 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)17 DataSchema (com.linkedin.data.schema.DataSchema)10 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)10 HashMap (java.util.HashMap)10 MapDataSchema (com.linkedin.data.schema.MapDataSchema)9 RecordTemplate (com.linkedin.data.template.RecordTemplate)9 Map (java.util.Map)8 DataProvider (org.testng.annotations.DataProvider)8 List (java.util.List)7 PathSpec (com.linkedin.data.schema.PathSpec)6 PatchTree (com.linkedin.data.transform.patch.request.PatchTree)5 DataComplex (com.linkedin.data.DataComplex)4 CustomPoint (com.linkedin.data.template.TestCustom.CustomPoint)4