Search in sources :

Example 21 with DataList

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

the class TestValidation method testValidationWithNormalCoercion.

@Test
public void testValidationWithNormalCoercion() throws IOException, CloneNotSupportedException {
    String schemaText = "{ \"type\" : \"record\", \"name\" : \"foo\", \"fields\" : \n" + "[ { \"name\" : \"bar\", \"type\" : { \"name\" : \"barType\", \"type\" : \"record\", \"fields\" : [ \n" + "{ \"name\" : \"boolean\", \"type\" : \"boolean\", \"optional\" : true }, \n" + "{ \"name\" : \"int\", \"type\" : \"int\", \"optional\" : true }, \n" + "{ \"name\" : \"long\", \"type\" : \"long\", \"optional\" : true }, \n" + "{ \"name\" : \"float\", \"type\" : \"float\", \"optional\" : true }, \n" + "{ \"name\" : \"double\", \"type\" : \"double\", \"optional\" : true }, \n" + "{ \"name\" : \"string\", \"type\" : \"string\", \"optional\" : true }, \n" + "{ \"name\" : \"bytes\", \"type\" : \"bytes\", \"optional\" : true }, \n" + "{ \"name\" : \"array\", \"type\" : { \"type\" : \"array\", \"items\" : \"int\" }, \"optional\" : true }, \n" + "{ \"name\" : \"enum\", \"type\" : { \"type\" : \"enum\", \"name\" : \"enumType\", \"symbols\" : [ \"apple\", \"orange\", \"banana\" ] }, \"optional\" : true }, \n" + "{ \"name\" : \"fixed\", \"type\" : { \"type\" : \"fixed\", \"name\" : \"fixedType\", \"size\" : 4 }, \"optional\" : true }, \n" + "{ \"name\" : \"map\", \"type\" : { \"type\" : \"map\", \"values\" : \"int\" }, \"optional\" : true }, \n" + "{ \"name\" : \"record\", \"type\" : { \"type\" : \"record\", \"name\" : \"recordType\", \"fields\" : [ { \"name\" : \"int\", \"type\" : \"int\" } ] }, \"optional\" : true }, \n" + "{ \"name\" : \"union\", \"type\" : [ \"int\", \"recordType\", \"enumType\", \"fixedType\" ], \"optional\" : true }, \n" + "{ \"name\" : \"unionWithNull\", \"type\" : [ \"null\", \"enumType\", \"fixedType\" ], \"optional\" : true } \n" + "] } } ] }";
    String key = "bar";
    DataSchema schema = dataSchemaFromString(schemaText);
    Object[][][] input = { { { new ValidationOptions(RequiredMode.IGNORE), new ValidationOptions(RequiredMode.MUST_BE_PRESENT), new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT), new ValidationOptions(RequiredMode.FIXUP_ABSENT_WITH_DEFAULT) }, // int
    { new DataMap(asMap("int", 1L)), new DataMap(asMap("int", 1)) }, { new DataMap(asMap("int", 1.0f)), new DataMap(asMap("int", 1)) }, { new DataMap(asMap("int", 1.0)), new DataMap(asMap("int", 1)) }, // long
    { new DataMap(asMap("long", 1)), new DataMap(asMap("long", 1L)) }, { new DataMap(asMap("long", 1.0f)), new DataMap(asMap("long", 1L)) }, { new DataMap(asMap("long", 1.0)), new DataMap(asMap("long", 1L)) }, // float
    { new DataMap(asMap("float", 1)), new DataMap(asMap("float", 1.0f)) }, { new DataMap(asMap("float", 1L)), new DataMap(asMap("float", 1.0f)) }, { new DataMap(asMap("float", 1.0)), new DataMap(asMap("float", 1.0f)) }, // double
    { new DataMap(asMap("double", 1)), new DataMap(asMap("double", 1.0)) }, { new DataMap(asMap("double", 1L)), new DataMap(asMap("double", 1.0)) }, { new DataMap(asMap("double", 1.0f)), new DataMap(asMap("double", 1.0)) }, // array of int's
    { new DataMap(asMap("array", new DataList(asList(1, 2, 3, 1.0, 2.0, 3.0, 1.0f, 2.0f, 3.0f, 1.0, 2.0, 3.0)))), new DataMap(asMap("array", new DataList(asList(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)))) }, // map of int's
    { new DataMap(asMap("map", new DataMap(asMap("int1", 1, "long", 1L, "float", 1.0f, "double", 1.0)))), new DataMap(asMap("map", new DataMap(asMap("int1", 1, "long", 1, "float", 1, "double", 1)))) }, // record with int fields
    { new DataMap(asMap("record", new DataMap(asMap("int", 1L)))), new DataMap(asMap("record", new DataMap(asMap("int", 1)))) }, { new DataMap(asMap("record", new DataMap(asMap("int", 1.0f)))), new DataMap(asMap("record", new DataMap(asMap("int", 1)))) }, { new DataMap(asMap("record", new DataMap(asMap("int", 1.0)))), new DataMap(asMap("record", new DataMap(asMap("int", 1)))) }, // union with int
    { new DataMap(asMap("union", new DataMap(asMap("int", 1L)))), new DataMap(asMap("union", new DataMap(asMap("int", 1)))) }, { new DataMap(asMap("union", new DataMap(asMap("int", 1.0f)))), new DataMap(asMap("union", new DataMap(asMap("int", 1)))) }, { new DataMap(asMap("union", new DataMap(asMap("int", 1.0)))), new DataMap(asMap("union", new DataMap(asMap("int", 1)))) }, // union with record containing int
    { new DataMap(asMap("union", new DataMap(asMap("recordType", new DataMap(asMap("int", 1L)))))), new DataMap(asMap("union", new DataMap(asMap("recordType", new DataMap(asMap("int", 1)))))) }, { new DataMap(asMap("union", new DataMap(asMap("recordType", new DataMap(asMap("int", 1.0f)))))), new DataMap(asMap("union", new DataMap(asMap("recordType", new DataMap(asMap("int", 1)))))) }, { new DataMap(asMap("union", new DataMap(asMap("recordType", new DataMap(asMap("int", 1.0)))))), new DataMap(asMap("union", new DataMap(asMap("recordType", new DataMap(asMap("int", 1)))))) } } };
    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 22 with DataList

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

