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());
}
Aggregations