-
Notifications
You must be signed in to change notification settings - Fork 54
/
QuerydslJdbcRepositoryTest.java
300 lines (237 loc) · 9.33 KB
/
QuerydslJdbcRepositoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package com.infobip.spring.data.jdbc;
import com.infobip.spring.data.jdbc.extension.CustomQuerydslJdbcRepository;
import com.querydsl.core.types.Projections;
import com.querydsl.sql.SQLQueryFactory;
import lombok.AllArgsConstructor;
import lombok.Value;
import org.junit.jupiter.api.*;
import org.springframework.transaction.annotation.Transactional;
import java.time.ZoneOffset;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.infobip.spring.data.jdbc.QPerson.person;
import static com.infobip.spring.data.jdbc.QPersonSettings.personSettings;
import static org.assertj.core.api.BDDAssertions.then;
@AllArgsConstructor
public class QuerydslJdbcRepositoryTest extends TestBase {
private static final TimeZone oldTimeZone = TimeZone.getDefault();
private final PersonRepository repository;
private final PersonSettingsRepository settingsRepository;
private final NoArgsRepository noArgsRepository;
private final SQLQueryFactory sqlQueryFactory;
@BeforeAll
void setupTimeZone() {
TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.UTC));
}
@AfterAll
void cleanUpTimeZone() {
TimeZone.setDefault(oldTimeZone);
}
@Transactional
@Test
void shouldStreamAll() {
// given
Person johnDoe = givenSavedPerson("John", "Doe");
Person johnyRoe = givenSavedPerson("Johny", "Roe");
Person janeDoe = givenSavedPerson("Jane", "Doe");
List<Person> actual = null;
// when
try (Stream<Person> stream = repository.streamAll()) {
actual = stream.collect(Collectors.toList());
}
then(actual).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(johnDoe, johnyRoe, janeDoe);
}
@Test
void shouldFindAll() {
// given
Person johnDoe = givenSavedPerson("John", "Doe");
Person johnyRoe = givenSavedPerson("Johny", "Roe");
Person janeDoe = givenSavedPerson("Jane", "Doe");
// when
List<Person> actual = repository.findAll();
then(actual).containsExactlyInAnyOrder(johnDoe, johnyRoe, janeDoe);
}
@Test
void shouldFindAllWithPredicate() {
// given
Person johnDoe = givenSavedPerson("John", "Doe");
Person johnyRoe = givenSavedPerson("Johny", "Roe");
givenSavedPerson("Jane", "Doe");
// when
List<Person> actual = repository.findAll(person.firstName.in("John", "Johny"));
then(actual).containsOnly(johnDoe, johnyRoe);
}
@Test
void shouldQuery() {
// given
Person johnDoe = givenSavedPerson("John", "Doe");
givenSavedPerson("Johny", "Roe");
givenSavedPerson("Jane", "Doe");
givenSavedPerson("John", "Roe");
givenSavedPerson("Janie", "Doe");
// when
List<Person> actual = repository.query(query -> query
.select(repository.entityProjection())
.from(person)
.where(person.firstName.in("John", "Jane"))
.orderBy(person.firstName.asc(), person.lastName.asc())
.limit(1)
.offset(1)
.fetch());
then(actual).containsOnly(johnDoe);
}
@Test
void shouldQueryOne() {
// given
Person johnDoe = givenSavedPerson("John", "Doe");
givenSavedPerson("Johny", "Roe");
givenSavedPerson("Jane", "Doe");
givenSavedPerson("John", "Roe");
givenSavedPerson("Janie", "Doe");
// when
Optional<Person> actual = repository.queryOne(query -> query
.select(repository.entityProjection())
.from(person)
.where(person.firstName.in("John", "Jane"))
.orderBy(person.firstName.asc(), person.lastName.asc())
.limit(1)
.offset(1));
then(actual).contains(johnDoe);
}
@Test
void shouldQueryMany() {
// given
Person johnDoe = givenSavedPerson("John", "Doe");
givenSavedPerson("Johny", "Roe");
givenSavedPerson("Jane", "Doe");
givenSavedPerson("John", "Roe");
givenSavedPerson("Janie", "Doe");
// when
List<Person> actual = repository.queryMany(query -> query
.select(repository.entityProjection())
.from(person)
.where(person.firstName.in("John", "Jane"))
.orderBy(person.firstName.asc(), person.lastName.asc())
.limit(1)
.offset(1));
then(actual).containsOnly(johnDoe);
}
@Test
void shouldProject() {
// given
Person johnDoe = givenSavedPerson("John", "Doe");
// when
List<PersonProjection> actual = repository.query(query -> query
.select(Projections.constructor(PersonProjection.class, person.firstName,
person.lastName))
.from(person)
.fetch());
// then
then(actual).containsExactly(new PersonProjection(johnDoe.getFirstName(), johnDoe.getLastName()));
}
@Test
void shouldUpdate() {
// given
givenSavedPerson("John", "Doe");
givenSavedPerson("Johny", "Roe");
givenSavedPerson("Jane", "Doe");
// when
Long actual = repository.update(query -> query
.set(person.firstName, "John")
.where(person.firstName.eq("Johny"))
.execute());
then(actual).isEqualTo(1);
then(repository.findAll()).extracting(Person::getFirstName)
.containsExactlyInAnyOrder("John", "John", "Jane")
.hasSize(3);
}
@Test
void shouldDelete() {
// given
givenSavedPerson("John", "Doe");
givenSavedPerson("Johny", "Roe");
Person janeDoe = givenSavedPerson("Jane", "Doe");
givenSavedPerson("John", "Roe");
// when
long actual = repository.deleteWhere(person.firstName.like("John%"));
then(repository.findAll()).containsExactly(janeDoe);
then(actual).isEqualTo(3L);
}
@Test
void shouldBeAbleToJoin() {
// given
Person johnDoe = givenSavedPerson("John", "Doe");
Person johnyRoe = givenSavedPerson("Johny", "Roe");
PersonSettings johnDoeSettings = givenSavedPersonSettings(johnDoe);
givenSavedPersonSettings(johnyRoe);
// when
List<Person> actual = repository.query(query -> query
.select(repository.entityProjection())
.from(person)
.innerJoin(personSettings)
.on(person.id.eq(personSettings.personId))
.where(personSettings.id.eq(johnDoeSettings.getId()))
.fetch());
then(actual).extracting(Person::getFirstName).containsExactly(johnDoe.getFirstName());
}
@Test
void shouldSupportMultipleConstructorsWithEntityProjection() {
// given
NoArgsEntity givenNoArgsEntity = giveNoArgsEntity();
// when
List<NoArgsEntity> actual = noArgsRepository.query(query -> query
.select(noArgsRepository.entityProjection())
.from(QNoArgsEntity.noArgsEntity)
.limit(1)
.fetch());
then(actual).containsExactly(givenNoArgsEntity);
}
@Test
void shouldSupportMultipleConstructors() {
// given
NoArgsEntity givenNoArgsEntity = giveNoArgsEntity();
// when
List<NoArgsEntity> actual = noArgsRepository.query(query -> query
.select(QNoArgsEntity.noArgsEntity)
.from(QNoArgsEntity.noArgsEntity)
.limit(1)
.fetch());
then(actual).containsExactly(givenNoArgsEntity);
}
@Test
void shouldExtendSimpleQuerydslJdbcRepository() {
// then
then(repository).isInstanceOf(QuerydslJdbcRepository.class)
.isNotInstanceOf(CustomQuerydslJdbcRepository.class);
}
@Test
void springDataAndQuerydslShouldHandleTimeZoneTheSameForSameTimeZone() {
// given
Person givenPerson = new Person(null, "givenFirstName", "givenLastName", BEGINNING_OF_2021);
// when
sqlQueryFactory.insert(person)
.columns(person.firstName, person.lastName, person.createdAt)
.values(givenPerson.getFirstName(), givenPerson.getLastName(), givenPerson.getCreatedAt())
.execute();
repository.save(givenPerson);
// then
List<Person> querydslResults = sqlQueryFactory.select(repository.entityProjection()).from(person).fetch();
List<Person> springDataResults = repository.findAll();
then(querydslResults).isEqualTo(springDataResults);
}
@Value
public static class PersonProjection {
private final String firstName;
private final String lastName;
}
private Person givenSavedPerson(String firstName, String lastName) {
return repository.save(new Person(null, firstName, lastName, BEGINNING_OF_2021));
}
private NoArgsEntity giveNoArgsEntity() {
return noArgsRepository.save(new NoArgsEntity());
}
private PersonSettings givenSavedPersonSettings(Person person) {
return settingsRepository.save(new PersonSettings(null, person.getId()));
}
}