public void testScanFieldValueFilter() throws Exception {
QName name = new QName("ns", "stringField");
Object value = "foo";
RecordScan scan = new RecordScan();
scan.setRecordFilter(new FieldValueFilter(name, value));
byte[] data = scanToBytes(scan);
RecordScan parsedScan = scanFromBytes(data);
assertNotNull(parsedScan.getRecordFilter());
assertTrue(parsedScan.getRecordFilter() instanceof FieldValueFilter);
FieldValueFilter filter = (FieldValueFilter) parsedScan.getRecordFilter();
assertEquals(name, filter.getField());
assertEquals(value, filter.getFieldValue());
assertTrue(filter.getFilterIfMissing());
// Check json structure
JsonNode node = new ObjectMapper().readTree(data);
assertEquals("org.lilyproject.repository.api.filter.FieldValueFilter",
node.get("recordFilter").get("@class").getTextValue());
assertEquals("foo", node.get("recordFilter").get("fieldValue").getTextValue());
// Try different data types as field value
value = 3L;
scan.setRecordFilter(new FieldValueFilter(new QName("ns", "longField"), value));
assertEquals(value, ((FieldValueFilter) scanFromBytes(scanToBytes(scan))
.getRecordFilter()).getFieldValue());
value = Lists.newArrayList("foo", "bar");
scan.setRecordFilter(new FieldValueFilter(new QName("ns", "stringListField"), value));
assertEquals(value, ((FieldValueFilter) scanFromBytes(scanToBytes(scan))
.getRecordFilter()).getFieldValue());
// The following test made more sense when we were using a generic type-detection
// in the json serialization rather than using TypeManager.
// Use a list as field value, but with a mixture of datatypes. This should fail,
// as lists in Lily should contain values of the same type.
value = Lists.newArrayList("foo", 123L);
scan.setRecordFilter(new FieldValueFilter(new QName("ns", "stringListField"), value));
try {
data = scanToBytes(scan);
fail("Expected exception with list containing different data types");
} catch (Exception e) {
// expected