17
17
package com .mongodb .async .client ;
18
18
19
19
import com .mongodb .MongoNamespace ;
20
+ import com .mongodb .client .model .Collation ;
21
+ import com .mongodb .client .model .CollationAlternate ;
22
+ import com .mongodb .client .model .CollationCaseFirst ;
23
+ import com .mongodb .client .model .CollationMaxVariable ;
24
+ import com .mongodb .client .model .CollationStrength ;
20
25
import com .mongodb .client .model .CountOptions ;
26
+ import com .mongodb .client .model .DeleteOptions ;
21
27
import com .mongodb .client .model .FindOneAndDeleteOptions ;
22
28
import com .mongodb .client .model .FindOneAndReplaceOptions ;
23
29
import com .mongodb .client .model .FindOneAndUpdateOptions ;
@@ -106,6 +112,10 @@ public static Collection<Object[]> data() throws URISyntaxException, IOException
106
112
List <Object []> data = new ArrayList <Object []>();
107
113
for (File file : JsonPoweredTestHelper .getTestFiles ("/crud" )) {
108
114
BsonDocument testDocument = JsonPoweredTestHelper .getTestDocument (file );
115
+ if (testDocument .containsKey ("minServerVersion" )
116
+ && !serverAtLeastMinVersion (testDocument .getString ("minServerVersion" ).getValue ())) {
117
+ continue ;
118
+ }
109
119
for (BsonValue test : testDocument .getArray ("tests" )) {
110
120
data .add (new Object []{file .getName (), test .asDocument ().getString ("description" ).getValue (),
111
121
testDocument .getArray ("data" ), test .asDocument ()});
@@ -114,6 +124,17 @@ public static Collection<Object[]> data() throws URISyntaxException, IOException
114
124
return data ;
115
125
}
116
126
127
+ private static boolean serverAtLeastMinVersion (final String minServerVersionString ) {
128
+ List <Integer > versionList = new ArrayList <Integer >();
129
+ for (String s : minServerVersionString .split ("\\ ." )) {
130
+ versionList .add (Integer .valueOf (s ));
131
+ }
132
+ while (versionList .size () < 3 ) {
133
+ versionList .add (0 );
134
+ }
135
+ return serverVersionAtLeast (versionList .subList (0 , 3 ));
136
+ }
137
+
117
138
private boolean checkResult () {
118
139
if (filename .contains ("insert" )) {
119
140
// We don't return any id's for insert commands
@@ -236,7 +257,15 @@ private AggregateIterable<BsonDocument> getAggregateMongoOperation(final BsonDoc
236
257
for (BsonValue stage : arguments .getArray ("pipeline" )) {
237
258
pipeline .add (stage .asDocument ());
238
259
}
239
- return collection .aggregate (pipeline ).batchSize (arguments .getNumber ("batchSize" ).intValue ());
260
+ AggregateIterable <BsonDocument > iterable = collection .aggregate (pipeline );
261
+ if (arguments .containsKey ("batchSize" )) {
262
+ iterable .batchSize (arguments .getNumber ("batchSize" ).intValue ());
263
+ }
264
+ if (arguments .containsKey ("collation" )) {
265
+ iterable .collation (getCollation (arguments .getDocument ("collation" )));
266
+ }
267
+
268
+ return iterable ;
240
269
}
241
270
242
271
private MongoOperationLong getCountMongoOperation (final BsonDocument arguments ) {
@@ -250,34 +279,51 @@ public void execute() {
250
279
if (arguments .containsKey ("limit" )) {
251
280
options .limit (arguments .getNumber ("limit" ).intValue ());
252
281
}
282
+ if (arguments .containsKey ("collation" )) {
283
+ options .collation (getCollation (arguments .getDocument ("collation" )));
284
+ }
253
285
collection .count (arguments .getDocument ("filter" ), options , getCallback ());
254
286
}
255
287
};
256
288
}
257
289
258
- private DistinctIterable <BsonInt32 > getDistinctMongoOperation (final BsonDocument arguments ) {
259
- return collection .distinct (arguments .getString ("fieldName" ).getValue (), arguments .getDocument ("filter" ), BsonInt32 .class );
290
+ private DistinctIterable <BsonValue > getDistinctMongoOperation (final BsonDocument arguments ) {
291
+ DistinctIterable <BsonValue > iterable = collection .distinct (arguments .getString ("fieldName" ).getValue (), BsonValue .class );
292
+ if (arguments .containsKey ("filter" )) {
293
+ iterable .filter (arguments .getDocument ("filter" ));
294
+ }
295
+ if (arguments .containsKey ("collation" )) {
296
+ iterable .collation (getCollation (arguments .getDocument ("collation" )));
297
+ }
298
+ return iterable ;
260
299
}
261
300
262
301
private FindIterable <BsonDocument > getFindMongoOperation (final BsonDocument arguments ) {
263
- FindIterable <BsonDocument > findIterable = collection .find (arguments .getDocument ("filter" ));
302
+ FindIterable <BsonDocument > iterable = collection .find (arguments .getDocument ("filter" ));
264
303
if (arguments .containsKey ("skip" )) {
265
- findIterable .skip (arguments .getNumber ("skip" ).intValue ());
304
+ iterable .skip (arguments .getNumber ("skip" ).intValue ());
266
305
}
267
306
if (arguments .containsKey ("limit" )) {
268
- findIterable .limit (arguments .getNumber ("limit" ).intValue ());
307
+ iterable .limit (arguments .getNumber ("limit" ).intValue ());
269
308
}
270
309
if (arguments .containsKey ("batchSize" )) {
271
- findIterable .batchSize (arguments .getNumber ("batchSize" ).intValue ());
310
+ iterable .batchSize (arguments .getNumber ("batchSize" ).intValue ());
311
+ }
312
+ if (arguments .containsKey ("collation" )) {
313
+ iterable .collation (getCollation (arguments .getDocument ("collation" )));
272
314
}
273
- return findIterable ;
315
+ return iterable ;
274
316
}
275
317
276
318
private MongoOperationDeleteResult getDeleteManyMongoOperation (final BsonDocument arguments ) {
277
319
return new MongoOperationDeleteResult () {
278
320
@ Override
279
321
public void execute () {
280
- collection .deleteMany (arguments .getDocument ("filter" ), getCallback ());
322
+ DeleteOptions options = new DeleteOptions ();
323
+ if (arguments .containsKey ("collation" )) {
324
+ options .collation (getCollation (arguments .getDocument ("collation" )));
325
+ }
326
+ collection .deleteMany (arguments .getDocument ("filter" ), options , getCallback ());
281
327
}
282
328
};
283
329
}
@@ -286,7 +332,11 @@ private MongoOperationDeleteResult getDeleteOneMongoOperation(final BsonDocument
286
332
return new MongoOperationDeleteResult () {
287
333
@ Override
288
334
public void execute () {
289
- collection .deleteOne (arguments .getDocument ("filter" ), getCallback ());
335
+ DeleteOptions options = new DeleteOptions ();
336
+ if (arguments .containsKey ("collation" )) {
337
+ options .collation (getCollation (arguments .getDocument ("collation" )));
338
+ }
339
+ collection .deleteOne (arguments .getDocument ("filter" ), options , getCallback ());
290
340
}
291
341
};
292
342
}
@@ -302,6 +352,9 @@ public void execute() {
302
352
if (arguments .containsKey ("sort" )) {
303
353
options .sort (arguments .getDocument ("sort" ));
304
354
}
355
+ if (arguments .containsKey ("collation" )) {
356
+ options .collation (getCollation (arguments .getDocument ("collation" )));
357
+ }
305
358
collection .findOneAndDelete (arguments .getDocument ("filter" ), options , getCallback ());
306
359
}
307
360
};
@@ -327,6 +380,9 @@ public void execute() {
327
380
options .returnDocument (arguments .getString ("returnDocument" ).getValue ().equals ("After" ) ? ReturnDocument .AFTER
328
381
: ReturnDocument .BEFORE );
329
382
}
383
+ if (arguments .containsKey ("collation" )) {
384
+ options .collation (getCollation (arguments .getDocument ("collation" )));
385
+ }
330
386
collection .findOneAndReplace (arguments .getDocument ("filter" ), arguments .getDocument ("replacement" ), options , getCallback ());
331
387
}
332
388
};
@@ -351,6 +407,9 @@ public void execute() {
351
407
options .returnDocument (arguments .getString ("returnDocument" ).getValue ().equals ("After" ) ? ReturnDocument .AFTER
352
408
: ReturnDocument .BEFORE );
353
409
}
410
+ if (arguments .containsKey ("collation" )) {
411
+ options .collation (getCollation (arguments .getDocument ("collation" )));
412
+ }
354
413
collection .findOneAndUpdate (arguments .getDocument ("filter" ), arguments .getDocument ("update" ), options , getCallback ());
355
414
}
356
415
};
@@ -386,6 +445,9 @@ public void execute() {
386
445
if (arguments .containsKey ("upsert" )) {
387
446
options .upsert (arguments .getBoolean ("upsert" ).getValue ());
388
447
}
448
+ if (arguments .containsKey ("collation" )) {
449
+ options .collation (getCollation (arguments .getDocument ("collation" )));
450
+ }
389
451
collection .replaceOne (arguments .getDocument ("filter" ), arguments .getDocument ("replacement" ), options , getCallback ());
390
452
}
391
453
};
@@ -399,6 +461,9 @@ public void execute() {
399
461
if (arguments .containsKey ("upsert" )) {
400
462
options .upsert (arguments .getBoolean ("upsert" ).getValue ());
401
463
}
464
+ if (arguments .containsKey ("collation" )) {
465
+ options .collation (getCollation (arguments .getDocument ("collation" )));
466
+ }
402
467
collection .updateMany (arguments .getDocument ("filter" ), arguments .getDocument ("update" ), options , getCallback ());
403
468
}
404
469
};
@@ -412,11 +477,50 @@ public void execute() {
412
477
if (arguments .containsKey ("upsert" )) {
413
478
options .upsert (arguments .getBoolean ("upsert" ).getValue ());
414
479
}
480
+ if (arguments .containsKey ("collation" )) {
481
+ options .collation (getCollation (arguments .getDocument ("collation" )));
482
+ }
415
483
collection .updateOne (arguments .getDocument ("filter" ), arguments .getDocument ("update" ), options , getCallback ());
416
484
}
417
485
};
418
486
}
419
487
488
+
489
+ Collation getCollation (final BsonDocument bsonCollation ) {
490
+ Collation .Builder builder = Collation .builder ();
491
+ if (bsonCollation .containsKey ("locale" )) {
492
+ builder .locale (bsonCollation .getString ("locale" ).getValue ());
493
+ }
494
+ if (bsonCollation .containsKey ("caseLevel" )) {
495
+ builder .caseLevel (bsonCollation .getBoolean ("caseLevel" ).getValue ());
496
+ }
497
+ if (bsonCollation .containsKey ("caseFirst" )) {
498
+ builder .collationCaseFirst (CollationCaseFirst .fromString (bsonCollation .getString ("caseFirst" ).getValue ()));
499
+ }
500
+ if (bsonCollation .containsKey ("strength" )) {
501
+ builder .collationStrength (CollationStrength .fromInt (bsonCollation .getInt32 ("strength" ).getValue ()));
502
+ }
503
+ if (bsonCollation .containsKey ("numericOrdering" )) {
504
+ builder .numericOrdering (bsonCollation .getBoolean ("numericOrdering" ).getValue ());
505
+ }
506
+ if (bsonCollation .containsKey ("strength" )) {
507
+ builder .collationStrength (CollationStrength .fromInt (bsonCollation .getInt32 ("strength" ).getValue ()));
508
+ }
509
+ if (bsonCollation .containsKey ("alternate" )) {
510
+ builder .collationAlternate (CollationAlternate .fromString (bsonCollation .getString ("alternate" ).getValue ()));
511
+ }
512
+ if (bsonCollation .containsKey ("maxVariable" )) {
513
+ builder .collationMaxVariable (CollationMaxVariable .fromString (bsonCollation .getString ("maxVariable" ).getValue ()));
514
+ }
515
+ if (bsonCollation .containsKey ("normalization" )) {
516
+ builder .normalization (bsonCollation .getBoolean ("normalization" ).getValue ());
517
+ }
518
+ if (bsonCollation .containsKey ("backwards" )) {
519
+ builder .backwards (bsonCollation .getBoolean ("backwards" ).getValue ());
520
+ }
521
+ return builder .build ();
522
+ }
523
+
420
524
abstract class MongoOperationLong extends MongoOperation <Long > {
421
525
}
422
526
0 commit comments