Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Reformat code. Suppress warnings in tests.

Original pull request: #4541
See #4495
  • Loading branch information
mp911de committed Nov 2, 2023
1 parent d46af55 commit bc18b52
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ public Mono<Boolean> collectionExists(String collectionName) {
public <T> Mono<Void> dropCollection(Class<T> entityClass) {
return dropCollection(getCollectionName(entityClass));
}

@Override
public Mono<Void> dropCollection(String collectionName) {

Expand Down Expand Up @@ -874,7 +875,7 @@ <T> Mono<Window<T>> doScroll(Query query, Class<?> sourceClass, Class<T> targetC
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()) {
Expand All @@ -884,7 +885,8 @@ <T> Mono<Window<T>> doScroll(Query query, Class<?> sourceClass, Class<T> targetC

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));
}
Expand Down Expand Up @@ -2012,8 +2014,8 @@ public <T> Flux<ChangeStreamEvent<T>> changeStream(@Nullable String database, @N
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));
Expand Down Expand Up @@ -2622,7 +2624,7 @@ private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc

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;
}
}
Expand Down Expand Up @@ -3200,8 +3202,7 @@ public FindPublisher<Document> prepare(FindPublisher<Document> findPublisher) {

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ protected MongoCursor<ChangeStreamDocument<Document>> initCursor(MongoTemplate t
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()) {
Expand All @@ -115,9 +115,7 @@ protected MongoCursor<ChangeStreamDocument<Document>> initCursor(MongoTemplate t
.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);
}
Expand Down Expand Up @@ -158,7 +156,7 @@ protected MongoCursor<ChangeStreamDocument<Document>> initCursor(MongoTemplate t
}

iterable = iterable.fullDocument(fullDocument);
if(fullDocumentBeforeChange != null) {
if (fullDocumentBeforeChange != null) {
iterable = iterable.fullDocumentBeforeChange(fullDocumentBeforeChange);
}

Expand All @@ -180,7 +178,8 @@ List<Document> prepareFilter(MongoTemplate template, ChangeStreamOptions options
template.getConverter().getMappingContext(), queryMapper)
: Aggregation.DEFAULT_CONTEXT;

return aggregation.toPipeline(new PrefixingDelegatingAggregationOperationContext(context, "fullDocument", denylist));
return aggregation
.toPipeline(new PrefixingDelegatingAggregationOperationContext(context, "fullDocument", denylist));
}

if (filter instanceof List) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.util.BsonUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
Expand Down Expand Up @@ -93,6 +92,7 @@
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.timeseries.Granularity;
import org.springframework.data.mongodb.util.BsonUtils;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.lang.Nullable;
import org.springframework.test.util.ReflectionTestUtils;
Expand All @@ -101,7 +101,6 @@
import com.mongodb.MongoClientSettings;
import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.client.model.CountOptions;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.client.model.DeleteOptions;
Expand Down Expand Up @@ -369,7 +368,8 @@ void updateUsesHintStringFromQuery() {
@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());
Expand Down Expand Up @@ -1617,11 +1617,11 @@ void changeStreamOptionFullDocumentBeforeChangeShouldBeApplied() {
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);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,21 @@
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() {
Expand All @@ -64,9 +67,7 @@ void setUp() {
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);
}

Expand All @@ -84,7 +85,7 @@ void shouldNotBreakLovelaceBehavior() {

initTask(request, Document.class);

verify(changeStreamIterable).resumeAfter(eq(resumeToken));
verify(changeStreamIterable).resumeAfter(resumeToken);
}

@Test // DATAMONGO-2258
Expand All @@ -102,7 +103,7 @@ void shouldApplyResumeAfterToChangeStream() {

initTask(request, Document.class);

verify(changeStreamIterable).resumeAfter(eq(resumeToken));
verify(changeStreamIterable).resumeAfter(resumeToken);
}

@Test // DATAMONGO-2258
Expand All @@ -120,7 +121,7 @@ void shouldApplyStartAfterToChangeStream() {

initTask(request, Document.class);

verify(changeStreamIterable).startAfter(eq(resumeToken));
verify(changeStreamIterable).startAfter(resumeToken);
}

@Test // GH-4495
Expand All @@ -136,7 +137,7 @@ void shouldApplyFullDocumentBeforeChangeToChangeStream() {

initTask(request, Document.class);

verify(changeStreamIterable).fullDocumentBeforeChange(eq(FullDocumentBeforeChange.REQUIRED));
verify(changeStreamIterable).fullDocumentBeforeChange(FullDocumentBeforeChange.REQUIRED);
}

private MongoCursor<ChangeStreamDocument<Document>> initTask(ChangeStreamRequest request, Class<?> targetType) {
Expand Down

0 comments on commit bc18b52

Please sign in to comment.