Skip to content

Commit cf26d7e

Browse files
Paul Jonesmp911de
Paul Jones
authored andcommitted
Add unit tests to verify DTO projection with AggregationReference.
Closes #1759
1 parent 702002a commit cf26d7e

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java

+71
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
* @author Diego Krupitza
105105
* @author Christopher Klein
106106
* @author Mikhail Polivakha
107+
* @author Paul Jones
107108
*/
108109
@IntegrationTest
109110
public class JdbcRepositoryIntegrationTests {
@@ -1286,6 +1287,36 @@ void fetchByExampleFluentOnlyInstantOneValueAsSimple() {
12861287
assertThat(match.get().getName()).contains(two.getName());
12871288
}
12881289

1290+
@Test
1291+
void fetchDtoWithNoArgsConstructorWithAggregateReferencePopulated() {
1292+
1293+
DummyEntity entity = new DummyEntity();
1294+
entity.setRef(AggregateReference.to(20L));
1295+
entity.setName("Test Dto");
1296+
repository.save(entity);
1297+
1298+
assertThat(repository.findById(entity.idProp).orElseThrow().getRef()).isEqualTo(AggregateReference.to(20L));
1299+
1300+
DummyDto foundDto = repository.findDtoByIdProp(entity.idProp).orElseThrow();
1301+
assertThat(foundDto.getName()).isEqualTo("Test Dto");
1302+
assertThat(foundDto.getRef()).isEqualTo(AggregateReference.to(20L));
1303+
}
1304+
1305+
@Test // GH-1759
1306+
void fetchDtoWithAllArgsConstructorWithAggregateReferencePopulated() {
1307+
1308+
DummyEntity entity = new DummyEntity();
1309+
entity.setRef(AggregateReference.to(20L));
1310+
entity.setName("Test Dto");
1311+
repository.save(entity);
1312+
1313+
assertThat(repository.findById(entity.idProp).orElseThrow().getRef()).isEqualTo(AggregateReference.to(20L));
1314+
1315+
DummyAllArgsDto foundDto = repository.findAllArgsDtoByIdProp(entity.idProp).orElseThrow();
1316+
assertThat(foundDto.getName()).isEqualTo("Test Dto");
1317+
assertThat(foundDto.getRef()).isEqualTo(AggregateReference.to(20L));
1318+
}
1319+
12891320
@Test // GH-1405
12901321
void withDelimitedColumnTest() {
12911322

@@ -1426,6 +1457,10 @@ interface DummyEntityRepository extends CrudRepository<DummyEntity, Long>, Query
14261457

14271458
@Query("SELECT * FROM DUMMY_ENTITY WHERE DIRECTION = :direction")
14281459
List<DummyEntity> findByEnumType(Direction direction);
1460+
1461+
Optional<DummyDto> findDtoByIdProp(Long idProp);
1462+
1463+
Optional<DummyAllArgsDto> findAllArgsDtoByIdProp(Long idProp);
14291464
}
14301465

14311466
interface RootRepository extends ListCrudRepository<Root, Long> {
@@ -1834,6 +1869,42 @@ enum Direction {
18341869
LEFT, CENTER, RIGHT
18351870
}
18361871

1872+
static class DummyDto {
1873+
@Id Long idProp;
1874+
String name;
1875+
AggregateReference<DummyEntity, Long> ref;
1876+
1877+
public DummyDto() {}
1878+
1879+
public String getName() {
1880+
return name;
1881+
}
1882+
1883+
public AggregateReference<DummyEntity, Long> getRef() {
1884+
return ref;
1885+
}
1886+
}
1887+
1888+
static class DummyAllArgsDto {
1889+
@Id Long idProp;
1890+
String name;
1891+
AggregateReference<DummyEntity, Long> ref;
1892+
1893+
public DummyAllArgsDto(Long idProp, String name, AggregateReference<DummyEntity, Long> ref) {
1894+
this.idProp = idProp;
1895+
this.name = name;
1896+
this.ref = ref;
1897+
}
1898+
1899+
public String getName() {
1900+
return name;
1901+
}
1902+
1903+
public AggregateReference<DummyEntity, Long> getRef() {
1904+
return ref;
1905+
}
1906+
}
1907+
18371908
interface DummyProjection {
18381909
String getName();
18391910
}

0 commit comments

Comments
 (0)