PersistenceExceptionMapper implements ExceptionMapper<PersistenceException> { public Response toResponse(final PersistenceException exception) { Throwable cause = exception.getCause(); if (cause instanceof ConstraintViolationException) { return ErrorResponseBuilder .from(Status.CONFLICT) .build(); } return ErrorResponseBuilder .from(exception) .build(); }  Response toResponse(final PersistenceException exception);  }
@Test public void testToResponse() { ConstraintViolationException cause = new ConstraintViolationException( "Test Exception", new SQLException(), "constraintName" ); PersistenceException e = new PersistenceException("Test", cause); PersistenceExceptionMapper mapper = new PersistenceExceptionMapper(); Response r = mapper.toResponse(e); ErrorResponse er = (ErrorResponse) r.getEntity(); assertEquals(Status.CONFLICT.getStatusCode(), r.getStatus()); assertEquals(Status.CONFLICT, er.getHttpStatus()); assertEquals("Conflict", er.getErrorDescription()); }
@Test public void testToResponseNoCause() { PersistenceExceptionMapper mapper = new PersistenceExceptionMapper(); PersistenceException e = new PersistenceException("test"); Response r = mapper.toResponse(e); ErrorResponse er = (ErrorResponse) r.getEntity(); assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus()); assertEquals(Status.INTERNAL_SERVER_ERROR, er.getHttpStatus()); assertEquals("Internal Server Error", er.getErrorDescription()); }