Skip to content

Commit 404c2dd

Browse files
committed
UPdate and clean up tests
1 parent 716d21f commit 404c2dd

File tree

5 files changed

+275
-251
lines changed

5 files changed

+275
-251
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/BatchFetchTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public void testQuery(VertxTestContext context) {
8484
} );
8585
} )
8686
) )
87-
.thenCompose( v -> openSession() )
88-
.thenCompose( s -> s.createSelectionQuery( "from Element e order by id", Element.class )
87+
.thenCompose( v -> getSessionFactory().withTransaction( s -> s
88+
.createSelectionQuery( "from Element e order by id", Element.class )
8989
.getResultList()
9090
.thenCompose( list -> {
9191
assertThat( list ).hasSize( 5 );
@@ -100,7 +100,7 @@ public void testQuery(VertxTestContext context) {
100100
list.forEach( element -> assertThat( s.getLockMode( element.node ) ).isEqualTo( LockMode.READ ) );
101101
} );
102102
} )
103-
)
103+
) )
104104
);
105105
}
106106

hibernate-reactive-core/src/test/java/org/hibernate/reactive/MutinySessionTest.java

Lines changed: 65 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import jakarta.persistence.LockModeType;
2626
import jakarta.persistence.Table;
2727
import jakarta.persistence.Version;
28+
import jakarta.persistence.metamodel.Attribute;
2829
import jakarta.persistence.metamodel.EntityType;
2930

