From 5736d6043b2ff1fa94b1a05b29930d1e7386395d Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Wed, 29 Nov 2023 11:55:31 +0100 Subject: [PATCH] [#1369] Test ConstraintViolationException SQLState code --- .../reactive/MutinyExceptionsTest.java | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/MutinyExceptionsTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/MutinyExceptionsTest.java index cb88cbd61c..c40e95f4ff 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/MutinyExceptionsTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/MutinyExceptionsTest.java @@ -5,10 +5,12 @@ */ package org.hibernate.reactive; +import java.sql.SQLException; import java.util.Collection; import java.util.List; import org.hibernate.exception.ConstraintViolationException; +import org.hibernate.reactive.containers.DatabaseConfiguration; import org.hibernate.reactive.mutiny.Mutiny; import org.junit.jupiter.api.Test; @@ -21,8 +23,10 @@ import jakarta.persistence.Table; import static java.util.concurrent.TimeUnit.MINUTES; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.SQLSERVER; +import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType; +import static org.hibernate.reactive.testing.ReactiveAssertions.assertThrown; @Timeout(value = 10, timeUnit = MINUTES) @@ -33,25 +37,34 @@ protected Collection> annotatedEntities() { return List.of( Person.class ); } - Class getExpectedException() { - return ConstraintViolationException.class; - } - @Test public void testDuplicateKeyException(VertxTestContext context) { - test( context, openMutinySession() + test( context, assertThrown( ConstraintViolationException.class, openMutinySession() .call( session -> session.persist( new Person( "testFLush1", "unique" ) ) ) .call( Mutiny.Session::flush ) .call( session -> session.persist( new Person( "testFlush2", "unique" ) ) ) - .call( Mutiny.Session::flush ) - .invoke( ignore -> fail( "Expected exception not thrown" ) ) - .onFailure().recoverWithItem( err -> { - assertEquals( getExpectedException(), err.getClass() ); - return null; - } ) + .call( Mutiny.Session::flush ) ) + .invoke( MutinyExceptionsTest::testExceptionMessage ) ); } + private static void testExceptionMessage(ConstraintViolationException exception) { + assertThat( exception.getConstraintName() ) + .as( "Failed constraint name should not be null" ) + .isNotNull(); + if ( dbType() == SQLSERVER ) { + // The SQL state code is always null in Sql Server (see https://github.com/eclipse-vertx/vertx-sql-client/issues/1385) + // We test the vendor code for now + SQLException sqlException = (SQLException) exception.getCause(); + assertThat( sqlException.getErrorCode() ).isEqualTo( 2601 ); + } + else { + assertThat( exception.getSQLState() ) + .as( "Constraint violation SQL state code should start with 23" ) + .matches( "23\\d{3}" ); + } + } + @Entity(name = "Person") @Table(name = "PersonForExceptionWithMutiny") public static class Person {