16
16
17
17
package org .springframework .data .couchbase .core ;
18
18
19
+ import static com .couchbase .client .java .query .QueryScanConsistency .REQUEST_PLUS ;
19
20
import static org .junit .jupiter .api .Assertions .assertEquals ;
20
21
import static org .junit .jupiter .api .Assertions .assertNotNull ;
21
22
import static org .junit .jupiter .api .Assertions .assertNull ;
26
27
import java .time .temporal .TemporalAccessor ;
27
28
import java .util .Arrays ;
28
29
import java .util .List ;
30
+ import java .util .Locale ;
29
31
import java .util .UUID ;
30
32
import java .util .stream .Collectors ;
31
33
34
+ import com .couchbase .client .java .query .QueryOptions ;
32
35
import org .junit .jupiter .api .BeforeEach ;
33
36
import org .junit .jupiter .api .Test ;
34
37
import org .springframework .data .couchbase .core .query .Query ;
50
53
import org .springframework .data .couchbase .util .JavaIntegrationTests ;
51
54
52
55
import 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 ;
53
59
54
60
/**
55
61
* Query tests Theses tests rely on a cb server running
@@ -73,7 +79,7 @@ public void beforeEach() {
73
79
// ensure each test starts with clean state
74
80
75
81
couchbaseTemplate .removeByQuery (User .class ).all ();
76
- couchbaseTemplate .findByQuery (User .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
82
+ couchbaseTemplate .findByQuery (User .class ).withConsistency (REQUEST_PLUS ).all ();
77
83
}
78
84
79
85
@ Test
@@ -85,7 +91,7 @@ void findByQueryAll() {
85
91
couchbaseTemplate .upsertById (User .class ).all (Arrays .asList (user1 , user2 ));
86
92
87
93
final List <User > foundUsers = couchbaseTemplate .findByQuery (User .class )
88
- .withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
94
+ .withConsistency (REQUEST_PLUS ).all ();
89
95
90
96
for (User u : foundUsers ) {
91
97
if (!(u .equals (user1 ) || u .equals (user2 ))) {
@@ -108,7 +114,7 @@ void findByQueryAll() {
108
114
couchbaseTemplate .findById (User .class ).one (user1 .getId ());
109
115
reactiveCouchbaseTemplate .findById (User .class ).one (user1 .getId ()).block ();
110
116
} finally {
111
- couchbaseTemplate .removeByQuery (User .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
117
+ couchbaseTemplate .removeByQuery (User .class ).withConsistency (REQUEST_PLUS ).all ();
112
118
}
113
119
114
120
User usery = couchbaseTemplate .findById (User .class ).one ("user1" );
@@ -128,7 +134,7 @@ void findByMatchingQuery() {
128
134
129
135
Query specialUsers = new Query (QueryCriteria .where (i ("firstname" )).like ("special" ));
130
136
final List <User > foundUsers = couchbaseTemplate .findByQuery (User .class )
131
- .withConsistency (QueryScanConsistency . REQUEST_PLUS ).matching (specialUsers ).all ();
137
+ .withConsistency (REQUEST_PLUS ).matching (specialUsers ).all ();
132
138
133
139
assertEquals (1 , foundUsers .size ());
134
140
}
@@ -142,7 +148,7 @@ void findAssessmentDO() {
142
148
143
149
Query specialUsers = new Query (QueryCriteria .where (i ("id" )).is (ado .getId ()));
144
150
final List <AssessmentDO > foundUsers = couchbaseTemplate .findByQuery (AssessmentDO .class )
145
- .withConsistency (QueryScanConsistency . REQUEST_PLUS ).matching (specialUsers ).all ();
151
+ .withConsistency (REQUEST_PLUS ).matching (specialUsers ).all ();
146
152
assertEquals ("123" , foundUsers .get (0 ).getId (), "id" );
147
153
assertEquals ("44444444" , foundUsers .get (0 ).getDocumentId (), "documentId" );
148
154
assertEquals (ado , foundUsers .get (0 ));
@@ -169,7 +175,7 @@ void findByMatchingQueryProjected() {
169
175
Query daveUsers = new Query (QueryCriteria .where ("username" ).like ("dave" ));
170
176
171
177
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 ();
173
179
assertEquals (1 , foundUserSubmissions .size ());
174
180
assertEquals (user .getUsername (), foundUserSubmissions .get (0 ).getUsername ());
175
181
assertEquals (user .getId (), foundUserSubmissions .get (0 ).getId ());
@@ -186,11 +192,11 @@ void findByMatchingQueryProjected() {
186
192
187
193
Query specialUsers = new Query (QueryCriteria .where ("firstname" ).like ("special" ));
188
194
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 ();
190
196
assertEquals (1 , foundUsers .size ());
191
197
192
198
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 ()
194
200
.collectList ().block ();
195
201
assertEquals (1 , foundUsersReactive .size ());
196
202
@@ -206,7 +212,7 @@ void removeByQueryAll() {
206
212
assertTrue (couchbaseTemplate .existsById ().one (user1 .getId ()));
207
213
assertTrue (couchbaseTemplate .existsById ().one (user2 .getId ()));
208
214
209
- couchbaseTemplate .removeByQuery (User .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
215
+ couchbaseTemplate .removeByQuery (User .class ).withConsistency (REQUEST_PLUS ).all ();
210
216
211
217
assertNull (couchbaseTemplate .findById (User .class ).one (user1 .getId ()));
212
218
assertNull (couchbaseTemplate .findById (User .class ).one (user2 .getId ()));
@@ -227,7 +233,7 @@ void removeByMatchingQuery() {
227
233
228
234
Query nonSpecialUsers = new Query (QueryCriteria .where (i ("firstname" )).notLike ("special" ));
229
235
230
- couchbaseTemplate .removeByQuery (User .class ).withConsistency (QueryScanConsistency . REQUEST_PLUS )
236
+ couchbaseTemplate .removeByQuery (User .class ).withConsistency (REQUEST_PLUS )
231
237
.matching (nonSpecialUsers ).all ();
232
238
233
239
assertNull (couchbaseTemplate .findById (User .class ).one (user1 .getId ()));
@@ -252,17 +258,17 @@ void distinct() {
252
258
253
259
// distinct icao
254
260
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 ();
256
262
assertEquals (2 , airports1 .size ());
257
263
258
264
// distinct all-fields-in-Airport.class
259
265
List <Airport > airports2 = couchbaseTemplate .findByQuery (Airport .class ).distinct (new String [] {}).as (Airport .class )
260
- .withConsistency (QueryScanConsistency . REQUEST_PLUS ).all ();
266
+ .withConsistency (REQUEST_PLUS ).all ();
261
267
assertEquals (7 , airports2 .size ());
262
268
263
269
// count( distinct { iata, icao } )
264
270
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 ();
266
272
assertEquals (7 , count1 );
267
273
268
274
} finally {
@@ -287,22 +293,22 @@ void distinctReactive() {
287
293
288
294
// distinct icao
289
295
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 ();
291
297
assertEquals (2 , airports1 .size ());
292
298
293
299
// distinct all-fields-in-Airport.class
294
300
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 ();
296
302
assertEquals (7 , airports2 .size ());
297
303
298
304
// count( distinct icao )
299
305
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 ();
301
307
assertEquals (2 , count1 );
302
308
303
309
// count( distinct { icao, iata } )
304
310
Long count2 = reactiveCouchbaseTemplate .findByQuery (Airport .class ).distinct (new String [] { "icao" , "iata" })
305
- .withConsistency (QueryScanConsistency . REQUEST_PLUS ).count ().block ();
311
+ .withConsistency (REQUEST_PLUS ).count ().block ();
306
312
assertEquals (7 , count2 );
307
313
308
314
} finally {
@@ -312,4 +318,31 @@ void distinctReactive() {
312
318
}
313
319
}
314
320
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
+
315
348
}
0 commit comments