use of com.linkedin.data.DataList in project rest.li by linkedin.
the class TestArrayTemplate method testIntegerArray.
@Test
public void testIntegerArray() {
ArrayDataSchema schema = (ArrayDataSchema) DataTemplateUtil.parseSchema("{ \"type\" : \"array\", \"items\" : \"int\" }");
// must be unique
List<Integer> input = Arrays.asList(1, 3, 5, 7, 11);
List<Integer> adds = Arrays.asList(13, 17, 19);
List<Object> badInput = asList(true, "hello", ByteString.empty(), new StringMap(), new StringArray(), null);
List<Object> badOutput = asList(true, "hello", ByteString.empty(), new DataMap(), new DataList());
testArray(IntegerArray.class, schema, input, adds);
testArrayBadInput(IntegerArray.class, schema, input, badInput, badOutput);
@SuppressWarnings("unchecked") List<? extends Number> castFrom = Arrays.asList(1L, 3.0f, 5.0, 7, 11);
testNumberArray(IntegerArray.class, schema, input, castFrom);
}
use of com.linkedin.data.DataList in project rest.li by linkedin.
the class TestArrayTemplate method testBytesArray.
@Test
public void testBytesArray() {
ArrayDataSchema schema = (ArrayDataSchema) DataTemplateUtil.parseSchema("{ \"type\" : \"array\", \"items\" : \"bytes\" }");
List<ByteString> input = Arrays.asList(ByteString.copyAvroString("1", false), ByteString.copyAvroString("3", false), ByteString.copyAvroString("5", false), ByteString.copyAvroString("7", false), ByteString.copyAvroString("11", false));
List<ByteString> adds = Arrays.asList(ByteString.copyAvroString("13", false), ByteString.copyAvroString("17", false), ByteString.copyAvroString("19", false));
List<Object> badInput = asList(true, 99, 999L, 88.0f, 888.0, "Ā", new StringMap(), new StringArray(), null);
List<Object> badOutput = asList(true, 99, 999L, 88.0f, 888.0, "Ā", new DataMap(), new DataList());
testArray(BytesArray.class, schema, input, adds);
testArrayBadInput(BytesArray.class, schema, input, badInput, badOutput);
}
use of com.linkedin.data.DataList in project rest.li by linkedin.
the class TestArrayTemplate method testStringArray.
@Test
public void testStringArray() {
ArrayDataSchema schema = (ArrayDataSchema) DataTemplateUtil.parseSchema("{ \"type\" : \"array\", \"items\" : \"string\" }");
// must be unique
List<String> input = Arrays.asList("apple", "banana", "orange", "pineapple", "graphs");
List<String> adds = Arrays.asList("foo", "bar", "baz");
List<Object> badInput = asList(true, 1, 2L, 3.0f, 4.0, ByteString.empty(), new StringMap(), new StringArray(), null);
List<Object> badOutput = asList(true, 1, 2L, 3.0f, 4.0, ByteString.empty(), new DataMap(), new DataList());
testArray(StringArray.class, schema, input, adds);
testArrayBadInput(StringArray.class, schema, input, badInput, badOutput);
}
use of com.linkedin.data.DataList in project rest.li by linkedin.
the class TestRecordAndUnionTemplate method testBytesField.
@Test
public void testBytesField() {
List<ByteString> good = Arrays.asList(ByteString.copyAvroString("11", false), ByteString.copyAvroString("22", false), ByteString.copyAvroString("33", false));
Foo foo = new Foo();
for (ByteString byteString : good) {
foo.setBytes(byteString);
assertEquals(foo.getBytes(), byteString);
assertTrue(foo.hasBytes());
assertEquals(foo.toString(), "{bytes=" + byteString + '}');
Exception exc = null;
try {
// clone
Foo fooClone = foo.clone();
assertEquals(foo, fooClone);
fooClone.removeBytes();
assertTrue(foo.hasBytes());
assertEquals(foo.getBytes(), byteString);
assertFalse(fooClone.hasBytes());
// copy
Foo fooCopy = foo.copy();
assertEquals(foo, fooCopy);
assertTrue(TestUtil.noCommonDataComplex(fooCopy.data(), foo.data()));
fooCopy.removeBytes();
assertTrue(foo.hasBytes());
assertEquals(foo.getBytes(), byteString);
assertFalse(fooCopy.hasBytes());
} catch (CloneNotSupportedException e) {
exc = e;
}
assertTrue(exc == null);
foo.removeBytes();
assertFalse(foo.hasBytes());
assertEquals(foo.toString(), "{}");
}
List<?> badInput = TestUtil.asList(false, 33, "Ā", new DataList());
DataMap map = new DataMap();
foo = new Foo(map);
for (Object bad : badInput) {
map.put("bytes", bad);
Exception exc = null;
try {
foo.getBytes();
} catch (Exception e) {
exc = e;
}
assertTrue(exc != null);
assertTrue(exc instanceof TemplateOutputCastException);
}
List<?> castFrom = TestUtil.asList("88");
List<?> castTo = TestUtil.asList(ByteString.copyAvroString("88", false));
for (int i = 0; i < castFrom.size(); ++i) {
map.put("bytes", castFrom.get(i));
ByteString result = foo.getBytes();
assertEquals(result, castTo.get(i));
}
// legacy test
ByteString[] t = { ByteString.copyAvroString("apple", false), ByteString.copyAvroString("orange", false), ByteString.copyAvroString("banana", false) };
foo = new Foo();
foo.set1Bytes(t[0]);
assertEquals(foo.getBytes(), t[0]);
foo.set2Bytes(t[1]);
assertEquals(foo.getBytes(), t[1]);
foo.set2Bytes(t[2], SetMode.DISALLOW_NULL);
assertEquals(foo.getBytes(), t[2]);
foo.set2Bytes(null, SetMode.REMOVE_IF_NULL);
assertFalse(foo.hasBytes());
}
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 = TestUtil.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 = TestUtil.asList(88, 99.0, 77.0);
List<?> castTo = TestUtil.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());
}
Aggregations