DATACMNS-1274 - Auditing now can set metadata in nested objects.

Based on the new infrastructure created by DATACMNS-1275, MappingAuditingMetadata now keeps PersistentPropertyPaths to point to the properties reflecting individual auditing metadata items rather than just PersistentPropertyPaths. With that in place we can now find those items in embedded types of the subject entity based on the detection setup of the MappingContext managing metadata for the entity.

As that means that multiple paths to a metadata item property can be found, we now use the first path found (the shortest one) for the lookup of the modification date.

Related tickets: DATACMNS-1275.
This commit is contained in:
Oliver Gierke
2018-03-31 09:01:17 +02:00
parent ac324b22a5
commit 170c25d355
2 changed files with 92 additions and 31 deletions

View File

@@ -61,6 +61,7 @@ public class MappingAuditableBeanWrapperFactoryUnitTests {
SampleMappingContext context = new SampleMappingContext();
context.getPersistentEntity(Sample.class);
context.getPersistentEntity(SampleWithInstant.class);
context.getPersistentEntity(WithEmbedded.class);
PersistentEntities entities = new PersistentEntities(Collections.singleton(context));
factory = new MappingAuditableBeanWrapperFactory(entities);
@@ -181,6 +182,35 @@ public class MappingAuditableBeanWrapperFactoryUnitTests {
assertLastModificationDate(reference, Instant.ofEpochMilli(reference));
}
@Test // DATACMNS-1274
public void writesNestedAuditingData() {
WithEmbedded target = new WithEmbedded();
target.embedded = new Embedded();
Optional<AuditableBeanWrapper> wrapper = factory.getBeanWrapperFor(target);
assertThat(wrapper).hasValueSatisfying(it -> {
Instant now = Instant.now();
String user = "user";
it.setCreatedBy(user);
it.setLastModifiedBy(user);
it.setLastModifiedDate(now);
it.setCreatedDate(now);
Embedded embedded = target.embedded;
assertThat(embedded.created).isEqualTo(now);
assertThat(embedded.creator).isEqualTo(user);
assertThat(embedded.modified).isEqualTo(now);
assertThat(embedded.modifier).isEqualTo(user);
assertThat(it.getLastModifiedDate()).hasValue(now);
});
}
private void assertLastModificationDate(Object source, TemporalAccessor expected) {
Sample sample = new Sample();
@@ -231,4 +261,18 @@ public class MappingAuditableBeanWrapperFactoryUnitTests {
static class NoAuditing {}
static abstract class ExtendingAuditable implements Auditable<Object, Long, LocalDateTime> {}
// DATACMNS-1274
static class Embedded {
@CreatedDate Instant created;
@CreatedBy String creator;
@LastModifiedDate Instant modified;
@LastModifiedBy String modifier;
}
static class WithEmbedded {
Embedded embedded;
}
}