From 684f3175aace61ce049dc35893bc418b1de06084 Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Thu, 23 Nov 2023 11:58:08 +0100 Subject: [PATCH] [#1792] Add test for getSingleResultOrNull and getSingleResult We are checking that it doesn't throw a NullPointerException if the result of the query doesn't have any columns names. --- .../CustomOneToOneStoredProcedureSqlTest.java | 19 +++++++++++++++++++ .../CustomStoredProcedureSqlTest.java | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CustomOneToOneStoredProcedureSqlTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CustomOneToOneStoredProcedureSqlTest.java index ef542e8a3..451c95fee 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CustomOneToOneStoredProcedureSqlTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CustomOneToOneStoredProcedureSqlTest.java @@ -9,9 +9,11 @@ import io.vertx.junit5.VertxTestContext; import jakarta.persistence.*; +import org.hibernate.HibernateException; import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLInsert; import org.hibernate.reactive.testing.DBSelectionExtension; +import org.hibernate.reactive.testing.ReactiveAssertions; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -23,6 +25,7 @@ import java.util.List; import static java.util.concurrent.TimeUnit.*; +import static org.assertj.core.api.Assertions.assertThat; import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.*; import static org.hibernate.reactive.testing.DBSelectionExtension.*; import static org.junit.jupiter.api.Assertions.*; @@ -84,6 +87,22 @@ public void populateDb(VertxTestContext context) { ); } + @Test + public void testFailureWithGetSingleResultOrNull(VertxTestContext context) { + test( context, ReactiveAssertions.assertThrown( HibernateException.class, getMutinySessionFactory() + .withTransaction( s -> s.createNativeQuery( INSERT_DRIVER_LICENCE_SQL ).getSingleResultOrNull() ) ) + .invoke( e -> assertThat( e ).hasMessageContainingAll( "HR000080:", INSERT_DRIVER_LICENCE_SQL ) ) + ); + } + + @Test + public void testFailureWithGetSingleResult(VertxTestContext context) { + test( context, ReactiveAssertions.assertThrown( HibernateException.class, getSessionFactory() + .withTransaction( s -> s.createNativeQuery( INSERT_DRIVER_LICENCE_SQL ).getSingleResult() ) ) + .thenAccept( e -> assertThat( e ).hasMessageContainingAll( "HR000080:", INSERT_DRIVER_LICENCE_SQL ) ) + ); + } + @Test public void testInsertStoredProcedureDriverLicence(VertxTestContext context) { test( context, openSession().thenCompose( session -> session diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CustomStoredProcedureSqlTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CustomStoredProcedureSqlTest.java index ac002b9db..512b23dda 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CustomStoredProcedureSqlTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CustomStoredProcedureSqlTest.java @@ -9,10 +9,12 @@ import java.util.Collection; import java.util.List; +import org.hibernate.HibernateException; import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLInsert; import org.hibernate.annotations.SQLUpdate; import org.hibernate.reactive.testing.DBSelectionExtension; +import org.hibernate.reactive.testing.ReactiveAssertions; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -29,6 +31,7 @@ import jakarta.persistence.Table; import static java.util.concurrent.TimeUnit.MINUTES; +import static org.assertj.core.api.Assertions.assertThat; import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL; import static org.hibernate.reactive.testing.DBSelectionExtension.runOnlyFor; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -102,6 +105,22 @@ public void populateDb(VertxTestContext context) { ); } + @Test + public void testFailureWithGetSingleResultOrNull(VertxTestContext context) { + test( context, ReactiveAssertions.assertThrown( HibernateException.class, getMutinySessionFactory() + .withTransaction( s -> s.createNativeQuery( INSERT_SP_SQL ).getSingleResultOrNull() ) ) + .invoke( e -> assertThat( e ).hasMessageContainingAll( "HR000080:", INSERT_SP_SQL ) ) + ); + } + + @Test + public void testFailureWithGetSingleResult(VertxTestContext context) { + test( context, ReactiveAssertions.assertThrown( HibernateException.class, getSessionFactory() + .withTransaction( s -> s.createNativeQuery( INSERT_SP_SQL ).getSingleResult() ) ) + .thenAccept( e -> assertThat( e ).hasMessageContainingAll( "HR000080:", INSERT_SP_SQL ) ) + ); + } + @Test public void testInsertStoredProcedure(VertxTestContext context) { test( context, openSession().thenCompose( session -> session