1717package com .mongodb .async .client ;
1818
1919import 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 ;
2025import com .mongodb .client .model .CountOptions ;
26+ import com .mongodb .client .model .DeleteOptions ;
2127import com .mongodb .client .model .FindOneAndDeleteOptions ;
2228import com .mongodb .client .model .FindOneAndReplaceOptions ;
2329import com .mongodb .client .model .FindOneAndUpdateOptions ;
@@ -106,6 +112,10 @@ public static Collection<Object[]> data() throws URISyntaxException, IOException
106112 List <Object []> data = new ArrayList <Object []>();
107113 for (File file : JsonPoweredTestHelper .getTestFiles ("/crud" )) {
108114 BsonDocument testDocument = JsonPoweredTestHelper .getTestDocument (file );
115+ if (testDocument .containsKey ("minServerVersion" )
116+ && !serverAtLeastMinVersion (testDocument .getString ("minServerVersion" ).getValue ())) {
117+ continue ;
118+ }
109119 for (BsonValue test : testDocument .getArray ("tests" )) {
110120 data .add (new Object []{file .getName (), test .asDocument ().getString ("description" ).getValue (),
111121 testDocument .getArray ("data" ), test .asDocument ()});
@@ -114,6 +124,17 @@ public static Collection<Object[]> data() throws URISyntaxException, IOException
114124 return data ;
115125 }
116126
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+
117138 private boolean checkResult () {
118139 if (filename .contains ("insert" )) {
119140 // We don't return any id's for insert commands
@@ -236,7 +257,15 @@ private AggregateIterable<BsonDocument> getAggregateMongoOperation(final BsonDoc
236257 for (BsonValue stage : arguments .getArray ("pipeline" )) {
237258 pipeline .add (stage .asDocument ());
238259 }
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 ;
240269 }
241270
242271 private MongoOperationLong getCountMongoOperation (final BsonDocument arguments ) {
@@ -250,34 +279,51 @@ public void execute() {
250279 if (arguments .containsKey ("limit" )) {
251280 options .limit (arguments .getNumber ("limit" ).intValue ());
252281 }
282+ if (arguments .containsKey ("collation" )) {
283+ options .collation (getCollation (arguments .getDocument ("collation" )));
284+ }
253285 collection .count (arguments .getDocument ("filter" ), options , getCallback ());
254286 }
255287 };
256288 }
257289
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 ;
260299 }
261300
262301 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" ));
264303 if (arguments .containsKey ("skip" )) {
265- findIterable .skip (arguments .getNumber ("skip" ).intValue ());
304+ iterable .skip (arguments .getNumber ("skip" ).intValue ());
266305 }
267306 if (arguments .containsKey ("limit" )) {
268- findIterable .limit (arguments .getNumber ("limit" ).intValue ());
307+ iterable .limit (arguments .getNumber ("limit" ).intValue ());
269308 }
270309 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" )));
272314 }
273- return findIterable ;
315+ return iterable ;
274316 }
275317
276318 private MongoOperationDeleteResult getDeleteManyMongoOperation (final BsonDocument arguments ) {
277319 return new MongoOperationDeleteResult () {
278320 @ Override
279321 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 ());
281327 }
282328 };
283329 }
@@ -286,7 +332,11 @@ private MongoOperationDeleteResult getDeleteOneMongoOperation(final BsonDocument
286332 return new MongoOperationDeleteResult () {
287333 @ Override
288334 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 ());
290340 }
291341 };
292342 }
@@ -302,6 +352,9 @@ public void execute() {
302352 if (arguments .containsKey ("sort" )) {
303353 options .sort (arguments .getDocument ("sort" ));
304354 }
355+ if (arguments .containsKey ("collation" )) {
356+ options .collation (getCollation (arguments .getDocument ("collation" )));
357+ }
305358 collection .findOneAndDelete (arguments .getDocument ("filter" ), options , getCallback ());
306359 }
307360 };
@@ -327,6 +380,9 @@ public void execute() {
327380 options .returnDocument (arguments .getString ("returnDocument" ).getValue ().equals ("After" ) ? ReturnDocument .AFTER
328381 : ReturnDocument .BEFORE );
329382 }
383+ if (arguments .containsKey ("collation" )) {
384+ options .collation (getCollation (arguments .getDocument ("collation" )));
385+ }
330386 collection .findOneAndReplace (arguments .getDocument ("filter" ), arguments .getDocument ("replacement" ), options , getCallback ());
331387 }
332388 };
@@ -351,6 +407,9 @@ public void execute() {
351407 options .returnDocument (arguments .getString ("returnDocument" ).getValue ().equals ("After" ) ? ReturnDocument .AFTER
352408 : ReturnDocument .BEFORE );
353409 }
410+ if (arguments .containsKey ("collation" )) {
411+ options .collation (getCollation (arguments .getDocument ("collation" )));
412+ }
354413 collection .findOneAndUpdate (arguments .getDocument ("filter" ), arguments .getDocument ("update" ), options , getCallback ());
355414 }
356415 };
@@ -386,6 +445,9 @@ public void execute() {
386445 if (arguments .containsKey ("upsert" )) {
387446 options .upsert (arguments .getBoolean ("upsert" ).getValue ());
388447 }
448+ if (arguments .containsKey ("collation" )) {
449+ options .collation (getCollation (arguments .getDocument ("collation" )));
450+ }
389451 collection .replaceOne (arguments .getDocument ("filter" ), arguments .getDocument ("replacement" ), options , getCallback ());
390452 }
391453 };
@@ -399,6 +461,9 @@ public void execute() {
399461 if (arguments .containsKey ("upsert" )) {
400462 options .upsert (arguments .getBoolean ("upsert" ).getValue ());
401463 }
464+ if (arguments .containsKey ("collation" )) {
465+ options .collation (getCollation (arguments .getDocument ("collation" )));
466+ }
402467 collection .updateMany (arguments .getDocument ("filter" ), arguments .getDocument ("update" ), options , getCallback ());
403468 }
404469 };
@@ -412,11 +477,50 @@ public void execute() {
412477 if (arguments .containsKey ("upsert" )) {
413478 options .upsert (arguments .getBoolean ("upsert" ).getValue ());
414479 }
480+ if (arguments .containsKey ("collation" )) {
481+ options .collation (getCollation (arguments .getDocument ("collation" )));
482+ }
415483 collection .updateOne (arguments .getDocument ("filter" ), arguments .getDocument ("update" ), options , getCallback ());
416484 }
417485 };
418486 }
419487
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+
420524 abstract class MongoOperationLong extends MongoOperation <Long > {
421525 }
422526
0 commit comments