|
19 | 19 | import static com.couchbase.client.java.query.QueryScanConsistency.REQUEST_PLUS;
|
20 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
21 | 21 | import static org.junit.jupiter.api.Assertions.assertNull;
|
| 22 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
22 | 23 | import static org.springframework.data.couchbase.util.Util.comprises;
|
23 | 24 | import static org.springframework.data.couchbase.util.Util.exactly;
|
24 | 25 |
|
25 | 26 | import java.util.Arrays;
|
| 27 | +import java.util.Comparator; |
26 | 28 | import java.util.Locale;
|
27 | 29 | import java.util.Optional;
|
| 30 | +import java.util.stream.StreamSupport; |
28 | 31 |
|
29 | 32 | import org.junit.jupiter.api.AfterAll;
|
30 | 33 | import org.junit.jupiter.api.BeforeAll;
|
|
53 | 56 | import org.springframework.data.couchbase.util.ClusterType;
|
54 | 57 | import org.springframework.data.couchbase.util.IgnoreWhen;
|
55 | 58 | import org.springframework.data.couchbase.util.JavaIntegrationTests;
|
| 59 | +import org.springframework.data.domain.Sort; |
56 | 60 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
57 | 61 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
58 | 62 |
|
|
67 | 71 | * Repository tests
|
68 | 72 | *
|
69 | 73 | * @author Michael Reiche
|
| 74 | + * @author Tigran Babloyan |
70 | 75 | */
|
71 | 76 | @SpringJUnitConfig(CouchbaseRepositoryQuerydslIntegrationTests.Config.class)
|
72 | 77 | @IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED)
|
@@ -410,6 +415,72 @@ void testIn() {
|
410 | 415 | assertEquals(" WHERE name in $1", bq(predicate));
|
411 | 416 | }
|
412 | 417 | }
|
| 418 | + |
| 419 | + @Test |
| 420 | + void testSort(){ |
| 421 | + { |
| 422 | + BooleanExpression predicate = airline.name.in(Arrays.stream(saved).map(Airline::getName).toList()); |
| 423 | + Iterable<Airline> result = airlineRepository.findAll(predicate, Sort.by("name").ascending()); |
| 424 | + assertArrayEquals(StreamSupport.stream(result.spliterator(), false).toArray(Airline[]::new), |
| 425 | + Arrays.stream(saved) |
| 426 | + .sorted(Comparator.comparing(Airline::getName)) |
| 427 | + .toArray(Airline[]::new), |
| 428 | + "Order of airlines does not match"); |
| 429 | + } |
| 430 | + |
| 431 | + { |
| 432 | + BooleanExpression predicate = airline.name.in(Arrays.stream(saved).map(Airline::getName).toList()); |
| 433 | + Iterable<Airline> result = airlineRepository.findAll(predicate, Sort.by("name").descending()); |
| 434 | + assertArrayEquals(StreamSupport.stream(result.spliterator(), false).toArray(Airline[]::new), |
| 435 | + Arrays.stream(saved) |
| 436 | + .sorted(Comparator.comparing(Airline::getName).reversed()) |
| 437 | + .toArray(Airline[]::new), |
| 438 | + "Order of airlines does not match"); |
| 439 | + } |
| 440 | + |
| 441 | + { |
| 442 | + BooleanExpression predicate = airline.name.in(Arrays.stream(saved).map(Airline::getName).toList()); |
| 443 | + Iterable<Airline> result = airlineRepository.findAll(predicate, airline.name.asc()); |
| 444 | + assertArrayEquals(StreamSupport.stream(result.spliterator(), false).toArray(Airline[]::new), |
| 445 | + Arrays.stream(saved) |
| 446 | + .sorted(Comparator.comparing(Airline::getName)) |
| 447 | + .toArray(Airline[]::new), |
| 448 | + "Order of airlines does not match"); |
| 449 | + } |
| 450 | + |
| 451 | + { |
| 452 | + BooleanExpression predicate = airline.name.in(Arrays.stream(saved).map(Airline::getName).toList()); |
| 453 | + Iterable<Airline> result = airlineRepository.findAll(predicate, airline.name.desc()); |
| 454 | + assertArrayEquals(StreamSupport.stream(result.spliterator(), false).toArray(Airline[]::new), |
| 455 | + Arrays.stream(saved) |
| 456 | + .sorted(Comparator.comparing(Airline::getName).reversed()) |
| 457 | + .toArray(Airline[]::new), |
| 458 | + "Order of airlines does not match"); |
| 459 | + } |
| 460 | + |
| 461 | + { |
| 462 | + Comparator<String> nullSafeStringComparator = Comparator |
| 463 | + .nullsFirst(String::compareTo); |
| 464 | + Iterable<Airline> result = airlineRepository.findAll(airline.hqCountry.asc().nullsFirst()); |
| 465 | + assertArrayEquals(StreamSupport.stream(result.spliterator(), false).toArray(Airline[]::new), |
| 466 | + Arrays.stream(saved) |
| 467 | + .sorted(Comparator.comparing(Airline::getHqCountry, nullSafeStringComparator)) |
| 468 | + .toArray(Airline[]::new), |
| 469 | + "Order of airlines does not match"); |
| 470 | + } |
| 471 | + |
| 472 | + { |
| 473 | + Comparator<String> nullSafeStringComparator = Comparator |
| 474 | + .nullsFirst(String::compareTo); |
| 475 | + Iterable<Airline> result = airlineRepository.findAll(airline.hqCountry.desc().nullsLast()); |
| 476 | + assertArrayEquals(StreamSupport.stream(result.spliterator(), false).toArray(Airline[]::new), |
| 477 | + Arrays.stream(saved) |
| 478 | + .sorted(Comparator.comparing(Airline::getHqCountry, nullSafeStringComparator).reversed()) |
| 479 | + .toArray(Airline[]::new), |
| 480 | + "Order of airlines does not match"); |
| 481 | + } |
| 482 | + } |
| 483 | + |
413 | 484 |
|
414 | 485 | @Test
|
415 | 486 | void testNotIn() {
|
|
0 commit comments