|
34 | 34 | import org.springframework.data.auditing.IsNewAwareAuditingHandler;
|
35 | 35 | import org.springframework.data.mapping.context.PersistentEntities;
|
36 | 36 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
| 37 | +import org.springframework.data.mongodb.core.mapping.Unwrapped; |
37 | 38 |
|
38 | 39 | /**
|
39 | 40 | * Unit tests for {@link AuditingEntityCallback}.
|
|
43 | 44 | @ExtendWith(MockitoExtension.class)
|
44 | 45 | public class AuditingEntityCallbackUnitTests {
|
45 | 46 |
|
| 47 | + private final MongoMappingContext mappingContext = new MongoMappingContext(); |
| 48 | + |
46 | 49 | private IsNewAwareAuditingHandler handler;
|
47 | 50 | private AuditingEntityCallback callback;
|
48 | 51 |
|
49 | 52 | @BeforeEach
|
50 | 53 | void setUp() {
|
51 | 54 |
|
52 |
| - MongoMappingContext mappingContext = new MongoMappingContext(); |
53 | 55 | mappingContext.getPersistentEntity(Sample.class);
|
54 | 56 |
|
55 | 57 | handler = spy(new IsNewAwareAuditingHandler(new PersistentEntities(Arrays.asList(mappingContext))));
|
@@ -105,13 +107,40 @@ void propagatesChangedInstanceToEvent() {
|
105 | 107 | assertThat(result).isSameAs(newSample);
|
106 | 108 | }
|
107 | 109 |
|
| 110 | + @Test // GH-4732 |
| 111 | + void shouldApplyAuditingToUnwrappedImmutableObject() { |
| 112 | + |
| 113 | + WithUnwrapped sample = new WithUnwrapped(); |
| 114 | + sample.auditingData = new MyAuditingData(null, null); |
| 115 | + |
| 116 | + IsNewAwareAuditingHandler handler = new IsNewAwareAuditingHandler(PersistentEntities.of(mappingContext)); |
| 117 | + |
| 118 | + AuditingEntityCallback listener = new AuditingEntityCallback(() -> handler); |
| 119 | + WithUnwrapped result = (WithUnwrapped) listener.onBeforeConvert(sample, "foo"); |
| 120 | + |
| 121 | + assertThat(result.auditingData.created).isNotNull(); |
| 122 | + assertThat(result.auditingData.modified).isNotNull(); |
| 123 | + } |
| 124 | + |
108 | 125 | static class Sample {
|
109 | 126 |
|
110 | 127 | @Id String id;
|
111 | 128 | @CreatedDate Date created;
|
112 | 129 | @LastModifiedDate Date modified;
|
113 | 130 | }
|
114 | 131 |
|
| 132 | + static class WithUnwrapped { |
| 133 | + |
| 134 | + @Id String id; |
| 135 | + |
| 136 | + @Unwrapped(onEmpty = Unwrapped.OnEmpty.USE_NULL) MyAuditingData auditingData; |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + record MyAuditingData(@CreatedDate Date created, @LastModifiedDate Date modified) { |
| 141 | + |
| 142 | + } |
| 143 | + |
115 | 144 | private static final class ImmutableSample {
|
116 | 145 |
|
117 | 146 | @Id private final String id;
|
|
0 commit comments