Skip to content

Commit 9fe0f5c

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-2150 - Polishing.
Fix imperative auditing test to use intended persist mechanism. Remove final keywords from method args and local variables in ReactiveMongoTemplate. Rename DBObject to Document. Original Pull Request: #627
1 parent 718a7ff commit 9fe0f5c

File tree

2 files changed

+45
-71
lines changed

2 files changed

+45
-71
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java

Lines changed: 41 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,15 @@ public Mono<Document> executeCommand(String jsonCommand) {
396396
* (non-Javadoc)
397397
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#executeCommand(org.bson.Document)
398398
*/
399-
public Mono<Document> executeCommand(final Document command) {
399+
public Mono<Document> executeCommand(Document command) {
400400
return executeCommand(command, null);
401401
}
402402

403403
/*
404404
* (non-Javadoc)
405405
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#executeCommand(org.bson.Document, com.mongodb.ReadPreference)
406406
*/
407-
public Mono<Document> executeCommand(final Document command, @Nullable ReadPreference readPreference) {
407+
public Mono<Document> executeCommand(Document command, @Nullable ReadPreference readPreference) {
408408

409409
Assert.notNull(command, "Command must not be null!");
410410

@@ -553,7 +553,7 @@ public <T> Flux<T> createFlux(ReactiveDatabaseCallback<T> callback) {
553553
* @param callback must not be {@literal null}
554554
* @return a {@link Mono} wrapping the {@link ReactiveDatabaseCallback}.
555555
*/
556-
public <T> Mono<T> createMono(final ReactiveDatabaseCallback<T> callback) {
556+
public <T> Mono<T> createMono(ReactiveDatabaseCallback<T> callback) {
557557

558558
Assert.notNull(callback, "ReactiveDatabaseCallback must not be null!");
559559

@@ -638,7 +638,7 @@ public Mono<MongoCollection<Document>> createCollection(String collectionName,
638638
* (non-Javadoc)
639639
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#getCollection(java.lang.String)
640640
*/
641-
public MongoCollection<Document> getCollection(final String collectionName) {
641+
public MongoCollection<Document> getCollection(String collectionName) {
642642
return execute((MongoDatabaseCallback<MongoCollection<Document>>) db -> db.getCollection(collectionName));
643643
}
644644

@@ -654,7 +654,7 @@ public <T> Mono<Boolean> collectionExists(Class<T> entityClass) {
654654
* (non-Javadoc)
655655
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#collectionExists(java.lang.String)
656656
*/
657-
public Mono<Boolean> collectionExists(final String collectionName) {
657+
public Mono<Boolean> collectionExists(String collectionName) {
658658
return createMono(db -> Flux.from(db.listCollectionNames()) //
659659
.filter(s -> s.equals(collectionName)) //
660660
.map(s -> true) //
@@ -673,7 +673,7 @@ public <T> Mono<Void> dropCollection(Class<T> entityClass) {
673673
* (non-Javadoc)
674674
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#dropCollection(java.lang.String)
675675
*/
676-
public Mono<Void> dropCollection(final String collectionName) {
676+
public Mono<Void> dropCollection(String collectionName) {
677677

678678
return createMono(collectionName, MongoCollection::drop).doOnSuccess(success -> {
679679
if (LOGGER.isDebugEnabled()) {
@@ -1006,15 +1006,15 @@ protected <T> Flux<GeoResult<T>> geoNear(NearQuery near, Class<?> entityClass, S
10061006
}
10071007

10081008
String collection = StringUtils.hasText(collectionName) ? collectionName : determineCollectionName(entityClass);
1009-
Document nearDbObject = near.toDocument();
1009+
Document nearDocument = near.toDocument();
10101010

10111011
Document command = new Document("geoNear", collection);
1012-
command.putAll(nearDbObject);
1012+
command.putAll(nearDocument);
10131013

10141014
return Flux.defer(() -> {
10151015

1016-
if (nearDbObject.containsKey("query")) {
1017-
Document query = (Document) nearDbObject.get("query");
1016+
if (nearDocument.containsKey("query")) {
1017+
Document query = (Document) nearDocument.get("query");
10181018
command.put("query", queryMapper.getMappedObject(query, getPersistentEntity(entityClass)));
10191019
}
10201020

@@ -1023,7 +1023,7 @@ protected <T> Flux<GeoResult<T>> geoNear(NearQuery near, Class<?> entityClass, S
10231023
entityClass, collectionName);
10241024
}
10251025

1026-
GeoNearResultDbObjectCallback<T> callback = new GeoNearResultDbObjectCallback<>(
1026+
GeoNearResultDocumentCallback<T> callback = new GeoNearResultDocumentCallback<>(
10271027
new ProjectingReadCallback<>(mongoConverter, entityClass, returnType, collectionName), near.getMetric());
10281028

10291029
return executeCommand(command, this.readPreference).flatMapMany(document -> {
@@ -1144,7 +1144,7 @@ public Mono<Long> count(Query query, Class<?> entityClass) {
11441144
* (non-Javadoc)
11451145
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#count(org.springframework.data.mongodb.core.query.Query, java.lang.String)
11461146
*/
1147-
public Mono<Long> count(final Query query, String collectionName) {
1147+
public Mono<Long> count(Query query, String collectionName) {
11481148
return count(query, null, collectionName);
11491149
}
11501150

@@ -1247,7 +1247,7 @@ protected <T> Mono<T> doInsert(String collectionName, T objectToSave, MongoWrite
12471247

12481248
maybeEmitEvent(new BeforeSaveEvent<>(initialized, dbDoc, collectionName));
12491249

1250-
Mono<T> afterInsert = insertDBObject(collectionName, dbDoc, initialized.getClass()).map(id -> {
1250+
Mono<T> afterInsert = insertDocument(collectionName, dbDoc, initialized.getClass()).map(id -> {
12511251

12521252
T saved = entity.populateIdIfNecessary(id);
12531253
maybeEmitEvent(new AfterSaveEvent<>(initialized, dbDoc, collectionName));
@@ -1293,7 +1293,7 @@ public <T> Flux<T> insertAll(Mono<? extends Collection<? extends T>> objectsToSa
12931293

12941294
protected <T> Flux<T> doInsertAll(Collection<? extends T> listToSave, MongoWriter<Object> writer) {
12951295

1296-
final Map<String, List<T>> elementsByCollection = new HashMap<>();
1296+
Map<String, List<T>> elementsByCollection = new HashMap<>();
12971297

12981298
listToSave.forEach(element -> {
12991299

@@ -1309,8 +1309,8 @@ protected <T> Flux<T> doInsertAll(Collection<? extends T> listToSave, MongoWrite
13091309
.flatMap(collectionName -> doInsertBatch(collectionName, elementsByCollection.get(collectionName), writer));
13101310
}
13111311

1312-
protected <T> Flux<T> doInsertBatch(final String collectionName, final Collection<? extends T> batchToSave,
1313-
final MongoWriter<Object> writer) {
1312+
protected <T> Flux<T> doInsertBatch(String collectionName, Collection<? extends T> batchToSave,
1313+
MongoWriter<Object> writer) {
13141314

13151315
Assert.notNull(writer, "MongoWriter must not be null!");
13161316

@@ -1330,9 +1330,9 @@ protected <T> Flux<T> doInsertBatch(final String collectionName, final Collectio
13301330

13311331
Flux<Tuple2<AdaptibleEntity<T>, Document>> insertDocuments = prepareDocuments.flatMapMany(tuples -> {
13321332

1333-
List<Document> dbObjects = tuples.stream().map(Tuple2::getT2).collect(Collectors.toList());
1333+
List<Document> documents = tuples.stream().map(Tuple2::getT2).collect(Collectors.toList());
13341334

1335-
return insertDocumentList(collectionName, dbObjects).thenMany(Flux.fromIterable(tuples));
1335+
return insertDocumentList(collectionName, documents).thenMany(Flux.fromIterable(tuples));
13361336
});
13371337

13381338
return insertDocuments.map(tuple -> {
@@ -1445,7 +1445,7 @@ protected <T> Mono<T> doSave(String collectionName, T objectToSave, MongoWriter<
14451445
});
14461446
}
14471447

1448-
protected Mono<Object> insertDBObject(final String collectionName, final Document dbDoc, final Class<?> entityClass) {
1448+
protected Mono<Object> insertDocument(String collectionName, Document dbDoc, Class<?> entityClass) {
14491449

14501450
if (LOGGER.isDebugEnabled()) {
14511451
LOGGER.debug("Inserting Document containing fields: " + dbDoc.keySet() + " in collection: " + collectionName);
@@ -1467,17 +1467,17 @@ protected Mono<Object> insertDBObject(final String collectionName, final Documen
14671467
return Flux.from(execute).last().map(success -> MappedDocument.of(document).getId());
14681468
}
14691469

1470-
protected Flux<ObjectId> insertDocumentList(final String collectionName, final List<Document> dbDocList) {
1470+
protected Flux<ObjectId> insertDocumentList(String collectionName, List<Document> dbDocList) {
14711471

14721472
if (dbDocList.isEmpty()) {
14731473
return Flux.empty();
14741474
}
14751475

14761476
if (LOGGER.isDebugEnabled()) {
1477-
LOGGER.debug("Inserting list of DBObjects containing " + dbDocList.size() + " items");
1477+
LOGGER.debug("Inserting list of Documents containing " + dbDocList.size() + " items");
14781478
}
14791479

1480-
final List<Document> documents = new ArrayList<>();
1480+
List<Document> documents = new ArrayList<>();
14811481

14821482
return execute(collectionName, collection -> {
14831483

@@ -1509,8 +1509,7 @@ private MongoCollection<Document> prepareCollection(MongoCollection<Document> co
15091509
return collectionToUse;
15101510
}
15111511

1512-
protected Mono<Object> saveDocument(final String collectionName, final Document document,
1513-
final Class<?> entityClass) {
1512+
protected Mono<Object> saveDocument(String collectionName, Document document, Class<?> entityClass) {
15141513

15151514
if (LOGGER.isDebugEnabled()) {
15161515
LOGGER.debug("Saving Document containing fields: " + document.keySet());
@@ -1572,7 +1571,7 @@ public Mono<UpdateResult> updateFirst(Query query, Update update, Class<?> entit
15721571
* (non-Javadoc)
15731572
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#updateFirst(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.String)
15741573
*/
1575-
public Mono<UpdateResult> updateFirst(final Query query, final Update update, final String collectionName) {
1574+
public Mono<UpdateResult> updateFirst(Query query, Update update, String collectionName) {
15761575
return doUpdate(collectionName, query, update, null, false, false);
15771576
}
15781577

@@ -1596,21 +1595,20 @@ public Mono<UpdateResult> updateMulti(Query query, Update update, Class<?> entit
15961595
* (non-Javadoc)
15971596
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#updateMulti(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.String)
15981597
*/
1599-
public Mono<UpdateResult> updateMulti(final Query query, final Update update, String collectionName) {
1598+
public Mono<UpdateResult> updateMulti(Query query, Update update, String collectionName) {
16001599
return doUpdate(collectionName, query, update, null, false, true);
16011600
}
16021601

16031602
/*
16041603
* (non-Javadoc)
16051604
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#updateMulti(org.springframework.data.mongodb.core.query.Query, org.springframework.data.mongodb.core.query.Update, java.lang.Class, java.lang.String)
16061605
*/
1607-
public Mono<UpdateResult> updateMulti(final Query query, final Update update, Class<?> entityClass,
1608-
String collectionName) {
1606+
public Mono<UpdateResult> updateMulti(Query query, Update update, Class<?> entityClass, String collectionName) {
16091607
return doUpdate(collectionName, query, update, entityClass, false, true);
16101608
}
16111609

1612-
protected Mono<UpdateResult> doUpdate(final String collectionName, Query query, @Nullable Update update,
1613-
@Nullable Class<?> entityClass, final boolean upsert, final boolean multi) {
1610+
protected Mono<UpdateResult> doUpdate(String collectionName, Query query, @Nullable Update update,
1611+
@Nullable Class<?> entityClass, boolean upsert, boolean multi) {
16141612

16151613
MongoPersistentEntity<?> entity = entityClass == null ? null : getPersistentEntity(entityClass);
16161614

@@ -1656,7 +1654,7 @@ protected Mono<UpdateResult> doUpdate(final String collectionName, Query query,
16561654
: queryMapper.getMappedObject(query.getQueryObject(), entity);
16571655
Document updateObj = update == null ? new Document()
16581656
: updateMapper.getMappedObject(update.getUpdateObject(), entity);
1659-
if (dbObjectContainsVersionProperty(queryObj, entity))
1657+
if (containsVersionProperty(queryObj, entity))
16601658
throw new OptimisticLockingFailureException("Optimistic lock exception on saving entity: "
16611659
+ updateObj.toString() + " to collection " + collectionName);
16621660
}
@@ -1676,8 +1674,7 @@ private void increaseVersionForUpdateIfNecessary(@Nullable MongoPersistentEntity
16761674
}
16771675
}
16781676

1679-
private boolean dbObjectContainsVersionProperty(Document document,
1680-
@Nullable MongoPersistentEntity<?> persistentEntity) {
1677+
private boolean containsVersionProperty(Document document, @Nullable MongoPersistentEntity<?> persistentEntity) {
16811678

16821679
if (persistentEntity == null || !persistentEntity.hasVersionProperty()) {
16831680
return false;
@@ -1780,8 +1777,8 @@ protected <T> Mono<DeleteResult> doRemove(String collectionName, Query query, @N
17801777

17811778
Assert.hasText(collectionName, "Collection name must not be null or empty!");
17821779

1783-
final Document queryObject = query.getQueryObject();
1784-
final MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
1780+
Document queryObject = query.getQueryObject();
1781+
MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
17851782

17861783
return execute(collectionName, collection -> {
17871784

@@ -1792,7 +1789,7 @@ protected <T> Mono<DeleteResult> doRemove(String collectionName, Query query, @N
17921789
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.REMOVE, collectionName, entityClass,
17931790
null, removeQuey);
17941791

1795-
final DeleteOptions deleteOptions = new DeleteOptions();
1792+
DeleteOptions deleteOptions = new DeleteOptions();
17961793
query.getCollation().map(Collation::toMongoCollation).ifPresent(deleteOptions::collation);
17971794

17981795
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
@@ -2148,8 +2145,8 @@ protected <T> Flux<T> doFindAndDelete(String collectionName, Query query, Class<
21482145
* @param collectionOptions
21492146
* @return the collection that was created
21502147
*/
2151-
protected Mono<MongoCollection<Document>> doCreateCollection(final String collectionName,
2152-
final CreateCollectionOptions collectionOptions) {
2148+
protected Mono<MongoCollection<Document>> doCreateCollection(String collectionName,
2149+
CreateCollectionOptions collectionOptions) {
21532150

21542151
return createMono(db -> db.createCollection(collectionName, collectionOptions)).map(success -> {
21552152

@@ -2586,17 +2583,6 @@ private MongoPersistentEntity<?> getPersistentEntity(@Nullable Class<?> type) {
25862583
return type == null ? null : mappingContext.getPersistentEntity(type);
25872584
}
25882585

2589-
@Nullable
2590-
private MongoPersistentProperty getIdPropertyFor(@Nullable Class<?> type) {
2591-
2592-
if (type == null) {
2593-
return null;
2594-
}
2595-
2596-
MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(type);
2597-
return persistentEntity != null ? persistentEntity.getIdProperty() : null;
2598-
}
2599-
26002586
private <T> String determineEntityCollectionName(@Nullable T obj) {
26012587

26022588
if (null != obj) {
@@ -2689,6 +2675,7 @@ public Publisher<Document> doInCollection(MongoCollection<Document> collection)
26892675
*
26902676
* @author Mark Paluch
26912677
*/
2678+
@RequiredArgsConstructor
26922679
private static class FindCallback implements ReactiveCollectionQueryCallback<Document> {
26932680

26942681
private final @Nullable Document query;
@@ -2698,11 +2685,6 @@ private static class FindCallback implements ReactiveCollectionQueryCallback<Doc
26982685
this(query, null);
26992686
}
27002687

2701-
FindCallback(Document query, Document fields) {
2702-
this.query = query;
2703-
this.fields = fields;
2704-
}
2705-
27062688
@Override
27072689
public FindPublisher<Document> doInCollection(MongoCollection<Document> collection) {
27082690

@@ -2756,6 +2738,7 @@ public Publisher<Document> doInCollection(MongoCollection<Document> collection)
27562738
/**
27572739
* @author Mark Paluch
27582740
*/
2741+
@RequiredArgsConstructor
27592742
private static class FindAndModifyCallback implements ReactiveCollectionCallback<Document> {
27602743

27612744
private final Document query;
@@ -2764,16 +2747,6 @@ private static class FindAndModifyCallback implements ReactiveCollectionCallback
27642747
private final Document update;
27652748
private final FindAndModifyOptions options;
27662749

2767-
FindAndModifyCallback(Document query, Document fields, Document sort, Document update,
2768-
FindAndModifyOptions options) {
2769-
2770-
this.query = query;
2771-
this.fields = fields;
2772-
this.sort = sort;
2773-
this.update = update;
2774-
this.options = options;
2775-
}
2776-
27772750
@Override
27782751
public Publisher<Document> doInCollection(MongoCollection<Document> collection)
27792752
throws MongoException, DataAccessException {
@@ -2981,18 +2954,18 @@ public T doWith(@Nullable Document object) {
29812954
*
29822955
* @author Mark Paluch
29832956
*/
2984-
static class GeoNearResultDbObjectCallback<T> implements DocumentCallback<GeoResult<T>> {
2957+
static class GeoNearResultDocumentCallback<T> implements DocumentCallback<GeoResult<T>> {
29852958

29862959
private final DocumentCallback<T> delegate;
29872960
private final Metric metric;
29882961

29892962
/**
2990-
* Creates a new {@link GeoNearResultDbObjectCallback} using the given {@link DocumentCallback} delegate for
2963+
* Creates a new {@link GeoNearResultDocumentCallback} using the given {@link DocumentCallback} delegate for
29912964
* {@link GeoResult} content unmarshalling.
29922965
*
29932966
* @param delegate must not be {@literal null}.
29942967
*/
2995-
GeoNearResultDbObjectCallback(DocumentCallback<T> delegate, Metric metric) {
2968+
GeoNearResultDocumentCallback(DocumentCallback<T> delegate, Metric metric) {
29962969

29972970
Assert.notNull(delegate, "DocumentCallback must not be null!");
29982971

@@ -3105,7 +3078,7 @@ public <T> FindPublisher<T> prepare(FindPublisher<T> findPublisher) {
31053078
}
31063079
}
31073080

3108-
private static List<? extends Document> toDocuments(final Collection<? extends Document> documents) {
3081+
private static List<? extends Document> toDocuments(Collection<? extends Document> documents) {
31093082
return new ArrayList<>(documents);
31103083
}
31113084

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
*
5050
* @author Thomas Darimont
5151
* @author Oliver Gierke
52+
* @author Mark Paluch
5253
*/
5354
@RunWith(SpringJUnit4ClassRunner.class)
5455
@ContextConfiguration
@@ -159,19 +160,19 @@ private <T extends AuditablePerson> void verifyAuditingViaVersionProperty(T inst
159160
assertThat(versionExtractor.apply(instance)).isEqualTo(expectedValues[0]);
160161
assertThat(entity.isNew(instance)).isTrue();
161162

162-
instance = auditablePersonRepository.save(instance);
163+
instance = persister.apply(instance);
163164

164165
assertThat(versionExtractor.apply(instance)).isEqualTo(expectedValues[1]);
165166
assertThat(entity.isNew(instance)).isFalse();
166167

167-
instance = auditablePersonRepository.save(instance);
168+
instance = persister.apply(instance);
168169

169170
assertThat(versionExtractor.apply(instance)).isEqualTo(expectedValues[2]);
170171
assertThat(entity.isNew(instance)).isFalse();
171172
}
172173

173174
@Repository
174-
static interface AuditablePersonRepository extends MongoRepository<AuditablePerson, String> {}
175+
interface AuditablePersonRepository extends MongoRepository<AuditablePerson, String> {}
175176

176177
@Configuration
177178
@EnableMongoRepositories

0 commit comments

Comments
 (0)