16
16
17
17
package org .springframework .data .couchbase .repository ;
18
18
19
+ import static com .couchbase .client .java .query .QueryScanConsistency .NOT_BOUNDED ;
20
+ import static com .couchbase .client .java .query .QueryScanConsistency .REQUEST_PLUS ;
19
21
import static java .util .Arrays .asList ;
20
22
import static org .assertj .core .api .Assertions .assertThat ;
21
23
import static org .junit .jupiter .api .Assertions .assertEquals ;
25
27
import static org .junit .jupiter .api .Assertions .assertNull ;
26
28
import static org .junit .jupiter .api .Assertions .assertThrows ;
27
29
import static org .junit .jupiter .api .Assertions .assertTrue ;
30
+ import static org .springframework .data .couchbase .config .BeanNames .COUCHBASE_TEMPLATE ;
28
31
29
32
import junit .framework .AssertionFailedError ;
30
33
46
49
47
50
import org .junit .jupiter .api .Test ;
48
51
import org .springframework .beans .factory .annotation .Autowired ;
52
+ import org .springframework .context .ApplicationContext ;
53
+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
49
54
import org .springframework .context .annotation .Bean ;
50
55
import org .springframework .context .annotation .Configuration ;
51
56
import org .springframework .dao .DataIntegrityViolationException ;
@@ -267,6 +272,44 @@ void findBySimplePropertyReturnType() {
267
272
}
268
273
}
269
274
275
+ @ Test
276
+ public void saveNotBoundedRequestPlus () {
277
+ ApplicationContext ac = new AnnotationConfigApplicationContext (ConfigRequestPlus .class );
278
+ // the Config class has been modified, these need to be loaded again
279
+ CouchbaseTemplate couchbaseTemplateRP = (CouchbaseTemplate ) ac .getBean (COUCHBASE_TEMPLATE );
280
+ AirportRepository airportRepositoryRP = (AirportRepository ) ac .getBean ("airportRepository" );
281
+
282
+ // save() followed by query with NOT_BOUNDED will result in not finding the document
283
+ Airport vie = new Airport ("airports::vie" , "vie" , "low9" );
284
+ Airport airport2 = null ;
285
+ for (int i = 1 ; i <= 100 ; i ++) {
286
+ // set version == 0 so save() will be an upsert, not a replace
287
+ Airport saved = airportRepositoryRP .save (vie .clearVersion ());
288
+ try {
289
+ airport2 = airportRepositoryRP .iata (saved .getIata ());
290
+ if (airport2 == null ) {
291
+ break ;
292
+ }
293
+ } catch (DataRetrievalFailureException drfe ) {
294
+ airport2 = null ; //
295
+ } finally {
296
+ // airportRepository.delete(vie);
297
+ // instead of delete, use removeResult to test QueryOptions.consistentWith()
298
+ RemoveResult removeResult = couchbaseTemplateRP .removeById ().one (vie .getId ());
299
+ assertEquals (vie .getId (), removeResult .getId ());
300
+ assertTrue (removeResult .getCas () != 0 );
301
+ assertTrue (removeResult .getMutationToken ().isPresent ());
302
+ Airport airport3 = airportRepositoryRP .iata (vie .getIata ());
303
+ assertNull (airport3 , "should have been removed" );
304
+ }
305
+ }
306
+ assertNotNull (airport2 , "airport2 should have never been null" );
307
+ Airport saved = airportRepositoryRP .save (vie .clearVersion ());
308
+ List <Airport > airports = couchbaseTemplateRP .findByQuery (Airport .class ).withConsistency (NOT_BOUNDED ).all ();
309
+ RemoveResult removeResult = couchbaseTemplateRP .removeById ().one (saved .getId ());
310
+ assertFalse (!airports .isEmpty (), "airports should have been empty" );
311
+ }
312
+
270
313
@ Test
271
314
void findByTypeAlias () {
272
315
Airport vie = null ;
@@ -386,9 +429,7 @@ public void saveNotBounded() {
386
429
// set version == 0 so save() will be an upsert, not a replace
387
430
Airport saved = airportRepository .save (vie .clearVersion ());
388
431
try {
389
- airport2 = airportRepository
390
- .withOptions (QueryOptions .queryOptions ().scanConsistency (QueryScanConsistency .NOT_BOUNDED ))
391
- .iata (saved .getIata ());
432
+ airport2 = airportRepository .iata (saved .getIata ());
392
433
if (airport2 == null ) {
393
434
break ;
394
435
}
@@ -401,14 +442,16 @@ public void saveNotBounded() {
401
442
assertEquals (vie .getId (), removeResult .getId ());
402
443
assertTrue (removeResult .getCas () != 0 );
403
444
assertTrue (removeResult .getMutationToken ().isPresent ());
404
- Airport airport3 = airportRepository
405
- .withOptions (QueryOptions .queryOptions ().scanConsistency (QueryScanConsistency .REQUEST_PLUS )
406
- .consistentWith (MutationState .from (removeResult .getMutationToken ().get ())))
407
- .iata (vie .getIata ());
445
+ Airport airport3 = airportRepository .iata (vie .getIata ());
408
446
assertNull (airport3 , "should have been removed" );
409
447
}
410
448
}
411
449
assertNull (airport2 , "airport2 should have likely been null at least once" );
450
+ Airport saved = airportRepository .save (vie .clearVersion ());
451
+ couchbaseTemplate .findByQuery (Airport .class ).withConsistency (REQUEST_PLUS ).all ();
452
+ airport2 = airportRepository .iata (vie .getIata ());
453
+ RemoveResult removeResult = couchbaseTemplate .removeById ().one (saved .getId ());
454
+ assertNotNull (airport2 , "airport2 should have been found" );
412
455
}
413
456
414
457
@ Test
@@ -474,7 +517,7 @@ void count() {
474
517
airportRepository .saveAll (
475
518
Arrays .stream (iatas ).map ((iata ) -> new Airport ("airports::" + iata , iata , iata .toLowerCase (Locale .ROOT )))
476
519
.collect (Collectors .toSet ()));
477
- couchbaseTemplate .findByQuery (Airport .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
520
+ couchbaseTemplate .findByQuery (Airport .class ).withConsistency (REQUEST_PLUS ).all ();
478
521
Long count = airportRepository .countFancyExpression (asList ("JFK" ), asList ("jfk" ), false );
479
522
assertEquals (1 , count );
480
523
@@ -677,7 +720,7 @@ void deleteAllById() {
677
720
void couchbaseRepositoryQuery () throws Exception {
678
721
User user = new User ("1" , "Dave" , "Wilson" );
679
722
userRepository .save (user );
680
- couchbaseTemplate .findByQuery (User .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS )
723
+ couchbaseTemplate .findByQuery (User .class ).withConsistency (REQUEST_PLUS )
681
724
.matching (QueryCriteria .where ("firstname" ).is ("Dave" ).and ("`1`" ).is ("`1`" )).all ();
682
725
String input = "findByFirstname" ;
683
726
Method method = UserRepository .class .getMethod (input , String .class );
@@ -809,4 +852,45 @@ public ValidatingCouchbaseEventListener validationEventListener() {
809
852
return new ValidatingCouchbaseEventListener (validator ());
810
853
}
811
854
}
855
+
856
+ @ Configuration
857
+ @ EnableCouchbaseRepositories ("org.springframework.data.couchbase" )
858
+ @ EnableCouchbaseAuditing (auditorAwareRef = "auditorAwareRef" , dateTimeProviderRef = "dateTimeProviderRef" )
859
+ static class ConfigRequestPlus extends AbstractCouchbaseConfiguration {
860
+
861
+ @ Override
862
+ public String getConnectionString () {
863
+ return connectionString ();
864
+ }
865
+
866
+ @ Override
867
+ public String getUserName () {
868
+ return config ().adminUsername ();
869
+ }
870
+
871
+ @ Override
872
+ public String getPassword () {
873
+ return config ().adminPassword ();
874
+ }
875
+
876
+ @ Override
877
+ public String getBucketName () {
878
+ return bucketName ();
879
+ }
880
+
881
+ @ Bean (name = "auditorAwareRef" )
882
+ public NaiveAuditorAware testAuditorAware () {
883
+ return new NaiveAuditorAware ();
884
+ }
885
+
886
+ @ Bean (name = "dateTimeProviderRef" )
887
+ public DateTimeProvider testDateTimeProvider () {
888
+ return new AuditingDateTimeProvider ();
889
+ }
890
+
891
+ @ Override
892
+ public QueryScanConsistency getDefaultConsistency () {
893
+ return REQUEST_PLUS ;
894
+ }
895
+ }
812
896
}
0 commit comments