5
5
*/
6
6
package org .hibernate .reactive ;
7
7
8
+ import java .sql .SQLException ;
8
9
import java .util .Collection ;
9
10
import java .util .List ;
10
11
21
22
import jakarta .persistence .Table ;
22
23
23
24
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 ;
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 ;
26
30
27
31
@ Timeout (value = 10 , timeUnit = MINUTES )
28
32
@@ -33,25 +37,41 @@ protected Collection<Class<?>> annotatedEntities() {
33
37
return List .of ( Person .class );
34
38
}
35
39
36
- Class <?> getExpectedException () {
37
- return ConstraintViolationException .class ;
38
- }
39
-
40
40
@ Test
41
41
public void testDuplicateKeyException (VertxTestContext context ) {
42
- test ( context , openMutinySession ()
42
+ test ( context , assertThrown ( ConstraintViolationException . class , openMutinySession ()
43
43
.call ( session -> session .persist ( new Person ( "testFLush1" , "unique" ) ) )
44
44
.call ( Mutiny .Session ::flush )
45
45
.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 )
52
49
);
53
50
}
54
51
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
+
55
75
@ Entity (name = "Person" )
56
76
@ Table (name = "PersonForExceptionWithMutiny" )
57
77
public static class Person {
0 commit comments