the class TestValidation method testIntegerNormalCoercionValidation.

@Test
public void testIntegerNormalCoercionValidation() throws IOException {
    String schemaText = "{ \"type\" : \"record\", \"name\" : \"foo\", \"fields\" : " + "[ { \"name\" : \"bar\", \"type\" : \"int\" } ] }";
    Object[][] input = { { Integer.valueOf(1), Integer.valueOf(1) }, { Integer.valueOf(-1), Integer.valueOf(-1) }, { Integer.MAX_VALUE, Integer.MAX_VALUE }, { Integer.MAX_VALUE - 1, Integer.MAX_VALUE - 1 }, { Long.valueOf(1), Integer.valueOf(1) }, { Float.valueOf(1f), Integer.valueOf(1) }, { Double.valueOf(1), Integer.valueOf(1) }, { Double.NaN, 0 }, { Float.POSITIVE_INFINITY, Integer.MAX_VALUE }, { Double.NEGATIVE_INFINITY, Integer.MIN_VALUE } };
    Object[] badObjects = { Boolean.TRUE, new String("abc"), ByteString.copyAvroString("bytes", false), new DataMap(), new DataList(), String.valueOf(Float.NaN), String.valueOf(Double.POSITIVE_INFINITY), String.valueOf(Float.NEGATIVE_INFINITY) };
    testNormalCoercionValidation(schemaText, "bar", input, badObjects);
}
Also used : 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 23 with DataList

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

the class TestValidation method testBytesValidation.

