Skip to content

Commit d5defd2

Browse files
blafondDavideD
authored andcommitted
[#1702] add test for upsert()
1 parent b8f4839 commit d5defd2

File tree

1 file changed

+166
-1
lines changed

1 file changed

+166
-1
lines changed

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

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import jakarta.persistence.criteria.Root;
2727

2828
import static java.util.concurrent.TimeUnit.MINUTES;
29+
import static org.assertj.core.api.Assertions.assertThat;
2930
import static org.junit.jupiter.api.Assertions.assertEquals;
3031
import static org.junit.jupiter.api.Assertions.assertFalse;
3132
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -37,7 +38,7 @@ public class ReactiveStatelessSessionTest extends BaseReactiveTest {
3738

3839
@Override
3940
protected Collection<Class<?>> annotatedEntities() {
40-
return List.of( GuineaPig.class );
41+
return List.of( Record.class, GuineaPig.class );
4142
}
4243

4344
@Test
@@ -206,6 +207,170 @@ private void assertThatPigsAreEqual( GuineaPig expected, GuineaPig actual) {
206207
assertEquals( expected.getName(), actual.getName() );
207208
}
208209

210+
/**
211+
* Mutiny version of Hibernate ORM org.hibernate.orm.test.stateless.UpsertTest
212+
*/
213+
@Test
214+
public void testMutinyUpsert(VertxTestContext context) {
215+
test( context, getMutinySessionFactory().withStatelessTransaction( ss -> ss
216+
.upsert( new Record( 123L, "hello earth" ) )
217+
.call( () -> ss.upsert( new Record( 456L, "hello mars" ) ) )
218+
)
219+
.call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
220+
.createQuery( "from Record order by id", Record.class ).getResultList() )
221+
.invoke( results -> assertThat( results ).containsExactly(
222+
new Record( 123L, "hello earth" ),
223+
new Record( 456L, "hello mars" )
224+
) )
225+
)
226+
.call( () -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
227+
.upsert( new Record( 123L, "goodbye earth" ) )
228+
) )
229+
.call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
230+
.createQuery( "from Record order by id", Record.class ).getResultList() )
231+
.invoke( results -> assertThat( results ).containsExactly(
232+
new Record( 123L, "goodbye earth" ),
233+
new Record( 456L, "hello mars" )
234+
) )
235+
)
236+
);
237+
}
238+
239+
/**
240+
* Mutiny version of Hibernate ORM org.hibernate.orm.test.stateless.UpsertTest
241+
*/
242+
@Test
243+
public void testMutinyUpsertWithEntityName(VertxTestContext context) {
244+
test( context, getMutinySessionFactory().withStatelessTransaction( ss -> ss
245+
.upsert( Record.class.getName(), new Record( 123L, "hello earth" ) )
246+
.call( () -> ss.upsert( Record.class.getName(), new Record( 456L, "hello mars" ) ) )
247+
)
248+
.call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
249+
.createQuery( "from Record order by id", Record.class ).getResultList() )
250+
.invoke( results -> assertThat( results ).containsExactly(
251+
new Record( 123L, "hello earth" ),
252+
new Record( 456L, "hello mars" )
253+
) )
254+
)
255+
.call( () -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
256+
.upsert( Record.class.getName(), new Record( 123L, "goodbye earth" ) )
257+
) )
258+
.call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
259+
.createQuery( "from Record order by id", Record.class ).getResultList() )
260+
.invoke( results -> assertThat( results ).containsExactly(
261+
new Record( 123L, "goodbye earth" ),
262+
new Record( 456L, "hello mars" )
263+
) )
264+
)
265+
);
266+
}
267+
268+
/**
269+
* CompletionStage version of Hibernate ORM org.hibernate.orm.test.stateless.UpsertTest
270+
*/
271+
@Test
272+
public void testStageUpsert(VertxTestContext context) {
273+
test( context, getSessionFactory().withStatelessTransaction( ss -> ss
274+
.upsert( new Record( 123L, "hello earth" ) )
275+
.thenCompose( v -> ss.upsert( new Record( 456L, "hello mars" ) ) )
276+
)
277+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
278+
.createQuery( "from Record order by id", Record.class ).getResultList() )
279+
.thenAccept( results -> assertThat( results ).containsExactly(
280+
new Record( 123L, "hello earth" ),
281+
new Record( 456L, "hello mars" )
282+
) )
283+
)
284+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
285+
.upsert( new Record( 123L, "goodbye earth" ) )
286+
) )
287+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
288+
.createQuery( "from Record order by id", Record.class ).getResultList() )
289+
.thenAccept( results -> assertThat( results ).containsExactly(
290+
new Record( 123L, "goodbye earth" ),
291+
new Record( 456L, "hello mars" )
292+
) )
293+
)
294+
);
295+
}
296+
297+
298+
/**
299+
* CompletionStage version of Hibernate ORM org.hibernate.orm.test.stateless.UpsertTest
300+
*/
301+
@Test
302+
public void testStageUpsertWithEntityName(VertxTestContext context) {
303+
test( context, getSessionFactory().withStatelessTransaction( ss -> ss
304+
.upsert( Record.class.getName(), new Record( 123L, "hello earth" ) )
305+
.thenCompose( v -> ss.upsert( Record.class.getName(), new Record( 456L, "hello mars" ) ) )
306+
)
307+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
308+
.createQuery( "from Record order by id", Record.class ).getResultList() )
309+
.thenAccept( results -> assertThat( results ).containsExactly(
310+
new Record( 123L, "hello earth" ),
311+
new Record( 456L, "hello mars" )
312+
) )
313+
)
314+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
315+
.upsert( Record.class.getName(), new Record( 123L, "goodbye earth" ) )
316+
) )
317+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
318+
.createQuery( "from Record order by id", Record.class ).getResultList() )
319+
.thenAccept( results -> assertThat( results ).containsExactly(
320+
new Record( 123L, "goodbye earth" ),
321+
new Record( 456L, "hello mars" )
322+
) )
323+
)
324+
);
325+
}
326+
327+
@Entity(name = "Record")
328+
@Table(name = "Record")
329+
public static class Record {
330+
@Id
331+
public Long id;
332+
public String message;
333+
334+
Record(Long id, String message) {
335+
this.id = id;
336+
this.message = message;
337+
}
338+
339+
Record() {
340+
}
341+
342+
public Long getId() {
343+
return id;
344+
}
345+
346+
public String getMessage() {
347+
return message;
348+
}
349+
350+
public void setMessage(String msg) {
351+
message = msg;
352+
}
353+
354+
// Equals and HashCode for simplifying the test assertions,
355+
// not to be taken as an example or for production.
356+
@Override
357+
public boolean equals(Object o) {
358+
if ( this == o ) {
359+
return true;
360+
}
361+
if ( o == null || getClass() != o.getClass() ) {
362+
return false;
363+
}
364+
Record record = (Record) o;
365+
return Objects.equals( id, record.id ) && Objects.equals( message, record.message );
366+
}
367+
368+
@Override
369+
public int hashCode() {
370+
return Objects.hash( id, message );
371+
}
372+
}
373+
209374
@NamedQuery(name = "findbyname", query = "from GuineaPig where name=:n")
210375
@NamedQuery(name = "updatebyname", query = "update GuineaPig set name='Y'")
211376
@NamedQuery(name = "findall", query = "from GuineaPig")

0 commit comments

Comments
 (0)