|
| 1 | +package org.hibernate.bugs; |
| 2 | + |
| 3 | +import org.hibernate.SessionFactory; |
| 4 | +import org.hibernate.boot.Metadata; |
| 5 | +import org.hibernate.boot.MetadataSources; |
| 6 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 7 | +import org.hibernate.bugs.model.Foo; |
| 8 | +import org.hibernate.reactive.mutiny.Mutiny; |
| 9 | +import org.hibernate.reactive.provider.ReactiveServiceRegistryBuilder; |
| 10 | +import org.hibernate.reactive.stage.Stage; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.AfterAll; |
| 13 | +import org.junit.jupiter.api.BeforeAll; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 16 | + |
| 17 | +import io.smallrye.mutiny.Uni; |
| 18 | +import io.vertx.junit5.VertxExtension; |
| 19 | +import io.vertx.junit5.VertxTestContext; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | + |
| 22 | +/** |
| 23 | + * Demonstrates how to develop a standalone test case for Hibernate Reactive using |
| 24 | + * the {@link org.hibernate.reactive.mutiny.Mutiny.SessionFactory} |
| 25 | + * or {@link org.hibernate.reactive.stage.Stage.SessionFactory}. |
| 26 | + * <p> |
| 27 | + * It uses the {@link VertxExtension} to ease the testing of async code using JUnit. |
| 28 | + * </p> |
| 29 | + * <p> |
| 30 | + * There are two test methods (feel free to pick your favorite): |
| 31 | + * <ui> |
| 32 | + * <li>{@link #testWithMutiny(VertxTestContext)} for using the Mutiny API</li> |
| 33 | + * <li>{@link #testWithStage(VertxTestContext)} for using the Stage API</li> |
| 34 | + * </ui> |
| 35 | + * </p> |
| 36 | + * <p> |
| 37 | + * By default, the tests expect a running PostgreSQL with {@code hreact} |
| 38 | + * as value for username, password, and database name. |
| 39 | + * </p> |
| 40 | + */ |
| 41 | +@ExtendWith(VertxExtension.class) |
| 42 | +public class ReactiveStandaloneTestCase { |
| 43 | + |
| 44 | + private static SessionFactory sf; |
| 45 | + |
| 46 | + @BeforeAll |
| 47 | + public static void setup() { |
| 48 | + StandardServiceRegistryBuilder srb = new ReactiveServiceRegistryBuilder() |
| 49 | + // Add in any settings that are specific to your test. See resources/hibernate.properties for the defaults. |
| 50 | + .applySetting( "hibernate.connection.url", ConnectionURL.POSTGRES ) |
| 51 | + .applySetting( "hibernate.show_sql", "true" ) |
| 52 | + .applySetting( "hibernate.format_sql", "true" ); |
| 53 | + |
| 54 | + Metadata metadata = new MetadataSources( srb.build() ) |
| 55 | + // Add your entities here. |
| 56 | + .addAnnotatedClass( Foo.class ) |
| 57 | + .buildMetadata(); |
| 58 | + |
| 59 | + sf = metadata.buildSessionFactory(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testWithMutiny(VertxTestContext context) { |
| 64 | + Mutiny.SessionFactory mutinySf = sf.unwrap( Mutiny.SessionFactory.class ); |
| 65 | + mutinySf |
| 66 | + // Example of transactional block |
| 67 | + .withTransaction( session -> { |
| 68 | + return Uni.createFrom().voidItem(); |
| 69 | + } ) |
| 70 | + // Subscribe the uni and wait until it completes |
| 71 | + .subscribe().with( res -> context.completeNow(), context::failNow ); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testWithStage(VertxTestContext context) { |
| 76 | + Stage.SessionFactory stageSf = sf.unwrap( Stage.SessionFactory.class ); |
| 77 | + stageSf |
| 78 | + // Example of transactional block |
| 79 | + .withTransaction( session -> { |
| 80 | + return CompletableFuture.completedFuture( null ); |
| 81 | + } ) |
| 82 | + // Stop the test when the CompletionStage completes |
| 83 | + .whenComplete( (res, err) -> { |
| 84 | + if ( err != null ) { |
| 85 | + context.failNow( err ); |
| 86 | + } |
| 87 | + else { |
| 88 | + context.completeNow(); |
| 89 | + } |
| 90 | + } ); |
| 91 | + } |
| 92 | + |
| 93 | + // It's important to always close the factory at the end of the tests. |
| 94 | + @AfterAll |
| 95 | + public static void closeSessionFactory() { |
| 96 | + if ( sf != null ) { |
| 97 | + sf.close(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments