Skip to content

Commit 92c19b3

Browse files
committed
🙄Tests
1 parent 8772742 commit 92c19b3

4 files changed

Lines changed: 99 additions & 61 deletions

File tree

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.mockito.Mockito.*;
2020

21+
import java.util.List;
22+
import java.util.function.BiConsumer;
23+
2124
import org.bson.Document;
25+
import org.bson.conversions.Bson;
2226
import org.junit.jupiter.api.BeforeEach;
2327
import org.junit.jupiter.api.Test;
2428
import org.junit.jupiter.api.extension.ExtendWith;
2529
import org.mockito.ArgumentCaptor;
2630
import org.mockito.Mock;
2731
import org.mockito.junit.jupiter.MockitoExtension;
32+
2833
import org.springframework.data.domain.Sort.Direction;
2934
import org.springframework.data.mongodb.MongoDatabaseFactory;
3035
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
@@ -37,6 +42,8 @@
3742

3843
import com.mongodb.client.MongoCollection;
3944
import com.mongodb.client.MongoDatabase;
45+
import com.mongodb.client.model.CreateIndexOptions;
46+
import com.mongodb.client.model.IndexModel;
4047
import com.mongodb.client.model.IndexOptions;
4148

4249
/**
@@ -64,7 +71,7 @@ void setUp() {
6471
when(factory.getMongoDatabase()).thenReturn(db);
6572
when(factory.getExceptionTranslator()).thenReturn(exceptionTranslator);
6673
when(db.getCollection(any(), any(Class.class))).thenReturn(collection);
67-
when(collection.createIndex(any(), any(IndexOptions.class))).thenReturn("OK");
74+
when(collection.createIndexes(anyList(), any(CreateIndexOptions.class))).thenReturn(List.of("OK"));
6875

6976
this.mappingContext = new MongoMappingContext();
7077
this.converter = spy(new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext));
@@ -76,50 +83,41 @@ void indexOperationsMapFieldNameCorrectly() {
7683

7784
indexOpsFor(Jedi.class).ensureIndex(new Index("name", Direction.DESC));
7885

79-
verify(collection).createIndex(eq(new Document("firstname", -1)), any());
86+
verifyCreateIndex((keys, options) -> assertThat(keys).isEqualTo(new Document("firstname", -1)));
8087
}
8188

8289
@Test // DATAMONGO-1854
8390
void ensureIndexDoesNotSetCollectionIfNoDefaultDefined() {
8491

8592
indexOpsFor(Jedi.class).ensureIndex(new Index("firstname", Direction.DESC));
8693

87-
ArgumentCaptor<IndexOptions> options = ArgumentCaptor.forClass(IndexOptions.class);
88-
verify(collection).createIndex(any(), options.capture());
89-
90-
assertThat(options.getValue().getCollation()).isNull();
94+
verifyCreateIndex((keys, options) -> assertThat(options.getCollation()).isNull());
9195
}
9296

9397
@Test // DATAMONGO-1854
9498
void ensureIndexUsesDefaultCollationIfNoneDefinedInOptions() {
9599

96100
indexOpsFor(Sith.class).ensureIndex(new Index("firstname", Direction.DESC));
97101

98-
ArgumentCaptor<IndexOptions> options = ArgumentCaptor.forClass(IndexOptions.class);
99-
verify(collection).createIndex(any(), options.capture());
100-
101-
assertThat(options.getValue().getCollation())
102-
.isEqualTo(com.mongodb.client.model.Collation.builder().locale("de_AT").build());
102+
verifyCreateIndex((keys, options) -> assertThat(options.getCollation())
103+
.isEqualTo(com.mongodb.client.model.Collation.builder().locale("de_AT").build()));
103104
}
104105

105106
@Test // DATAMONGO-1854
106107
void ensureIndexDoesNotUseDefaultCollationIfExplicitlySpecifiedInTheIndex() {
107108

108109
indexOpsFor(Sith.class).ensureIndex(new Index("firstname", Direction.DESC).collation(Collation.of("en_US")));
109110

110-
ArgumentCaptor<IndexOptions> options = ArgumentCaptor.forClass(IndexOptions.class);
111-
verify(collection).createIndex(any(), options.capture());
112-
113-
assertThat(options.getValue().getCollation())
114-
.isEqualTo(com.mongodb.client.model.Collation.builder().locale("en_US").build());
111+
verifyCreateIndex((keys, options) -> assertThat(options.getCollation())
112+
.isEqualTo(com.mongodb.client.model.Collation.builder().locale("en_US").build()));
115113
}
116114

117115
@Test // DATAMONGO-1183
118116
void shouldCreateHashedIndexCorrectly() {
119117

120118
indexOpsFor(Jedi.class).ensureIndex(HashedIndex.hashed("name"));
121119

122-
verify(collection).createIndex(eq(new Document("firstname", "hashed")), any());
120+
verifyCreateIndex((keys, options) -> assertThat(keys).isEqualTo(new Document("firstname", "hashed")));
123121
}
124122

125123
@Test // GH-4698
@@ -131,6 +129,16 @@ void shouldConsiderGivenCollectionName() {
131129
verify(db).getCollection(eq("foo"), any(Class.class));
132130
}
133131

132+
private void verifyCreateIndex(BiConsumer<Bson, IndexOptions> consumer) {
133+
134+
ArgumentCaptor<List<IndexModel>> captor = ArgumentCaptor.forClass(List.class);
135+
136+
verify(collection).createIndexes(captor.capture(), any());
137+
138+
IndexModel indexModel = captor.getValue().get(0);
139+
consumer.accept(indexModel.getKeys(), indexModel.getOptions());
140+
}
141+
134142
private DefaultIndexOperations indexOpsFor(Class<?> type) {
135143
return new DefaultIndexOperations(template, template.getCollectionName(type), type);
136144
}

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@
2020

2121
import reactor.core.publisher.Mono;
2222

23+
import java.util.List;
24+
import java.util.function.BiConsumer;
25+
2326
import org.bson.Document;
27+
import org.bson.conversions.Bson;
2428
import org.junit.jupiter.api.BeforeEach;
2529
import org.junit.jupiter.api.Test;
2630
import org.junit.jupiter.api.extension.ExtendWith;
2731
import org.mockito.ArgumentCaptor;
2832
import org.mockito.Mock;
2933
import org.mockito.junit.jupiter.MockitoExtension;
3034
import org.reactivestreams.Publisher;
35+
3136
import org.springframework.data.domain.Sort.Direction;
3237
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
3338
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
@@ -38,13 +43,18 @@
3843
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
3944
import org.springframework.data.mongodb.core.query.Collation;
4045

46+
import com.mongodb.client.model.CreateIndexOptions;
47+
import com.mongodb.client.model.IndexModel;
4148
import com.mongodb.client.model.IndexOptions;
4249
import com.mongodb.reactivestreams.client.MongoCollection;
4350
import com.mongodb.reactivestreams.client.MongoDatabase;
4451

4552
/**
53+
* Unit tests for {@link DefaultReactiveIndexOperations}.
54+
*
4655
* @author Christoph Strobl
4756
* @author Mathieu Ouellet
57+
* @author Mark Paluch
4858
*/
4959
@ExtendWith(MockitoExtension.class)
5060
public class DefaultReactiveIndexOperationsUnitTests {
@@ -66,7 +76,7 @@ void setUp() {
6676
when(factory.getMongoDatabase()).thenReturn(Mono.just(db));
6777
when(factory.getExceptionTranslator()).thenReturn(exceptionTranslator);
6878
when(db.getCollection(any(), any(Class.class))).thenReturn(collection);
69-
when(collection.createIndex(any(), any(IndexOptions.class))).thenReturn(publisher);
79+
when(collection.createIndexes(anyList(), any(CreateIndexOptions.class))).thenReturn(publisher);
7080

7181
this.mappingContext = new MongoMappingContext();
7282
this.converter = spy(new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext));
@@ -78,22 +88,16 @@ void ensureIndexDoesNotSetCollectionIfNoDefaultDefined() {
7888

7989
indexOpsFor(Jedi.class).ensureIndex(new Index("firstname", Direction.DESC)).subscribe();
8090

81-
ArgumentCaptor<IndexOptions> options = ArgumentCaptor.forClass(IndexOptions.class);
82-
verify(collection).createIndex(any(), options.capture());
83-
84-
assertThat(options.getValue().getCollation()).isNull();
91+
verifyCreateIndex((keys, options) -> assertThat(options.getCollation()).isNull());
8592
}
8693

8794
@Test // DATAMONGO-1854
8895
void ensureIndexUsesDefaultCollationIfNoneDefinedInOptions() {
8996

9097
indexOpsFor(Sith.class).ensureIndex(new Index("firstname", Direction.DESC)).subscribe();
9198

92-
ArgumentCaptor<IndexOptions> options = ArgumentCaptor.forClass(IndexOptions.class);
93-
verify(collection).createIndex(any(), options.capture());
94-
95-
assertThat(options.getValue().getCollation())
96-
.isEqualTo(com.mongodb.client.model.Collation.builder().locale("de_AT").build());
99+
verifyCreateIndex((keys, options) -> assertThat(options.getCollation())
100+
.isEqualTo(com.mongodb.client.model.Collation.builder().locale("de_AT").build()));
97101
}
98102

99103
@Test // DATAMONGO-1854
@@ -102,11 +106,19 @@ void ensureIndexDoesNotUseDefaultCollationIfExplicitlySpecifiedInTheIndex() {
102106
indexOpsFor(Sith.class).ensureIndex(new Index("firstname", Direction.DESC).collation(Collation.of("en_US")))
103107
.subscribe();
104108

105-
ArgumentCaptor<IndexOptions> options = ArgumentCaptor.forClass(IndexOptions.class);
106-
verify(collection).createIndex(any(), options.capture());
107109

108-
assertThat(options.getValue().getCollation())
109-
.isEqualTo(com.mongodb.client.model.Collation.builder().locale("en_US").build());
110+
verifyCreateIndex((keys, options) -> assertThat(options.getCollation())
111+
.isEqualTo(com.mongodb.client.model.Collation.builder().locale("en_US").build()));
112+
}
113+
114+
private void verifyCreateIndex(BiConsumer<Bson, IndexOptions> consumer) {
115+
116+
ArgumentCaptor<List<IndexModel>> captor = ArgumentCaptor.forClass(List.class);
117+
118+
verify(collection).createIndexes(captor.capture(), any());
119+
120+
IndexModel indexModel = captor.getValue().get(0);
121+
consumer.accept(indexModel.getKeys(), indexModel.getOptions());
110122
}
111123

112124
private DefaultReactiveIndexOperations indexOpsFor(Class<?> type) {

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

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.util.Collections;
2222
import java.util.Date;
23+
import java.util.List;
2324
import java.util.concurrent.TimeUnit;
2425

2526
import org.junit.jupiter.api.BeforeEach;
@@ -46,6 +47,8 @@
4647
import com.mongodb.MongoException;
4748
import com.mongodb.client.MongoCollection;
4849
import com.mongodb.client.MongoDatabase;
50+
import com.mongodb.client.model.CreateIndexOptions;
51+
import com.mongodb.client.model.IndexModel;
4952
import com.mongodb.client.model.IndexOptions;
5053

5154
/**
@@ -67,25 +70,22 @@ public class MongoPersistentEntityIndexCreatorUnitTests {
6770
private @Mock MongoCollection<org.bson.Document> collection;
6871
private MongoTemplate mongoTemplate;
6972

70-
private ArgumentCaptor<org.bson.Document> keysCaptor;
71-
private ArgumentCaptor<IndexOptions> optionsCaptor;
73+
private ArgumentCaptor<List<IndexModel>> indexModelCaptor;
7274
private ArgumentCaptor<String> collectionCaptor;
7375

7476
@BeforeEach
7577
void setUp() {
7678

77-
keysCaptor = ArgumentCaptor.forClass(org.bson.Document.class);
78-
optionsCaptor = ArgumentCaptor.forClass(IndexOptions.class);
7979
collectionCaptor = ArgumentCaptor.forClass(String.class);
80+
indexModelCaptor = ArgumentCaptor.forClass(List.class);
8081

8182
when(factory.getMongoDatabase()).thenReturn(db);
8283
when(factory.getExceptionTranslator()).thenReturn(new MongoExceptionTranslator());
8384
when(db.getCollection(collectionCaptor.capture(), eq(org.bson.Document.class)))
8485
.thenReturn((MongoCollection) collection);
8586

8687
mongoTemplate = new MongoTemplate(factory);
87-
88-
when(collection.createIndex(keysCaptor.capture(), optionsCaptor.capture())).thenReturn("OK");
88+
when(collection.createIndexes(indexModelCaptor.capture(), any(CreateIndexOptions.class))).thenReturn(List.of("OK"));
8989
}
9090

9191
@Test
@@ -95,10 +95,12 @@ void buildsIndexDefinitionUsingFieldName() {
9595

9696
new MongoPersistentEntityIndexCreator(mappingContext, mongoTemplate);
9797

98-
assertThat(keysCaptor.getValue()).isNotNull().containsKey("fieldname");
99-
assertThat(optionsCaptor.getValue().getName()).isEqualTo("indexName");
100-
assertThat(optionsCaptor.getValue().isBackground()).isFalse();
101-
assertThat(optionsCaptor.getValue().getExpireAfter(TimeUnit.SECONDS)).isNull();
98+
IndexModel indexModel = indexModelCaptor.getValue().get(0);
99+
100+
assertThat(indexModel.getKeys().toBsonDocument()).isNotNull().containsKey("fieldname");
101+
assertThat(indexModel.getOptions().getName()).isEqualTo("indexName");
102+
assertThat(indexModel.getOptions().isBackground()).isFalse();
103+
assertThat(indexModel.getOptions().getExpireAfter(TimeUnit.SECONDS)).isNull();
102104
}
103105

104106
@Test
@@ -135,10 +137,12 @@ void triggersBackgroundIndexingIfConfigured() {
135137
MongoMappingContext mappingContext = prepareMappingContext(AnotherPerson.class);
136138
new MongoPersistentEntityIndexCreator(mappingContext, mongoTemplate);
137139

138-
assertThat(keysCaptor.getValue()).isNotNull().containsKey("lastname");
139-
assertThat(optionsCaptor.getValue().getName()).isEqualTo("lastname");
140-
assertThat(optionsCaptor.getValue().isBackground()).isTrue();
141-
assertThat(optionsCaptor.getValue().getExpireAfter(TimeUnit.SECONDS)).isNull();
140+
IndexModel indexModel = indexModelCaptor.getValue().get(0);
141+
142+
assertThat(indexModel.getKeys().toBsonDocument()).isNotNull().containsKey("lastname");
143+
assertThat(indexModel.getOptions().getName()).isEqualTo("lastname");
144+
assertThat(indexModel.getOptions().isBackground()).isTrue();
145+
assertThat(indexModel.getOptions().getExpireAfter(TimeUnit.SECONDS)).isNull();
142146
}
143147

144148
@Test // DATAMONGO-544
@@ -147,8 +151,10 @@ void expireAfterSecondsIfConfigured() {
147151
MongoMappingContext mappingContext = prepareMappingContext(Milk.class);
148152
new MongoPersistentEntityIndexCreator(mappingContext, mongoTemplate);
149153

150-
assertThat(keysCaptor.getValue()).isNotNull().containsKey("expiry");
151-
assertThat(optionsCaptor.getValue().getExpireAfter(TimeUnit.SECONDS)).isEqualTo(60);
154+
IndexModel indexModel = indexModelCaptor.getValue().get(0);
155+
156+
assertThat(indexModel.getKeys().toBsonDocument()).isNotNull().containsKey("expiry");
157+
assertThat(indexModel.getOptions().getExpireAfter(TimeUnit.SECONDS)).isEqualTo(60);
152158
}
153159

154160
@Test // DATAMONGO-899
@@ -157,9 +163,11 @@ void createsNotNestedGeoSpatialIndexCorrectly() {
157163
MongoMappingContext mappingContext = prepareMappingContext(Wrapper.class);
158164
new MongoPersistentEntityIndexCreator(mappingContext, mongoTemplate);
159165

160-
assertThat(keysCaptor.getValue()).isEqualTo(new org.bson.Document("company.address.location", "2d"));
166+
IndexModel indexModel = indexModelCaptor.getValue().get(0);
167+
168+
assertThat(indexModel.getKeys()).isEqualTo(new org.bson.Document("company.address.location", "2d"));
161169

162-
IndexOptions opts = optionsCaptor.getValue();
170+
IndexOptions opts = indexModel.getOptions();
163171
assertThat(opts.getName()).isEqualTo("company.address.location");
164172
assertThat(opts.getMin()).isCloseTo(-180d, offset(0d));
165173
assertThat(opts.getMax()).isCloseTo(180d, offset(0d));
@@ -172,8 +180,10 @@ void autoGeneratedIndexNameShouldGenerateNoName() {
172180
MongoMappingContext mappingContext = prepareMappingContext(EntityWithGeneratedIndexName.class);
173181
new MongoPersistentEntityIndexCreator(mappingContext, mongoTemplate);
174182

175-
assertThat(keysCaptor.getValue()).doesNotContainKey("name").containsKey("lastname");
176-
assertThat(optionsCaptor.getValue().getName()).isNull();
183+
IndexModel indexModel = indexModelCaptor.getValue().get(0);
184+
185+
assertThat(indexModel.getKeys().toBsonDocument()).doesNotContainKey("name").containsKey("lastname");
186+
assertThat(indexModel.getOptions().getName()).isNull();
177187
}
178188

179189
@Test // DATAMONGO-367
@@ -203,8 +213,8 @@ void indexCreationShouldNotCreateNewCollectionForNestedIndexStructures() {
203213
@Test // DATAMONGO-1125
204214
void createIndexShouldUsePersistenceExceptionTranslatorForNonDataIntegrityConcerns() {
205215

206-
doThrow(new MongoException(6, "HostUnreachable")).when(collection).createIndex(any(org.bson.Document.class),
207-
any(IndexOptions.class));
216+
doThrow(new MongoException(6, "HostUnreachable")).when(collection).createIndexes(anyList(),
217+
any(CreateIndexOptions.class));
208218

209219
MongoMappingContext mappingContext = prepareMappingContext(Person.class);
210220

@@ -215,8 +225,7 @@ void createIndexShouldUsePersistenceExceptionTranslatorForNonDataIntegrityConcer
215225
@Test // DATAMONGO-1125
216226
void createIndexShouldNotConvertUnknownExceptionTypes() {
217227

218-
doThrow(new ClassCastException("o_O")).when(collection).createIndex(any(org.bson.Document.class),
219-
any(IndexOptions.class));
228+
doThrow(new ClassCastException("o_O")).when(collection).createIndexes(anyList(), any(CreateIndexOptions.class));
220229

221230
MongoMappingContext mappingContext = prepareMappingContext(Person.class);
222231

0 commit comments

Comments
 (0)