Polishing.
Reformat code. Suppress warnings in tests. Original pull request: #4541 See #4495
This commit is contained in:
@@ -746,6 +746,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
public <T> Mono<Void> dropCollection(Class<T> entityClass) {
|
||||
return dropCollection(getCollectionName(entityClass));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> dropCollection(String collectionName) {
|
||||
|
||||
@@ -884,7 +885,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
Assert.notNull(targetClass, "Target type must not be null");
|
||||
|
||||
EntityProjection<T, ?> projection = operations.introspectProjection(targetClass, sourceClass);
|
||||
ProjectingReadCallback<?,T> callback = new ProjectingReadCallback<>(mongoConverter, projection, collectionName);
|
||||
ProjectingReadCallback<?, T> callback = new ProjectingReadCallback<>(mongoConverter, projection, collectionName);
|
||||
int limit = query.isLimited() ? query.getLimit() + 1 : Integer.MAX_VALUE;
|
||||
|
||||
if (query.hasKeyset()) {
|
||||
@@ -894,7 +895,8 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
|
||||
Mono<List<T>> result = doFind(collectionName, ReactiveCollectionPreparerDelegate.of(query),
|
||||
keysetPaginationQuery.query(), keysetPaginationQuery.fields(), sourceClass,
|
||||
new QueryFindPublisherPreparer(query, keysetPaginationQuery.sort(), limit, 0, sourceClass), callback).collectList();
|
||||
new QueryFindPublisherPreparer(query, keysetPaginationQuery.sort(), limit, 0, sourceClass), callback)
|
||||
.collectList();
|
||||
|
||||
return result.map(it -> ScrollUtils.createWindow(query, it, sourceClass, operations));
|
||||
}
|
||||
@@ -1976,11 +1978,12 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
return replace(query, (Class<T>) ClassUtils.getUserClass(replacement), replacement, options, collectionName);
|
||||
}
|
||||
|
||||
protected <S,T> Mono<UpdateResult> replace(Query query, Class<S> entityType, T replacement, ReplaceOptions options,
|
||||
protected <S, T> Mono<UpdateResult> replace(Query query, Class<S> entityType, T replacement, ReplaceOptions options,
|
||||
String collectionName) {
|
||||
|
||||
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityType);
|
||||
UpdateContext updateContext = queryOperations.replaceSingleContext(query, operations.forEntity(replacement).toMappedDocument(this.mongoConverter), options.isUpsert());
|
||||
UpdateContext updateContext = queryOperations.replaceSingleContext(query,
|
||||
operations.forEntity(replacement).toMappedDocument(this.mongoConverter), options.isUpsert());
|
||||
|
||||
return createMono(collectionName, collection -> {
|
||||
|
||||
@@ -1991,9 +1994,10 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
|
||||
MongoCollection<Document> collectionToUse = createCollectionPreparer(query, action).prepare(collection);
|
||||
|
||||
return collectionToUse.replaceOne(updateContext.getMappedQuery(entity), mappedUpdate, updateContext.getReplaceOptions(entityType, it -> {
|
||||
it.upsert(options.isUpsert());
|
||||
}));
|
||||
return collectionToUse.replaceOne(updateContext.getMappedQuery(entity), mappedUpdate,
|
||||
updateContext.getReplaceOptions(entityType, it -> {
|
||||
it.upsert(options.isUpsert());
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2050,8 +2054,8 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
publisher = options.getCollation().map(Collation::toMongoCollation).map(publisher::collation)
|
||||
.orElse(publisher);
|
||||
publisher = options.getResumeBsonTimestamp().map(publisher::startAtOperationTime).orElse(publisher);
|
||||
|
||||
if(options.getFullDocumentBeforeChangeLookup().isPresent()) {
|
||||
|
||||
if (options.getFullDocumentBeforeChangeLookup().isPresent()) {
|
||||
publisher = publisher.fullDocumentBeforeChange(options.getFullDocumentBeforeChangeLookup().get());
|
||||
}
|
||||
return publisher.fullDocument(options.getFullDocumentLookup().orElse(fullDocument));
|
||||
@@ -2675,7 +2679,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
|
||||
if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)) {
|
||||
if (wc == null || wc.getWObject() == null
|
||||
|| (wc.getWObject() instanceof Number concern && concern.intValue() < 1)) {
|
||||
|| (wc.getWObject()instanceof Number concern && concern.intValue() < 1)) {
|
||||
return WriteConcern.ACKNOWLEDGED;
|
||||
}
|
||||
}
|
||||
@@ -3253,8 +3257,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
|
||||
HintFunction hintFunction = HintFunction.from(query.getHint());
|
||||
Meta meta = query.getMeta();
|
||||
if (skip <= 0 && limit <= 0 && ObjectUtils.isEmpty(sortObject) && hintFunction.isEmpty()
|
||||
&& !meta.hasValues()) {
|
||||
if (skip <= 0 && limit <= 0 && ObjectUtils.isEmpty(sortObject) && hintFunction.isEmpty() && !meta.hasValues()) {
|
||||
return findPublisherToUse;
|
||||
}
|
||||
|
||||
|
||||
@@ -91,9 +91,9 @@ class ChangeStreamTask extends CursorReadingTask<ChangeStreamDocument<Document>,
|
||||
BsonTimestamp startAt = null;
|
||||
boolean resumeAfter = true;
|
||||
|
||||
if (options instanceof ChangeStreamRequest.ChangeStreamRequestOptions changeStreamRequestOptions) {
|
||||
if (options instanceof ChangeStreamRequest.ChangeStreamRequestOptions requestOptions) {
|
||||
|
||||
ChangeStreamOptions changeStreamOptions = changeStreamRequestOptions.getChangeStreamOptions();
|
||||
ChangeStreamOptions changeStreamOptions = requestOptions.getChangeStreamOptions();
|
||||
filter = prepareFilter(template, changeStreamOptions);
|
||||
|
||||
if (changeStreamOptions.getFilter().isPresent()) {
|
||||
@@ -115,9 +115,7 @@ class ChangeStreamTask extends CursorReadingTask<ChangeStreamDocument<Document>,
|
||||
.orElseGet(() -> ClassUtils.isAssignable(Document.class, targetType) ? FullDocument.DEFAULT
|
||||
: FullDocument.UPDATE_LOOKUP);
|
||||
|
||||
if(changeStreamOptions.getFullDocumentBeforeChangeLookup().isPresent()) {
|
||||
fullDocumentBeforeChange = changeStreamOptions.getFullDocumentBeforeChangeLookup().get();
|
||||
}
|
||||
fullDocumentBeforeChange = changeStreamOptions.getFullDocumentBeforeChangeLookup().orElse(null);
|
||||
|
||||
startAt = changeStreamOptions.getResumeBsonTimestamp().orElse(null);
|
||||
}
|
||||
|
||||
@@ -367,7 +367,8 @@ public class ReactiveMongoTemplateUnitTests {
|
||||
@Test // GH-3218
|
||||
void updateUsesHintDocumentFromQuery() {
|
||||
|
||||
template.updateFirst(new Query().withHint("{ firstname : 1 }"), new Update().set("spring", "data"), Person.class).subscribe();
|
||||
template.updateFirst(new Query().withHint("{ firstname : 1 }"), new Update().set("spring", "data"), Person.class)
|
||||
.subscribe();
|
||||
|
||||
ArgumentCaptor<UpdateOptions> options = ArgumentCaptor.forClass(UpdateOptions.class);
|
||||
verify(collection).updateOne(any(Bson.class), any(Bson.class), options.capture());
|
||||
@@ -1615,11 +1616,11 @@ public class ReactiveMongoTemplateUnitTests {
|
||||
when(changeStreamPublisher.fullDocument(any())).thenReturn(changeStreamPublisher);
|
||||
when(changeStreamPublisher.fullDocumentBeforeChange(any())).thenReturn(changeStreamPublisher);
|
||||
|
||||
template
|
||||
.changeStream("database", "collection", ChangeStreamOptions.builder().fullDocumentBeforeChangeLookup(FullDocumentBeforeChange.REQUIRED).build(), Object.class)
|
||||
.subscribe();
|
||||
ChangeStreamOptions options = ChangeStreamOptions.builder()
|
||||
.fullDocumentBeforeChangeLookup(FullDocumentBeforeChange.REQUIRED).build();
|
||||
template.changeStream("database", "collection", options, Object.class).subscribe();
|
||||
|
||||
verify(changeStreamPublisher).fullDocumentBeforeChange(eq(FullDocumentBeforeChange.REQUIRED));
|
||||
verify(changeStreamPublisher).fullDocumentBeforeChange(FullDocumentBeforeChange.REQUIRED);
|
||||
|
||||
}
|
||||
|
||||
@@ -1651,7 +1652,8 @@ public class ReactiveMongoTemplateUnitTests {
|
||||
@Test // GH-4462
|
||||
void replaceShouldUpsert() {
|
||||
|
||||
template.replace(new BasicQuery("{}"), new MongoTemplateUnitTests.Sith(), org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
|
||||
template.replace(new BasicQuery("{}"), new MongoTemplateUnitTests.Sith(),
|
||||
org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
|
||||
|
||||
ArgumentCaptor<com.mongodb.client.model.ReplaceOptions> options = ArgumentCaptor
|
||||
.forClass(com.mongodb.client.model.ReplaceOptions.class);
|
||||
@@ -1663,7 +1665,8 @@ public class ReactiveMongoTemplateUnitTests {
|
||||
@Test // GH-4462
|
||||
void replaceShouldUseDefaultCollationWhenPresent() {
|
||||
|
||||
template.replace(new BasicQuery("{}"), new MongoTemplateUnitTests.Sith(), org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions()).subscribe();
|
||||
template.replace(new BasicQuery("{}"), new MongoTemplateUnitTests.Sith(),
|
||||
org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions()).subscribe();
|
||||
|
||||
ArgumentCaptor<com.mongodb.client.model.ReplaceOptions> options = ArgumentCaptor
|
||||
.forClass(com.mongodb.client.model.ReplaceOptions.class);
|
||||
@@ -1675,7 +1678,8 @@ public class ReactiveMongoTemplateUnitTests {
|
||||
@Test // GH-4462
|
||||
void replaceShouldUseHintIfPresent() {
|
||||
|
||||
template.replace(new BasicQuery("{}").withHint("index-to-use"), new MongoTemplateUnitTests.Sith(), org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
|
||||
template.replace(new BasicQuery("{}").withHint("index-to-use"), new MongoTemplateUnitTests.Sith(),
|
||||
org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
|
||||
|
||||
ArgumentCaptor<com.mongodb.client.model.ReplaceOptions> options = ArgumentCaptor
|
||||
.forClass(com.mongodb.client.model.ReplaceOptions.class);
|
||||
@@ -1695,7 +1699,8 @@ public class ReactiveMongoTemplateUnitTests {
|
||||
}
|
||||
});
|
||||
|
||||
template.replace(new BasicQuery("{}").withHint("index-to-use"), new Sith(), org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
|
||||
template.replace(new BasicQuery("{}").withHint("index-to-use"), new Sith(),
|
||||
org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
|
||||
|
||||
verify(collection).withWriteConcern(eq(WriteConcern.UNACKNOWLEDGED));
|
||||
}
|
||||
|
||||
@@ -41,18 +41,21 @@ import com.mongodb.client.model.changestream.ChangeStreamDocument;
|
||||
import com.mongodb.client.model.changestream.FullDocumentBeforeChange;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ChangeStreamTask}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Myroslav Kosinskyi
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
class ChangeStreamTaskUnitTests {
|
||||
|
||||
ChangeStreamTask task;
|
||||
@Mock MongoTemplate template;
|
||||
@Mock MongoDatabase mongoDatabase;
|
||||
@Mock MongoCollection<Document> mongoCollection;
|
||||
@Mock ChangeStreamIterable<Document> changeStreamIterable;
|
||||
private MongoConverter converter;
|
||||
|
||||
MongoConverter converter;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
@@ -64,9 +67,7 @@ class ChangeStreamTaskUnitTests {
|
||||
when(template.getDb()).thenReturn(mongoDatabase);
|
||||
|
||||
when(mongoDatabase.getCollection(any())).thenReturn(mongoCollection);
|
||||
|
||||
when(mongoCollection.watch(eq(Document.class))).thenReturn(changeStreamIterable);
|
||||
|
||||
when(changeStreamIterable.fullDocument(any())).thenReturn(changeStreamIterable);
|
||||
}
|
||||
|
||||
@@ -84,7 +85,7 @@ class ChangeStreamTaskUnitTests {
|
||||
|
||||
initTask(request, Document.class);
|
||||
|
||||
verify(changeStreamIterable).resumeAfter(eq(resumeToken));
|
||||
verify(changeStreamIterable).resumeAfter(resumeToken);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2258
|
||||
@@ -102,7 +103,7 @@ class ChangeStreamTaskUnitTests {
|
||||
|
||||
initTask(request, Document.class);
|
||||
|
||||
verify(changeStreamIterable).resumeAfter(eq(resumeToken));
|
||||
verify(changeStreamIterable).resumeAfter(resumeToken);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2258
|
||||
@@ -120,7 +121,7 @@ class ChangeStreamTaskUnitTests {
|
||||
|
||||
initTask(request, Document.class);
|
||||
|
||||
verify(changeStreamIterable).startAfter(eq(resumeToken));
|
||||
verify(changeStreamIterable).startAfter(resumeToken);
|
||||
}
|
||||
|
||||
@Test // GH-4495
|
||||
@@ -136,7 +137,7 @@ class ChangeStreamTaskUnitTests {
|
||||
|
||||
initTask(request, Document.class);
|
||||
|
||||
verify(changeStreamIterable).fullDocumentBeforeChange(eq(FullDocumentBeforeChange.REQUIRED));
|
||||
verify(changeStreamIterable).fullDocumentBeforeChange(FullDocumentBeforeChange.REQUIRED);
|
||||
}
|
||||
|
||||
private MongoCursor<ChangeStreamDocument<Document>> initTask(ChangeStreamRequest request, Class<?> targetType) {
|
||||
|
||||
Reference in New Issue
Block a user