Skip to content

Commit 8ea0ac6

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

File tree

1 file changed

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

1 file changed

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

0 commit comments

Comments
 (0)