DATACMNS-1780 - PersistentEntities allows in flight metadata creation if the associated mapping context can be identified.

Use a more lenient approach, that allows metadata creation, when looking up persistent entities. This allows eg. a configured AuditingHandler to kick in without having to register an initial entity set in first place.

Original pull request: #462.
This commit is contained in:
Christoph Strobl
2020-08-03 14:28:08 +02:00
committed by Mark Paluch
parent a600d8160c
commit fd15c099e9
3 changed files with 81 additions and 17 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.auditing;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Optional;
@@ -103,6 +104,19 @@ class IsNewAwareAuditingHandlerUnitTests extends AuditingHandlerUnitTests {
getHandler().markAudited(Optional.of(new EntityWithoutId()));
}
@Test // DATACMNS-1780
void singleContextAllowsInFlightMetadataCreationForUnknownPersistentEntities() {
SampleMappingContext mappingContext = spy(new SampleMappingContext());
mappingContext.afterPropertiesSet();
AuditedUser user = new AuditedUser();
user.id = 1L;
new IsNewAwareAuditingHandler(PersistentEntities.of(mappingContext)).markAudited(user);
verify(mappingContext).getPersistentEntity(AuditedUser.class);
}
static class Domain {
@Id Long id;

View File

@@ -28,12 +28,14 @@ import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Reference;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.util.ClassTypeInformation;
/**
* Unit tests for {@link PersistentEntities}.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
@ExtendWith(MockitoExtension.class)
class PersistentEntitiesUnitTests {
@@ -51,6 +53,7 @@ class PersistentEntitiesUnitTests {
when(first.hasPersistentEntityFor(Sample.class)).thenReturn(false);
when(second.hasPersistentEntityFor(Sample.class)).thenReturn(true);
when(second.getRequiredPersistentEntity(Sample.class)).thenReturn(mock(BasicPersistentEntity.class));
PersistentEntities.of(first, second).getPersistentEntity(Sample.class);
@@ -143,6 +146,21 @@ class PersistentEntitiesUnitTests {
assertThat(entity.getType()).isEqualTo(SecondWithGenericId.class);
}
@Test // DATACMNS-1780
void getRequiredPersistentEntityAllowsInFlightEntityCreationForUnknownTypesWhenHavingJustASingleMappingContext() {
SampleMappingContext mappingContext = spy(new SampleMappingContext());
mappingContext.afterPropertiesSet();
assertThat(PersistentEntities.of(mappingContext).getRequiredPersistentEntity(Sample.class)).isNotNull();
}
@Test // DATACMNS-1780
void getRequiredPersistentEntityErrorsOnInFlightEntityCreationForUnknownTypesWhenHavingMultipleMappingContexts() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> PersistentEntities.of(first, second).getRequiredPersistentEntity(Sample.class));
}
static class Sample {
@Id String id;
}