Skip to content

Commit a8a364c

Browse files
DATAMONGO-2586 - Polishing.
Add tests to ensure no reactive auditing callback is registered when using imperative configuration and vice versa. Update wording and minor code style tweaks. Original Pull Request: #877
1 parent 6bafcea commit a8a364c

File tree

4 files changed

+57
-24
lines changed

4 files changed

+57
-24
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/PersistentEntitiesFactoryBean.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2020 the original author or authors.
2+
* Copyright 2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,8 @@
2525
*
2626
* @author Oliver Gierke
2727
* @author Mark Paluch
28+
* @author Christoph Strobl
29+
* @since 3.1
2830
*/
2931
class PersistentEntitiesFactoryBean implements FactoryBean<PersistentEntities> {
3032

@@ -44,7 +46,7 @@ public PersistentEntitiesFactoryBean(MappingMongoConverter converter) {
4446
* @see org.springframework.beans.factory.FactoryBean#getObject()
4547
*/
4648
@Override
47-
public PersistentEntities getObject() throws Exception {
49+
public PersistentEntities getObject() {
4850
return PersistentEntities.of(converter.getMappingContext());
4951
}
5052

@@ -56,13 +58,4 @@ public PersistentEntities getObject() throws Exception {
5658
public Class<?> getObjectType() {
5759
return PersistentEntities.class;
5860
}
59-
60-
/*
61-
* (non-Javadoc)
62-
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
63-
*/
64-
@Override
65-
public boolean isSingleton() {
66-
return true;
67-
}
6861
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AuditingViaJavaConfigRepositoriesTests.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Arrays;
2222
import java.util.Collections;
2323
import java.util.HashSet;
24+
import java.util.List;
2425
import java.util.Optional;
2526
import java.util.Set;
2627
import java.util.function.Function;
@@ -34,12 +35,16 @@
3435
import org.springframework.context.annotation.ComponentScan.Filter;
3536
import org.springframework.context.annotation.Configuration;
3637
import org.springframework.context.annotation.FilterType;
38+
import org.springframework.core.ResolvableType;
3739
import org.springframework.data.annotation.Version;
3840
import org.springframework.data.domain.AuditorAware;
41+
import org.springframework.data.mapping.callback.EntityCallback;
3942
import org.springframework.data.mongodb.core.AuditablePerson;
4043
import org.springframework.data.mongodb.core.MongoOperations;
4144
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
4245
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
46+
import org.springframework.data.mongodb.core.mapping.event.AuditingEntityCallback;
47+
import org.springframework.data.mongodb.core.mapping.event.ReactiveAuditingEntityCallback;
4348
import org.springframework.data.mongodb.repository.MongoRepository;
4449
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
4550
import org.springframework.data.mongodb.test.util.Client;
@@ -48,6 +53,7 @@
4853
import org.springframework.stereotype.Repository;
4954
import org.springframework.test.context.ContextConfiguration;
5055
import org.springframework.test.context.junit.jupiter.SpringExtension;
56+
import org.springframework.test.util.ReflectionTestUtils;
5157

5258
import com.mongodb.client.MongoClient;
5359

@@ -60,7 +66,7 @@
6066
*/
6167
@ExtendWith({ MongoClientExtension.class, SpringExtension.class })
6268
@ContextConfiguration
63-
public class AuditingViaJavaConfigRepositoriesTests {
69+
class AuditingViaJavaConfigRepositoriesTests {
6470

6571
static @Client MongoClient mongoClient;
6672

@@ -79,6 +85,7 @@ static class Config extends AbstractMongoClientConfiguration {
7985

8086
@Override
8187
protected String getDatabaseName() {
88+
8289
return "database";
8390
}
8491

@@ -101,13 +108,13 @@ protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
101108
}
102109

103110
@BeforeEach
104-
public void setup() {
111+
void setup() {
105112
auditablePersonRepository.deleteAll();
106113
this.auditor = auditablePersonRepository.save(new AuditablePerson("auditor"));
107114
}
108115

109116
@Test // DATAMONGO-792, DATAMONGO-883
110-
public void basicAuditing() {
117+
void basicAuditing() {
111118

112119
doReturn(Optional.of(this.auditor)).when(this.auditorAware).getCurrentAuditor();
113120

@@ -122,18 +129,18 @@ public void basicAuditing() {
122129

123130
@Test // DATAMONGO-843
124131
@SuppressWarnings("resource")
125-
public void auditingUsesFallbackMappingContextIfNoneConfiguredWithRepositories() {
132+
void auditingUsesFallbackMappingContextIfNoneConfiguredWithRepositories() {
126133
new AnnotationConfigApplicationContext(SimpleConfigWithRepositories.class);
127134
}
128135

129136
@Test // DATAMONGO-843
130137
@SuppressWarnings("resource")
131-
public void auditingUsesFallbackMappingContextIfNoneConfigured() {
138+
void auditingUsesFallbackMappingContextIfNoneConfigured() {
132139
new AnnotationConfigApplicationContext(SimpleConfig.class);
133140
}
134141

135142
@Test // DATAMONGO-2139
136-
public void auditingWorksForVersionedEntityWithWrapperVersion() {
143+
void auditingWorksForVersionedEntityWithWrapperVersion() {
137144

138145
verifyAuditingViaVersionProperty(new VersionedAuditablePerson(), //
139146
it -> it.version, //
@@ -143,7 +150,7 @@ public void auditingWorksForVersionedEntityWithWrapperVersion() {
143150
}
144151

145152
@Test // DATAMONGO-2179
146-
public void auditingWorksForVersionedEntityBatchWithWrapperVersion() {
153+
void auditingWorksForVersionedEntityBatchWithWrapperVersion() {
147154

148155
verifyAuditingViaVersionProperty(new VersionedAuditablePerson(), //
149156
it -> it.version, //
@@ -153,7 +160,7 @@ public void auditingWorksForVersionedEntityBatchWithWrapperVersion() {
153160
}
154161

155162
@Test // DATAMONGO-2139
156-
public void auditingWorksForVersionedEntityWithSimpleVersion() {
163+
void auditingWorksForVersionedEntityWithSimpleVersion() {
157164

158165
verifyAuditingViaVersionProperty(new SimpleVersionedAuditablePerson(), //
159166
it -> it.version, //
@@ -163,7 +170,7 @@ public void auditingWorksForVersionedEntityWithSimpleVersion() {
163170
}
164171

165172
@Test // DATAMONGO-2139
166-
public void auditingWorksForVersionedEntityWithWrapperVersionOnTemplate() {
173+
void auditingWorksForVersionedEntityWithWrapperVersionOnTemplate() {
167174

168175
verifyAuditingViaVersionProperty(new VersionedAuditablePerson(), //
169176
it -> it.version, //
@@ -173,7 +180,7 @@ public void auditingWorksForVersionedEntityWithWrapperVersionOnTemplate() {
173180
}
174181

175182
@Test // DATAMONGO-2139
176-
public void auditingWorksForVersionedEntityWithSimpleVersionOnTemplate() {
183+
void auditingWorksForVersionedEntityWithSimpleVersionOnTemplate() {
177184

178185
verifyAuditingViaVersionProperty(new SimpleVersionedAuditablePerson(), //
179186
it -> it.version, //
@@ -182,6 +189,19 @@ public void auditingWorksForVersionedEntityWithSimpleVersionOnTemplate() {
182189
0L, 1L, 2L);
183190
}
184191

192+
@Test // DATAMONGO-2586
193+
void auditingShouldOnlyRegisterImperativeAuditingCallback() {
194+
195+
Object callbacks = ReflectionTestUtils.getField(operations, "entityCallbacks");
196+
Object callbackDiscoverer = ReflectionTestUtils.getField(callbacks, "callbackDiscoverer");
197+
List<EntityCallback<?>> actualCallbacks = ReflectionTestUtils.invokeMethod(callbackDiscoverer, "getEntityCallbacks",
198+
AuditablePerson.class, ResolvableType.forClass(EntityCallback.class));
199+
200+
assertThat(actualCallbacks) //
201+
.hasAtLeastOneElementOfType(AuditingEntityCallback.class) //
202+
.doesNotHaveAnyElementsOfTypes(ReactiveAuditingEntityCallback.class);
203+
}
204+
185205
private <T extends AuditablePerson> void verifyAuditingViaVersionProperty(T instance,
186206
Function<T, Object> versionExtractor, Function<T, Object> createdDateExtractor, Function<T, T> persister,
187207
Object... expectedValues) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/ReactiveAuditingTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import org.springframework.core.ResolvableType;
21+
import org.springframework.data.mapping.callback.EntityCallback;
22+
import org.springframework.data.mongodb.core.mapping.event.AuditingEntityCallback;
23+
import org.springframework.data.mongodb.core.mapping.event.ReactiveAuditingEntityCallback;
24+
import org.springframework.test.util.ReflectionTestUtils;
2025
import reactor.core.publisher.Mono;
2126
import reactor.test.StepVerifier;
2227

2328
import java.util.Arrays;
2429
import java.util.Collections;
2530
import java.util.HashSet;
31+
import java.util.List;
2632
import java.util.Set;
2733
import java.util.concurrent.atomic.AtomicReference;
2834
import java.util.function.Function;
@@ -146,6 +152,19 @@ void auditingWorksForVersionedEntityWithSimpleVersionOnTemplate() {
146152
0L, 1L, 2L);
147153
}
148154

155+
@Test // DATAMONGO-2586
156+
void auditingShouldOnlyRegisterReactiveAuditingCallback() {
157+
158+
Object callbacks = ReflectionTestUtils.getField(operations, "entityCallbacks");
159+
Object callbackDiscoverer = ReflectionTestUtils.getField(callbacks, "callbackDiscoverer");
160+
List<EntityCallback<?>> actualCallbacks = ReflectionTestUtils.invokeMethod(callbackDiscoverer, "getEntityCallbacks",
161+
AuditablePerson.class, ResolvableType.forClass(EntityCallback.class));
162+
163+
assertThat(actualCallbacks) //
164+
.hasAtLeastOneElementOfType(ReactiveAuditingEntityCallback.class) //
165+
.doesNotHaveAnyElementsOfTypes(AuditingEntityCallback.class);
166+
}
167+
149168
private <T extends AuditablePerson> void verifyAuditingViaVersionProperty(T instance,
150169
Function<T, Object> versionExtractor, Function<T, Object> createdDateExtractor, Function<T, Mono<T>> persister,
151170
Object... expectedValues) {

src/main/asciidoc/reference/mongo-auditing.adoc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ To activate auditing functionality via XML, add the Spring Data Mongo `auditing`
3030
----
3131
====
3232

33-
If you wish to use auditing with the reactive programming model, then enable auditing through `@EnableReactiveMongoAuditing`
33+
To enable auditing, leveraging a reactive programming model, use the `@EnableReactiveMongoAuditing` annotation. +
34+
If you expose a bean of type `ReactiveAuditorAware` to the `ApplicationContext`, the auditing infrastructure picks it up automatically and uses it to determine the current user to be set on domain types. If you have multiple implementations registered in the `ApplicationContext`, you can select the one to be used by explicitly setting the `auditorAwareRef` attribute of `@EnableReactiveMongoAuditing`.
3435

35-
.Activating auditing using JavaConfig
36+
.Activating reactive auditing using JavaConfig
3637
====
3738
[source,java]
3839
----
@@ -47,5 +48,5 @@ class Config {
4748
}
4849
----
4950
====
50-
If you expose a bean of type `ReactiveAuditorAware` to the `ApplicationContext`, the auditing infrastructure picks it up automatically and uses it to determine the current user to be set on domain types. If you have multiple implementations registered in the `ApplicationContext`, you can select the one to be used by explicitly setting the `auditorAwareRef` attribute of `@EnableReactiveMongoAuditing`.
51+
5152

0 commit comments

Comments
 (0)