Search in sources :

Example 1 with HibernateConstraintViolationExceptionMapper

use of io.crnk.jpa.internal.HibernateConstraintViolationExceptionMapper in project crnk-framework by crnk-project.

the class JpaExceptionMapperTests method testConstraintException.

@Test
public void testConstraintException() {
    ConstraintViolationException exception = new ConstraintViolationException("message", null, "constraint");
    ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
    HibernateConstraintViolationExceptionMapper mapper = (HibernateConstraintViolationExceptionMapper) exceptionMapperRegistry.findMapperFor(ConstraintViolationException.class).get();
    ErrorResponse response = mapper.toErrorResponse(exception);
    ErrorData errorData = response.getErrors().iterator().next();
    Assert.assertEquals(Integer.toString(HttpStatus.UNPROCESSABLE_ENTITY_422), errorData.getStatus());
    Assert.assertEquals(exception.getConstraintName(), errorData.getCode());
    Assert.assertEquals(exception.getMessage(), errorData.getDetail());
    Assert.assertTrue(mapper.accepts(response));
    ConstraintViolationException deserializedException = mapper.fromErrorResponse(response);
    Assert.assertEquals(exception.getMessage(), deserializedException.getMessage());
    Assert.assertEquals(exception.getConstraintName(), deserializedException.getConstraintName());
}
Also used : HibernateConstraintViolationExceptionMapper(io.crnk.jpa.internal.HibernateConstraintViolationExceptionMapper) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Aggregations

ErrorData (io.crnk.core.engine.document.ErrorData)1 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)1 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)1 HibernateConstraintViolationExceptionMapper (io.crnk.jpa.internal.HibernateConstraintViolationExceptionMapper)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 Test (org.junit.Test)1