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