Skip to content

Commit

Permalink
[#1639] Test ConstraintViolationException SQLState code
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed Nov 29, 2023
1 parent 7ed6d24 commit d357392
Showing 1 changed file with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
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.testing.ReactiveAssertions.assertThrown;

@Timeout(value = 10, timeUnit = MINUTES)

Expand All @@ -33,25 +33,22 @@ protected Collection<Class<?>> 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() ).isNotNull();
assertThat( exception.getSQLState() ).matches( "23\\d{3}" );
}

@Entity(name = "Person")
@Table(name = "PersonForExceptionWithMutiny")
public static class Person {
Expand Down

0 comments on commit d357392

Please sign in to comment.