41
41
import reactor .core .publisher .Mono ;
42
42
43
43
import java .lang .reflect .Method ;
44
+ import java .util .Collection ;
45
+ import java .util .Collections ;
44
46
import java .util .Date ;
45
47
46
48
import static org .assertj .core .api .Assertions .assertThat ;
@@ -56,7 +58,8 @@ public class PartTreeR2dbcQueryIntegrationTests {
56
58
+ TABLE + ".first_name, "
57
59
+ TABLE + ".last_name, "
58
60
+ TABLE + ".date_of_birth, "
59
- + TABLE + ".age" ;
61
+ + TABLE + ".age, "
62
+ + TABLE + ".active" ;
60
63
61
64
@ Mock
62
65
private ConnectionFactory connectionFactory ;
@@ -242,7 +245,7 @@ public void createsQueryToFindAllEntitiesByStringAttributeLike() throws Exceptio
242
245
R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByFirstNameLike" , String .class );
243
246
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
244
247
dataAccessStrategy );
245
- RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] {"%John%" });
248
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object []{"%John%" });
246
249
BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
247
250
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE ?" ;
248
251
assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
@@ -253,7 +256,7 @@ public void createsQueryToFindAllEntitiesByStringAttributeNotLike() throws Excep
253
256
R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByFirstNameNotLike" , String .class );
254
257
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
255
258
dataAccessStrategy );
256
- RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] {"%John%" });
259
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object []{"%John%" });
257
260
BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
258
261
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name NOT LIKE ?" ;
259
262
assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
@@ -264,7 +267,7 @@ public void createsQueryToFindAllEntitiesByStringAttributeStartingWith() throws
264
267
R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByFirstNameStartingWith" , String .class );
265
268
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
266
269
dataAccessStrategy );
267
- RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] {"Jo" });
270
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object []{"Jo" });
268
271
BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
269
272
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE ?" ;
270
273
assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
@@ -276,7 +279,7 @@ public void appendsLikeOperatorParameterWithPercentSymbolForStartingWithQuery()
276
279
R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByFirstNameStartingWith" , String .class );
277
280
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
278
281
dataAccessStrategy );
279
- RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] {"Jo" });
282
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object []{"Jo" });
280
283
BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
281
284
DatabaseClient .BindSpec bindSpecMock = mock (DatabaseClient .BindSpec .class );
282
285
bindableQuery .bind (bindSpecMock );
@@ -288,7 +291,7 @@ public void createsQueryToFindAllEntitiesByStringAttributeEndingWith() throws Ex
288
291
R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByFirstNameEndingWith" , String .class );
289
292
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
290
293
dataAccessStrategy );
291
- RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] {"hn" });
294
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object []{"hn" });
292
295
BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
293
296
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE ?" ;
294
297
assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
@@ -300,7 +303,7 @@ public void prependsLikeOperatorParameterWithPercentSymbolForStartingWithQuery()
300
303
R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByFirstNameEndingWith" , String .class );
301
304
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
302
305
dataAccessStrategy );
303
- RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] {"hn" });
306
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object []{"hn" });
304
307
BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
305
308
DatabaseClient .BindSpec bindSpecMock = mock (DatabaseClient .BindSpec .class );
306
309
bindableQuery .bind (bindSpecMock );
@@ -312,7 +315,7 @@ public void createsQueryToFindAllEntitiesByStringAttributeContaining() throws Ex
312
315
R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByFirstNameContaining" , String .class );
313
316
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
314
317
dataAccessStrategy );
315
- RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] {"oh" });
318
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object []{"oh" });
316
319
BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
317
320
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE ?" ;
318
321
assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
@@ -324,7 +327,7 @@ public void wrapsLikeOperatorParameterWithPercentSymbolsForContainingQuery() thr
324
327
R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByFirstNameContaining" , String .class );
325
328
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
326
329
dataAccessStrategy );
327
- RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] {"hn" });
330
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object []{"hn" });
328
331
BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
329
332
DatabaseClient .BindSpec bindSpecMock = mock (DatabaseClient .BindSpec .class );
330
333
bindableQuery .bind (bindSpecMock );
@@ -337,13 +340,81 @@ public void createsQueryToFindAllEntitiesByIntegerAttributeWithDescendingOrderin
337
340
R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByAgeOrderByLastNameDesc" , Integer .class );
338
341
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
339
342
dataAccessStrategy );
340
- RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] {"oh" });
343
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object []{"oh" });
341
344
BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
342
345
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE
343
346
+ " WHERE " + TABLE + ".age = ? ORDER BY last_name DESC" ;
344
347
assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
345
348
}
346
349
350
+ @ Test
351
+ public void createsQueryToFindAllEntitiesByStringAttributeNot () throws Exception {
352
+ R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByLastNameNot" , String .class );
353
+ PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
354
+ dataAccessStrategy );
355
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object []{"Doe" });
356
+ BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
357
+ String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".last_name != ?" ;
358
+ assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
359
+ }
360
+
361
+ @ Test
362
+ public void createsQueryToFindAllEntitiesByIntegerAttributeIn () throws Exception {
363
+ R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByAgeIn" , Collection .class );
364
+ PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
365
+ dataAccessStrategy );
366
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod ,
367
+ new Object []{Collections .singleton (25 )});
368
+ BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
369
+ String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IN (?)" ;
370
+ assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
371
+ }
372
+
373
+ @ Test
374
+ public void createsQueryToFindAllEntitiesByIntegerAttributeNotIn () throws Exception {
375
+ R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByAgeNotIn" , Collection .class );
376
+ PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
377
+ dataAccessStrategy );
378
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod ,
379
+ new Object []{Collections .singleton (25 )});
380
+ BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
381
+ String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age NOT IN (?)" ;
382
+ assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
383
+ }
384
+
385
+ @ Test
386
+ public void createsQueryToFindAllEntitiesByBooleanAttributeTrue () throws Exception {
387
+ R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByActiveTrue" );
388
+ PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
389
+ dataAccessStrategy );
390
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [0 ]);
391
+ BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
392
+ String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = TRUE" ;
393
+ assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
394
+ }
395
+
396
+ @ Test
397
+ public void createsQueryToFindAllEntitiesByBooleanAttributeFalse () throws Exception {
398
+ R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByActiveFalse" );
399
+ PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
400
+ dataAccessStrategy );
401
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [0 ]);
402
+ BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
403
+ String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = FALSE" ;
404
+ assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
405
+ }
406
+
407
+ @ Test
408
+ public void createsQueryToFindAllEntitiesByStringAttributeIgnoringCase () throws Exception {
409
+ R2dbcQueryMethod queryMethod = getQueryMethod ("findAllByFirstNameIgnoreCase" , String .class );
410
+ PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery (queryMethod , databaseClient , r2dbcConverter ,
411
+ dataAccessStrategy );
412
+ RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object []{"John" });
413
+ BindableQuery bindableQuery = r2dbcQuery .createQuery (accessor );
414
+ String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE UPPER(" + TABLE + ".first_name) = UPPER(?)" ;
415
+ assertThat (bindableQuery .get ()).isEqualTo (expectedSql );
416
+ }
417
+
347
418
private R2dbcQueryMethod getQueryMethod (String methodName , Class <?>... parameterTypes ) throws Exception {
348
419
Method method = UserRepository .class .getMethod (methodName , parameterTypes );
349
420
return new R2dbcQueryMethod (method , new DefaultRepositoryMetadata (UserRepository .class ),
@@ -392,6 +463,18 @@ private interface UserRepository extends Repository<User, Long> {
392
463
Flux <User > findAllByFirstNameContaining (String containing );
393
464
394
465
Flux <User > findAllByAgeOrderByLastNameDesc (Integer age );
466
+
467
+ Flux <User > findAllByLastNameNot (String lastName );
468
+
469
+ Flux <User > findAllByAgeIn (Collection <Integer > ages );
470
+
471
+ Flux <User > findAllByAgeNotIn (Collection <Integer > ages );
472
+
473
+ Flux <User > findAllByActiveTrue ();
474
+
475
+ Flux <User > findAllByActiveFalse ();
476
+
477
+ Flux <User > findAllByFirstNameIgnoreCase (String firstName );
395
478
}
396
479
397
480
@ Table ("users" )
@@ -403,5 +486,6 @@ private static class User {
403
486
private String lastName ;
404
487
private Date dateOfBirth ;
405
488
private Integer age ;
489
+ private Boolean active ;
406
490
}
407
491
}
0 commit comments