Skip to content

Commit 4a1a9a7

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 adf16bb commit 4a1a9a7

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
@@ -395,15 +395,15 @@ public Mono<Document> executeCommand(String jsonCommand) {
395395
* (non-Javadoc)
396396
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#executeCommand(org.bson.Document)
397397
*/
398-
public Mono<Document> executeCommand(final Document command) {
398+
public Mono<Document> executeCommand(Document command) {
399399
return executeCommand(command, null);
400400
}
401401

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

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

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

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

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

@@ -653,7 +653,7 @@ public <T> Mono<Boolean> collectionExists(Class<T> entityClass) {
653653
* (non-Javadoc)
654654
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#collectionExists(java.lang.String)
655655
*/
656-
public Mono<Boolean> collectionExists(final String collectionName) {
656+
public Mono<Boolean> collectionExists(String collectionName) {
657657
return createMono(db -> Flux.from(db.listCollectionNames()) //
658658
.filter(s -> s.equals(collectionName)) //
659659
.map(s -> true) //
@@ -672,7 +672,7 @@ public <T> Mono<Void> dropCollection(Class<T> entityClass) {
672672
* (non-Javadoc)
673673
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#dropCollection(java.lang.String)
674674
*/
675-
public Mono<Void> dropCollection(final String collectionName) {
675+
public Mono<Void> dropCollection(String collectionName) {
676676

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

10071007
String collection = StringUtils.hasText(collectionName) ? collectionName : determineCollectionName(entityClass);
1008-
Document nearDbObject = near.toDocument();
1008+
Document nearDocument = near.toDocument();
10091009

10101010
Document command = new Document("geoNear", collection);
1011-
command.putAll(nearDbObject);
1011+
command.putAll(nearDocument);
10121012

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

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

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

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

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

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

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

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

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

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

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

12971297
listToSave.forEach(element -> {
12981298

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

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

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

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

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

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

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

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

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

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

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

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

14751475
if (LOGGER.isDebugEnabled()) {
1476-
LOGGER.debug("Inserting list of DBObjects containing " + dbDocList.size() + " items");
1476+
LOGGER.debug("Inserting list of Documents containing " + dbDocList.size() + " items");
14771477
}
14781478

1479-
final List<Document> documents = new ArrayList<>();
1479+
List<Document> documents = new ArrayList<>();
14801480

14811481
return execute(collectionName, collection -> {
14821482

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

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

15141513
if (LOGGER.isDebugEnabled()) {
15151514
LOGGER.debug("Saving Document containing fields: " + document.keySet());
@@ -1571,7 +1570,7 @@ public Mono<UpdateResult> updateFirst(Query query, Update update, Class<?> entit
15711570
* (non-Javadoc)
15721571
* @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)
15731572
*/
1574-
public Mono<UpdateResult> updateFirst(final Query query, final Update update, final String collectionName) {
1573+
public Mono<UpdateResult> updateFirst(Query query, Update update, String collectionName) {
15751574
return doUpdate(collectionName, query, update, null, false, false);
15761575
}
15771576

@@ -1595,21 +1594,20 @@ public Mono<UpdateResult> updateMulti(Query query, Update update, Class<?> entit
15951594
* (non-Javadoc)
15961595
* @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)
15971596
*/
1598-
public Mono<UpdateResult> updateMulti(final Query query, final Update update, String collectionName) {
1597+
public Mono<UpdateResult> updateMulti(Query query, Update update, String collectionName) {
15991598
return doUpdate(collectionName, query, update, null, false, true);
16001599
}
16011600

16021601
/*
16031602
* (non-Javadoc)
16041603
* @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)
16051604
*/
1606-
public Mono<UpdateResult> updateMulti(final Query query, final Update update, Class<?> entityClass,
1607-
String collectionName) {
1605+
public Mono<UpdateResult> updateMulti(Query query, Update update, Class<?> entityClass, String collectionName) {
16081606
return doUpdate(collectionName, query, update, entityClass, false, true);
16091607
}
16101608

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

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

@@ -1655,7 +1653,7 @@ protected Mono<UpdateResult> doUpdate(final String collectionName, Query query,
16551653
: queryMapper.getMappedObject(query.getQueryObject(), entity);
16561654
Document updateObj = update == null ? new Document()
16571655
: updateMapper.getMappedObject(update.getUpdateObject(), entity);
1658-
if (dbObjectContainsVersionProperty(queryObj, entity))
1656+
if (containsVersionProperty(queryObj, entity))
16591657
throw new OptimisticLockingFailureException("Optimistic lock exception on saving entity: "
16601658
+ updateObj.toString() + " to collection " + collectionName);
16611659
}
@@ -1675,8 +1673,7 @@ private void increaseVersionForUpdateIfNecessary(@Nullable MongoPersistentEntity
16751673
}
16761674
}
16771675

1678-
private boolean dbObjectContainsVersionProperty(Document document,
1679-
@Nullable MongoPersistentEntity<?> persistentEntity) {
1676+
private boolean containsVersionProperty(Document document, @Nullable MongoPersistentEntity<?> persistentEntity) {
16801677

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

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

1782-
final Document queryObject = query.getQueryObject();
1783-
final MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
1779+
Document queryObject = query.getQueryObject();
1780+
MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
17841781

17851782
return execute(collectionName, collection -> {
17861783

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

1794-
final DeleteOptions deleteOptions = new DeleteOptions();
1791+
DeleteOptions deleteOptions = new DeleteOptions();
17951792
query.getCollation().map(Collation::toMongoCollation).ifPresent(deleteOptions::collation);
17961793

17971794
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
@@ -2146,8 +2143,8 @@ protected <T> Flux<T> doFindAndDelete(String collectionName, Query query, Class<
21462143
* @param collectionOptions
21472144
* @return the collection that was created
21482145
*/
2149-
protected Mono<MongoCollection<Document>> doCreateCollection(final String collectionName,
2150-
final CreateCollectionOptions collectionOptions) {
2146+
protected Mono<MongoCollection<Document>> doCreateCollection(String collectionName,
2147+
CreateCollectionOptions collectionOptions) {
21512148

21522149
return createMono(db -> db.createCollection(collectionName, collectionOptions)).map(success -> {
21532150

@@ -2584,17 +2581,6 @@ private MongoPersistentEntity<?> getPersistentEntity(@Nullable Class<?> type) {
25842581
return type == null ? null : mappingContext.getPersistentEntity(type);
25852582
}
25862583

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

26002586
if (null != obj) {
@@ -2687,6 +2673,7 @@ public Publisher<Document> doInCollection(MongoCollection<Document> collection)
26872673
*
26882674
* @author Mark Paluch
26892675
*/
2676+
@RequiredArgsConstructor
26902677
private static class FindCallback implements ReactiveCollectionQueryCallback<Document> {
26912678

26922679
private final @Nullable Document query;
@@ -2696,11 +2683,6 @@ private static class FindCallback implements ReactiveCollectionQueryCallback<Doc
26962683
this(query, null);
26972684
}
26982685

2699-
FindCallback(Document query, Document fields) {
2700-
this.query = query;
2701-
this.fields = fields;
2702-
}
2703-
27042686
@Override
27052687
public FindPublisher<Document> doInCollection(MongoCollection<Document> collection) {
27062688

@@ -2754,6 +2736,7 @@ public Publisher<Document> doInCollection(MongoCollection<Document> collection)
27542736
/**
27552737
* @author Mark Paluch
27562738
*/
2739+
@RequiredArgsConstructor
27572740
private static class FindAndModifyCallback implements ReactiveCollectionCallback<Document> {
27582741

27592742
private final Document query;
@@ -2762,16 +2745,6 @@ private static class FindAndModifyCallback implements ReactiveCollectionCallback
27622745
private final Document update;
27632746
private final FindAndModifyOptions options;
27642747

2765-
FindAndModifyCallback(Document query, Document fields, Document sort, Document update,
2766-
FindAndModifyOptions options) {
2767-
2768-
this.query = query;
2769-
this.fields = fields;
2770-
this.sort = sort;
2771-
this.update = update;
2772-
this.options = options;
2773-
}
2774-
27752748
@Override
27762749
public Publisher<Document> doInCollection(MongoCollection<Document> collection)
27772750
throws MongoException, DataAccessException {
@@ -2979,18 +2952,18 @@ public T doWith(@Nullable Document object) {
29792952
*
29802953
* @author Mark Paluch
29812954
*/
2982-
static class GeoNearResultDbObjectCallback<T> implements DocumentCallback<GeoResult<T>> {
2955+
static class GeoNearResultDocumentCallback<T> implements DocumentCallback<GeoResult<T>> {
29832956

29842957
private final DocumentCallback<T> delegate;
29852958
private final Metric metric;
29862959

29872960
/**
2988-
* Creates a new {@link GeoNearResultDbObjectCallback} using the given {@link DocumentCallback} delegate for
2961+
* Creates a new {@link GeoNearResultDocumentCallback} using the given {@link DocumentCallback} delegate for
29892962
* {@link GeoResult} content unmarshalling.
29902963
*
29912964
* @param delegate must not be {@literal null}.
29922965
*/
2993-
GeoNearResultDbObjectCallback(DocumentCallback<T> delegate, Metric metric) {
2966+
GeoNearResultDocumentCallback(DocumentCallback<T> delegate, Metric metric) {
29942967

29952968
Assert.notNull(delegate, "DocumentCallback must not be null!");
29962969

@@ -3103,7 +3076,7 @@ public <T> FindPublisher<T> prepare(FindPublisher<T> findPublisher) {
31033076
}
31043077
}
31053078

3106-
private static List<? extends Document> toDocuments(final Collection<? extends Document> documents) {
3079+
private static List<? extends Document> toDocuments(Collection<? extends Document> documents) {
31073080
return new ArrayList<>(documents);
31083081
}
31093082

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)