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 = { Boolean.TRUE, Integer.valueOf(1), Long.valueOf(1), Float.valueOf(1f), Double.valueOf(1), new String(), new DataList(), new DataMap(asMap("int", 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", Boolean.TRUE)), new DataMap(asMap("string", Integer.valueOf(1))), new DataMap(asMap("string", Long.valueOf(1L))), new DataMap(asMap("string", Float.valueOf(1.0f))), new DataMap(asMap("string", Double.valueOf(1.0))), new DataMap(asMap("string", new DataMap())), new DataMap(asMap("string", new DataList())), new DataMap(asMap("Fruits", "foobar")), new DataMap(asMap("Fruits", Integer.valueOf(1))), new DataMap(asMap("Fruits", new DataMap())), new DataMap(asMap("Fruits", new DataList())), new DataMap(asMap("int", Integer.valueOf(1), "string", "x")), new DataMap(asMap("x", Integer.valueOf(1), "y", Long.valueOf(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 = { Float.valueOf(1f), Float.valueOf(-1f), Float.NaN, Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY };
Object[] badObjects = { Boolean.TRUE, Integer.valueOf(1), Long.valueOf(1), Double.valueOf(1), new String("abc"), ByteString.copyAvroString("bytes", false), new DataMap(), new DataList(), "1.1", "-1.1", String.valueOf(Float.NaN), String.valueOf(Float.POSITIVE_INFINITY), String.valueOf(Float.NEGATIVE_INFINITY) };
testCoercionValidation(schemaText, "bar", goodObjects, badObjects, noCoercionValidationOption());
}
use of com.linkedin.data.DataList in project rest.li by linkedin.
the class TestPdlSchemaParser method testParseNestedProperties.
@Test
public void testParseNestedProperties() throws IOException {
String sourcePdl = "namespace com.linkedin.test\n" + "\n" + "@validate.one.two.arrayOne = [\"a\", \"b\"]\n" + "@validate.one.two.arrayTwo = [1,2,3,4]\n" + "@validate.`com.linkedin.CustomValidator`.low = 5\n" + "@validate.`com.linkedin.namespace.CustomValidator`.low = 15\n" + "@validate.`com.linkedin.namespace.CustomValidator`.`union`.low.`record` = \"Date\"\n" + "@`com.linkedin.CustomValidator` = \"abc\"\n" + "@pathProp.`/*`.`*/.$` = false\n" + "@`/*.*/.$`\n" + "@grammarChars.`foo[type=a.b.c].bar` = \"grammarChars\"\n" + "record RecordDataSchema {}";
// construct expected data map
Map<String, Object> expected = new HashMap<>();
DataMap validate = new DataMap();
// @validate.one.two.arrayOne = ["a", "b"]"
// @validate.one.two.arrayTwo = [1,2,3,4]"
DataMap one = new DataMap();
DataMap two = new DataMap();
two.put("arrayOne", new DataList(Arrays.asList("a", "b")));
two.put("arrayTwo", new DataList(Arrays.asList(1, 2, 3, 4)));
one.put("two", two);
validate.put("one", one);
// @validate.`com.linkedin.CustomValidator`.low = 5"
DataMap customValidator = new DataMap();
customValidator.put("low", 5);
validate.put("com.linkedin.CustomValidator", customValidator);
// @validate.`com.linkedin.namespace.CustomValidator`.low = 15"
// @validate.`com.linkedin.namespace.CustomValidator`.`union`.low.`record` = "Date"
DataMap customValidator2 = new DataMap();
customValidator2.put("low", 15);
DataMap unionMap = new DataMap();
customValidator2.put("union", unionMap);
DataMap lowMap = new DataMap();
unionMap.put("low", lowMap);
lowMap.put("record", "Date");
validate.put("com.linkedin.namespace.CustomValidator", customValidator2);
expected.put("validate", validate);
// @`com.linkedin.CustomValidator` = "abc"
expected.put("com.linkedin.CustomValidator", "abc");
// @pathProp.`/*`.`*/.$` = false
DataMap propertyWithPath = new DataMap();
DataMap propertyWithSpecialChars = new DataMap();
propertyWithPath.put("/*", propertyWithSpecialChars);
propertyWithSpecialChars.put("*/.$", false);
expected.put("pathProp", propertyWithPath);
// @`/*.*/.$`
expected.put("/*.*/.$", true);
// "@grammarChars.`foo[type=a.b.c].bar` = "grammarChars"
DataMap grammarChars = new DataMap();
grammarChars.put("foo[type=a.b.c].bar", "grammarChars");
expected.put("grammarChars", grammarChars);
DataSchema encoded = TestUtil.dataSchemaFromPdlString(sourcePdl);
Assert.assertNotNull(encoded);
TestUtil.assertEquivalent(encoded.getProperties(), expected);
}
use of com.linkedin.data.DataList in project rest.li by linkedin.
the class TestRecordAndUnionTemplate method testFloatField.
@Test
public void testFloatField() {
List<Float> good = Arrays.asList(11.0f, 22.0f, 33.0f);
Foo foo = new Foo();
for (Float i : good) {
foo.setFloat(i.floatValue());
assertEquals(foo.getFloat(), i);
assertTrue(foo.hasFloat());
assertEquals(foo.toString(), "{float=" + i + '}');
Exception exc = null;
try {
// clone
Foo fooClone = foo.clone();
assertEquals(foo, fooClone);
fooClone.removeFloat();
assertTrue(foo.hasFloat());
assertEquals(foo.getFloat(), i);
assertFalse(fooClone.hasFloat());
// copy
Foo fooCopy = foo.copy();
assertEquals(foo, fooCopy);
assertTrue(TestUtil.noCommonDataComplex(fooCopy.data(), foo.data()));
fooCopy.removeFloat();
assertTrue(foo.hasFloat());
assertEquals(foo.getFloat(), i);
assertFalse(fooCopy.hasFloat());
} catch (CloneNotSupportedException e) {
exc = e;
}
assertTrue(exc == null);
foo.removeFloat();
assertFalse(foo.hasFloat());
assertEquals(foo.toString(), "{}");
}
List<?> badInput = asList(false, "abc", new DataList());
DataMap map = new DataMap();
foo = new Foo(map);
for (Object bad : badInput) {
map.put("float", bad);
Exception exc = null;
try {
foo.getFloat();
} catch (Exception e) {
exc = e;
}
assertTrue(exc != null);
assertTrue(exc instanceof TemplateOutputCastException);
}
List<?> castFrom = asList(88, 99.0, 77.0);
List<?> castTo = asList(88.0f, 99.0f, 77.0f);
for (int i = 0; i < castFrom.size(); ++i) {
map.put("float", castFrom.get(i));
Float result = foo.getFloat();
assertEquals(result, castTo.get(i));
}
// legacy test
Float[] t = { 77.0f, 88.0f, 99.0f };
foo = new Foo();
foo.set1Float(t[0].floatValue());
assertEquals(foo.getFloat(), t[0]);
foo.set2Float(t[1].floatValue());
assertEquals(foo.getFloat(), t[1]);
foo.set2Float(t[2], SetMode.DISALLOW_NULL);
assertEquals(foo.getFloat(), t[2]);
foo.set2Float(null, SetMode.REMOVE_IF_NULL);
assertFalse(foo.hasFloat());
}
use of com.linkedin.data.DataList in project rest.li by linkedin.
the class TestRecordAndUnionTemplate method testBooleanField.
@Test
public void testBooleanField() {
List<Boolean> good = Arrays.asList(Boolean.FALSE, Boolean.TRUE);
Foo foo = new Foo();
for (Boolean aBoolean : good) {
// Object
foo.setBoolean(aBoolean, SetMode.DISALLOW_NULL);
assertEquals(foo.isBoolean(), aBoolean);
assertTrue(foo.hasBoolean());
assertEquals(foo.toString(), "{boolean=" + aBoolean + '}');
Exception exc = null;
try {
// clone
Foo fooClone = foo.clone();
assertEquals(foo, fooClone);
fooClone.removeBoolean();
assertTrue(foo.hasBoolean());
assertEquals(foo.isBoolean(), aBoolean);
assertFalse(fooClone.hasBoolean());
// copy
Foo fooCopy = foo.copy();
assertEquals(foo, fooCopy);
assertTrue(TestUtil.noCommonDataComplex(fooCopy.data(), foo.data()));
fooCopy.removeBoolean();
assertTrue(foo.hasBoolean());
assertEquals(foo.isBoolean(), aBoolean);
assertFalse(fooCopy.hasBoolean());
} catch (CloneNotSupportedException e) {
exc = e;
}
assertTrue(exc == null);
foo.removeBoolean();
assertFalse(foo.hasBoolean());
assertEquals(foo.toString(), "{}");
// primitive
foo.setBoolean(aBoolean.booleanValue());
assertEquals(foo.isBoolean(), aBoolean);
assertTrue(foo.hasBoolean());
assertEquals(foo.toString(), "{boolean=" + aBoolean + '}');
exc = null;
try {
// clone
Foo fooClone = foo.clone();
assertEquals(foo, fooClone);
fooClone.removeBoolean();
assertTrue(foo.hasBoolean());
assertEquals(foo.isBoolean(), aBoolean);
assertFalse(fooClone.hasBoolean());
// copy
Foo fooCopy = foo.copy();
assertEquals(foo, fooCopy);
assertTrue(TestUtil.noCommonDataComplex(fooCopy.data(), foo.data()));
fooCopy.removeBoolean();
assertTrue(foo.hasBoolean());
assertEquals(foo.isBoolean(), aBoolean);
assertFalse(fooCopy.hasBoolean());
} catch (CloneNotSupportedException e) {
exc = e;
}
assertTrue(exc == null);
foo.removeBoolean();
assertFalse(foo.hasBoolean());
assertEquals(foo.toString(), "{}");
}
List<?> badInput = asList(3, "abc", new DataList());
DataMap map = new DataMap();
foo = new Foo(map);
for (Object bad : badInput) {
map.put("boolean", bad);
Exception exc = null;
try {
foo.isBoolean();
} catch (Exception e) {
exc = e;
}
assertTrue(exc != null);
assertTrue(exc instanceof TemplateOutputCastException);
}
// legacy test
Boolean[] t = { true, false, true };
foo = new Foo();
foo.set1Boolean(t[0].booleanValue());
assertEquals(foo.isBoolean(), t[0]);
foo.set2Boolean(t[1].booleanValue());
assertEquals(foo.isBoolean(), t[1]);
foo.set2Boolean(t[2], SetMode.DISALLOW_NULL);
assertEquals(foo.isBoolean(), t[2]);
foo.set2Boolean(null, SetMode.REMOVE_IF_NULL);
assertFalse(foo.hasBoolean());
}
Aggregations