1616
1717package org .springframework .data .couchbase .core ;
1818
19+ import static com .couchbase .client .java .query .QueryScanConsistency .REQUEST_PLUS ;
1920import static org .junit .jupiter .api .Assertions .assertEquals ;
2021import static org .junit .jupiter .api .Assertions .assertNotNull ;
2122import static org .junit .jupiter .api .Assertions .assertNull ;
2627import java .time .temporal .TemporalAccessor ;
2728import java .util .Arrays ;
2829import java .util .List ;
30+ import java .util .Locale ;
2931import java .util .UUID ;
3032import java .util .stream .Collectors ;
3133
34+ import com .couchbase .client .java .query .QueryOptions ;
3235import org .junit .jupiter .api .BeforeEach ;
3336import org .junit .jupiter .api .Test ;
3437import org .springframework .data .couchbase .core .query .Query ;
5053import org .springframework .data .couchbase .util .JavaIntegrationTests ;
5154
5255import com .couchbase .client .java .query .QueryScanConsistency ;
56+ import org .springframework .data .domain .PageRequest ;
57+ import org .springframework .data .domain .Pageable ;
58+ import org .springframework .data .domain .Sort ;
5359
5460/**
5561 * Query tests Theses tests rely on a cb server running
@@ -73,7 +79,7 @@ public void beforeEach() {
7379 // ensure each test starts with clean state
7480
7581 couchbaseTemplate .removeByQuery (User .class ).all ();
76- couchbaseTemplate .findByQuery (User .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
82+ couchbaseTemplate .findByQuery (User .class ).withConsistency (REQUEST_PLUS ).all ();
7783 }
7884
7985 @ Test
@@ -85,7 +91,7 @@ void findByQueryAll() {
8591 couchbaseTemplate .upsertById (User .class ).all (Arrays .asList (user1 , user2 ));
8692
8793 final List <User > foundUsers = couchbaseTemplate .findByQuery (User .class )
88- .withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
94+ .withConsistency (REQUEST_PLUS ).all ();
8995
9096 for (User u : foundUsers ) {
9197 if (!(u .equals (user1 ) || u .equals (user2 ))) {
@@ -108,7 +114,7 @@ void findByQueryAll() {
108114 couchbaseTemplate .findById (User .class ).one (user1 .getId ());
109115 reactiveCouchbaseTemplate .findById (User .class ).one (user1 .getId ()).block ();
110116 } finally {
111- couchbaseTemplate .removeByQuery (User .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
117+ couchbaseTemplate .removeByQuery (User .class ).withConsistency (REQUEST_PLUS ).all ();
112118 }
113119
114120 User usery = couchbaseTemplate .findById (User .class ).one ("user1" );
@@ -128,7 +134,7 @@ void findByMatchingQuery() {
128134
129135 Query specialUsers = new Query (QueryCriteria .where (i ("firstname" )).like ("special" ));
130136 final List <User > foundUsers = couchbaseTemplate .findByQuery (User .class )
131- .withConsistency (QueryScanConsistency . REQUEST_PLUS ).matching (specialUsers ).all ();
137+ .withConsistency (REQUEST_PLUS ).matching (specialUsers ).all ();
132138
133139 assertEquals (1 , foundUsers .size ());
134140 }
@@ -142,7 +148,7 @@ void findAssessmentDO() {
142148
143149 Query specialUsers = new Query (QueryCriteria .where (i ("id" )).is (ado .getId ()));
144150 final List <AssessmentDO > foundUsers = couchbaseTemplate .findByQuery (AssessmentDO .class )
145- .withConsistency (QueryScanConsistency . REQUEST_PLUS ).matching (specialUsers ).all ();
151+ .withConsistency (REQUEST_PLUS ).matching (specialUsers ).all ();
146152 assertEquals ("123" , foundUsers .get (0 ).getId (), "id" );
147153 assertEquals ("44444444" , foundUsers .get (0 ).getDocumentId (), "documentId" );
148154 assertEquals (ado , foundUsers .get (0 ));
@@ -169,7 +175,7 @@ void findByMatchingQueryProjected() {
169175 Query daveUsers = new Query (QueryCriteria .where ("username" ).like ("dave" ));
170176
171177 final List <UserSubmissionProjected > foundUserSubmissions = couchbaseTemplate .findByQuery (UserSubmission .class )
172- .as (UserSubmissionProjected .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).matching (daveUsers ).all ();
178+ .as (UserSubmissionProjected .class ).withConsistency (REQUEST_PLUS ).matching (daveUsers ).all ();
173179 assertEquals (1 , foundUserSubmissions .size ());
174180 assertEquals (user .getUsername (), foundUserSubmissions .get (0 ).getUsername ());
175181 assertEquals (user .getId (), foundUserSubmissions .get (0 ).getId ());
@@ -186,11 +192,11 @@ void findByMatchingQueryProjected() {
186192
187193 Query specialUsers = new Query (QueryCriteria .where ("firstname" ).like ("special" ));
188194 final List <UserJustLastName > foundUsers = couchbaseTemplate .findByQuery (User .class ).as (UserJustLastName .class )
189- .withConsistency (QueryScanConsistency . REQUEST_PLUS ).matching (specialUsers ).all ();
195+ .withConsistency (REQUEST_PLUS ).matching (specialUsers ).all ();
190196 assertEquals (1 , foundUsers .size ());
191197
192198 final List <UserJustLastName > foundUsersReactive = reactiveCouchbaseTemplate .findByQuery (User .class )
193- .as (UserJustLastName .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).matching (specialUsers ).all ()
199+ .as (UserJustLastName .class ).withConsistency (REQUEST_PLUS ).matching (specialUsers ).all ()
194200 .collectList ().block ();
195201 assertEquals (1 , foundUsersReactive .size ());
196202
@@ -206,7 +212,7 @@ void removeByQueryAll() {
206212 assertTrue (couchbaseTemplate .existsById ().one (user1 .getId ()));
207213 assertTrue (couchbaseTemplate .existsById ().one (user2 .getId ()));
208214
209- couchbaseTemplate .removeByQuery (User .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
215+ couchbaseTemplate .removeByQuery (User .class ).withConsistency (REQUEST_PLUS ).all ();
210216
211217 assertNull (couchbaseTemplate .findById (User .class ).one (user1 .getId ()));
212218 assertNull (couchbaseTemplate .findById (User .class ).one (user2 .getId ()));
@@ -227,7 +233,7 @@ void removeByMatchingQuery() {
227233
228234 Query nonSpecialUsers = new Query (QueryCriteria .where (i ("firstname" )).notLike ("special" ));
229235
230- couchbaseTemplate .removeByQuery (User .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS )
236+ couchbaseTemplate .removeByQuery (User .class ).withConsistency (REQUEST_PLUS )
231237 .matching (nonSpecialUsers ).all ();
232238
233239 assertNull (couchbaseTemplate .findById (User .class ).one (user1 .getId ()));
@@ -252,17 +258,17 @@ void distinct() {
252258
253259 // distinct icao
254260 List <Airport > airports1 = couchbaseTemplate .findByQuery (Airport .class ).distinct (new String [] { "icao" })
255- .as (Airport .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
261+ .as (Airport .class ).withConsistency (REQUEST_PLUS ).all ();
256262 assertEquals (2 , airports1 .size ());
257263
258264 // distinct all-fields-in-Airport.class
259265 List <Airport > airports2 = couchbaseTemplate .findByQuery (Airport .class ).distinct (new String [] {}).as (Airport .class )
260- .withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
266+ .withConsistency (REQUEST_PLUS ).all ();
261267 assertEquals (7 , airports2 .size ());
262268
263269 // count( distinct { iata, icao } )
264270 long count1 = couchbaseTemplate .findByQuery (Airport .class ).distinct (new String [] { "iata" , "icao" })
265- .as (Airport .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).count ();
271+ .as (Airport .class ).withConsistency (REQUEST_PLUS ).count ();
266272 assertEquals (7 , count1 );
267273
268274 } finally {
@@ -287,22 +293,22 @@ void distinctReactive() {
287293
288294 // distinct icao
289295 List <Airport > airports1 = reactiveCouchbaseTemplate .findByQuery (Airport .class ).distinct (new String [] { "icao" })
290- .as (Airport .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ().collectList ().block ();
296+ .as (Airport .class ).withConsistency (REQUEST_PLUS ).all ().collectList ().block ();
291297 assertEquals (2 , airports1 .size ());
292298
293299 // distinct all-fields-in-Airport.class
294300 List <Airport > airports2 = reactiveCouchbaseTemplate .findByQuery (Airport .class ).distinct (new String [] {})
295- .as (Airport .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ().collectList ().block ();
301+ .as (Airport .class ).withConsistency (REQUEST_PLUS ).all ().collectList ().block ();
296302 assertEquals (7 , airports2 .size ());
297303
298304 // count( distinct icao )
299305 Long count1 = reactiveCouchbaseTemplate .findByQuery (Airport .class ).distinct (new String [] { "icao" })
300- .as (Airport .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).count ().block ();
306+ .as (Airport .class ).withConsistency (REQUEST_PLUS ).count ().block ();
301307 assertEquals (2 , count1 );
302308
303309 // count( distinct { icao, iata } )
304310 Long count2 = reactiveCouchbaseTemplate .findByQuery (Airport .class ).distinct (new String [] { "icao" , "iata" })
305- .withConsistency (QueryScanConsistency . REQUEST_PLUS ).count ().block ();
311+ .withConsistency (REQUEST_PLUS ).count ().block ();
306312 assertEquals (7 , count2 );
307313
308314 } finally {
@@ -312,4 +318,31 @@ void distinctReactive() {
312318 }
313319 }
314320
321+ @ Test
322+ void sortedTemplate () {
323+ couchbaseTemplate .removeByQuery (Airport .class ).withConsistency (REQUEST_PLUS ).all ();
324+ String [] iatas = { "JFK" , "IAD" , "SFO" , "SJC" , "SEA" , "LAX" , "PHX" };
325+
326+ try {
327+ couchbaseTemplate .insertById (Airport .class ).all (
328+ Arrays .stream (iatas ).map ((iata ) -> new Airport ("airports::" + iata , iata , iata .toLowerCase (Locale .ROOT )))
329+ .collect (Collectors .toSet ()));
330+
331+ org .springframework .data .couchbase .core .query .Query query = org .springframework .data .couchbase .core .query .Query .query (QueryCriteria .where ("iata" ).isNotNull ());
332+ Pageable pageableWithSort = PageRequest .of (0 , 7 , Sort .by ("iata" ));
333+ query .with (pageableWithSort );
334+ List <Airport > airports = couchbaseTemplate .findByQuery (Airport .class ).withConsistency (REQUEST_PLUS ).matching (query ).all ();
335+
336+ String [] sortedIatas = iatas .clone ();
337+ System .out .println ("" +iatas .length +" " +sortedIatas .length );
338+ Arrays .sort (sortedIatas );
339+ for (int i =0 ; i < pageableWithSort .getPageSize (); i ++){
340+ System .out .println (airports .get (i ).getIata ());
341+ assertEquals (sortedIatas [i ], airports .get (i ).getIata ());
342+ }
343+ } finally {
344+ couchbaseTemplate .removeById (Airport .class ).all (Arrays .stream (iatas ).map ((iata ) -> "airports::" + iata ).collect (Collectors .toSet ()));
345+ }
346+ }
347+
315348}
0 commit comments