}
@Test
public void testMetadata() throws Exception {
Record record = repository.getRecordFactory().newRecord();
record.setMetadata(new QName("ns", "field1"),
new MetadataBuilder()
.value("stringfield", "string")
.value("intfield", 55)
.value("longfield", 999999999999L)
.value("booleanfield", Boolean.TRUE)
.value("floatfield", 33.33f)
.value("doublefield", 66.66d)
.value("binaryfield", new ByteArray("foo".getBytes()))
.build());
record.setMetadata(new QName("ns", "field2"),
new MetadataBuilder()
.value("stringfield", "another_string")
.build());
ObjectNode recordNode = RecordWriter.INSTANCE.toJson(record, null, repository);
// go through ser/deser
String recordJson = JsonFormat.serializeAsString(recordNode);
recordNode = (ObjectNode)JsonFormat.deserializeNonStd(recordJson);
// Do some structural validations of the json
ObjectNode metadataNode = (ObjectNode)recordNode.get("metadata");
assertNotNull(metadataNode);
assertNull(recordNode.get("metadataToDelete"));
String prefix = recordNode.get("namespaces").get("ns").getTextValue();
assertEquals(2, metadataNode.size());
assertTrue(metadataNode.get(prefix + "$field1").get("stringfield").isTextual());
assertEquals("string", metadataNode.get(prefix + "$field1").get("stringfield").getTextValue());
assertTrue(metadataNode.get(prefix + "$field1").get("intfield").isInt());
assertEquals(55, metadataNode.get(prefix + "$field1").get("intfield").getIntValue());
assertTrue(metadataNode.get(prefix + "$field1").get("longfield").isLong());
assertEquals(999999999999L, metadataNode.get(prefix + "$field1").get("longfield").getLongValue());
assertTrue(metadataNode.get(prefix + "$field1").get("booleanfield").isBoolean());
assertEquals(Boolean.TRUE, metadataNode.get(prefix + "$field1").get("booleanfield").getBooleanValue());
// in JSON, no distinction between floats & doubles
assertTrue(metadataNode.get(prefix + "$field1").get("floatfield").isFloatingPointNumber());
assertEquals(33.33d, metadataNode.get(prefix + "$field1").get("floatfield").getDoubleValue(), 0.001);
assertTrue(metadataNode.get(prefix + "$field1").get("doublefield").isFloatingPointNumber());
assertEquals(33.33d, metadataNode.get(prefix + "$field1").get("floatfield").getDoubleValue(), 0.001);
assertTrue(metadataNode.get(prefix + "$field1").get("binaryfield").isObject());
ObjectNode binaryNode = (ObjectNode)metadataNode.get(prefix + "$field1").get("binaryfield");
assertEquals("binary", binaryNode.get("type").getTextValue());
assertArrayEquals("foo".getBytes(), binaryNode.get("value").getBinaryValue());
assertTrue(metadataNode.get(prefix + "$field2").get("stringfield").isTextual());
assertEquals("another_string", metadataNode.get(prefix + "$field2").get("stringfield").getTextValue());
// Now parse json again to API objects
record = RecordReader.INSTANCE.fromJson(recordNode, repository);
assertEquals(2, record.getMetadataMap().size());
Metadata metadata = record.getMetadata(new QName("ns", "field1"));
assertEquals("string", metadata.get("stringfield"));
assertEquals(55, metadata.getInt("intfield", null).intValue());
assertEquals(999999999999L, metadata.getLong("longfield", null).longValue());
assertEquals(Boolean.TRUE, metadata.getBoolean("booleanfield", null));
assertEquals(33.33f, metadata.getFloat("floatfield", null), 0.001);
assertEquals(66.66d, metadata.getDouble("doublefield", null), 0.001);
assertEquals(new ByteArray("foo".getBytes()), metadata.getBytes("binaryfield"));
metadata = record.getMetadata(new QName("ns", "field2"));
assertEquals(1, metadata.getMap().size());
assertEquals(0, metadata.getFieldsToDelete().size());
}