@Test
public void testBytesValidation() throws IOException {
    String schemaText = "{ \"type\" : \"record\", \"name\" : \"foo\", \"fields\" : " + "[ { \"name\" : \"bar\", \"type\" : \"bytes\" } ] }";
    Object[] goodObjects = { "abc", ByteString.copyAvroString("bytes", false), ByteString.empty() };
    Object[] badObjects = { Boolean.TRUE, Integer.valueOf(1), Long.valueOf(1), Float.valueOf(1f), Double.valueOf(1), new DataMap(), new DataList(), new String("\u0100"), new String("ab\u0100c"), new String("ab\u0100c\u0200") };
    testCoercionValidation(schemaText, "bar", goodObjects, badObjects, noCoercionValidationOption());
    Object[][] inputs = { { ByteString.copyAvroString("abc", false), ByteString.copyAvroString("abc", false) }, { "abc", ByteString.copyAvroString("abc", false) } };
    testNormalCoercionValidation(schemaText, "bar", inputs, badObjects);
    testStringToPrimitiveCoercionValidation(schemaText, "bar", inputs, badObjects);
}
Also used : 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 24 with DataList

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

the class TestValidation method testFloatNormalCoercionValidation.

@Test
public void testFloatNormalCoercionValidation() throws IOException {
    String schemaText = "{ \"type\" : \"record\", \"name\" : \"foo\", \"fields\" : " + "[ { \"name\" : \"bar\", \"type\" : \"float\" } ] }";
    Object[][] inputs = { { Float.valueOf(1f), Float.valueOf(1f) }, { Float.valueOf(-1f), Float.valueOf(-1f) }, { Integer.valueOf(1), Float.valueOf(1f) }, { Long.valueOf(1), Float.valueOf(1f) }, { Double.valueOf(1), Float.valueOf(1f) }, { String.valueOf(Float.NaN), Float.NaN }, { String.valueOf(Float.POSITIVE_INFINITY), Float.POSITIVE_INFINITY }, { String.valueOf(Float.NEGATIVE_INFINITY), Float.NEGATIVE_INFINITY }, { Double.NaN, Float.NaN }, { Double.POSITIVE_INFINITY, Float.POSITIVE_INFINITY }, { Double.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY } };
    Object[] badObjects = { Boolean.TRUE, new String("abc"), ByteString.copyAvroString("bytes", false), new DataMap(), new DataList(), "1.1", "-1.1" };
    testNormalCoercionValidation(schemaText, "bar", inputs, badObjects);
}
Also used : 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 25 with DataList

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

the class TestValidation method testArrayNoCoercionValidation.

@Test
public void testArrayNoCoercionValidation() throws IOException {
    String schemaText = "{ \"type\" : \"record\", \"name\" : \"foo\", \"fields\" : " + "[ { \"name\" : \"bar\", \"type\" : { \"type\" : \"array\", \"items\" : \"int\" } } ] }";
    Object[] goodObjects = { new DataList(), new DataList(asList(Integer.valueOf(1))), new DataList(asList(Integer.valueOf(2), Integer.valueOf(3))) };
    Object[] badObjects = { Boolean.TRUE, Integer.valueOf(1), Long.valueOf(1), Float.valueOf(1f), Double.valueOf(1), new String(), new DataMap(), new DataList(asList(Boolean.TRUE)), new DataList(asList(Long.valueOf(1))), new DataList(asList(Float.valueOf(1f))), new DataList(asList(Double.valueOf(1))), new DataList(asList(new String("1"))), new DataList(asList(new DataMap())), new DataList(asList(new DataList())), new DataList(asList(Boolean.TRUE, Integer.valueOf(1))), new DataList(asList(Integer.valueOf(1), Boolean.TRUE)) };
    testCoercionValidation(schemaText, "bar", goodObjects, badObjects, noCoercionValidationOption());
}
Also used : 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)

Aggregations

DataList (com.linkedin.data.DataList)202 DataMap (com.linkedin.data.DataMap)151 Test (org.testng.annotations.Test)109 ByteString (com.linkedin.data.ByteString)72 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)34 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)33 ArrayList (java.util.ArrayList)27 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)22 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)15 DataSchema (com.linkedin.data.schema.DataSchema)14 RecordTemplate (com.linkedin.data.template.RecordTemplate)14 HashMap (java.util.HashMap)13 MapDataSchema (com.linkedin.data.schema.MapDataSchema)12 TestUtil.asReadOnlyDataMap (com.linkedin.data.TestUtil.asReadOnlyDataMap)11 Map (java.util.Map)11 List (java.util.List)10 DataProvider (org.testng.annotations.DataProvider)10 DataComplex (com.linkedin.data.DataComplex)9 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)5 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)5