Skip to content

Commit d3d2176

Browse files
blafondDavideD
authored andcommitted
[#1702] add test for upsert()
1 parent 9b65978 commit d3d2176

File tree

1 file changed

+137
-1
lines changed

1 file changed

+137
-1
lines changed

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

Lines changed: 137 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,141 @@ 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 testMutinyUpsertWithEntityName(VertxTestContext context) {
215+
test( context, getMutinySessionFactory().withStatelessTransaction( ss -> ss
216+
.upsert( Record.class.getName(), new Record( 123L, "hello earth" ) )
217+
.call( () -> ss.upsert( Record.class.getName(), 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( Record.class.getName(), 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+
* CompletionStage version of Hibernate ORM org.hibernate.orm.test.stateless.UpsertTest
241+
*/
242+
@Test
243+
public void testStageUpsert(VertxTestContext context) {
244+
test( context, getSessionFactory().withStatelessTransaction( ss -> ss
245+
.upsert( new Record( 123L, "hello earth" ) )
246+
.thenCompose( v -> ss.upsert( new Record( 456L, "hello mars" ) ) )
247+
)
248+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
249+
.createQuery( "from Record order by id", Record.class ).getResultList() )
250+
.thenAccept( results -> assertThat( results ).containsExactly(
251+
new Record( 123L, "hello earth" ),
252+
new Record( 456L, "hello mars" )
253+
) )
254+
)
255+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
256+
.upsert( new Record( 123L, "goodbye earth" ) )
257+
) )
258+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
259+
.createQuery( "from Record order by id", Record.class ).getResultList() )
260+
.thenAccept( results -> assertThat( results ).containsExactly(
261+
new Record( 123L, "goodbye earth" ),
262+
new Record( 456L, "hello mars" )
263+
) )
264+
)
265+
);
266+
}
267+
268+
269+
/**
270+
* CompletionStage version of Hibernate ORM org.hibernate.orm.test.stateless.UpsertTest
271+
*/
272+
@Test
273+
public void testStageUpsertWithEntityName(VertxTestContext context) {
274+
test( context, getSessionFactory().withStatelessTransaction( ss -> ss
275+
.upsert( Record.class.getName(), new Record( 123L, "hello earth" ) )
276+
.thenCompose( v -> ss.upsert( Record.class.getName(), new Record( 456L, "hello mars" ) ) )
277+
)
278+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
279+
.createQuery( "from Record order by id", Record.class ).getResultList() )
280+
.thenAccept( results -> assertThat( results ).containsExactly(
281+
new Record( 123L, "hello earth" ),
282+
new Record( 456L, "hello mars" )
283+
) )
284+
)
285+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
286+
.upsert( Record.class.getName(), new Record( 123L, "goodbye earth" ) )
287+
) )
288+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
289+
.createQuery( "from Record order by id", Record.class ).getResultList() )
290+
.thenAccept( results -> assertThat( results ).containsExactly(
291+
new Record( 123L, "goodbye earth" ),
292+
new Record( 456L, "hello mars" )
293+
) )
294+
)
295+
);
296+
}
297+
298+
@Entity(name = "Record")
299+
@Table(name = "Record")
300+
public static class Record {
301+
@Id
302+
public Long id;
303+
public String message;
304+
305+
Record(Long id, String message) {
306+
this.id = id;
307+
this.message = message;
308+
}
309+
310+
Record() {
311+
}
312+
313+
public Long getId() {
314+
return id;
315+
}
316+
317+
public String getMessage() {
318+
return message;
319+
}
320+
321+
public void setMessage(String msg) {
322+
message = msg;
323+
}
324+
325+
// Equals and HashCode for simplifying the test assertions,
326+
// not to be taken as an example or for production.
327+
@Override
328+
public boolean equals(Object o) {
329+
if ( this == o ) {
330+
return true;
331+
}
332+
if ( o == null || getClass() != o.getClass() ) {
333+
return false;
334+
}
335+
Record record = (Record) o;
336+
return Objects.equals( id, record.id ) && Objects.equals( message, record.message );
337+
}
338+
339+
@Override
340+
public int hashCode() {
341+
return Objects.hash( id, message );
342+
}
343+
}
344+
209345
@NamedQuery(name = "findbyname", query = "from GuineaPig where name=:n")
210346
@NamedQuery(name = "updatebyname", query = "update GuineaPig set name='Y'")
211347
@NamedQuery(name = "findall", query = "from GuineaPig")

0 commit comments

Comments
 (0)