15
15
import org .junit .jupiter .api .BeforeEach ;
16
16
import org .junit .jupiter .api .Test ;
17
17
18
+ import io .smallrye .mutiny .Uni ;
18
19
import io .vertx .junit5 .Timeout ;
19
20
import io .vertx .junit5 .VertxTestContext ;
20
21
import jakarta .persistence .Entity ;
23
24
import java .util .List ;
24
25
import java .util .Objects ;
25
26
import java .util .Set ;
27
+ import java .util .concurrent .CompletionStage ;
26
28
27
29
import static java .util .concurrent .TimeUnit .MINUTES ;
28
30
import static org .assertj .core .api .Assertions .assertThat ;
@@ -44,6 +46,19 @@ public class ReactiveStatelessDefaultBatchSizeTest extends BaseReactiveTest {
44
46
new GuineaPig ( 66 , "Six" )
45
47
};
46
48
49
+ private static final Object [] PIGS_AFTER_DELETE = List .of ( PIGS )
50
+ .subList ( 2 , PIGS .length )
51
+ .toArray ();
52
+
53
+ private static final Object [] PIGS_AFTER_UPDATE = {
54
+ new GuineaPig ( 11 , "One updated" ),
55
+ new GuineaPig ( 22 , "Two updated" ),
56
+ new GuineaPig ( 33 , "Three" ),
57
+ new GuineaPig ( 44 , "Four" ),
58
+ new GuineaPig ( 55 , "Five" ),
59
+ new GuineaPig ( 66 , "Six" )
60
+ };
61
+
47
62
@ Override
48
63
protected Set <Class <?>> annotatedEntities () {
49
64
return Set .of ( GuineaPig .class );
@@ -87,6 +102,7 @@ public void testMutinyBatchingInsert(VertxTestContext context) {
87
102
test ( context , getMutinySessionFactory ()
88
103
.withStatelessTransaction ( s -> s .insertAll ( 10 , PIGS ) )
89
104
.invoke ( () -> assertSqlLogTracker ( "insert into pig \\ (name,id\\ ) values (.*)" ) )
105
+ .chain ( () -> Uni .createFrom ().completionStage ( assertExpectedResult ( PIGS ) ) )
90
106
);
91
107
}
92
108
@@ -95,11 +111,7 @@ public void testMutinyBatchingInsertMultiple(VertxTestContext context) {
95
111
test ( context , getMutinySessionFactory ()
96
112
.withStatelessTransaction ( s -> s .insertMultiple ( List .of ( PIGS ) ) )
97
113
.invoke ( () -> assertSqlLogTracker ( "insert into pig \\ (name,id\\ ) values (.*)" ) )
98
- .invoke ( v -> getMutinySessionFactory ().withStatelessTransaction ( s -> s
99
- .createQuery ( "from GuineaPig p" , GuineaPig .class )
100
- .getResultList ()
101
- .invoke ( pigs -> assertThat ( pigs ).hasSize ( PIGS .length ) )
102
- ) )
114
+ .chain ( () -> Uni .createFrom ().completionStage ( assertExpectedResult ( PIGS ) ) )
103
115
);
104
116
}
105
117
@@ -108,11 +120,7 @@ public void testMutinyBatchingInsertAllNoBatchSizeParameter(VertxTestContext con
108
120
test ( context , getMutinySessionFactory ()
109
121
.withStatelessTransaction ( s -> s .insertAll ( PIGS ) )
110
122
.invoke ( () -> assertSqlLogTracker ( "insert into pig \\ (name,id\\ ) values (.*)" ) )
111
- .invoke ( v -> getMutinySessionFactory ().withStatelessTransaction ( s -> s
112
- .createQuery ( "from GuineaPig p" , GuineaPig .class )
113
- .getResultList ()
114
- .invoke ( pigs -> assertThat ( pigs ).hasSize ( PIGS .length ) )
115
- ) )
123
+ .chain ( () -> Uni .createFrom ().completionStage ( assertExpectedResult ( PIGS ) ) )
116
124
);
117
125
}
118
126
@@ -121,11 +129,7 @@ public void testStageBatchingInsert(VertxTestContext context) {
121
129
test ( context , getSessionFactory ()
122
130
.withStatelessTransaction ( s -> s .insert ( 10 , PIGS ) )
123
131
.thenAccept ( v -> assertSqlLogTracker ( "insert into pig \\ (name,id\\ ) values (.*)" ) )
124
- .thenAccept ( v -> getSessionFactory ().withStatelessTransaction ( s -> s
125
- .createQuery ( "from GuineaPig p" , GuineaPig .class )
126
- .getResultList ()
127
- .thenAccept ( pigs -> assertThat ( pigs ).hasSize ( PIGS .length ) )
128
- ) )
132
+ .thenCompose ( v -> assertExpectedResult ( PIGS ) )
129
133
);
130
134
}
131
135
@@ -134,23 +138,15 @@ public void testStageBatchingInsertMultiple(VertxTestContext context) {
134
138
test ( context , getSessionFactory ()
135
139
.withStatelessTransaction ( s -> s .insertMultiple ( List .of ( PIGS ) ) )
136
140
.thenAccept ( v -> assertSqlLogTracker ( "insert into pig \\ (name,id\\ ) values (.*)" ) )
137
- .thenAccept ( v -> getSessionFactory ().withStatelessTransaction ( s -> s
138
- .createQuery ( "from GuineaPig p" , GuineaPig .class )
139
- .getResultList ()
140
- .thenAccept ( pigs -> assertThat ( pigs ).hasSize ( PIGS .length ) )
141
- ) )
141
+ .thenCompose ( v -> assertExpectedResult ( PIGS ) )
142
142
);
143
143
}
144
144
145
145
@ Test
146
146
public void testStageBatchingInsertNoBatchSizeParameter (VertxTestContext context ) {
147
147
test ( context , getSessionFactory ().withStatelessTransaction ( s -> s .insert ( PIGS ) )
148
148
.thenAccept ( v -> assertSqlLogTracker ( "insert into pig \\ (name,id\\ ) values (.*)" ) )
149
- .thenAccept ( v -> getSessionFactory ().withStatelessTransaction ( s -> s
150
- .createQuery ( "select p from GuineaPig p" , GuineaPig .class )
151
- .getResultList ()
152
- .thenAccept ( pigs -> assertThat ( pigs ).hasSize ( PIGS .length ) )
153
- ) )
149
+ .thenCompose ( v -> assertExpectedResult ( PIGS ) )
154
150
);
155
151
}
156
152
@@ -166,11 +162,7 @@ public void testMutinyBatchingDelete(VertxTestContext context) {
166
162
.deleteAll ( 10 , pigs .subList ( 0 , 2 ).toArray () )
167
163
) )
168
164
.invoke ( () -> assertSqlLogTracker ( "delete from pig where id=.*" ) )
169
- .chain ( () -> getMutinySessionFactory ().withStatelessTransaction ( s -> s
170
- .createQuery ( "select p from GuineaPig p" , GuineaPig .class )
171
- .getResultList ()
172
- .invoke ( guineaPigs -> assertThat ( guineaPigs ).hasSize ( 4 ) )
173
- ) )
165
+ .call ( () -> Uni .createFrom ().completionStage ( assertExpectedResult ( PIGS_AFTER_DELETE ) ) )
174
166
);
175
167
}
176
168
@@ -186,11 +178,7 @@ public void testMutinyBatchingDeleteMultiple(VertxTestContext context) {
186
178
.deleteMultiple ( pigs .subList ( 0 , 2 ) ) )
187
179
)
188
180
.invoke ( () -> assertSqlLogTracker ( "delete from pig where id=.*" ) )
189
- .chain ( () -> getMutinySessionFactory ().withStatelessTransaction ( s -> s
190
- .createQuery ( "from GuineaPig p" , GuineaPig .class )
191
- .getResultList ()
192
- .invoke ( guineaPigs -> assertThat ( guineaPigs ).hasSize ( 4 ) )
193
- ) )
181
+ .call ( () -> Uni .createFrom ().completionStage ( assertExpectedResult ( PIGS_AFTER_DELETE ) ) )
194
182
);
195
183
}
196
184
@@ -206,11 +194,7 @@ public void testMutinyBatchingDeleteAllNoBatchSizeParameter(VertxTestContext con
206
194
.deleteAll ( pigs .subList ( 0 , 2 ).toArray () )
207
195
) )
208
196
.invoke ( () -> assertSqlLogTracker ( "delete from pig where id=.*" ) )
209
- .chain ( () -> getMutinySessionFactory ().withStatelessTransaction ( s -> s
210
- .createQuery ( "from GuineaPig p" , GuineaPig .class )
211
- .getResultList ()
212
- .invoke ( guineaPigs -> assertThat ( guineaPigs ).hasSize ( 4 ) )
213
- ) )
197
+ .call ( () -> Uni .createFrom ().completionStage ( assertExpectedResult ( PIGS_AFTER_DELETE ) ) )
214
198
);
215
199
}
216
200
@@ -224,11 +208,7 @@ public void testStageBatchingDelete(VertxTestContext context) {
224
208
.thenCompose ( pigs -> s .delete ( 10 , pigs .subList ( 0 , 2 ).toArray () ) )
225
209
) )
226
210
.thenAccept ( v -> assertSqlLogTracker ( "delete from pig where id=.*" ) )
227
- .thenCompose ( v -> getSessionFactory ().withStatelessTransaction ( s -> s
228
- .createQuery ( "from GuineaPig p" , GuineaPig .class )
229
- .getResultList ()
230
- .thenAccept ( guineaPigs -> assertThat ( guineaPigs ).hasSize ( 4 ) )
231
- ) )
211
+ .thenCompose ( v -> assertExpectedResult ( PIGS_AFTER_DELETE ) )
232
212
);
233
213
}
234
214
@@ -242,11 +222,7 @@ public void testStageBatchingDeleteMultiple(VertxTestContext context) {
242
222
.thenCompose ( pigs -> s .deleteMultiple ( pigs .subList ( 0 , 2 ) ) )
243
223
) )
244
224
.thenAccept ( v -> assertSqlLogTracker ( "delete from pig where id=.*" ) )
245
- .thenCompose ( v -> getSessionFactory ().withStatelessTransaction ( s -> s
246
- .createQuery ( "from GuineaPig p" , GuineaPig .class )
247
- .getResultList ()
248
- .thenAccept ( guineaPigs -> assertThat ( guineaPigs ).hasSize ( 4 ) )
249
- ) )
225
+ .thenCompose ( v -> assertExpectedResult ( PIGS_AFTER_DELETE ) )
250
226
);
251
227
}
252
228
@@ -260,11 +236,7 @@ public void testStageBatchingDeleteNoBatchSizeParameter(VertxTestContext context
260
236
.thenCompose ( pigs -> s .delete ( pigs .subList ( 0 , 2 ).toArray () ) )
261
237
) )
262
238
.thenAccept ( v -> assertSqlLogTracker ( "delete from pig where id=.*" ) )
263
- .thenCompose ( v -> getSessionFactory ().withStatelessTransaction ( s -> s
264
- .createQuery ( "from GuineaPig p" , GuineaPig .class )
265
- .getResultList ()
266
- .thenAccept ( guineaPigs -> assertThat ( guineaPigs ).hasSize ( 4 ) )
267
- ) )
239
+ .thenCompose ( v -> assertExpectedResult ( PIGS_AFTER_DELETE ) )
268
240
);
269
241
}
270
242
@@ -283,11 +255,7 @@ public void testMutinyBatchingUpdate(VertxTestContext context) {
283
255
} )
284
256
) )
285
257
.invoke ( () -> assertSqlLogTracker ( "update pig set name=.* where id=.*" ) )
286
- .chain ( () -> getMutinySessionFactory ().withStatelessTransaction ( s -> s
287
- .createQuery ( "select p from GuineaPig p order by id" , GuineaPig .class )
288
- .getResultList ()
289
- .invoke ( ReactiveStatelessDefaultBatchSizeTest ::checkPigsAreCorrectlyUpdated )
290
- ) )
258
+ .call ( () -> Uni .createFrom ().completionStage ( assertExpectedResult ( PIGS_AFTER_UPDATE ) ) )
291
259
);
292
260
}
293
261
@@ -306,11 +274,7 @@ public void testMutinyBatchingUpdateMultiple(VertxTestContext context) {
306
274
} ) )
307
275
)
308
276
.invoke ( () -> assertSqlLogTracker ( "update pig set name=.* where id=.*" ) )
309
- .chain ( () -> getMutinySessionFactory ().withStatelessTransaction ( s -> s
310
- .createQuery ( "from GuineaPig p order by id" , GuineaPig .class )
311
- .getResultList ()
312
- .invoke ( ReactiveStatelessDefaultBatchSizeTest ::checkPigsAreCorrectlyUpdated )
313
- ) )
277
+ .call ( () -> Uni .createFrom ().completionStage ( assertExpectedResult ( PIGS_AFTER_UPDATE ) ) )
314
278
);
315
279
}
316
280
@@ -329,11 +293,7 @@ public void testMutinyBatchingUpdateAllNoBatchSizeParameter(VertxTestContext con
329
293
} ) )
330
294
)
331
295
.invoke ( () -> assertSqlLogTracker ( "update pig set name=.* where id=.*" ) )
332
- .chain ( () -> getMutinySessionFactory ().withStatelessTransaction ( s -> s
333
- .createQuery ( "select p from GuineaPig p order by id" , GuineaPig .class )
334
- .getResultList ()
335
- .invoke ( ReactiveStatelessDefaultBatchSizeTest ::checkPigsAreCorrectlyUpdated )
336
- ) )
296
+ .call ( () -> Uni .createFrom ().completionStage ( assertExpectedResult ( PIGS_AFTER_UPDATE ) ) )
337
297
);
338
298
}
339
299
@@ -352,11 +312,7 @@ public void testStageBatchingUpdate(VertxTestContext context) {
352
312
} )
353
313
) )
354
314
.thenAccept ( v -> assertSqlLogTracker ( "update pig set name=.* where id=.*" ) )
355
- .thenCompose ( vo -> getSessionFactory ().withStatelessTransaction ( s -> s
356
- .createQuery ( "from GuineaPig p order by id" , GuineaPig .class )
357
- .getResultList ()
358
- .thenAccept ( ReactiveStatelessDefaultBatchSizeTest ::checkPigsAreCorrectlyUpdated )
359
- ) )
315
+ .thenCompose ( v -> assertExpectedResult ( PIGS_AFTER_UPDATE ) )
360
316
);
361
317
}
362
318
@@ -375,11 +331,7 @@ public void testStageBatchingUpdateMultiple(VertxTestContext context) {
375
331
} )
376
332
) )
377
333
.thenAccept ( v -> assertSqlLogTracker ( "update pig set name=.* where id=.*" ) )
378
- .thenCompose ( vo -> getSessionFactory ().withStatelessTransaction ( s -> s
379
- .createQuery ( "from GuineaPig p order by id" , GuineaPig .class )
380
- .getResultList ()
381
- .thenAccept ( ReactiveStatelessDefaultBatchSizeTest ::checkPigsAreCorrectlyUpdated )
382
- ) )
334
+ .thenCompose ( v -> assertExpectedResult ( PIGS_AFTER_UPDATE ) )
383
335
);
384
336
}
385
337
@@ -398,32 +350,24 @@ public void testStageBatchingUpdateNoBatchSizeParameter(VertxTestContext context
398
350
} )
399
351
) )
400
352
.thenAccept ( v -> assertSqlLogTracker ( "update pig set name=.* where id=.*" ) )
401
- .thenCompose ( v -> getSessionFactory ().withStatelessTransaction ( s -> s
402
- .createQuery ( "from GuineaPig p order by id" , GuineaPig .class )
403
- .getResultList ()
404
- .thenAccept ( ReactiveStatelessDefaultBatchSizeTest ::checkPigsAreCorrectlyUpdated )
405
- ) )
353
+ .thenCompose ( v -> assertExpectedResult ( PIGS_AFTER_UPDATE ) )
406
354
);
407
355
}
408
356
357
+ private CompletionStage <Void > assertExpectedResult (Object [] expected ) {
358
+ return getSessionFactory ().withStatelessTransaction ( s -> s
359
+ .createQuery ( "from GuineaPig p" , Object .class )
360
+ .getResultList ()
361
+ .thenAccept ( pigs -> assertThat ( pigs ).containsExactlyInAnyOrder ( expected ) ) );
362
+ }
363
+
409
364
private static void assertSqlLogTracker (String queryRegex ) {
410
365
// We expect only one query for each batched operations
411
366
assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
412
367
// Parameters are different for different dbs, so the regex must keep that in consideration
413
368
assertThat ( sqlTracker .getLoggedQueries () ).allMatch ( s -> s .matches ( queryRegex ) );
414
369
}
415
370
416
- private static void checkPigsAreCorrectlyUpdated (List <GuineaPig > guineaPigs ) {
417
- // Only the first 2 elements should have been updated
418
- assertThat ( guineaPigs .subList ( 0 , 2 ) )
419
- .extracting ( "name" )
420
- .containsExactly ( "One updated" , "Two updated" );
421
-
422
- // Every other entity should be the same
423
- GuineaPig [] array = guineaPigs .subList ( 2 , guineaPigs .size () ).toArray ( new GuineaPig [0 ] );
424
- assertThat ( guineaPigs .subList ( 2 , guineaPigs .size () ) ).containsExactly ( array );
425
- }
426
-
427
371
@ Entity (name = "GuineaPig" )
428
372
@ Table (name = "pig" )
429
373
public static class GuineaPig {
0 commit comments