55 */
66package org .hibernate .reactive ;
77
8+ import java .sql .SQLException ;
89import java .util .Collection ;
910import java .util .List ;
1011
2122import jakarta .persistence .Table ;
2223
2324import static java .util .concurrent .TimeUnit .MINUTES ;
24- import static org .junit .jupiter .api .Assertions .assertEquals ;
25- import static org .junit .jupiter .api .Assertions .fail ;
25+ import static org .assertj .core .api .Assertions .assertThat ;
26+ import static org .hibernate .reactive .containers .DatabaseConfiguration .DBType .DB2 ;
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,41 @@ 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 ::assertSqlStateCode )
48+ .invoke ( MutinyExceptionsTest ::assertConstraintName )
5249 );
5350 }
5451
52+ private static void assertSqlStateCode (ConstraintViolationException exception ) {
53+ if ( dbType () == SQLSERVER ) {
54+ // The SQL state code is always null in Sql Server (see https://github.com/eclipse-vertx/vertx-sql-client/issues/1385)
55+ // We test the vendor code for now
56+ SQLException sqlException = (SQLException ) exception .getCause ();
57+ assertThat ( sqlException .getErrorCode () ).isEqualTo ( 2601 );
58+ }
59+ else {
60+ assertThat ( exception .getSQLState () )
61+ .as ( "Constraint violation SQL state code should start with 23" )
62+ .matches ( "23\\ d{3}" );
63+ }
64+ }
65+
66+ private static void assertConstraintName (ConstraintViolationException exception ) {
67+ // DB2 does not return the constraint name
68+ if ( dbType () != DB2 ) {
69+ assertThat ( exception .getConstraintName () )
70+ .as ( "Failed constraint name should not be null" )
71+ .isNotNull ();
72+ }
73+ }
74+
5575 @ Entity (name = "Person" )
5676 @ Table (name = "PersonForExceptionWithMutiny" )
5777 public static class Person {
0 commit comments