24
24
25
25
import org .bson .BsonRegularExpression ;
26
26
import org .bson .Document ;
27
- import org .junit .Test ;
27
+ import org .junit .jupiter .api .Test ;
28
+
28
29
import org .springframework .data .geo .Point ;
29
30
import org .springframework .data .mongodb .InvalidMongoDbApiUsageException ;
30
31
import org .springframework .data .mongodb .core .geo .GeoJsonLineString ;
44
45
* @author Mark Paluch
45
46
* @author James McNee
46
47
*/
47
- public class CriteriaUnitTests {
48
+ class CriteriaUnitTests {
48
49
49
50
@ Test
50
- public void testSimpleCriteria () {
51
+ void testSimpleCriteria () {
51
52
Criteria c = new Criteria ("name" ).is ("Bubba" );
52
53
assertThat (c .getCriteriaObject ()).isEqualTo ("{ \" name\" : \" Bubba\" }" );
53
54
}
54
55
55
56
@ Test // GH-4850
56
- public void testCombiningSimpleCriteria () {
57
+ void testCombiningSimpleCriteria () {
57
58
58
59
Document expected = Document .parse ("{ name : { $eq : 123, $type : ['long'] } }" );
59
60
60
61
Criteria c = Criteria .where ("name" ) //
61
- .is (123 ) //
62
- .type (Type .INT_64 );
62
+ .is (123 ) //
63
+ .type (Type .INT_64 );
63
64
64
65
assertThat (c .getCriteriaObject ()).isEqualTo (expected );
65
66
66
67
c = Criteria .where ("name" ) //
67
- .type (Type .INT_64 )
68
- .is (123 );
68
+ .type (Type .INT_64 ).is (123 );
69
69
70
70
assertThat (c .getCriteriaObject ()).isEqualTo (expected );
71
71
}
72
72
73
73
@ Test // GH-4850
74
- public void testCombiningBsonRegexCriteria () {
74
+ void testCombiningBsonRegexCriteria () {
75
75
76
- Criteria c = Criteria .where ("name" )
77
- .regex (new BsonRegularExpression ("^spring$" ))
78
- .type (Type .INT_64 );
76
+ Criteria c = Criteria .where ("name" ).regex (new BsonRegularExpression ("^spring$" )).type (Type .INT_64 );
79
77
80
- assertThat (c .getCriteriaObject ()).isEqualTo (Document .parse ("{ name : { $regex : RegExp('^spring$'), $type : ['long'] } }" ));
78
+ assertThat (c .getCriteriaObject ())
79
+ .isEqualTo (Document .parse ("{ name : { $regex : RegExp('^spring$'), $type : ['long'] } }" ));
81
80
}
82
81
83
82
@ Test // GH-4850
84
- public void testCombiningRegexCriteria () {
83
+ void testCombiningRegexCriteria () {
85
84
86
- Criteria c = Criteria .where ("name" )
87
- .regex ("^spring$" )
88
- .type (Type .INT_64 );
85
+ Criteria c = Criteria .where ("name" ).regex ("^spring$" ).type (Type .INT_64 );
89
86
90
- assertThat (c .getCriteriaObject ()).hasEntrySatisfying ("name.$regex" , it -> assertThat (it ).isInstanceOf (Pattern .class ));
87
+ assertThat (c .getCriteriaObject ()).hasEntrySatisfying ("name.$regex" ,
88
+ it -> assertThat (it ).isInstanceOf (Pattern .class ));
91
89
}
92
90
93
91
@ Test
94
- public void testNotEqualCriteria () {
92
+ void testNotEqualCriteria () {
95
93
Criteria c = new Criteria ("name" ).ne ("Bubba" );
96
94
assertThat (c .getCriteriaObject ()).isEqualTo ("{ \" name\" : { \" $ne\" : \" Bubba\" }}" );
97
95
}
98
96
99
97
@ Test
100
- public void buildsIsNullCriteriaCorrectly () {
98
+ void buildsIsNullCriteriaCorrectly () {
101
99
102
100
Document reference = new Document ("name" , null );
103
101
@@ -106,19 +104,20 @@ public void buildsIsNullCriteriaCorrectly() {
106
104
}
107
105
108
106
@ Test
109
- public void testChainedCriteria () {
107
+ void testChainedCriteria () {
110
108
Criteria c = new Criteria ("name" ).is ("Bubba" ).and ("age" ).lt (21 );
111
109
assertThat (c .getCriteriaObject ()).isEqualTo ("{ \" name\" : \" Bubba\" , \" age\" : { \" $lt\" : 21}}" );
112
110
}
113
111
114
- @ Test ( expected = InvalidMongoDbApiUsageException . class )
115
- public void testCriteriaWithMultipleConditionsForSameKey () {
112
+ @ Test
113
+ void testCriteriaWithMultipleConditionsForSameKey () {
116
114
Criteria c = new Criteria ("name" ).gte ("M" ).and ("name" ).ne ("A" );
117
- c .getCriteriaObject ();
115
+
116
+ assertThatExceptionOfType (InvalidMongoDbApiUsageException .class ).isThrownBy (c ::getCriteriaObject );
118
117
}
119
118
120
119
@ Test
121
- public void equalIfCriteriaMatches () {
120
+ void equalIfCriteriaMatches () {
122
121
123
122
Criteria left = new Criteria ("name" ).is ("Foo" ).and ("lastname" ).is ("Bar" );
124
123
Criteria right = new Criteria ("name" ).is ("Bar" ).and ("lastname" ).is ("Bar" );
@@ -128,7 +127,7 @@ public void equalIfCriteriaMatches() {
128
127
}
129
128
130
129
@ Test // GH-3286
131
- public void shouldBuildCorrectAndOperator () {
130
+ void shouldBuildCorrectAndOperator () {
132
131
133
132
Collection <Criteria > operatorCriteria = Arrays .asList (Criteria .where ("x" ).is (true ), Criteria .where ("y" ).is (42 ),
134
133
Criteria .where ("z" ).is ("value" ));
@@ -140,7 +139,7 @@ public void shouldBuildCorrectAndOperator() {
140
139
}
141
140
142
141
@ Test // GH-3286
143
- public void shouldBuildCorrectOrOperator () {
142
+ void shouldBuildCorrectOrOperator () {
144
143
145
144
Collection <Criteria > operatorCriteria = Arrays .asList (Criteria .where ("x" ).is (true ), Criteria .where ("y" ).is (42 ),
146
145
Criteria .where ("z" ).is ("value" ));
@@ -152,7 +151,7 @@ public void shouldBuildCorrectOrOperator() {
152
151
}
153
152
154
153
@ Test // GH-3286
155
- public void shouldBuildCorrectNorOperator () {
154
+ void shouldBuildCorrectNorOperator () {
156
155
157
156
Collection <Criteria > operatorCriteria = Arrays .asList (Criteria .where ("x" ).is (true ), Criteria .where ("y" ).is (42 ),
158
157
Criteria .where ("z" ).is ("value" ));
@@ -164,28 +163,28 @@ public void shouldBuildCorrectNorOperator() {
164
163
}
165
164
166
165
@ Test // DATAMONGO-507
167
- public void shouldThrowExceptionWhenTryingToNegateAndOperation () {
166
+ void shouldThrowExceptionWhenTryingToNegateAndOperation () {
168
167
assertThatIllegalArgumentException ().isThrownBy (() -> new Criteria () //
169
168
.not () //
170
169
.andOperator (Criteria .where ("delete" ).is (true ).and ("_id" ).is (42 )));
171
170
}
172
171
173
172
@ Test // DATAMONGO-507
174
- public void shouldThrowExceptionWhenTryingToNegateOrOperation () {
173
+ void shouldThrowExceptionWhenTryingToNegateOrOperation () {
175
174
assertThatIllegalArgumentException ().isThrownBy (() -> new Criteria () //
176
175
.not () //
177
176
.orOperator (Criteria .where ("delete" ).is (true ).and ("_id" ).is (42 )));
178
177
}
179
178
180
179
@ Test // DATAMONGO-507
181
- public void shouldThrowExceptionWhenTryingToNegateNorOperation () {
180
+ void shouldThrowExceptionWhenTryingToNegateNorOperation () {
182
181
assertThatIllegalArgumentException ().isThrownBy (() -> new Criteria () //
183
182
.not () //
184
183
.norOperator (Criteria .where ("delete" ).is (true ).and ("_id" ).is (42 )));
185
184
}
186
185
187
186
@ Test // DATAMONGO-507
188
- public void shouldNegateFollowingSimpleExpression () {
187
+ void shouldNegateFollowingSimpleExpression () {
189
188
190
189
Criteria c = Criteria .where ("age" ).not ().gt (18 ).and ("status" ).is ("student" );
191
190
Document co = c .getCriteriaObject ();
@@ -195,111 +194,111 @@ public void shouldNegateFollowingSimpleExpression() {
195
194
}
196
195
197
196
@ Test // GH-3726
198
- public void shouldBuildCorrectSampleRateOperation () {
197
+ void shouldBuildCorrectSampleRateOperation () {
199
198
Criteria c = new Criteria ().sampleRate (0.4 );
200
199
assertThat (c .getCriteriaObject ()).isEqualTo ("{ \" $sampleRate\" : 0.4 }" );
201
200
}
202
201
203
202
@ Test // GH-3726
204
- public void shouldThrowExceptionWhenSampleRateIsNegative () {
203
+ void shouldThrowExceptionWhenSampleRateIsNegative () {
205
204
assertThatIllegalArgumentException ().isThrownBy (() -> new Criteria ().sampleRate (-1 ));
206
205
}
207
206
208
207
@ Test // GH-3726
209
- public void shouldThrowExceptionWhenSampleRateIsGreatedThanOne () {
208
+ void shouldThrowExceptionWhenSampleRateIsGreatedThanOne () {
210
209
assertThatIllegalArgumentException ().isThrownBy (() -> new Criteria ().sampleRate (1.01 ));
211
210
}
212
211
213
212
@ Test // DATAMONGO-1068
214
- public void getCriteriaObjectShouldReturnEmptyDocumentWhenNoCriteriaSpecified () {
213
+ void getCriteriaObjectShouldReturnEmptyDocumentWhenNoCriteriaSpecified () {
215
214
216
215
Document document = new Criteria ().getCriteriaObject ();
217
216
218
217
assertThat (document ).isEqualTo (new Document ());
219
218
}
220
219
221
220
@ Test // DATAMONGO-1068
222
- public void getCriteriaObjectShouldUseCritieraValuesWhenNoKeyIsPresent () {
221
+ void getCriteriaObjectShouldUseCritieraValuesWhenNoKeyIsPresent () {
223
222
224
223
Document document = new Criteria ().lt ("foo" ).getCriteriaObject ();
225
224
226
225
assertThat (document ).isEqualTo (new Document ().append ("$lt" , "foo" ));
227
226
}
228
227
229
228
@ Test // DATAMONGO-1068
230
- public void getCriteriaObjectShouldUseCritieraValuesWhenNoKeyIsPresentButMultipleCriteriasPresent () {
229
+ void getCriteriaObjectShouldUseCritieraValuesWhenNoKeyIsPresentButMultipleCriteriasPresent () {
231
230
232
231
Document document = new Criteria ().lt ("foo" ).gt ("bar" ).getCriteriaObject ();
233
232
234
233
assertThat (document ).isEqualTo (new Document ().append ("$lt" , "foo" ).append ("$gt" , "bar" ));
235
234
}
236
235
237
236
@ Test // DATAMONGO-1068
238
- public void getCriteriaObjectShouldRespectNotWhenNoKeyPresent () {
237
+ void getCriteriaObjectShouldRespectNotWhenNoKeyPresent () {
239
238
240
239
Document document = new Criteria ().lt ("foo" ).not ().getCriteriaObject ();
241
240
242
241
assertThat (document ).isEqualTo (new Document ().append ("$not" , new Document ("$lt" , "foo" )));
243
242
}
244
243
245
244
@ Test // GH-4220
246
- public void usesCorrectBsonType () {
245
+ void usesCorrectBsonType () {
247
246
248
247
Document document = new Criteria ("foo" ).type (Type .BOOLEAN ).getCriteriaObject ();
249
248
250
249
assertThat (document ).containsEntry ("foo.$type" , Collections .singletonList ("bool" ));
251
250
}
252
251
253
252
@ Test // DATAMONGO-1135
254
- public void geoJsonTypesShouldBeWrappedInGeometry () {
253
+ void geoJsonTypesShouldBeWrappedInGeometry () {
255
254
256
255
Document document = new Criteria ("foo" ).near (new GeoJsonPoint (100 , 200 )).getCriteriaObject ();
257
256
258
257
assertThat (document ).containsEntry ("foo.$near.$geometry" , new GeoJsonPoint (100 , 200 ));
259
258
}
260
259
261
260
@ Test // DATAMONGO-1135
262
- public void legacyCoordinateTypesShouldNotBeWrappedInGeometry () {
261
+ void legacyCoordinateTypesShouldNotBeWrappedInGeometry () {
263
262
264
263
Document document = new Criteria ("foo" ).near (new Point (100 , 200 )).getCriteriaObject ();
265
264
266
265
assertThat (document ).doesNotContainKey ("foo.$near.$geometry" );
267
266
}
268
267
269
268
@ Test // DATAMONGO-1135
270
- public void maxDistanceShouldBeMappedInsideNearWhenUsedAlongWithGeoJsonType () {
269
+ void maxDistanceShouldBeMappedInsideNearWhenUsedAlongWithGeoJsonType () {
271
270
272
271
Document document = new Criteria ("foo" ).near (new GeoJsonPoint (100 , 200 )).maxDistance (50D ).getCriteriaObject ();
273
272
274
273
assertThat (document ).containsEntry ("foo.$near.$maxDistance" , 50D );
275
274
}
276
275
277
276
@ Test // DATAMONGO-1135
278
- public void maxDistanceShouldBeMappedInsideNearSphereWhenUsedAlongWithGeoJsonType () {
277
+ void maxDistanceShouldBeMappedInsideNearSphereWhenUsedAlongWithGeoJsonType () {
279
278
280
279
Document document = new Criteria ("foo" ).nearSphere (new GeoJsonPoint (100 , 200 )).maxDistance (50D ).getCriteriaObject ();
281
280
282
281
assertThat (document ).containsEntry ("foo.$nearSphere.$maxDistance" , 50D );
283
282
}
284
283
285
284
@ Test // DATAMONGO-1110
286
- public void minDistanceShouldBeMappedInsideNearWhenUsedAlongWithGeoJsonType () {
285
+ void minDistanceShouldBeMappedInsideNearWhenUsedAlongWithGeoJsonType () {
287
286
288
287
Document document = new Criteria ("foo" ).near (new GeoJsonPoint (100 , 200 )).minDistance (50D ).getCriteriaObject ();
289
288
290
289
assertThat (document ).containsEntry ("foo.$near.$minDistance" , 50D );
291
290
}
292
291
293
292
@ Test // DATAMONGO-1110
294
- public void minDistanceShouldBeMappedInsideNearSphereWhenUsedAlongWithGeoJsonType () {
293
+ void minDistanceShouldBeMappedInsideNearSphereWhenUsedAlongWithGeoJsonType () {
295
294
296
295
Document document = new Criteria ("foo" ).nearSphere (new GeoJsonPoint (100 , 200 )).minDistance (50D ).getCriteriaObject ();
297
296
298
297
assertThat (document ).containsEntry ("foo.$nearSphere.$minDistance" , 50D );
299
298
}
300
299
301
300
@ Test // DATAMONGO-1110
302
- public void minAndMaxDistanceShouldBeMappedInsideNearSphereWhenUsedAlongWithGeoJsonType () {
301
+ void minAndMaxDistanceShouldBeMappedInsideNearSphereWhenUsedAlongWithGeoJsonType () {
303
302
304
303
Document document = new Criteria ("foo" ).nearSphere (new GeoJsonPoint (100 , 200 )).minDistance (50D ).maxDistance (100D )
305
304
.getCriteriaObject ();
@@ -309,12 +308,12 @@ public void minAndMaxDistanceShouldBeMappedInsideNearSphereWhenUsedAlongWithGeoJ
309
308
}
310
309
311
310
@ Test // DATAMONGO-1134
312
- public void intersectsShouldThrowExceptionWhenCalledWihtNullValue () {
311
+ void intersectsShouldThrowExceptionWhenCalledWihtNullValue () {
313
312
assertThatIllegalArgumentException ().isThrownBy (() -> new Criteria ("foo" ).intersects (null ));
314
313
}
315
314
316
315
@ Test // DATAMONGO-1134
317
- public void intersectsShouldWrapGeoJsonTypeInGeometryCorrectly () {
316
+ void intersectsShouldWrapGeoJsonTypeInGeometryCorrectly () {
318
317
319
318
GeoJsonLineString lineString = new GeoJsonLineString (new Point (0 , 0 ), new Point (10 , 10 ));
320
319
Document document = new Criteria ("foo" ).intersects (lineString ).getCriteriaObject ();
@@ -323,7 +322,7 @@ public void intersectsShouldWrapGeoJsonTypeInGeometryCorrectly() {
323
322
}
324
323
325
324
@ Test // DATAMONGO-1835
326
- public void extractsJsonSchemaInChainCorrectly () {
325
+ void extractsJsonSchemaInChainCorrectly () {
327
326
328
327
MongoJsonSchema schema = MongoJsonSchema .builder ().required ("name" ).build ();
329
328
Criteria criteria = Criteria .where ("foo" ).is ("bar" ).andDocumentStructureMatches (schema );
@@ -333,7 +332,7 @@ public void extractsJsonSchemaInChainCorrectly() {
333
332
}
334
333
335
334
@ Test // DATAMONGO-1835
336
- public void extractsJsonSchemaFromFactoryMethodCorrectly () {
335
+ void extractsJsonSchemaFromFactoryMethodCorrectly () {
337
336
338
337
MongoJsonSchema schema = MongoJsonSchema .builder ().required ("name" ).build ();
339
338
Criteria criteria = Criteria .matchingDocumentStructure (schema );
@@ -343,15 +342,15 @@ public void extractsJsonSchemaFromFactoryMethodCorrectly() {
343
342
}
344
343
345
344
@ Test // DATAMONGO-1808
346
- public void shouldAppendBitsAllClearWithIntBitmaskCorrectly () {
345
+ void shouldAppendBitsAllClearWithIntBitmaskCorrectly () {
347
346
348
347
Criteria numericBitmaskCriteria = new Criteria ("field" ).bits ().allClear (0b101);
349
348
350
349
assertThat (numericBitmaskCriteria .getCriteriaObject ()).isEqualTo ("{ \" field\" : { \" $bitsAllClear\" : 5} }" );
351
350
}
352
351
353
352
@ Test // DATAMONGO-1808
354
- public void shouldAppendBitsAllClearWithPositionListCorrectly () {
353
+ void shouldAppendBitsAllClearWithPositionListCorrectly () {
355
354
356
355
Criteria bitPositionsBitmaskCriteria = new Criteria ("field" ).bits ().allClear (Arrays .asList (0 , 2 ));
357
356
@@ -360,15 +359,15 @@ public void shouldAppendBitsAllClearWithPositionListCorrectly() {
360
359
}
361
360
362
361
@ Test // DATAMONGO-1808
363
- public void shouldAppendBitsAllSetWithIntBitmaskCorrectly () {
362
+ void shouldAppendBitsAllSetWithIntBitmaskCorrectly () {
364
363
365
364
Criteria numericBitmaskCriteria = new Criteria ("field" ).bits ().allSet (0b101);
366
365
367
366
assertThat (numericBitmaskCriteria .getCriteriaObject ()).isEqualTo ("{ \" field\" : { \" $bitsAllSet\" : 5} }" );
368
367
}
369
368
370
369
@ Test // DATAMONGO-1808
371
- public void shouldAppendBitsAllSetWithPositionListCorrectly () {
370
+ void shouldAppendBitsAllSetWithPositionListCorrectly () {
372
371
373
372
Criteria bitPositionsBitmaskCriteria = new Criteria ("field" ).bits ().allSet (Arrays .asList (0 , 2 ));
374
373
@@ -377,15 +376,15 @@ public void shouldAppendBitsAllSetWithPositionListCorrectly() {
377
376
}
378
377
379
378
@ Test // DATAMONGO-1808
380
- public void shouldAppendBitsAnyClearWithIntBitmaskCorrectly () {
379
+ void shouldAppendBitsAnyClearWithIntBitmaskCorrectly () {
381
380
382
381
Criteria numericBitmaskCriteria = new Criteria ("field" ).bits ().anyClear (0b101);
383
382
384
383
assertThat (numericBitmaskCriteria .getCriteriaObject ()).isEqualTo ("{ \" field\" : { \" $bitsAnyClear\" : 5} }" );
385
384
}
386
385
387
386
@ Test // DATAMONGO-1808
388
- public void shouldAppendBitsAnyClearWithPositionListCorrectly () {
387
+ void shouldAppendBitsAnyClearWithPositionListCorrectly () {
389
388
390
389
Criteria bitPositionsBitmaskCriteria = new Criteria ("field" ).bits ().anyClear (Arrays .asList (0 , 2 ));
391
390
@@ -394,15 +393,15 @@ public void shouldAppendBitsAnyClearWithPositionListCorrectly() {
394
393
}
395
394
396
395
@ Test // DATAMONGO-1808
397
- public void shouldAppendBitsAnySetWithIntBitmaskCorrectly () {
396
+ void shouldAppendBitsAnySetWithIntBitmaskCorrectly () {
398
397
399
398
Criteria numericBitmaskCriteria = new Criteria ("field" ).bits ().anySet (0b101);
400
399
401
400
assertThat (numericBitmaskCriteria .getCriteriaObject ()).isEqualTo ("{ \" field\" : { \" $bitsAnySet\" : 5} }" );
402
401
}
403
402
404
403
@ Test // DATAMONGO-1808
405
- public void shouldAppendBitsAnySetWithPositionListCorrectly () {
404
+ void shouldAppendBitsAnySetWithPositionListCorrectly () {
406
405
407
406
Criteria bitPositionsBitmaskCriteria = new Criteria ("field" ).bits ().anySet (Arrays .asList (0 , 2 ));
408
407
@@ -411,7 +410,7 @@ public void shouldAppendBitsAnySetWithPositionListCorrectly() {
411
410
}
412
411
413
412
@ Test // DATAMONGO-2002
414
- public void shouldEqualForSamePattern () {
413
+ void shouldEqualForSamePattern () {
415
414
416
415
Criteria left = new Criteria ("field" ).regex ("foo" );
417
416
Criteria right = new Criteria ("field" ).regex ("foo" );
@@ -420,7 +419,7 @@ public void shouldEqualForSamePattern() {
420
419
}
421
420
422
421
@ Test // DATAMONGO-2002
423
- public void shouldEqualForDocument () {
422
+ void shouldEqualForDocument () {
424
423
425
424
assertThat (new Criteria ("field" ).is (new Document ("one" , 1 ).append ("two" , "two" ).append ("null" , null )))
426
425
.isEqualTo (new Criteria ("field" ).is (new Document ("one" , 1 ).append ("two" , "two" ).append ("null" , null )));
@@ -439,7 +438,7 @@ public void shouldEqualForDocument() {
439
438
}
440
439
441
440
@ Test // DATAMONGO-2002
442
- public void shouldEqualForCollection () {
441
+ void shouldEqualForCollection () {
443
442
444
443
assertThat (new Criteria ("field" ).is (Arrays .asList ("foo" , "bar" )))
445
444
.isEqualTo (new Criteria ("field" ).is (Arrays .asList ("foo" , "bar" )));
@@ -459,7 +458,7 @@ public void shouldEqualForCollection() {
459
458
}
460
459
461
460
@ Test // GH-3414
462
- public void shouldEqualForSamePatternAndFlags () {
461
+ void shouldEqualForSamePatternAndFlags () {
463
462
464
463
Criteria left = new Criteria ("field" ).regex ("foo" , "iu" );
465
464
Criteria right = new Criteria ("field" ).regex ("foo" );
@@ -468,7 +467,7 @@ public void shouldEqualForSamePatternAndFlags() {
468
467
}
469
468
470
469
@ Test // GH-3414
471
- public void shouldEqualForNestedPattern () {
470
+ void shouldEqualForNestedPattern () {
472
471
473
472
Criteria left = new Criteria ("a" ).orOperator (new Criteria ("foo" ).regex ("value" , "i" ),
474
473
new Criteria ("bar" ).regex ("value" ));
0 commit comments