use of com.linkedin.data.DataList in project rest.li by linkedin.
the class TestValidation method testUnionNormalCoercionValidation.
@Test
public void testUnionNormalCoercionValidation() throws IOException {
String schemaText = "{\n" + " \"type\" : \"record\",\n" + " \"name\" : \"foo\",\n" + " \"fields\" : [\n" + " {\n" + " \"name\" : \"bar\",\n" + " \"type\" : [\n" + " \"null\",\n" + " \"int\",\n" + " \"string\",\n" + " { \"type\" : \"enum\", \"name\" : \"Fruits\", \"symbols\" : [ \"APPLE\", \"ORANGE\" ] }\n" + " ]\n" + " }\n" + " ]\n" + "}";
Object[][] inputs = { { Data.NULL, Data.NULL }, { new DataMap(asMap("int", 1)), new DataMap(asMap("int", 1)) }, { new DataMap(asMap("string", "x")), new DataMap(asMap("string", "x")) }, { new DataMap(asMap("Fruits", "APPLE")), new DataMap(asMap("Fruits", "APPLE")) }, { new DataMap(asMap("Fruits", "ORANGE")), new DataMap(asMap("Fruits", "ORANGE")) }, { 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)) } };
Object[] badObjects = { new Boolean(true), new Integer(1), new Long(1), new Float(1), new Double(1), new String(), new DataList(), new DataMap(asMap("int", new Boolean(true))), new DataMap(asMap("int", new String("1"))), new DataMap(asMap("int", new DataMap())), new DataMap(asMap("int", new DataList())), new DataMap(asMap("string", new Boolean(true))), new DataMap(asMap("string", new Integer(1))), new DataMap(asMap("string", new Long(1L))), new DataMap(asMap("string", new Float(1.0f))), new DataMap(asMap("string", new Double(1.0))), new DataMap(asMap("string", new DataMap())), new DataMap(asMap("string", new DataList())), new DataMap(asMap("Fruits", "foobar")), new DataMap(asMap("Fruits", new Integer(1))), new DataMap(asMap("Fruits", new DataMap())), new DataMap(asMap("Fruits", new DataList())), new DataMap(asMap("int", new Integer(1), "string", "x")), new DataMap(asMap("x", new Integer(1), "y", new Long(1))) };
testNormalCoercionValidation(schemaText, "bar", inputs, badObjects);
}
use of com.linkedin.data.DataList in project rest.li by linkedin.
the class TestValidation method testFloatNoCoercionValidation.
@Test
public void testFloatNoCoercionValidation() throws IOException {
String schemaText = "{ \"type\" : \"record\", \"name\" : \"foo\", \"fields\" : " + "[ { \"name\" : \"bar\", \"type\" : \"float\" } ] }";
Object[] goodObjects = { new Float(1), new Float(-1) };
Object[] badObjects = { new Boolean(true), new Integer(1), new Long(1), new Double(1), new String("abc"), ByteString.copyAvroString("bytes", false), new DataMap(), new DataList() };
testCoercionValidation(schemaText, "bar", goodObjects, badObjects, noCoercionValidationOption());
}
use of com.linkedin.data.DataList in project rest.li by linkedin.
the class TestValidation method testDoubleStringToPrimitiveCoercionValidation.
@Test
public void testDoubleStringToPrimitiveCoercionValidation() throws IOException {
String schemaText = "{ \"type\" : \"record\", \"name\" : \"foo\", \"fields\" : " + "[ { \"name\" : \"bar\", \"type\" : \"double\" } ] }";
Object[][] inputs = { { new String("1"), new Double(1) }, { new String("-1"), new Double(-1) }, { new String("1.01"), new Double(1.01) }, { new String("-1.01"), new Double(-1.01) }, { new String("" + Double.MAX_VALUE), Double.MAX_VALUE }, { new Double(1), new Double(1) }, { new Double(-1), new Double(-1) }, { new Integer(1), new Double(1) }, { new Long(1), new Double(1) }, { new Float(1), new Double(1) } };
Object[] badObjects = { new Boolean(true), new String("abc"), ByteString.copyAvroString("bytes", false), new DataMap(), new DataList() };
testStringToPrimitiveCoercionValidation(schemaText, "bar", inputs, badObjects);
}
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);
}
use of com.linkedin.data.DataList in project rest.li by linkedin.
the class TestCustom method testCustomPointArray.
@Test
public void testCustomPointArray() {
final List<String> input = new ArrayList<String>(Arrays.asList("1,1", "2,2", "3,3"));
final DataList inputDataList = new DataList(input);
CustomPointArray a1 = new CustomPointArray();
for (String s : input) {
a1.add(new CustomPoint(s));
assertTrue(a1.contains(new CustomPoint(s)));
}
CustomPointArray a2 = new CustomPointArray(inputDataList);
assertEquals(a1, a2);
assertEquals(a1.data(), a2.data());
for (String s : input) {
assertTrue(a2.contains(new CustomPoint(s)));
}
for (int i = 0; i < input.size(); i++) {
CustomPoint p = a1.get(i);
assertEquals(p, new CustomPoint(input.get(i)));
}
CustomPointArray a3 = new CustomPointArray(input.size());
for (int i = 0; i < input.size(); i++) {
a3.add(new CustomPoint(input.get(i)));
assertEquals(a3.get(i), new CustomPoint(input.get(i)));
}
for (int i = 0; i < input.size(); i++) {
int j = input.size() - i - 1;
a3.set(j, new CustomPoint(input.get(i)));
assertEquals(a3.get(j), new CustomPoint(input.get(i)));
}
}
Aggregations