Skip to content

Commit e2d0734

Browse files
committed
Polishing.
Reformat code. Suppress warnings in tests. Original pull request: #4541 See #4495
1 parent bf54054 commit e2d0734

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ public Mono<Boolean> collectionExists(String collectionName) {
746746
public <T> Mono<Void> dropCollection(Class<T> entityClass) {
747747
return dropCollection(getCollectionName(entityClass));
748748
}
749+
749750
@Override
750751
public Mono<Void> dropCollection(String collectionName) {
751752

@@ -884,7 +885,7 @@ <T> Mono<Window<T>> doScroll(Query query, Class<?> sourceClass, Class<T> targetC
884885
Assert.notNull(targetClass, "Target type must not be null");
885886

886887
EntityProjection<T, ?> projection = operations.introspectProjection(targetClass, sourceClass);
887-
ProjectingReadCallback<?,T> callback = new ProjectingReadCallback<>(mongoConverter, projection, collectionName);
888+
ProjectingReadCallback<?, T> callback = new ProjectingReadCallback<>(mongoConverter, projection, collectionName);
888889
int limit = query.isLimited() ? query.getLimit() + 1 : Integer.MAX_VALUE;
889890

890891
if (query.hasKeyset()) {
@@ -894,7 +895,8 @@ <T> Mono<Window<T>> doScroll(Query query, Class<?> sourceClass, Class<T> targetC
894895

895896
Mono<List<T>> result = doFind(collectionName, ReactiveCollectionPreparerDelegate.of(query),
896897
keysetPaginationQuery.query(), keysetPaginationQuery.fields(), sourceClass,
897-
new QueryFindPublisherPreparer(query, keysetPaginationQuery.sort(), limit, 0, sourceClass), callback).collectList();
898+
new QueryFindPublisherPreparer(query, keysetPaginationQuery.sort(), limit, 0, sourceClass), callback)
899+
.collectList();
898900

899901
return result.map(it -> ScrollUtils.createWindow(query, it, sourceClass, operations));
900902
}
@@ -1976,11 +1978,12 @@ public <T> Mono<UpdateResult> replace(Query query, T replacement, ReplaceOptions
19761978
return replace(query, (Class<T>) ClassUtils.getUserClass(replacement), replacement, options, collectionName);
19771979
}
19781980

1979-
protected <S,T> Mono<UpdateResult> replace(Query query, Class<S> entityType, T replacement, ReplaceOptions options,
1981+
protected <S, T> Mono<UpdateResult> replace(Query query, Class<S> entityType, T replacement, ReplaceOptions options,
19801982
String collectionName) {
19811983

19821984
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityType);
1983-
UpdateContext updateContext = queryOperations.replaceSingleContext(query, operations.forEntity(replacement).toMappedDocument(this.mongoConverter), options.isUpsert());
1985+
UpdateContext updateContext = queryOperations.replaceSingleContext(query,
1986+
operations.forEntity(replacement).toMappedDocument(this.mongoConverter), options.isUpsert());
19841987

19851988
return createMono(collectionName, collection -> {
19861989

@@ -1991,9 +1994,10 @@ protected <S,T> Mono<UpdateResult> replace(Query query, Class<S> entityType, T r
19911994

19921995
MongoCollection<Document> collectionToUse = createCollectionPreparer(query, action).prepare(collection);
19931996

1994-
return collectionToUse.replaceOne(updateContext.getMappedQuery(entity), mappedUpdate, updateContext.getReplaceOptions(entityType, it -> {
1995-
it.upsert(options.isUpsert());
1996-
}));
1997+
return collectionToUse.replaceOne(updateContext.getMappedQuery(entity), mappedUpdate,
1998+
updateContext.getReplaceOptions(entityType, it -> {
1999+
it.upsert(options.isUpsert());
2000+
}));
19972001
});
19982002
}
19992003

@@ -2050,8 +2054,8 @@ public <T> Flux<ChangeStreamEvent<T>> changeStream(@Nullable String database, @N
20502054
publisher = options.getCollation().map(Collation::toMongoCollation).map(publisher::collation)
20512055
.orElse(publisher);
20522056
publisher = options.getResumeBsonTimestamp().map(publisher::startAtOperationTime).orElse(publisher);
2053-
2054-
if(options.getFullDocumentBeforeChangeLookup().isPresent()) {
2057+
2058+
if (options.getFullDocumentBeforeChangeLookup().isPresent()) {
20552059
publisher = publisher.fullDocumentBeforeChange(options.getFullDocumentBeforeChangeLookup().get());
20562060
}
20572061
return publisher.fullDocument(options.getFullDocumentLookup().orElse(fullDocument));
@@ -2675,7 +2679,7 @@ private WriteConcern potentiallyForceAcknowledgedWrite(@Nullable WriteConcern wc
26752679

26762680
if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)) {
26772681
if (wc == null || wc.getWObject() == null
2678-
|| (wc.getWObject() instanceof Number concern && concern.intValue() < 1)) {
2682+
|| (wc.getWObject()instanceof Number concern && concern.intValue() < 1)) {
26792683
return WriteConcern.ACKNOWLEDGED;
26802684
}
26812685
}
@@ -3253,8 +3257,7 @@ public FindPublisher<Document> prepare(FindPublisher<Document> findPublisher) {
32533257

32543258
HintFunction hintFunction = HintFunction.from(query.getHint());
32553259
Meta meta = query.getMeta();
3256-
if (skip <= 0 && limit <= 0 && ObjectUtils.isEmpty(sortObject) && hintFunction.isEmpty()
3257-
&& !meta.hasValues()) {
3260+
if (skip <= 0 && limit <= 0 && ObjectUtils.isEmpty(sortObject) && hintFunction.isEmpty() && !meta.hasValues()) {
32583261
return findPublisherToUse;
32593262
}
32603263

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ protected MongoCursor<ChangeStreamDocument<Document>> initCursor(MongoTemplate t
9191
BsonTimestamp startAt = null;
9292
boolean resumeAfter = true;
9393

94-
if (options instanceof ChangeStreamRequest.ChangeStreamRequestOptions changeStreamRequestOptions) {
94+
if (options instanceof ChangeStreamRequest.ChangeStreamRequestOptions requestOptions) {
9595

96-
ChangeStreamOptions changeStreamOptions = changeStreamRequestOptions.getChangeStreamOptions();
96+
ChangeStreamOptions changeStreamOptions = requestOptions.getChangeStreamOptions();
9797
filter = prepareFilter(template, changeStreamOptions);
9898

9999
if (changeStreamOptions.getFilter().isPresent()) {
@@ -115,9 +115,7 @@ protected MongoCursor<ChangeStreamDocument<Document>> initCursor(MongoTemplate t
115115
.orElseGet(() -> ClassUtils.isAssignable(Document.class, targetType) ? FullDocument.DEFAULT
116116
: FullDocument.UPDATE_LOOKUP);
117117

118-
if(changeStreamOptions.getFullDocumentBeforeChangeLookup().isPresent()) {
119-
fullDocumentBeforeChange = changeStreamOptions.getFullDocumentBeforeChangeLookup().get();
120-
}
118+
fullDocumentBeforeChange = changeStreamOptions.getFullDocumentBeforeChangeLookup().orElse(null);
121119

122120
startAt = changeStreamOptions.getResumeBsonTimestamp().orElse(null);
123121
}

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ void updateUsesHintStringFromQuery() {
367367
@Test // GH-3218
368368
void updateUsesHintDocumentFromQuery() {
369369

370-
template.updateFirst(new Query().withHint("{ firstname : 1 }"), new Update().set("spring", "data"), Person.class).subscribe();
370+
template.updateFirst(new Query().withHint("{ firstname : 1 }"), new Update().set("spring", "data"), Person.class)
371+
.subscribe();
371372

372373
ArgumentCaptor<UpdateOptions> options = ArgumentCaptor.forClass(UpdateOptions.class);
373374
verify(collection).updateOne(any(Bson.class), any(Bson.class), options.capture());
@@ -1615,11 +1616,11 @@ void changeStreamOptionFullDocumentBeforeChangeShouldBeApplied() {
16151616
when(changeStreamPublisher.fullDocument(any())).thenReturn(changeStreamPublisher);
16161617
when(changeStreamPublisher.fullDocumentBeforeChange(any())).thenReturn(changeStreamPublisher);
16171618

1618-
template
1619-
.changeStream("database", "collection", ChangeStreamOptions.builder().fullDocumentBeforeChangeLookup(FullDocumentBeforeChange.REQUIRED).build(), Object.class)
1620-
.subscribe();
1619+
ChangeStreamOptions options = ChangeStreamOptions.builder()
1620+
.fullDocumentBeforeChangeLookup(FullDocumentBeforeChange.REQUIRED).build();
1621+
template.changeStream("database", "collection", options, Object.class).subscribe();
16211622

1622-
verify(changeStreamPublisher).fullDocumentBeforeChange(eq(FullDocumentBeforeChange.REQUIRED));
1623+
verify(changeStreamPublisher).fullDocumentBeforeChange(FullDocumentBeforeChange.REQUIRED);
16231624

16241625
}
16251626

@@ -1651,7 +1652,8 @@ void replaceShouldNotUpsertByDefault() {
16511652
@Test // GH-4462
16521653
void replaceShouldUpsert() {
16531654

1654-
template.replace(new BasicQuery("{}"), new MongoTemplateUnitTests.Sith(), org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
1655+
template.replace(new BasicQuery("{}"), new MongoTemplateUnitTests.Sith(),
1656+
org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
16551657

16561658
ArgumentCaptor<com.mongodb.client.model.ReplaceOptions> options = ArgumentCaptor
16571659
.forClass(com.mongodb.client.model.ReplaceOptions.class);
@@ -1663,7 +1665,8 @@ void replaceShouldUpsert() {
16631665
@Test // GH-4462
16641666
void replaceShouldUseDefaultCollationWhenPresent() {
16651667

1666-
template.replace(new BasicQuery("{}"), new MongoTemplateUnitTests.Sith(), org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions()).subscribe();
1668+
template.replace(new BasicQuery("{}"), new MongoTemplateUnitTests.Sith(),
1669+
org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions()).subscribe();
16671670

16681671
ArgumentCaptor<com.mongodb.client.model.ReplaceOptions> options = ArgumentCaptor
16691672
.forClass(com.mongodb.client.model.ReplaceOptions.class);
@@ -1675,7 +1678,8 @@ void replaceShouldUseDefaultCollationWhenPresent() {
16751678
@Test // GH-4462
16761679
void replaceShouldUseHintIfPresent() {
16771680

1678-
template.replace(new BasicQuery("{}").withHint("index-to-use"), new MongoTemplateUnitTests.Sith(), org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
1681+
template.replace(new BasicQuery("{}").withHint("index-to-use"), new MongoTemplateUnitTests.Sith(),
1682+
org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
16791683

16801684
ArgumentCaptor<com.mongodb.client.model.ReplaceOptions> options = ArgumentCaptor
16811685
.forClass(com.mongodb.client.model.ReplaceOptions.class);
@@ -1695,7 +1699,8 @@ public WriteConcern resolve(MongoAction action) {
16951699
}
16961700
});
16971701

1698-
template.replace(new BasicQuery("{}").withHint("index-to-use"), new Sith(), org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
1702+
template.replace(new BasicQuery("{}").withHint("index-to-use"), new Sith(),
1703+
org.springframework.data.mongodb.core.ReplaceOptions.replaceOptions().upsert()).subscribe();
16991704

17001705
verify(collection).withWriteConcern(eq(WriteConcern.UNACKNOWLEDGED));
17011706
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,21 @@
4141
import com.mongodb.client.model.changestream.FullDocumentBeforeChange;
4242

4343
/**
44+
* Unit tests for {@link ChangeStreamTask}.
45+
*
4446
* @author Christoph Strobl
4547
* @author Myroslav Kosinskyi
4648
*/
4749
@ExtendWith(MockitoExtension.class)
50+
@SuppressWarnings({ "unchecked", "rawtypes" })
4851
class ChangeStreamTaskUnitTests {
4952

50-
ChangeStreamTask task;
5153
@Mock MongoTemplate template;
5254
@Mock MongoDatabase mongoDatabase;
5355
@Mock MongoCollection<Document> mongoCollection;
5456
@Mock ChangeStreamIterable<Document> changeStreamIterable;
55-
private MongoConverter converter;
57+
58+
MongoConverter converter;
5659

5760
@BeforeEach
5861
void setUp() {
@@ -64,9 +67,7 @@ void setUp() {
6467
when(template.getDb()).thenReturn(mongoDatabase);
6568

6669
when(mongoDatabase.getCollection(any())).thenReturn(mongoCollection);
67-
6870
when(mongoCollection.watch(eq(Document.class))).thenReturn(changeStreamIterable);
69-
7071
when(changeStreamIterable.fullDocument(any())).thenReturn(changeStreamIterable);
7172
}
7273

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

8586
initTask(request, Document.class);
8687

87-
verify(changeStreamIterable).resumeAfter(eq(resumeToken));
88+
verify(changeStreamIterable).resumeAfter(resumeToken);
8889
}
8990

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

103104
initTask(request, Document.class);
104105

105-
verify(changeStreamIterable).resumeAfter(eq(resumeToken));
106+
verify(changeStreamIterable).resumeAfter(resumeToken);
106107
}
107108

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

121122
initTask(request, Document.class);
122123

123-
verify(changeStreamIterable).startAfter(eq(resumeToken));
124+
verify(changeStreamIterable).startAfter(resumeToken);
124125
}
125126

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

137138
initTask(request, Document.class);
138139

139-
verify(changeStreamIterable).fullDocumentBeforeChange(eq(FullDocumentBeforeChange.REQUIRED));
140+
verify(changeStreamIterable).fullDocumentBeforeChange(FullDocumentBeforeChange.REQUIRED);
140141
}
141142

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

0 commit comments

Comments
 (0)