|
15 | 15 | */
|
16 | 16 | package org.springframework.data.jpa.repository;
|
17 | 17 |
|
18 |
| -import static org.assertj.core.api.Assertions.*; |
19 |
| -import static org.springframework.data.domain.Sort.Direction.*; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 20 | +import static org.springframework.data.domain.Sort.Direction.ASC; |
| 21 | +import static org.springframework.data.domain.Sort.Direction.DESC; |
20 | 22 |
|
21 | 23 | import jakarta.persistence.EntityManager;
|
22 | 24 |
|
23 | 25 | import java.util.Arrays;
|
24 | 26 | import java.util.List;
|
| 27 | +import java.util.Map; |
25 | 28 |
|
| 29 | +import org.assertj.core.data.Offset; |
26 | 30 | import org.junit.jupiter.api.AfterEach;
|
27 | 31 | import org.junit.jupiter.api.BeforeEach;
|
28 | 32 | import org.junit.jupiter.api.Test;
|
29 | 33 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 34 | +import org.junit.jupiter.params.ParameterizedTest; |
| 35 | +import org.junit.jupiter.params.provider.ValueSource; |
30 | 36 | import org.springframework.beans.factory.annotation.Autowired;
|
31 | 37 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
32 | 38 | import org.springframework.data.domain.Limit;
|
|
45 | 51 | import org.springframework.data.jpa.repository.sample.UserRepository.IdOnly;
|
46 | 52 | import org.springframework.data.jpa.repository.sample.UserRepository.NameOnly;
|
47 | 53 | import org.springframework.data.jpa.repository.sample.UserRepository.RolesAndFirstname;
|
| 54 | +import org.springframework.data.jpa.repository.sample.UserRepository.UserExcerpt; |
| 55 | +import org.springframework.data.jpa.repository.sample.UserRepository.UserRoleCountDtoProjection; |
| 56 | +import org.springframework.data.jpa.repository.sample.UserRepository.UserRoleCountInterfaceProjection; |
48 | 57 | import org.springframework.data.repository.query.QueryLookupStrategy;
|
49 | 58 | import org.springframework.test.context.ContextConfiguration;
|
50 | 59 | import org.springframework.test.context.junit.jupiter.SpringExtension;
|
@@ -247,9 +256,9 @@ void executesQueryWithLimitAndScrollPosition() {
|
247 | 256 | @Test // GH-3409
|
248 | 257 | void executesWindowQueryWithPageable() {
|
249 | 258 |
|
250 |
| - Window<User> first = userRepository.findByLastnameOrderByFirstname("Matthews", PageRequest.of(0,1)); |
| 259 | + Window<User> first = userRepository.findByLastnameOrderByFirstname("Matthews", PageRequest.of(0, 1)); |
251 | 260 |
|
252 |
| - Window<User> next = userRepository.findByLastnameOrderByFirstname("Matthews", PageRequest.of(1,1)); |
| 261 | + Window<User> next = userRepository.findByLastnameOrderByFirstname("Matthews", PageRequest.of(1, 1)); |
253 | 262 |
|
254 | 263 | assertThat(first).containsExactly(dave);
|
255 | 264 | assertThat(next).containsExactly(oliver);
|
@@ -406,21 +415,92 @@ void findByNegatingSimplePropertyUsingMixedNullNonNullArgument() {
|
406 | 415 | assertThat(result).containsExactly(carter);
|
407 | 416 | }
|
408 | 417 |
|
409 |
| - @Test // GH-3076 |
410 |
| - void dtoProjectionShouldApplyConstructorExpressionRewriting() { |
| 418 | + @Test // GH-3076 |
| 419 | + void dtoProjectionShouldApplyConstructorExpressionRewriting() { |
411 | 420 |
|
412 |
| - List<UserRepository.UserExcerpt> dtos = userRepository.findRecordProjection(); |
| 421 | + List<UserExcerpt> dtos = userRepository.findRecordProjection(); |
413 | 422 |
|
414 |
| - assertThat(dtos).flatExtracting(UserRepository.UserExcerpt::firstname) // |
415 |
| - .contains("Dave", "Carter", "Oliver August"); |
416 |
| - } |
| 423 | + assertThat(dtos).flatExtracting(UserRepository.UserExcerpt::firstname) // |
| 424 | + .contains("Dave", "Carter", "Oliver August"); |
| 425 | + } |
| 426 | + |
| 427 | + @Test // GH-3076 |
| 428 | + void dtoMultiselectProjectionShouldApplyConstructorExpressionRewriting() { |
| 429 | + |
| 430 | + List<UserExcerpt> dtos = userRepository.findMultiselectRecordProjection(); |
| 431 | + |
| 432 | + assertThat(dtos).flatExtracting(UserRepository.UserExcerpt::firstname) // |
| 433 | + .contains("Dave", "Carter", "Oliver August"); |
| 434 | + } |
| 435 | + |
| 436 | + @Test // GH-3076 |
| 437 | + void dynamicDtoProjection() { |
| 438 | + |
| 439 | + List<UserExcerpt> dtos = userRepository.findRecordProjection(UserExcerpt.class); |
| 440 | + |
| 441 | + assertThat(dtos).flatExtracting(UserRepository.UserExcerpt::firstname) // |
| 442 | + .contains("Dave", "Carter", "Oliver August"); |
| 443 | + } |
| 444 | + |
| 445 | + @Test // GH-3076 |
| 446 | + void dtoProjectionWithEntityAndAggregatedValue() { |
| 447 | + |
| 448 | + Map<String, User> musicians = Map.of(carter.getFirstname(), carter, dave.getFirstname(), dave, |
| 449 | + oliver.getFirstname(), oliver); |
| 450 | + |
| 451 | + assertThat(userRepository.dtoProjectionEntityAndAggregatedValue()).allSatisfy(projection -> { |
| 452 | + assertThat(projection.user()).isIn(musicians.values()); |
| 453 | + assertThat(projection.roleCount()).isCloseTo(musicians.get(projection.user().getFirstname()).getRoles().size(), |
| 454 | + Offset.offset(0L)); |
| 455 | + }); |
| 456 | + } |
417 | 457 |
|
418 |
| - @Test // GH-3076 |
419 |
| - void dtoMultiselectProjectionShouldApplyConstructorExpressionRewriting() { |
| 458 | + @Test // GH-3076 |
| 459 | + void interfaceProjectionWithEntityAndAggregatedValue() { |
420 | 460 |
|
421 |
| - List<UserRepository.UserExcerpt> dtos = userRepository.findMultiselectRecordProjection(); |
| 461 | + Map<String, User> musicians = Map.of(carter.getFirstname(), carter, dave.getFirstname(), dave, |
| 462 | + oliver.getFirstname(), oliver); |
| 463 | + |
| 464 | + assertThat(userRepository.interfaceProjectionEntityAndAggregatedValue()).allSatisfy(projection -> { |
| 465 | + assertThat(projection.getUser()).isIn(musicians.values()); |
| 466 | + assertThat(projection.getRoleCount()) |
| 467 | + .isCloseTo(musicians.get(projection.getUser().getFirstname()).getRoles().size(), Offset.offset(0L)); |
| 468 | + }); |
| 469 | + } |
| 470 | + |
| 471 | + @Test // GH-3076 |
| 472 | + void rawMapProjectionWithEntityAndAggregatedValue() { |
| 473 | + |
| 474 | + Map<String, User> musicians = Map.of(carter.getFirstname(), carter, dave.getFirstname(), dave, |
| 475 | + oliver.getFirstname(), oliver); |
| 476 | + |
| 477 | + assertThat(userRepository.rawMapProjectionEntityAndAggregatedValue()).allSatisfy(projection -> { |
| 478 | + assertThat(projection.get("user")).isIn(musicians.values()); |
| 479 | + assertThat(projection).containsKey("roleCount"); |
| 480 | + }); |
| 481 | + } |
| 482 | + |
| 483 | + @Test // GH-3076 |
| 484 | + void dtoProjectionWithEntityAndAggregatedValueWithPageable() { |
| 485 | + |
| 486 | + Map<String, User> musicians = Map.of(carter.getFirstname(), carter, dave.getFirstname(), dave, |
| 487 | + oliver.getFirstname(), oliver); |
| 488 | + |
| 489 | + assertThat( |
| 490 | + userRepository.dtoProjectionEntityAndAggregatedValue(PageRequest.of(0, 10).withSort(Sort.by("firstname")))) |
| 491 | + .allSatisfy(projection -> { |
| 492 | + assertThat(projection.user()).isIn(musicians.values()); |
| 493 | + assertThat(projection.roleCount()) |
| 494 | + .isCloseTo(musicians.get(projection.user().getFirstname()).getRoles().size(), Offset.offset(0L)); |
| 495 | + }); |
| 496 | + } |
| 497 | + |
| 498 | + @ParameterizedTest // GH-3076 |
| 499 | + @ValueSource(classes = { UserRoleCountDtoProjection.class, UserRoleCountInterfaceProjection.class }) |
| 500 | + <T> void dynamicProjectionWithEntityAndAggregated(Class<T> resultType) { |
| 501 | + |
| 502 | + assertThat(userRepository.findMultiselectRecordDynamicProjection(resultType)).hasSize(3) |
| 503 | + .hasOnlyElementsOfType(resultType); |
| 504 | + } |
422 | 505 |
|
423 |
| - assertThat(dtos).flatExtracting(UserRepository.UserExcerpt::firstname) // |
424 |
| - .contains("Dave", "Carter", "Oliver August"); |
425 |
| - } |
426 | 506 | }
|
0 commit comments