1515import org .junit .jupiter .api .BeforeEach ;
1616import org .junit .jupiter .api .Test ;
1717
18+ import io .smallrye .mutiny .Uni ;
1819import io .vertx .junit5 .Timeout ;
1920import io .vertx .junit5 .VertxTestContext ;
2021import jakarta .persistence .Entity ;
2324import java .util .List ;
2425import java .util .Objects ;
2526import java .util .Set ;
27+ import java .util .concurrent .CompletionStage ;
2628
2729import static java .util .concurrent .TimeUnit .MINUTES ;
2830import static org .assertj .core .api .Assertions .assertThat ;
@@ -44,6 +46,19 @@ public class ReactiveStatelessDefaultBatchSizeTest extends BaseReactiveTest {
4446 new GuineaPig ( 66 , "Six" )
4547 };
4648
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+
4762 @ Override
4863 protected Set <Class <?>> annotatedEntities () {
4964 return Set .of ( GuineaPig .class );
@@ -87,6 +102,7 @@ public void testMutinyBatchingInsert(VertxTestContext context) {
87102 test ( context , getMutinySessionFactory ()
88103 .withStatelessTransaction ( s -> s .insertAll ( 10 , PIGS ) )
89104 .invoke ( () -> assertSqlLogTracker ( "insert into pig \\ (name,id\\ ) values (.*)" ) )
105+ .chain ( () -> Uni .createFrom ().completionStage ( assertExpectedResult ( PIGS ) ) )
90106 );
91107 }
92108
@@ -95,11 +111,7 @@ public void testMutinyBatchingInsertMultiple(VertxTestContext context) {
95111 test ( context , getMutinySessionFactory ()
96112 .withStatelessTransaction ( s -> s .insertMultiple ( List .of ( PIGS ) ) )
97113 .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 ) ) )
103115 );
104116 }
105117
@@ -108,11 +120,7 @@ public void testMutinyBatchingInsertAllNoBatchSizeParameter(VertxTestContext con
108120 test ( context , getMutinySessionFactory ()
109121 .withStatelessTransaction ( s -> s .insertAll ( PIGS ) )
110122 .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 ) ) )
116124 );
117125 }
118126
@@ -121,11 +129,7 @@ public void testStageBatchingInsert(VertxTestContext context) {
121129 test ( context , getSessionFactory ()
122130 .withStatelessTransaction ( s -> s .insert ( 10 , PIGS ) )
123131 .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 ) )
129133 );
130134 }
131135
@@ -134,23 +138,15 @@ public void testStageBatchingInsertMultiple(VertxTestContext context) {
134138 test ( context , getSessionFactory ()
135139 .withStatelessTransaction ( s -> s .insertMultiple ( List .of ( PIGS ) ) )
136140 .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 ) )
142142 );
143143 }
144144
145145 @ Test
146146 public void testStageBatchingInsertNoBatchSizeParameter (VertxTestContext context ) {
147147 test ( context , getSessionFactory ().withStatelessTransaction ( s -> s .insert ( PIGS ) )
148148 .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 ) )
154150 );
155151 }
156152
@@ -166,11 +162,7 @@ public void testMutinyBatchingDelete(VertxTestContext context) {
166162 .deleteAll ( 10 , pigs .subList ( 0 , 2 ).toArray () )
167163 ) )
168164 .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 ) ) )
174166 );
175167 }
176168
@@ -186,11 +178,7 @@ public void testMutinyBatchingDeleteMultiple(VertxTestContext context) {
186178 .deleteMultiple ( pigs .subList ( 0 , 2 ) ) )
187179 )
188180 .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 ) ) )
194182 );
195183 }
196184
@@ -206,11 +194,7 @@ public void testMutinyBatchingDeleteAllNoBatchSizeParameter(VertxTestContext con
206194 .deleteAll ( pigs .subList ( 0 , 2 ).toArray () )
207195 ) )
208196 .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 ) ) )
214198 );
215199 }
216200
@@ -224,11 +208,7 @@ public void testStageBatchingDelete(VertxTestContext context) {
224208 .thenCompose ( pigs -> s .delete ( 10 , pigs .subList ( 0 , 2 ).toArray () ) )
225209 ) )
226210 .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 ) )
232212 );
233213 }
234214
@@ -242,11 +222,7 @@ public void testStageBatchingDeleteMultiple(VertxTestContext context) {
242222 .thenCompose ( pigs -> s .deleteMultiple ( pigs .subList ( 0 , 2 ) ) )
243223 ) )
244224 .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 ) )
250226 );
251227 }
252228
@@ -260,11 +236,7 @@ public void testStageBatchingDeleteNoBatchSizeParameter(VertxTestContext context
260236 .thenCompose ( pigs -> s .delete ( pigs .subList ( 0 , 2 ).toArray () ) )
261237 ) )
262238 .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 ) )
268240 );
269241 }
270242
@@ -283,11 +255,7 @@ public void testMutinyBatchingUpdate(VertxTestContext context) {
283255 } )
284256 ) )
285257 .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 ) ) )
291259 );
292260 }
293261
@@ -306,11 +274,7 @@ public void testMutinyBatchingUpdateMultiple(VertxTestContext context) {
306274 } ) )
307275 )
308276 .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 ) ) )
314278 );
315279 }
316280
@@ -329,11 +293,7 @@ public void testMutinyBatchingUpdateAllNoBatchSizeParameter(VertxTestContext con
329293 } ) )
330294 )
331295 .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 ) ) )
337297 );
338298 }
339299
@@ -352,11 +312,7 @@ public void testStageBatchingUpdate(VertxTestContext context) {
352312 } )
353313 ) )
354314 .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 ) )
360316 );
361317 }
362318
@@ -375,11 +331,7 @@ public void testStageBatchingUpdateMultiple(VertxTestContext context) {
375331 } )
376332 ) )
377333 .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 ) )
383335 );
384336 }
385337
@@ -398,32 +350,24 @@ public void testStageBatchingUpdateNoBatchSizeParameter(VertxTestContext context
398350 } )
399351 ) )
400352 .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 ) )
406354 );
407355 }
408356
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+
409364 private static void assertSqlLogTracker (String queryRegex ) {
410365 // We expect only one query for each batched operations
411366 assertThat ( sqlTracker .getLoggedQueries () ).hasSize ( 1 );
412367 // Parameters are different for different dbs, so the regex must keep that in consideration
413368 assertThat ( sqlTracker .getLoggedQueries () ).allMatch ( s -> s .matches ( queryRegex ) );
414369 }
415370
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-
427371 @ Entity (name = "GuineaPig" )
428372 @ Table (name = "pig" )
429373 public static class GuineaPig {
0 commit comments