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
11
12
import org .hibernate .exception .ConstraintViolationException ;
13
+ import org .hibernate .reactive .containers .DatabaseConfiguration ;
12
14
import org .hibernate .reactive .mutiny .Mutiny ;
13
15
14
16
import org .junit .jupiter .api .Test ;
21
23
import jakarta .persistence .Table ;
22
24
23
25
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 ;
26
30
27
31
@ Timeout (value = 10 , timeUnit = MINUTES )
28
32
@@ -33,25 +37,34 @@ 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 ::testExceptionMessage )
52
48
);
53
49
}
54
50
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
+
55
68
@ Entity (name = "Person" )
56
69
@ Table (name = "PersonForExceptionWithMutiny" )
57
70
public static class Person {
0 commit comments