public void testMetadataPartialUpdate() throws Exception {
Record record = repository.newRecord();
record.setRecordType(recordType1.getName());
record.setField(fieldType1.getName(), "field value");
Metadata metadata = new MetadataBuilder()
.value("field1", "value1")
.value("field2", "value2")
.value("field3", "value3").build();
record.setMetadata(fieldType1.getName(), metadata);
record = repository.create(record);
RecordId recordId = record.getId();
// Now update metadata:
// - field1 is left unchanged and not specified
// - field2 is updated
// - field3 is deleted
// - a new field4 is added
metadata = new MetadataBuilder()
.value("field2", "value2a")
.delete("field3")
.value("field4", "value4").build();
record = repository.newRecord(recordId);
record.setMetadata(fieldType1.getName(), metadata);
record = repository.update(record);
// Check state of returned record object
Metadata returnedMetadata = record.getMetadata(fieldType1.getName());
assertNotNull(returnedMetadata);
assertEquals(2, returnedMetadata.getMap().size());
assertEquals("value2a", returnedMetadata.get("field2"));
assertEquals("value4", returnedMetadata.get("field4"));
assertEquals(0, returnedMetadata.getFieldsToDelete().size());
// Check state when freshly reading record
record = repository.read(record.getId());
Metadata readMetadata = record.getMetadataMap().get(fieldType1.getName());
assertNotNull(readMetadata);
assertEquals(3, readMetadata.getMap().size());
assertEquals("value1", readMetadata.get("field1"));
assertEquals("value2a", readMetadata.get("field2"));
assertEquals("value4", readMetadata.get("field4"));
}