3031
import static java.util.concurrent.TimeUnit.MINUTES;
@@ -69,12 +70,12 @@ public void reactiveFindMultipleIds(VertxTestContext context) {
6970
final GuineaPig emma = new GuineaPig( 77, "Emma" );
7071
test(
7172
context, populateDB()
72-
.chain( () -> getMutinySessionFactory().withTransaction( s -> s.persistAll( emma, rump ) ) )
73-
.chain( () -> getMutinySessionFactory().withTransaction( s -> s.find(
74-
GuineaPig.class,
75-
emma.getId(),
76-
rump.getId()
77-
) ) )
73+
.chain( () -> getMutinySessionFactory().withTransaction( s -> s
74+
.persistAll( emma, rump ) )
75+
)
76+
.chain( () -> getMutinySessionFactory().withTransaction( s -> s
77+
.find( GuineaPig.class, emma.getId(), rump.getId() )
78+
) )
7879
.invoke( pigs -> assertThat( pigs ).containsExactlyInAnyOrder( emma, rump ) )
7980
);
8081
}
@@ -101,24 +102,22 @@ public void sessionClear(VertxTestContext context) {
101102
@Test
102103
public void reactiveWithTransactionStatelessSession(VertxTestContext context) {
103104
final GuineaPig guineaPig = new GuineaPig( 61, "Mr. Peanutbutter" );
104-
test(
105-
context, getMutinySessionFactory()
106-
.withStatelessTransaction( s -> s.insert( guineaPig ) )
107-
.chain( () -> getMutinySessionFactory()
108-
.withSession( s -> s.find( GuineaPig.class, guineaPig.getId() ) ) )
109-
.invoke( result -> assertThatPigsAreEqual( guineaPig, result ) )
105+
test( context, getMutinySessionFactory()
106+
.withStatelessTransaction( s -> s.insert( guineaPig ) )
107+
.chain( () -> getMutinySessionFactory()
108+
.withSession( s -> s.find( GuineaPig.class, guineaPig.getId() ) ) )
109+
.invoke( result -> assertThatPigsAreEqual( guineaPig, result ) )
110110
);
111111
}
112112

113113
@Test
114114
public void reactiveWithTransactionSession(VertxTestContext context) {
115115
final GuineaPig guineaPig = new GuineaPig( 61, "Mr. Peanutbutter" );
116-
test(
117-
context, getMutinySessionFactory()
118-
.withTransaction( s -> s.persist( guineaPig ) )
119-
.chain( () -> getMutinySessionFactory()
120-
.withSession( s -> s.find( GuineaPig.class, guineaPig.getId() ) ) )
121-
.invoke( result -> assertThatPigsAreEqual( guineaPig, result ) )
116+
test( context, getMutinySessionFactory()
117+
.withTransaction( s -> s.persist( guineaPig ) )
118+
.chain( () -> getMutinySessionFactory()
119+
.withSession( s -> s.find( GuineaPig.class, guineaPig.getId() ) ) )
120+
.invoke( result -> assertThatPigsAreEqual( guineaPig, result ) )
122121
);
123122
}
124123

@@ -128,7 +127,7 @@ public void reactiveFind(VertxTestContext context) {
128127
test(
129128
context,
130129
populateDB()
131-
.call( () -> getMutinySessionFactory().withSession( session -> session
130+
.call( () -> getMutinySessionFactory().withTransaction( session -> session
132131
.find( GuineaPig.class, expectedPig.getId() )
133132
.invoke( actualPig -> {
134133
assertThatPigsAreEqual( expectedPig, actualPig );
@@ -139,26 +138,20 @@ public void reactiveFind(VertxTestContext context) {
139138
assertThat( session.contains( actualPig ) ).isFalse();
140139
} )
141140
) )
142-
143141
);
144142
}
145143

146144
@Test
147145
public void reactiveFindWithLock(VertxTestContext context) {
148146
final GuineaPig expectedPig = new GuineaPig( 5, "Aloi" );
149147
test(
150-
context,
151-
populateDB()
152-
.call( () -> getMutinySessionFactory().withSession(
153-
session -> session.find(
154-
GuineaPig.class,
155-
expectedPig.getId(),
156-
LockMode.PESSIMISTIC_WRITE
157-
)
158-
.invoke( actualPig -> {
159-
assertThatPigsAreEqual( expectedPig, actualPig );
160-
assertThat( session.getLockMode( actualPig ) ).isEqualTo( LockMode.PESSIMISTIC_WRITE );
161-
} )
148+
context, populateDB()
149+
.call( () -> getMutinySessionFactory().withSession( session -> session
150+
.find( GuineaPig.class, expectedPig.getId(), LockMode.PESSIMISTIC_WRITE )
151+
.invoke( actualPig -> {
152+
assertThatPigsAreEqual( expectedPig, actualPig );
153+
assertThat( session.getLockMode( actualPig ) ).isEqualTo( LockMode.PESSIMISTIC_WRITE );
154+
} )
162155
) )
163156
);
164157
}
@@ -167,15 +160,14 @@ public void reactiveFindWithLock(VertxTestContext context) {
167160
public void reactiveFindRefreshWithLock(VertxTestContext context) {
168161
final GuineaPig expectedPig = new GuineaPig( 5, "Aloi" );
169162
test(
170-
context,
171-
populateDB()
172-
.call( () -> getMutinySessionFactory().withSession(
173-
session -> session.find( GuineaPig.class, expectedPig.getId() )
174-
.call( pig -> session.refresh( pig, LockMode.PESSIMISTIC_WRITE ) )
175-
.invoke( actualPig -> {
176-
assertThatPigsAreEqual( expectedPig, actualPig );
177-
assertThat( session.getLockMode( actualPig ) ).isEqualTo( LockMode.PESSIMISTIC_WRITE );
178-
} )
163+
context, populateDB()
164+
.call( () -> getMutinySessionFactory().withSession( session -> session
165+
.find( GuineaPig.class, expectedPig.getId() )
166+
.call( pig -> session.refresh( pig, LockMode.PESSIMISTIC_WRITE ) )
167+
.invoke( actualPig -> {
168+
assertThatPigsAreEqual( expectedPig, actualPig );
169+
assertThat( session.getLockMode( actualPig ) ).isEqualTo( LockMode.PESSIMISTIC_WRITE );
170+
} )
179171
) )
180172
);
181173
}
@@ -184,8 +176,8 @@ public void reactiveFindRefreshWithLock(VertxTestContext context) {
184176
public void reactiveFindReadOnlyRefreshWithLock(VertxTestContext context) {
185177
final GuineaPig expectedPig = new GuineaPig( 5, "Aloi" );
186178
test(
187-
context,
188-
populateDB().call( () -> getMutinySessionFactory().withSession( session -> session
179+
context, populateDB()
180+
.call( () -> getMutinySessionFactory().withSession( session -> session
189181
.find( GuineaPig.class, expectedPig.getId() )
190182
.call( pig -> {
191183
session.setReadOnly( pig, true );
@@ -207,7 +199,7 @@ public void reactiveFindReadOnlyRefreshWithLock(VertxTestContext context) {
207199
.call( v -> session.refresh( pig ) )
208200
.invoke( v -> {
209201
assertThat( pig.name ).isEqualTo( "XXXX" );
210-
assertThat( session.isReadOnly( pig ) ).isTrue();
202+
assertThat( session.isReadOnly( pig ) ).isFalse();
211203
} );
212204
} )
213205
) )
@@ -218,8 +210,7 @@ public void reactiveFindReadOnlyRefreshWithLock(VertxTestContext context) {
218210
public void reactiveFindThenUpgradeLock(VertxTestContext context) {
219211
final GuineaPig expectedPig = new GuineaPig( 5, "Aloi" );
220212
test(
221-
context,
222-
populateDB()
213+
context, populateDB()
223214
.call( () -> getMutinySessionFactory().withSession( session -> session
224215
.find( GuineaPig.class, expectedPig.getId() )
225216
.call( pig -> session.lock( pig, LockMode.PESSIMISTIC_READ ) )
@@ -235,8 +226,7 @@ public void reactiveFindThenUpgradeLock(VertxTestContext context) {
235226
public void reactiveFindThenWriteLock(VertxTestContext context) {
236227
final GuineaPig expectedPig = new GuineaPig( 5, "Aloi" );
237228
test(
238-
context,
239-
populateDB()
229+
context, populateDB()
240230
.call( () -> getMutinySessionFactory().withSession( session -> session
241231
.find( GuineaPig.class, expectedPig.getId() )
242232
.call( pig -> session.lock( pig, LockMode.PESSIMISTIC_WRITE ) )
@@ -262,7 +252,7 @@ context, getMutinySessionFactory()
262252
public void reactivePersistInTx(VertxTestContext context) {
263253
test(
264254
context, getMutinySessionFactory()
265-
.withTransaction( (s, t) -> s.persist( new GuineaPig( 10, "Tulip" ) ) )
255+
.withTransaction( s -> s.persist( new GuineaPig( 10, "Tulip" ) ) )
266256
.chain( () -> selectNameFromId( 10 ) )
267257
.invoke( selectRes -> assertThat( selectRes ).isEqualTo( "Tulip" ) )
268258
);
@@ -273,10 +263,10 @@ public void reactiveRollbackTx(VertxTestContext context) {
273263
final RuntimeException expectedException = new RuntimeException( "For test, After flush" );
274264
test(
275265
context, assertThrown(
276-
RuntimeException.class,
277-
getMutinySessionFactory()
266+
RuntimeException.class, getMutinySessionFactory()
278267
.withTransaction( s -> s
279268
.persist( new GuineaPig( 10, "Tulip" ) )
269+
// Flush the changes but don't commit the transaction
280270
.call( s::flush )
281271
.invoke( () -> {
282272
// Throw an exception before committing the transaction
@@ -307,12 +297,14 @@ context, getMutinySessionFactory()
307297
@Test
308298
public void reactiveRemoveTransientEntity(VertxTestContext context) {
309299
test(
310-
context,
311-
populateDB()
300+
context, populateDB()
312301
.chain( () -> selectNameFromId( 5 ) )
313302
.invoke( name -> assertThat( name ).isNotNull() )
314303
.chain( this::openMutinySession )
315-
.chain( session -> assertThrown( HibernateException.class, session.remove( new GuineaPig( 5, "Aloi" ) ) ) )
304+
.chain( session -> assertThrown(
305+
HibernateException.class,
306+
session.remove( new GuineaPig( 5, "Aloi" ) )
307+
) )
316308
.invoke( e -> assertThat( e )
317309
.hasMessageContaining( "unmanaged instance passed to remove" )
318310
)
@@ -323,13 +315,12 @@ public void reactiveRemoveTransientEntity(VertxTestContext context) {
323315
public void reactiveRemoveManagedEntity(VertxTestContext context) {
324316
test(
325317
context, populateDB()
326-
.call( () -> getMutinySessionFactory().withSession( session -> session
318+
.call( () -> getMutinySessionFactory().withTransaction( session -> session
327319
.find( GuineaPig.class, 5 )
328320
.call( session::remove )
329-
.call( session::flush )
330321
) )
331322
.chain( () -> selectNameFromId( 5 ) )
332-
.invoke( name -> assertThat( name ).isNotNull() )
323+
.invoke( name -> assertThat( name ).isNull() )
333324
);
334325
}
335326

@@ -370,19 +361,19 @@ context, populateDB()
370361
public void reactiveUpdateVersion(VertxTestContext context) {
371362
final String NEW_NAME = "Tina";
372363
test(
373-
context, populateDB().call( () -> getMutinySessionFactory()
374-
.withSession( session -> session
375-
.find( GuineaPig.class, 5 )
376-
.invoke( pig -> {
377-
assertThat( pig ).isNotNull();
378-
// Checking we are actually changing the name
379-
assertThat( pig.getName() ).isNotEqualTo( NEW_NAME );
380-
assertThat( pig.version ).isNotEqualTo( 0 );
381-
pig.setName( NEW_NAME );
382-
pig.version = 10; //ignored by Hibernate
383-
} )
384-
.call( session::flush )
385-
) )
364+
context, populateDB()
365+
.call( () -> getMutinySessionFactory().withSession( session -> session
366+
.find( GuineaPig.class, 5 )
367+
.invoke( pig -> {
368+
assertThat( pig ).isNotNull();
369+
// Checking we are actually changing the name
370+
assertThat( pig.getName() ).isNotEqualTo( NEW_NAME );
371+
assertThat( pig.version ).isEqualTo( 0 );
372+
pig.setName( NEW_NAME );
373+
pig.version = 10; //ignored by Hibernate
374+
} )
375+
.call( session::flush ) )
376+
)
386377
.chain( () -> getMutinySessionFactory()
387378
.withSession( s -> s.find( GuineaPig.class, 5 ) ) )
388379
.invoke( pig -> assertThat( pig.version ).isEqualTo( 1 ) )
@@ -431,8 +422,7 @@ public void reactiveMultiQuery(VertxTestContext context) {
431422
AtomicInteger i = new AtomicInteger();
432423

433424
test(
434-
context,
435-
getMutinySessionFactory()
425+
context, getMutinySessionFactory()
436426
.withTransaction( session -> session.persistAll( foo, bar, baz ) )
437427
.call( () -> getMutinySessionFactory().withSession( session -> session
438428
.createSelectionQuery( "from GuineaPig", GuineaPig.class )
@@ -477,7 +467,9 @@ context, getMutinySessionFactory().withSession( session -> {
477467
public void testMetamodel() {
478468
EntityType<GuineaPig> pig = getSessionFactory().getMetamodel().entity( GuineaPig.class );
479469
assertThat( pig ).isNotNull();
480-
assertThat( pig.getAttributes() ).hasSize( 2 );
470+
assertThat( pig.getAttributes() )
471+
.map( Attribute::getName )
472+
.containsExactlyInAnyOrder( "id", "version", "name" );
481473
assertThat( pig.getName() ).isEqualTo( "GuineaPig" );
482474
}
483475

hibernate-reactive-core/src/test/java/org/hibernate/reactive/ReactiveMultitenantTest.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,25 @@ public void reactivePersistFindDelete(VertxTestContext context) {
6363
final GuineaPig guineaPig = new GuineaPig( 5, "Aloi" );
6464
test(
6565
context,
66-
getSessionFactory().openSession().thenCompose( session -> session
67-
.persist( guineaPig )
68-
.thenCompose( v -> session.flush() )
69-
.thenAccept( v -> session.detach( guineaPig ) )
70-
.thenAccept( v -> assertFalse( session.contains( guineaPig ) ) )
71-
.thenCompose( v -> session.find( GuineaPig.class, guineaPig.getId() ) )
72-
.thenAccept( actualPig -> {
73-
assertThatPigsAreEqual( guineaPig, actualPig );
74-
assertTrue( session.contains( actualPig ) );
75-
assertFalse( session.contains( guineaPig ) );
76-
assertEquals( LockMode.READ, session.getLockMode( actualPig ) );
77-
session.detach( actualPig );
78-
assertFalse( session.contains( actualPig ) );
79-
} )
80-
.thenCompose( v -> session.find( GuineaPig.class, guineaPig.getId() ) )
81-
.thenCompose( session::remove )
82-
.thenCompose( v -> session.flush() ) )
66+
getSessionFactory().openSession()
67+
.thenCompose( session -> session.withTransaction( t -> session
68+
.persist( guineaPig )
69+
.thenCompose( v -> session.flush() )
70+
.thenAccept( v -> session.detach( guineaPig ) )
71+
.thenAccept( v -> assertFalse( session.contains( guineaPig ) ) )
72+
.thenCompose( v -> session.find( GuineaPig.class, guineaPig.getId() ) )
73+
.thenAccept( actualPig -> {
74+
assertThatPigsAreEqual( guineaPig, actualPig );
75+
assertTrue( session.contains( actualPig ) );
76+
assertFalse( session.contains( guineaPig ) );
77+
assertEquals( LockMode.READ, session.getLockMode( actualPig ) );
78+
session.detach( actualPig );
79+
assertFalse( session.contains( actualPig ) );
80+
} )
81+
.thenCompose( v -> session.find( GuineaPig.class, guineaPig.getId() ) )
82+
.thenCompose( session::remove )
83+
.thenCompose( v -> session.flush() ) )
84+
)
8385
);
8486
}
8587

0 commit comments

Comments
 (0)