18
18
import static org .assertj .core .api .Assertions .*;
19
19
import static org .hamcrest .core .Is .*;
20
20
import static org .junit .Assume .*;
21
+ import static org .springframework .data .mongodb .core .index .PartialIndexFilter .*;
22
+ import static org .springframework .data .mongodb .core .query .Criteria .*;
21
23
24
+ import reactor .core .publisher .Mono ;
22
25
import reactor .test .StepVerifier ;
23
26
27
+ import java .util .function .Predicate ;
28
+
24
29
import org .bson .Document ;
25
30
import org .junit .Before ;
26
31
import org .junit .Test ;
31
36
import org .springframework .data .mongodb .config .AbstractReactiveMongoConfiguration ;
32
37
import org .springframework .data .mongodb .core .query .Collation ;
33
38
import org .springframework .data .mongodb .core .query .Collation .CaseFirst ;
34
- import org .springframework .data .mongodb .core .DefaultIndexOperationsIntegrationTests . DefaultIndexOperationsIntegrationTestsSample ;
39
+ import org .springframework .data .mongodb .core .convert . QueryMapper ;
35
40
import org .springframework .data .mongodb .core .index .Index ;
36
41
import org .springframework .data .mongodb .core .index .IndexDefinition ;
42
+ import org .springframework .data .mongodb .core .index .IndexInfo ;
43
+ import org .springframework .data .mongodb .core .mapping .Field ;
37
44
import org .springframework .data .util .Version ;
38
45
import org .springframework .test .context .ContextConfiguration ;
39
46
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
@@ -64,6 +71,7 @@ protected String getDatabaseName() {
64
71
}
65
72
}
66
73
74
+ private static final Version THREE_DOT_TWO = new Version (3 , 2 );
67
75
private static final Version THREE_DOT_FOUR = new Version (3 , 4 );
68
76
private static Version mongoVersion ;
69
77
@@ -79,8 +87,10 @@ public void setUp() {
79
87
String collectionName = this .template .getCollectionName (DefaultIndexOperationsIntegrationTestsSample .class );
80
88
81
89
this .collection = this .template .getMongoDatabase ().getCollection (collectionName , Document .class );
82
- this .collection .dropIndexes ();
83
- this .indexOps = new DefaultReactiveIndexOperations (template , collectionName );
90
+ Mono .from (this .collection .dropIndexes ()).subscribe ();
91
+
92
+ this .indexOps = new DefaultReactiveIndexOperations (template , collectionName ,
93
+ new QueryMapper (template .getConverter ()));
84
94
}
85
95
86
96
private void queryMongoVersionIfNecessary () {
@@ -111,7 +121,7 @@ public void shouldCreateIndexWithCollationCorrectly() {
111
121
.append ("normalization" , false ) //
112
122
.append ("backwards" , false );
113
123
114
- StepVerifier .create (indexOps .getIndexInfo ().filter (val -> val . getName (). equals ( "with-collation" )))
124
+ StepVerifier .create (indexOps .getIndexInfo ().filter (this . indexByName ( "with-collation" ))) //
115
125
.consumeNextWith (indexInfo -> {
116
126
117
127
assertThat (indexInfo .getCollation ()).isPresent ();
@@ -122,7 +132,96 @@ public void shouldCreateIndexWithCollationCorrectly() {
122
132
123
133
assertThat (result ).isEqualTo (expected );
124
134
}) //
135
+ .thenAwait ();
136
+ }
137
+
138
+ @ Test // DATAMONGO-1682
139
+ public void shouldApplyPartialFilterCorrectly () {
140
+
141
+ assumeThat (mongoVersion .isGreaterThanOrEqualTo (THREE_DOT_TWO ), is (true ));
142
+
143
+ IndexDefinition id = new Index ().named ("partial-with-criteria" ).on ("k3y" , Direction .ASC )
144
+ .partial (of (where ("q-t-y" ).gte (10 )));
145
+
146
+ indexOps .ensureIndex (id ).subscribe ();
147
+
148
+ StepVerifier .create (indexOps .getIndexInfo ().filter (this .indexByName ("partial-with-criteria" ))) //
149
+ .consumeNextWith (indexInfo -> {
150
+ assertThat (indexInfo .getPartialFilterExpression ()).isEqualTo ("{ \" q-t-y\" : { \" $gte\" : 10 } }" );
151
+ }) //
152
+ .thenAwait ();
153
+ }
154
+
155
+ @ Test // DATAMONGO-1682
156
+ public void shouldApplyPartialFilterWithMappedPropertyCorrectly () {
157
+
158
+ assumeThat (mongoVersion .isGreaterThanOrEqualTo (THREE_DOT_TWO ), is (true ));
159
+
160
+ IndexDefinition id = new Index ().named ("partial-with-mapped-criteria" ).on ("k3y" , Direction .ASC )
161
+ .partial (of (where ("quantity" ).gte (10 )));
162
+
163
+ indexOps .ensureIndex (id ).subscribe ();
164
+
165
+ StepVerifier .create (indexOps .getIndexInfo ().filter (this .indexByName ("partial-with-mapped-criteria" ))) //
166
+ .consumeNextWith (indexInfo -> {
167
+ assertThat (indexInfo .getPartialFilterExpression ()).isEqualTo ("{ \" qty\" : { \" $gte\" : 10 } }" );
168
+ }).thenAwait ();
169
+ }
170
+
171
+ @ Test // DATAMONGO-1682
172
+ public void shouldApplyPartialDBOFilterCorrectly () {
173
+
174
+ assumeThat (mongoVersion .isGreaterThanOrEqualTo (THREE_DOT_TWO ), is (true ));
175
+
176
+ IndexDefinition id = new Index ().named ("partial-with-dbo" ).on ("k3y" , Direction .ASC )
177
+ .partial (of (new org .bson .Document ("qty" , new org .bson .Document ("$gte" , 10 ))));
178
+
179
+ indexOps .ensureIndex (id ).subscribe ();
180
+
181
+ StepVerifier .create (indexOps .getIndexInfo ().filter (this .indexByName ("partial-with-dbo" ))) //
182
+ .consumeNextWith (indexInfo -> {
183
+ assertThat (indexInfo .getPartialFilterExpression ()).isEqualTo ("{ \" qty\" : { \" $gte\" : 10 } }" );
184
+ }) //
185
+ .thenAwait ();
186
+
187
+ }
188
+
189
+ @ Test // DATAMONGO-1682
190
+ public void shouldFavorExplicitMappingHintViaClass () {
191
+
192
+ assumeThat (mongoVersion .isGreaterThanOrEqualTo (THREE_DOT_TWO ), is (true ));
193
+
194
+ IndexDefinition id = new Index ().named ("partial-with-inheritance" ).on ("k3y" , Direction .ASC )
195
+ .partial (of (where ("age" ).gte (10 )));
196
+
197
+ indexOps = new DefaultReactiveIndexOperations (template ,
198
+ this .template .getCollectionName (DefaultIndexOperationsIntegrationTestsSample .class ),
199
+ new QueryMapper (template .getConverter ()), MappingToSameCollection .class );
200
+
201
+ indexOps .ensureIndex (id ).subscribe ();
202
+
203
+ StepVerifier .create (indexOps .getIndexInfo ().filter (this .indexByName ("partial-with-inheritance" ))) //
204
+ .consumeNextWith (indexInfo -> {
205
+ assertThat (indexInfo .getPartialFilterExpression ()).isEqualTo ("{ \" a_g_e\" : { \" $gte\" : 10 } }" );
206
+ }) //
125
207
.verifyComplete ();
126
208
}
127
209
210
+ Predicate <IndexInfo > indexByName (String name ) {
211
+ return indexInfo -> indexInfo .getName ().equals (name );
212
+ }
213
+
214
+ @ org .springframework .data .mongodb .core .mapping .Document (collection = "default-index-operations-tests" )
215
+ static class DefaultIndexOperationsIntegrationTestsSample {
216
+
217
+ @ Field ("qty" ) Integer quantity ;
218
+ }
219
+
220
+ @ org .springframework .data .mongodb .core .mapping .Document (collection = "default-index-operations-tests" )
221
+ static class MappingToSameCollection
222
+ extends DefaultIndexOperationsIntegrationTests .DefaultIndexOperationsIntegrationTestsSample {
223
+
224
+ @ Field ("a_g_e" ) Integer age ;
225
+ }
226
+
128
227
}
0 commit comments