- Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathMySQLReactiveTest.java.qute
executable file
·213 lines (179 loc) · 6.92 KB
/
MySQLReactiveTest.java.qute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
///usr/bin/env jbang "$0" "$@" ; exit $?
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
//DEPS io.vertx:vertx-mysql-client:$\{vertx.version:4.5.14}
//DEPS io.vertx:vertx-unit:$\{vertx.version:4.5.14}
//DEPS org.hibernate.reactive:hibernate-reactive-core:$\{hibernate-reactive.version:3.0.0.Beta1}
//DEPS org.assertj:assertj-core:3.26.3
//DEPS junit:junit:4.13.2
//DEPS org.testcontainers:mysql:1.20.6
//DEPS org.slf4j:slf4j-simple:2.0.7
//// Testcontainer needs the JDBC drivers to start the container
//// Hibernate Reactive doesn't need it
//DEPS com.mysql:mysql-connector-j:9.1.0
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.reactive.mutiny.Mutiny;
import org.hibernate.reactive.provider.ReactiveServiceRegistryBuilder;
import org.hibernate.reactive.provider.Settings;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runner.notification.Failure;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;
import static org.assertj.core.api.Assertions.assertThat;
//DESCRIPTION An example of a JUnit test class for Hibernate Reactive using
//DESCRIPTION [Vert.x Unit](https://vertx.io/docs/vertx-unit/java),
//DESCRIPTION [Testcontainers](https://www.testcontainers.org)
//DESCRIPTION and [MySQL](https://www.mysql.com/)
//DESCRIPTION that you can run using [JBang](JBang).
//DESCRIPTION
//DESCRIPTION Before running the tests, Testcontainers will start the selected
//DESCRIPTION Docker image with the required database created.
//DESCRIPTION
//DESCRIPTION The `DATABASE` constant define which database to use and
//DESCRIPTION it can be change to any of the values in `Database`.
//DESCRIPTION
//DESCRIPTION Usage example:
//DESCRIPTION 1. Use as jbang template `jbang init -t mysql-reproducer@hibernate/hibernate-reactive mytest.java`
//DESCRIPTION 2. Run the test with JBang: `jbang mytest.java`
//DESCRIPTION 3. (Optional) Edit the file (with IntelliJ IDEA for example):
//DESCRIPTION jbang edit --live --open=idea mytest.java
@RunWith(VertxUnitRunner.class)
public class {baseName} {
public static DockerImageName imageName(String registry, String image, String version) {
return DockerImageName
.parse( registry + "/" + image + ":" + version )
.asCompatibleSubstituteFor( image );
}
@ClassRule
public final static MySQLContainer<?> database = new MySQLContainer<>( imageName( "docker.io", "mysql", "9.2.0" ) );
private Mutiny.SessionFactory sessionFactory;
@BeforeClass
public static void startContainer() {
database.start();
}
/**
* The \{@link Configuration} for the \{@link Mutiny.SessionFactory}.
*/
private Configuration createConfiguration() {
Configuration configuration = new Configuration();
// JDBC url
configuration.setProperty( Settings.URL, database.getJdbcUrl() );
// Credentials
configuration.setProperty( Settings.USER, database.getUsername() );
configuration.setProperty( Settings.PASS, database.getPassword() );
// Schema generation. Supported values are create, drop, create-drop, drop-create, none
configuration.setProperty( Settings.HBM2DDL_AUTO, "create" );
// Register new entity classes here
configuration.addAnnotatedClass( MyEntity.class );
// (Optional) Log the SQL queries
configuration.setProperty( Settings.SHOW_SQL, "true" );
configuration.setProperty( Settings.HIGHLIGHT_SQL, "true" );
configuration.setProperty( Settings.FORMAT_SQL, "true" );
return configuration;
}
/*
* Create a new factory and a new schema before each test (see
* property `hibernate.hbm2ddl.auto`).
* This way each test will start with a clean database.
*
* The drawback is that, in a real case scenario with multiple tests,
* it can slow down the whole test suite considerably. If that happens,
* it's possible to make the session factory static and, if necessary,
* delete the content of the tables manually (without dropping them).
*/
@Before
public void createSessionFactory() {
Configuration configuration = createConfiguration();
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder()
.applySettings( configuration.getProperties() );
StandardServiceRegistry registry = builder.build();
sessionFactory = configuration.buildSessionFactory( registry )
.unwrap( Mutiny.SessionFactory.class );
}
@Test
public void testInsertAndSelect(TestContext context) {
// the test will wait until async.complete or context.fail are called
Async async = context.async();
MyEntity entity = new MyEntity( "first entity", 1 );
sessionFactory
// insert the entity in the database
.withTransaction( (session, tx) -> session.persist( entity ) )
.chain( () -> sessionFactory
.withSession( session -> session
// look for the entity by id
.find( MyEntity.class, entity.getId() )
// assert that the returned entity is the right one
.invoke( foundEntity -> assertThat( foundEntity.getName() ).isEqualTo( entity.getName() ) ) ) )
.subscribe()
.with( res -> async.complete(), context::fail );
}
@After
public void closeFactory() {
if ( sessionFactory != null ) {
sessionFactory.close();
}
}
/**
* Example of a class representing an entity.
* <p>
* If you create new entities, be sure to add them in .
* For example:
* <pre>
* configuration.addAnnotatedClass( MyOtherEntity.class );
* </pre>
*/
@Entity(name = "MyEntity")
public static class MyEntity {
@Id
public Integer id;
public String name;
public MyEntity() {
}
public MyEntity(String name, Integer id) {
this.name = name;
this.id = id;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "MyEntity"
+ "\n\t id = " + id
+ "\n\t name = " + name;
}
}
// This main class is only for JBang so that it can run the tests with `jbang {baseName}.java`
public static void main(String[] args) {
System.out.println( "Starting the test suite with MySQL");
Result result = JUnitCore.runClasses( {baseName}.class );
for ( Failure failure : result.getFailures() ) {
System.out.println();
System.err.println( "Test " + failure.getTestHeader() + " FAILED!" );
System.err.println( "\t" + failure.getTrace() );
}
System.out.println();
System.out.print("Tests result summary: ");
System.out.println( result.wasSuccessful() ? "SUCCESS" : "FAILURE" );
}
}