Skip to content

Commit dfc5088

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

File tree

1 file changed

+198
-0
lines changed
  • hibernate-reactive-core/src/test/java/org/hibernate/reactive

1 file changed

+198
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive;
7+
8+
import java.util.Collection;
9+
import java.util.List;
10+
import java.util.Objects;
11+
12+
import org.hibernate.reactive.testing.DBSelectionExtension;
13+
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.extension.RegisterExtension;
16+
17+
import io.vertx.junit5.Timeout;
18+
import io.vertx.junit5.VertxTestContext;
19+
import jakarta.persistence.Entity;
20+
import jakarta.persistence.Id;
21+
import jakarta.persistence.Table;
22+
23+
import static java.util.concurrent.TimeUnit.MINUTES;
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MARIA;
26+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MYSQL;
27+
import static org.hibernate.reactive.testing.DBSelectionExtension.skipTestsFor;
28+
29+
/**
30+
* Same as Hibernate ORM org.hibernate.orm.test.stateless.UpsertTest
31+
* <p>
32+
* These tests are in a separate class because we need to skip the execution on some databases,
33+
* but once this has been resolved, they could be in {@link ReactiveStatelessSessionTest}.
34+
* </p>
35+
*/
36+
@Timeout(value = 10, timeUnit = MINUTES)
37+
public class UpsertTest extends BaseReactiveTest {
38+
39+
// MySQL, MariaDB: Something is missing in HR to make it work
40+
@RegisterExtension
41+
public DBSelectionExtension dbSelection = skipTestsFor( MARIA, MYSQL );
42+
43+
@Override
44+
protected Collection<Class<?>> annotatedEntities() {
45+
return List.of( Record.class );
46+
}
47+
48+
@Test
49+
public void testMutinyUpsert(VertxTestContext context) {
50+
test( context, getMutinySessionFactory().withStatelessTransaction( ss -> ss
51+
.upsert( new Record( 123L, "hello earth" ) )
52+
.call( () -> ss.upsert( new Record( 456L, "hello mars" ) ) )
53+
)
54+
.call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
55+
.createQuery( "from Record order by id", Record.class ).getResultList() )
56+
.invoke( results -> assertThat( results ).containsExactly(
57+
new Record( 123L, "hello earth" ),
58+
new Record( 456L, "hello mars" )
59+
) )
60+
)
61+
.call( () -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
62+
.upsert( new Record( 123L, "goodbye earth" ) )
63+
) )
64+
.call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
65+
.createQuery( "from Record order by id", Record.class ).getResultList() )
66+
.invoke( results -> assertThat( results ).containsExactly(
67+
new Record( 123L, "goodbye earth" ),
68+
new Record( 456L, "hello mars" )
69+
) )
70+
)
71+
);
72+
}
73+
74+
@Test
75+
public void testMutinyUpsertWithEntityName(VertxTestContext context) {
76+
test( context, getMutinySessionFactory().withStatelessTransaction( ss -> ss
77+
.upsert( Record.class.getName(), new Record( 123L, "hello earth" ) )
78+
.call( () -> ss.upsert( Record.class.getName(), new Record( 456L, "hello mars" ) ) )
79+
)
80+
.call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
81+
.createQuery( "from Record order by id", Record.class ).getResultList() )
82+
.invoke( results -> assertThat( results ).containsExactly(
83+
new Record( 123L, "hello earth" ),
84+
new Record( 456L, "hello mars" )
85+
) )
86+
)
87+
.call( () -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
88+
.upsert( Record.class.getName(), new Record( 123L, "goodbye earth" ) )
89+
) )
90+
.call( v -> getMutinySessionFactory().withStatelessTransaction( ss -> ss
91+
.createQuery( "from Record order by id", Record.class ).getResultList() )
92+
.invoke( results -> assertThat( results ).containsExactly(
93+
new Record( 123L, "goodbye earth" ),
94+
new Record( 456L, "hello mars" )
95+
) )
96+
)
97+
);
98+
}
99+
100+
@Test
101+
public void testStageUpsert(VertxTestContext context) {
102+
test( context, getSessionFactory().withStatelessTransaction( ss -> ss
103+
.upsert( new Record( 123L, "hello earth" ) )
104+
.thenCompose( v -> ss.upsert( new Record( 456L, "hello mars" ) ) )
105+
)
106+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
107+
.createQuery( "from Record order by id", Record.class ).getResultList() )
108+
.thenAccept( results -> assertThat( results ).containsExactly(
109+
new Record( 123L, "hello earth" ),
110+
new Record( 456L, "hello mars" )
111+
) )
112+
)
113+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
114+
.upsert( new Record( 123L, "goodbye earth" ) )
115+
) )
116+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
117+
.createQuery( "from Record order by id", Record.class ).getResultList() )
118+
.thenAccept( results -> assertThat( results ).containsExactly(
119+
new Record( 123L, "goodbye earth" ),
120+
new Record( 456L, "hello mars" )
121+
) )
122+
)
123+
);
124+
}
125+
126+
@Test
127+
public void testStageUpsertWithEntityName(VertxTestContext context) {
128+
test( context, getSessionFactory().withStatelessTransaction( ss -> ss
129+
.upsert( Record.class.getName(), new Record( 123L, "hello earth" ) )
130+
.thenCompose( v -> ss.upsert( Record.class.getName(), new Record( 456L, "hello mars" ) ) )
131+
)
132+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
133+
.createQuery( "from Record order by id", Record.class ).getResultList() )
134+
.thenAccept( results -> assertThat( results ).containsExactly(
135+
new Record( 123L, "hello earth" ),
136+
new Record( 456L, "hello mars" )
137+
) )
138+
)
139+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
140+
.upsert( Record.class.getName(), new Record( 123L, "goodbye earth" ) )
141+
) )
142+
.thenCompose( v -> getSessionFactory().withStatelessTransaction( ss -> ss
143+
.createQuery( "from Record order by id", Record.class ).getResultList() )
144+
.thenAccept( results -> assertThat( results ).containsExactly(
145+
new Record( 123L, "goodbye earth" ),
146+
new Record( 456L, "hello mars" )
147+
) )
148+
)
149+
);
150+
}
151+
152+
@Entity(name = "Record")
153+
@Table(name = "Record")
154+
public static class Record {
155+
@Id
156+
public Long id;
157+
public String message;
158+
159+
Record(Long id, String message) {
160+
this.id = id;
161+
this.message = message;
162+
}
163+
164+
Record() {
165+
}
166+
167+
public Long getId() {
168+
return id;
169+
}
170+
171+
public String getMessage() {
172+
return message;
173+
}
174+
175+
public void setMessage(String msg) {
176+
message = msg;
177+
}
178+
179+
// Equals and HashCode for simplifying the test assertions,
180+
// not to be taken as an example or for production.
181+
@Override
182+
public boolean equals(Object o) {
183+
if ( this == o ) {
184+
return true;
185+
}
186+
if ( o == null || getClass() != o.getClass() ) {
187+
return false;
188+
}
189+
Record record = (Record) o;
190+
return Objects.equals( id, record.id ) && Objects.equals( message, record.message );
191+
}
192+
193+
@Override
194+
public int hashCode() {
195+
return Objects.hash( id, message );
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)