Skip to content

Commit 5736d60

Browse files
committed
[#1369] Test ConstraintViolationException SQLState code
1 parent 7ed6d24 commit 5736d60

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/MutinyExceptionsTest.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66
package org.hibernate.reactive;
77

8+
import java.sql.SQLException;
89
import java.util.Collection;
910
import java.util.List;
1011

1112
import org.hibernate.exception.ConstraintViolationException;
13+
import org.hibernate.reactive.containers.DatabaseConfiguration;
1214
import org.hibernate.reactive.mutiny.Mutiny;
1315

1416
import org.junit.jupiter.api.Test;
@@ -21,8 +23,10 @@
2123
import jakarta.persistence.Table;
2224

2325
import static java.util.concurrent.TimeUnit.MINUTES;
24-
import static org.junit.jupiter.api.Assertions.assertEquals;
25-
import static org.junit.jupiter.api.Assertions.fail;
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.SQLSERVER;
28+
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
29+
import static org.hibernate.reactive.testing.ReactiveAssertions.assertThrown;
2630

2731
@Timeout(value = 10, timeUnit = MINUTES)
2832

@@ -33,25 +37,34 @@ protected Collection<Class<?>> annotatedEntities() {
3337
return List.of( Person.class );
3438
}
3539

36-
Class<?> getExpectedException() {
37-
return ConstraintViolationException.class;
38-
}
39-
4040
@Test
4141
public void testDuplicateKeyException(VertxTestContext context) {
42-
test( context, openMutinySession()
42+
test( context, assertThrown( ConstraintViolationException.class, openMutinySession()
4343
.call( session -> session.persist( new Person( "testFLush1", "unique" ) ) )
4444
.call( Mutiny.Session::flush )
4545
.call( session -> session.persist( new Person( "testFlush2", "unique" ) ) )
46-
.call( Mutiny.Session::flush )
47-
.invoke( ignore -> fail( "Expected exception not thrown" ) )
48-
.onFailure().recoverWithItem( err -> {
49-
assertEquals( getExpectedException(), err.getClass() );
50-
return null;
51-
} )
46+
.call( Mutiny.Session::flush ) )
47+
.invoke( MutinyExceptionsTest::testExceptionMessage )
5248
);
5349
}
5450

51+
private static void testExceptionMessage(ConstraintViolationException exception) {
52+
assertThat( exception.getConstraintName() )
53+
.as( "Failed constraint name should not be null" )
54+
.isNotNull();
55+
if ( dbType() == SQLSERVER ) {
56+
// The SQL state code is always null in Sql Server (see https://github.com/eclipse-vertx/vertx-sql-client/issues/1385)
57+
// We test the vendor code for now
58+
SQLException sqlException = (SQLException) exception.getCause();
59+
assertThat( sqlException.getErrorCode() ).isEqualTo( 2601 );
60+
}
61+
else {
62+
assertThat( exception.getSQLState() )
63+
.as( "Constraint violation SQL state code should start with 23" )
64+
.matches( "23\\d{3}" );
65+
}
66+
}
67+
5568
@Entity(name = "Person")
5669
@Table(name = "PersonForExceptionWithMutiny")
5770
public static class Person {

0 commit comments

Comments
 (0)