Skip to content

Commit 03bcfbd

Browse files
BAEL-8108 Hibernate Reactive and Quarkus (#17214)
* BAEL-8108 * BAEL-8108 remove lombok * correct test with given when then format * Update quarkus-modules/quarkus-hibernate-reactive/src/test/java/com/baeldung/web/DepositResourceIntegrationTest.java Co-authored-by: Liam Williams <[email protected]> --------- Co-authored-by: Liam Williams <[email protected]>
1 parent 0fd4fdb commit 03bcfbd

File tree

7 files changed

+368
-0
lines changed

7 files changed

+368
-0
lines changed

quarkus-modules/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<module>quarkus-funqy</module>
2323
<module>quarkus-testcontainers</module>
2424
<!-- requires Java 21 <module>quarkus-virtual-threads</module> -->
25+
<module>quarkus-hibernate-reactive</module>
2526
</modules>
2627

2728
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>quarkus-hibernate-reactive</artifactId>
6+
<version>1.0.0-SNAPSHOT</version>
7+
8+
9+
<properties>
10+
<compiler-plugin.version>3.12.1</compiler-plugin.version>
11+
<maven.compiler.release>17</maven.compiler.release>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
14+
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
15+
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
16+
<quarkus.platform.version>3.11.0</quarkus.platform.version>
17+
<skipITs>true</skipITs>
18+
<surefire-plugin.version>3.2.5</surefire-plugin.version>
19+
<quarkus-test-hibernate-reactive-panache.version>3.3.3</quarkus-test-hibernate-reactive-panache.version>
20+
<quarkus-jdbc-h2.version>3.12.0</quarkus-jdbc-h2.version>
21+
<quarkus-reactive-h2-client.version>0.3.4</quarkus-reactive-h2-client.version>
22+
</properties>
23+
24+
<dependencyManagement>
25+
<dependencies>
26+
<dependency>
27+
<groupId>${quarkus.platform.group-id}</groupId>
28+
<artifactId>${quarkus.platform.artifact-id}</artifactId>
29+
<version>${quarkus.platform.version}</version>
30+
<type>pom</type>
31+
<scope>import</scope>
32+
</dependency>
33+
</dependencies>
34+
</dependencyManagement>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>io.quarkus</groupId>
39+
<artifactId>quarkus-agroal</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>io.quarkus</groupId>
43+
<artifactId>quarkus-hibernate-reactive-panache</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.quarkus</groupId>
47+
<artifactId>quarkus-rest</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>io.quarkus</groupId>
51+
<artifactId>quarkus-rest-jackson</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.rest-assured</groupId>
55+
<artifactId>rest-assured</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>io.quarkus</groupId>
60+
<artifactId>quarkus-junit5</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>io.quarkus</groupId>
65+
<artifactId>quarkus-test-hibernate-reactive-panache</artifactId>
66+
<version>${quarkus-test-hibernate-reactive-panache.version}</version>
67+
<scope>test</scope>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>io.quarkus</groupId>
72+
<artifactId>quarkus-jdbc-h2</artifactId>
73+
<version>${quarkus-jdbc-h2.version}</version>
74+
</dependency>
75+
76+
77+
<dependency>
78+
<groupId>io.quarkiverse.quarkus-reactive-h2-client</groupId>
79+
<artifactId>quarkus-reactive-h2-client</artifactId>
80+
<version>${quarkus-reactive-h2-client.version}</version>
81+
</dependency>
82+
</dependencies>
83+
84+
<build>
85+
<plugins>
86+
<plugin>
87+
<groupId>${quarkus.platform.group-id}</groupId>
88+
<artifactId>quarkus-maven-plugin</artifactId>
89+
<version>${quarkus.platform.version}</version>
90+
<extensions>true</extensions>
91+
<executions>
92+
<execution>
93+
<goals>
94+
<goal>build</goal>
95+
<goal>generate-code</goal>
96+
<goal>generate-code-tests</goal>
97+
</goals>
98+
</execution>
99+
</executions>
100+
</plugin>
101+
<plugin>
102+
<artifactId>maven-compiler-plugin</artifactId>
103+
<version>${compiler-plugin.version}</version>
104+
<configuration>
105+
<compilerArgs>
106+
<arg>-parameters</arg>
107+
</compilerArgs>
108+
</configuration>
109+
</plugin>
110+
<plugin>
111+
<artifactId>maven-surefire-plugin</artifactId>
112+
<version>${surefire-plugin.version}</version>
113+
<configuration>
114+
<systemPropertyVariables>
115+
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
116+
<maven.home>${maven.home}</maven.home>
117+
</systemPropertyVariables>
118+
</configuration>
119+
</plugin>
120+
<plugin>
121+
<artifactId>maven-failsafe-plugin</artifactId>
122+
<version>${surefire-plugin.version}</version>
123+
<executions>
124+
<execution>
125+
<goals>
126+
<goal>integration-test</goal>
127+
<goal>verify</goal>
128+
</goals>
129+
</execution>
130+
</executions>
131+
<configuration>
132+
<systemPropertyVariables>
133+
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
134+
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
135+
<maven.home>${maven.home}</maven.home>
136+
</systemPropertyVariables>
137+
</configuration>
138+
</plugin>
139+
</plugins>
140+
</build>
141+
142+
<profiles>
143+
<profile>
144+
<id>native</id>
145+
<activation>
146+
<property>
147+
<name>native</name>
148+
</property>
149+
</activation>
150+
<properties>
151+
<skipITs>false</skipITs>
152+
<quarkus.native.enabled>true</quarkus.native.enabled>
153+
</properties>
154+
</profile>
155+
</profiles>
156+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.entity;
2+
3+
import jakarta.persistence.Entity;
4+
5+
import io.quarkus.hibernate.reactive.panache.PanacheEntity;
6+
import io.vertx.mutiny.sqlclient.Row;
7+
8+
@Entity
9+
public class Deposit extends PanacheEntity {
10+
11+
private String depositCode;
12+
private String currency;
13+
private String amount;
14+
15+
public static Deposit from(Row row) {
16+
return new Deposit(row.getString("DEPOSITCODE"), row.getString("CURRENCY"), row.getString("AMOUNT"));
17+
}
18+
19+
public Deposit() {
20+
}
21+
22+
public Deposit(String depositCode, String currency, String amount) {
23+
this.depositCode = depositCode;
24+
this.currency = currency;
25+
this.amount = amount;
26+
}
27+
28+
public String getDepositCode() {
29+
return depositCode;
30+
}
31+
32+
public void setDepositCode(String depositCode) {
33+
this.depositCode = depositCode;
34+
}
35+
36+
public String getCurrency() {
37+
return currency;
38+
}
39+
40+
public void setCurrency(String currency) {
41+
this.currency = currency;
42+
}
43+
44+
public String getAmount() {
45+
return amount;
46+
}
47+
48+
public void setAmount(String amount) {
49+
this.amount = amount;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.repository;
2+
3+
import com.baeldung.entity.Deposit;
4+
5+
import io.smallrye.mutiny.Multi;
6+
import io.smallrye.mutiny.Uni;
7+
import io.vertx.mutiny.jdbcclient.JDBCPool;
8+
9+
import jakarta.enterprise.context.ApplicationScoped;
10+
import jakarta.inject.Inject;
11+
12+
import org.hibernate.reactive.mutiny.Mutiny;
13+
14+
@ApplicationScoped
15+
public class DepositRepository {
16+
17+
@Inject
18+
Mutiny.SessionFactory sessionFactory;
19+
20+
@Inject
21+
JDBCPool client;
22+
23+
public Uni<Deposit> save(Deposit deposit) {
24+
return sessionFactory.withTransaction((session, transaction) -> session.persist(deposit)
25+
.replaceWith(deposit));
26+
}
27+
28+
public Multi<Deposit> streamAll() {
29+
return client.query("SELECT depositCode, currency,amount FROM Deposit ")
30+
.execute()
31+
.onItem()
32+
.transformToMulti(set -> Multi.createFrom()
33+
.iterable(set))
34+
.onItem()
35+
.transform(Deposit::from);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.web;
2+
3+
import com.baeldung.entity.Deposit;
4+
import com.baeldung.repository.DepositRepository;
5+
6+
import io.smallrye.mutiny.Multi;
7+
import io.smallrye.mutiny.Uni;
8+
9+
import jakarta.inject.Inject;
10+
import jakarta.ws.rs.*;
11+
import jakarta.ws.rs.core.MediaType;
12+
import jakarta.ws.rs.core.Response;
13+
14+
@Path("/deposits")
15+
@Produces(MediaType.APPLICATION_JSON)
16+
@Consumes(MediaType.APPLICATION_JSON)
17+
public class DepositResource {
18+
19+
@Inject
20+
DepositRepository repository;
21+
22+
@GET
23+
public Uni<Response> getAllDeposits() {
24+
return Deposit.listAll()
25+
.map(deposits -> Response.ok(deposits)
26+
.build());
27+
}
28+
29+
@POST
30+
public Uni<Response> createDeposit(Deposit deposit) {
31+
return deposit.persistAndFlush()
32+
.map(v -> Response.status(Response.Status.CREATED)
33+
.build());
34+
}
35+
36+
@GET
37+
@Path("stream")
38+
public Multi<Deposit> streamDeposits() {
39+
return repository.streamAll();
40+
}
41+
}
42+
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Configure your datasource
2+
quarkus.datasource.db-kind=h2
3+
quarkus.datasource.jdbc.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
4+
quarkus.datasource.jdbc.driver=org.h2.Driver
5+
quarkus.datasource.username=sa
6+
quarkus.datasource.password=sa
7+
quarkus.hibernate-orm.database.generation=drop-and-create
8+
quarkus.hibernate-orm.log.sql=true
9+
10+
# Configure the reactive JDBC datasource
11+
quarkus.datasource.reactive.url=vertx-reactive:h2:mem://test
12+
quarkus.datasource.reactive.driver=io.vertx.mutiny.jdbcclient.JDBCPool
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.web;
2+
3+
import static io.restassured.RestAssured.given;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.hasSize;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
import jakarta.inject.Inject;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import com.baeldung.entity.Deposit;
13+
import com.baeldung.repository.DepositRepository;
14+
15+
import io.quarkus.test.hibernate.reactive.panache.TransactionalUniAsserter;
16+
import io.quarkus.test.junit.QuarkusTest;
17+
import io.quarkus.test.vertx.RunOnVertxContext;
18+
import io.restassured.RestAssured;
19+
import io.restassured.response.Response;
20+
21+
@QuarkusTest
22+
public class DepositResourceIntegrationTest {
23+
24+
@Inject
25+
DepositRepository repository;
26+
27+
@Test
28+
public void givenAccountWithDeposits_whenGetDeposits_thenReturnAllDeposits() {
29+
given().when()
30+
.get("/deposits")
31+
.then()
32+
.statusCode(200);
33+
}
34+
35+
@Test
36+
@RunOnVertxContext
37+
public void givenDeposit_whenSaveDepositCalled_thenCheckCount(TransactionalUniAsserter asserter) {
38+
asserter.execute(() -> repository.save(new Deposit("DEP20230201", "10", "USD")));
39+
asserter.assertEquals(() -> Deposit.count(), 2l);
40+
}
41+
42+
@Test
43+
public void givenDepositsInDatabase_whenStreamAllDeposits_thenDepositsAreStreamedWithDelay() {
44+
Deposit deposit1 = new Deposit("67890", "USD", "200.0");
45+
Deposit deposit2 = new Deposit("67891", "USD", "300.0");
46+
repository.save(deposit1)
47+
.await()
48+
.indefinitely();
49+
repository.save(deposit2)
50+
.await()
51+
.indefinitely();
52+
Response response = RestAssured.get("/deposits/stream")
53+
.then()
54+
.extract()
55+
.response();
56+
57+
// Then: the response contains the streamed books with a delay
58+
response.then()
59+
.statusCode(200);
60+
response.then()
61+
.body("$", hasSize(2));
62+
response.then()
63+
.body("[0].depositCode", equalTo("67890"));
64+
response.then()
65+
.body("[1].depositCode", equalTo("67891"));
66+
}
67+
}

0 commit comments

Comments
 (0)