Browse Source

Polishing.

Reformat code. Suppress warnings in tests.

Original pull request: #4541
See #4495
pull/4545/head
Mark Paluch 2 years ago
parent
commit
e2d0734ae6
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 27
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java
  2. 8
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/messaging/ChangeStreamTask.java
  3. 23
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java
  4. 17
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/messaging/ChangeStreamTaskUnitTests.java

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

@ -746,6 +746,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
public <T> Mono<Void> dropCollection(Class<T> entityClass) { public <T> Mono<Void> dropCollection(Class<T> entityClass) {
return dropCollection(getCollectionName(entityClass)); return dropCollection(getCollectionName(entityClass));
} }
@Override @Override
public Mono<Void> dropCollection(String collectionName) { 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"); Assert.notNull(targetClass, "Target type must not be null");
EntityProjection<T, ?> projection = operations.introspectProjection(targetClass, sourceClass); 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; int limit = query.isLimited() ? query.getLimit() + 1 : Integer.MAX_VALUE;
if (query.hasKeyset()) { if (query.hasKeyset()) {
@ -894,7 +895,8 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
Mono<List<T>> result = doFind(collectionName, ReactiveCollectionPreparerDelegate.of(query), Mono<List<T>> result = doFind(collectionName, ReactiveCollectionPreparerDelegate.of(query),
keysetPaginationQuery.query(), keysetPaginationQuery.fields(), sourceClass, 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)); 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); 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) { String collectionName) {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityType); 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 -> { return createMono(collectionName, collection -> {
@ -1991,9 +1994,10 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
MongoCollection<Document> collectionToUse = createCollectionPreparer(query, action).prepare(collection); MongoCollection<Document> collectionToUse = createCollectionPreparer(query, action).prepare(collection);
return collectionToUse.replaceOne(updateContext.getMappedQuery(entity), mappedUpdate, updateContext.getReplaceOptions(entityType, it -> { return collectionToUse.replaceOne(updateContext.getMappedQuery(entity), mappedUpdate,
it.upsert(options.isUpsert()); 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) publisher = options.getCollation().map(Collation::toMongoCollation).map(publisher::collation)
.orElse(publisher); .orElse(publisher);
publisher = options.getResumeBsonTimestamp().map(publisher::startAtOperationTime).orElse(publisher); publisher = options.getResumeBsonTimestamp().map(publisher::startAtOperationTime).orElse(publisher);
if(options.getFullDocumentBeforeChangeLookup().isPresent()) { if (options.getFullDocumentBeforeChangeLookup().isPresent()) {
publisher = publisher.fullDocumentBeforeChange(options.getFullDocumentBeforeChangeLookup().get()); publisher = publisher.fullDocumentBeforeChange(options.getFullDocumentBeforeChangeLookup().get());
} }
return publisher.fullDocument(options.getFullDocumentLookup().orElse(fullDocument)); return publisher.fullDocument(options.getFullDocumentLookup().orElse(fullDocument));
@ -2675,7 +2679,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)) { if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)) {
if (wc == null || wc.getWObject() == null 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; return WriteConcern.ACKNOWLEDGED;
} }
} }
@ -3253,8 +3257,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
HintFunction hintFunction = HintFunction.from(query.getHint()); HintFunction hintFunction = HintFunction.from(query.getHint());
Meta meta = query.getMeta(); Meta meta = query.getMeta();
if (skip <= 0 && limit <= 0 && ObjectUtils.isEmpty(sortObject) && hintFunction.isEmpty() if (skip <= 0 && limit <= 0 && ObjectUtils.isEmpty(sortObject) && hintFunction.isEmpty() && !meta.hasValues()) {
&& !meta.hasValues()) {
return findPublisherToUse; return findPublisherToUse;
} }

8
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/messaging/ChangeStreamTask.java

@ -91,9 +91,9 @@ class ChangeStreamTask extends CursorReadingTask<ChangeStreamDocument<Document>,
BsonTimestamp startAt = null; BsonTimestamp startAt = null;
boolean resumeAfter = true; 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); filter = prepareFilter(template, changeStreamOptions);
if (changeStreamOptions.getFilter().isPresent()) { if (changeStreamOptions.getFilter().isPresent()) {
@ -115,9 +115,7 @@ class ChangeStreamTask extends CursorReadingTask<ChangeStreamDocument<Document>,
.orElseGet(() -> ClassUtils.isAssignable(Document.class, targetType) ? FullDocument.DEFAULT .orElseGet(() -> ClassUtils.isAssignable(Document.class, targetType) ? FullDocument.DEFAULT
: FullDocument.UPDATE_LOOKUP); : FullDocument.UPDATE_LOOKUP);
if(changeStreamOptions.getFullDocumentBeforeChangeLookup().isPresent()) { fullDocumentBeforeChange = changeStreamOptions.getFullDocumentBeforeChangeLookup().orElse(null);
fullDocumentBeforeChange = changeStreamOptions.getFullDocumentBeforeChangeLookup().get();
}
startAt = changeStreamOptions.getResumeBsonTimestamp().orElse(null); startAt = changeStreamOptions.getResumeBsonTimestamp().orElse(null);
} }

23
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java

@ -367,7 +367,8 @@ public class ReactiveMongoTemplateUnitTests {
@Test // GH-3218 @Test // GH-3218
void updateUsesHintDocumentFromQuery() { 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); ArgumentCaptor<UpdateOptions> options = ArgumentCaptor.forClass(UpdateOptions.class);
verify(collection).updateOne(any(Bson.class), any(Bson.class), options.capture()); 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.fullDocument(any())).thenReturn(changeStreamPublisher);
when(changeStreamPublisher.fullDocumentBeforeChange(any())).thenReturn(changeStreamPublisher); when(changeStreamPublisher.fullDocumentBeforeChange(any())).thenReturn(changeStreamPublisher);
template ChangeStreamOptions options = ChangeStreamOptions.builder()
.changeStream("database", "collection", ChangeStreamOptions.builder().fullDocumentBeforeChangeLookup(FullDocumentBeforeChange.REQUIRED).build(), Object.class) .fullDocumentBeforeChangeLookup(FullDocumentBeforeChange.REQUIRED).build();
.subscribe(); 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 @Test // GH-4462
void replaceShouldUpsert() { 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 ArgumentCaptor<com.mongodb.client.model.ReplaceOptions> options = ArgumentCaptor
.forClass(com.mongodb.client.model.ReplaceOptions.class); .forClass(com.mongodb.client.model.ReplaceOptions.class);
@ -1663,7 +1665,8 @@ public class ReactiveMongoTemplateUnitTests {
@Test // GH-4462 @Test // GH-4462
void replaceShouldUseDefaultCollationWhenPresent() { 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 ArgumentCaptor<com.mongodb.client.model.ReplaceOptions> options = ArgumentCaptor
.forClass(com.mongodb.client.model.ReplaceOptions.class); .forClass(com.mongodb.client.model.ReplaceOptions.class);
@ -1675,7 +1678,8 @@ public class ReactiveMongoTemplateUnitTests {
@Test // GH-4462 @Test // GH-4462
void replaceShouldUseHintIfPresent() { 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 ArgumentCaptor<com.mongodb.client.model.ReplaceOptions> options = ArgumentCaptor
.forClass(com.mongodb.client.model.ReplaceOptions.class); .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)); verify(collection).withWriteConcern(eq(WriteConcern.UNACKNOWLEDGED));
} }

17
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/messaging/ChangeStreamTaskUnitTests.java

@ -41,18 +41,21 @@ import com.mongodb.client.model.changestream.ChangeStreamDocument;
import com.mongodb.client.model.changestream.FullDocumentBeforeChange; import com.mongodb.client.model.changestream.FullDocumentBeforeChange;
/** /**
* Unit tests for {@link ChangeStreamTask}.
*
* @author Christoph Strobl * @author Christoph Strobl
* @author Myroslav Kosinskyi * @author Myroslav Kosinskyi
*/ */
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
@SuppressWarnings({ "unchecked", "rawtypes" })
class ChangeStreamTaskUnitTests { class ChangeStreamTaskUnitTests {
ChangeStreamTask task;
@Mock MongoTemplate template; @Mock MongoTemplate template;
@Mock MongoDatabase mongoDatabase; @Mock MongoDatabase mongoDatabase;
@Mock MongoCollection<Document> mongoCollection; @Mock MongoCollection<Document> mongoCollection;
@Mock ChangeStreamIterable<Document> changeStreamIterable; @Mock ChangeStreamIterable<Document> changeStreamIterable;
private MongoConverter converter;
MongoConverter converter;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
@ -64,9 +67,7 @@ class ChangeStreamTaskUnitTests {
when(template.getDb()).thenReturn(mongoDatabase); when(template.getDb()).thenReturn(mongoDatabase);
when(mongoDatabase.getCollection(any())).thenReturn(mongoCollection); when(mongoDatabase.getCollection(any())).thenReturn(mongoCollection);
when(mongoCollection.watch(eq(Document.class))).thenReturn(changeStreamIterable); when(mongoCollection.watch(eq(Document.class))).thenReturn(changeStreamIterable);
when(changeStreamIterable.fullDocument(any())).thenReturn(changeStreamIterable); when(changeStreamIterable.fullDocument(any())).thenReturn(changeStreamIterable);
} }
@ -84,7 +85,7 @@ class ChangeStreamTaskUnitTests {
initTask(request, Document.class); initTask(request, Document.class);
verify(changeStreamIterable).resumeAfter(eq(resumeToken)); verify(changeStreamIterable).resumeAfter(resumeToken);
} }
@Test // DATAMONGO-2258 @Test // DATAMONGO-2258
@ -102,7 +103,7 @@ class ChangeStreamTaskUnitTests {
initTask(request, Document.class); initTask(request, Document.class);
verify(changeStreamIterable).resumeAfter(eq(resumeToken)); verify(changeStreamIterable).resumeAfter(resumeToken);
} }
@Test // DATAMONGO-2258 @Test // DATAMONGO-2258
@ -120,7 +121,7 @@ class ChangeStreamTaskUnitTests {
initTask(request, Document.class); initTask(request, Document.class);
verify(changeStreamIterable).startAfter(eq(resumeToken)); verify(changeStreamIterable).startAfter(resumeToken);
} }
@Test // GH-4495 @Test // GH-4495
@ -136,7 +137,7 @@ class ChangeStreamTaskUnitTests {
initTask(request, Document.class); initTask(request, Document.class);
verify(changeStreamIterable).fullDocumentBeforeChange(eq(FullDocumentBeforeChange.REQUIRED)); verify(changeStreamIterable).fullDocumentBeforeChange(FullDocumentBeforeChange.REQUIRED);
} }
private MongoCursor<ChangeStreamDocument<Document>> initTask(ChangeStreamRequest request, Class<?> targetType) { private MongoCursor<ChangeStreamDocument<Document>> initTask(ChangeStreamRequest request, Class<?> targetType) {

Loading…
Cancel
Save