Skip to content

Commit eaff803

Browse files
committed
[#1885] Add test for createMutationQuery methods that accept update and delete criterias
1 parent 7fcc525 commit eaff803

File tree

2 files changed

+318
-0
lines changed

2 files changed

+318
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import io.vertx.junit5.Timeout;
16+
import io.vertx.junit5.VertxTestContext;
17+
import jakarta.persistence.Entity;
18+
import jakarta.persistence.Id;
19+
import jakarta.persistence.Table;
20+
import jakarta.persistence.criteria.CriteriaBuilder;
21+
import jakarta.persistence.criteria.CriteriaDelete;
22+
import jakarta.persistence.criteria.Root;
23+
24+
import static java.util.concurrent.TimeUnit.MINUTES;
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
@Timeout(value = 10, timeUnit = MINUTES)
28+
public class MutationQueryDeleteCriteriaTest extends BaseReactiveTest {
29+
30+
Flour spelt = new Flour( 1, "Spelt", "An ancient grain, is a hexaploid species of wheat.", "Wheat flour" );
31+
Flour rye = new Flour( 2, "Rye", "Used to bake the traditional sourdough breads of Germany.", "Wheat flour" );
32+
Flour almond = new Flour( 3, "Almond", "made from ground almonds.", "Gluten free" );
33+
34+
@Override
35+
protected Collection<Class<?>> annotatedEntities() {
36+
return List.of( Flour.class );
37+
}
38+
39+
@BeforeEach
40+
public void populateDb(VertxTestContext context) {
41+
test( context, getSessionFactory().withTransaction( s -> s.persist( spelt, rye, almond ) ) );
42+
}
43+
44+
@Test
45+
public void testDeleteCriteriaQuery(VertxTestContext context) {
46+
test(
47+
context,
48+
openSession()
49+
.thenCompose( s -> {
50+
CriteriaBuilder criteriaBuilder = s.getFactory().getCriteriaBuilder();
51+
CriteriaDelete<Flour> criteriaDelete = criteriaBuilder.createCriteriaDelete( Flour.class );
52+
Root<Flour> from = criteriaDelete.from( Flour.class );
53+
criteriaDelete.where( criteriaBuilder.equal( from.get( "id" ), rye.getId() ) );
54+
return s.createMutationQuery( criteriaDelete ).executeUpdate();
55+
} )
56+
.thenAccept( resultCount -> assertThat( resultCount ).isEqualTo( 1 ) )
57+
.thenCompose( v -> openSession() )
58+
.thenCompose( s -> s.find( Flour.class, rye.getId() ) )
59+
.thenAccept( result -> assertThat( result ).isNull() )
60+
);
61+
}
62+
63+
@Test
64+
public void testMutinyDeleteCriteriaQuery(VertxTestContext context) {
65+
test(
66+
context,
67+
openMutinySession()
68+
.chain( s -> {
69+
CriteriaBuilder criteriaBuilder = s.getFactory().getCriteriaBuilder();
70+
CriteriaDelete<Flour> criteriaDelete = criteriaBuilder.createCriteriaDelete( Flour.class );
71+
Root<Flour> from = criteriaDelete.from( Flour.class );
72+
criteriaDelete.where( criteriaBuilder.equal( from.get( "id" ), almond.getId() ) );
73+
return s.createMutationQuery( criteriaDelete ).executeUpdate();
74+
} )
75+
.invoke( resultCount -> assertThat( resultCount ).isEqualTo( 1 ) )
76+
.chain( v -> openMutinySession() )
77+
.chain( s -> s.find( Flour.class, almond.getId() ) )
78+
.invoke( result -> assertThat( result ).isNull() )
79+
);
80+
}
81+
82+
@Entity(name = "Flour")
83+
@Table(name = "Flour")
84+
public static class Flour {
85+
@Id
86+
private Integer id;
87+
private String name;
88+
private String description;
89+
private String type;
90+
91+
public Flour() {
92+
}
93+
94+
public Flour(Integer id, String name, String description, String type) {
95+
this.id = id;
96+
this.name = name;
97+
this.description = description;
98+
this.type = type;
99+
}
100+
101+
public Integer getId() {
102+
return id;
103+
}
104+
105+
public void setId(Integer id) {
106+
this.id = id;
107+
}
108+
109+
public String getName() {
110+
return name;
111+
}
112+
113+
public void setName(String name) {
114+
this.name = name;
115+
}
116+
117+
public String getDescription() {
118+
return description;
119+
}
120+
121+
public void setDescription(String description) {
122+
this.description = description;
123+
}
124+
125+
public String getType() {
126+
return type;
127+
}
128+
129+
public void setType(String type) {
130+
this.type = type;
131+
}
132+
133+
@Override
134+
public String toString() {
135+
return name;
136+
}
137+
138+
@Override
139+
public boolean equals(Object o) {
140+
if ( this == o ) {
141+
return true;
142+
}
143+
if ( o == null || getClass() != o.getClass() ) {
144+
return false;
145+
}
146+
Flour flour = (Flour) o;
147+
return Objects.equals( name, flour.name ) &&
148+
Objects.equals( description, flour.description ) &&
149+
Objects.equals( type, flour.type );
150+
}
151+
152+
@Override
153+
public int hashCode() {
154+
return Objects.hash( name, description, type );
155+
}
156+
}
157+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import io.vertx.junit5.Timeout;
16+
import io.vertx.junit5.VertxTestContext;
17+
import jakarta.persistence.Entity;
18+
import jakarta.persistence.Id;
19+
import jakarta.persistence.Table;
20+
import jakarta.persistence.criteria.CriteriaBuilder;
21+
import jakarta.persistence.criteria.CriteriaUpdate;
22+
import jakarta.persistence.criteria.Root;
23+
24+
import static java.util.concurrent.TimeUnit.MINUTES;
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
@Timeout(value = 10, timeUnit = MINUTES)
28+
public class MutationQueryUpdateCriteriaTest extends BaseReactiveTest {
29+
30+
Flour spelt = new Flour( 1, "Spelt", "An ancient grain, is a hexaploid species of wheat.", "Wheat flour" );
31+
Flour rye = new Flour( 2, "Rye", "Used to bake the traditional sourdough breads of Germany.", "Wheat flour" );
32+
Flour almond = new Flour( 3, "Almond", "", "Gluten free" );
33+
34+
@Override
35+
protected Collection<Class<?>> annotatedEntities() {
36+
return List.of( Flour.class );
37+
}
38+
39+
@BeforeEach
40+
public void populateDb(VertxTestContext context) {
41+
test( context, getSessionFactory().withTransaction( s -> s.persist( spelt, rye, almond ) ) );
42+
}
43+
44+
@Test
45+
public void testUpdateCriteriaQuery(VertxTestContext context) {
46+
String updatedDescription = "Most rye breads use a mix of rye and wheat flours";
47+
test(
48+
context,
49+
openSession()
50+
.thenCompose( s -> {
51+
CriteriaBuilder criteriaBuilder = s.getFactory().getCriteriaBuilder();
52+
CriteriaUpdate<Flour> criteriaUpdate = criteriaBuilder.createCriteriaUpdate( Flour.class );
53+
Root<Flour> from = criteriaUpdate.from( Flour.class );
54+
criteriaUpdate.set( from.get( "description" ), updatedDescription );
55+
criteriaUpdate.where( criteriaBuilder.equal( from.get( "id" ), rye.getId() ) );
56+
return s.createMutationQuery( criteriaUpdate ).executeUpdate();
57+
} )
58+
.thenAccept( resultCount -> assertThat( resultCount ).isEqualTo( 1 ) )
59+
.thenCompose( v -> openSession() )
60+
.thenCompose( s -> s.find( Flour.class, rye.getId() ) )
61+
.thenAccept( result -> assertThat( result.getDescription() ).isEqualTo( updatedDescription ) )
62+
);
63+
}
64+
65+
@Test
66+
public void testMutinyUpdateCriteriaQuery(VertxTestContext context) {
67+
String updatedDescription = "made from ground almonds.";
68+
test(
69+
context,
70+
openMutinySession()
71+
.chain( s -> {
72+
CriteriaBuilder criteriaBuilder = s.getFactory().getCriteriaBuilder();
73+
CriteriaUpdate<Flour> criteriaUpdate = criteriaBuilder.createCriteriaUpdate( Flour.class );
74+
Root<Flour> from = criteriaUpdate.from( Flour.class );
75+
criteriaUpdate.set( from.get( "description" ), updatedDescription );
76+
criteriaUpdate.where( criteriaBuilder.equal( from.get( "id" ), almond.getId() ) );
77+
return s.createMutationQuery( criteriaUpdate ).executeUpdate();
78+
} )
79+
.invoke( resultCount -> assertThat( resultCount ).isEqualTo( 1 ) )
80+
.chain( v -> openMutinySession() )
81+
.chain( s -> s.find( Flour.class, almond.getId() ) )
82+
.invoke( result -> assertThat( result.getDescription() ).isEqualTo( updatedDescription ) )
83+
);
84+
}
85+
86+
@Entity(name = "Flour")
87+
@Table(name = "Flour")
88+
public static class Flour {
89+
@Id
90+
private Integer id;
91+
private String name;
92+
private String description;
93+
private String type;
94+
95+
public Flour() {
96+
}
97+
98+
public Flour(Integer id, String name, String description, String type) {
99+
this.id = id;
100+
this.name = name;
101+
this.description = description;
102+
this.type = type;
103+
}
104+
105+
public Integer getId() {
106+
return id;
107+
}
108+
109+
public void setId(Integer id) {
110+
this.id = id;
111+
}
112+
113+
public String getName() {
114+
return name;
115+
}
116+
117+
public void setName(String name) {
118+
this.name = name;
119+
}
120+
121+
public String getDescription() {
122+
return description;
123+
}
124+
125+
public void setDescription(String description) {
126+
this.description = description;
127+
}
128+
129+
public String getType() {
130+
return type;
131+
}
132+
133+
public void setType(String type) {
134+
this.type = type;
135+
}
136+
137+
@Override
138+
public String toString() {
139+
return name;
140+
}
141+
142+
@Override
143+
public boolean equals(Object o) {
144+
if ( this == o ) {
145+
return true;
146+
}
147+
if ( o == null || getClass() != o.getClass() ) {
148+
return false;
149+
}
150+
Flour flour = (Flour) o;
151+
return Objects.equals( name, flour.name ) &&
152+
Objects.equals( description, flour.description ) &&
153+
Objects.equals( type, flour.type );
154+
}
155+
156+
@Override
157+
public int hashCode() {
158+
return Objects.hash( name, description, type );
159+
}
160+
}
161+
}

0 commit comments

Comments
 (0)