use of org.jboss.resteasy.api.validation.ResteasyViolationException in project quarkus by quarkusio.
the class ResteasyViolationExceptionMapper method toResponse.
@Override
public Response toResponse(ValidationException exception) {
if (!(exception instanceof ResteasyViolationException)) {
// which will return HTTP status 500 and log the exception.
throw exception;
}
ResteasyViolationException restEasyException = (ResteasyViolationException) exception;
Exception e = restEasyException.getException();
if (e != null | restEasyException.getReturnValueViolations().size() != 0) {
// which will return HTTP status 500 and log the exception.
throw restEasyException;
}
return buildViolationReportResponse(restEasyException);
}
use of org.jboss.resteasy.api.validation.ResteasyViolationException in project resteasy by resteasy.
the class ValidationExceptionsTest method testCrazyMessage.
/**
* @tpTestDetails Resource with crazy message in constraint
* @tpSince RESTEasy 3.0.16
*/
@Test
@OperateOnDeployment(CRAZY_EXCEPTION)
public void testCrazyMessage() throws Exception {
Response response = client.target(generateURL("/", CRAZY_EXCEPTION)).request().get();
Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
String header = response.getStringHeaders().getFirst(Validation.VALIDATION_HEADER);
Assert.assertNotNull(ERROR_HEADER_MESSAGE, header);
Assert.assertTrue(ERROR_HEADER_VALIDATION_EXCEPTION_MESSAGE, Boolean.valueOf(header));
ResteasyViolationException resteasyViolationException = new ResteasyViolationExceptionImpl(response.readEntity(String.class));
List<ResteasyConstraintViolation> classViolations = resteasyViolationException.getClassViolations();
Assert.assertEquals(1, classViolations.size());
Assert.assertEquals(ValidationExceptionCrazyConstraint.DEFAULT_MESSAGE, classViolations.get(0).getMessage());
logger.info("entity: " + resteasyViolationException);
}
use of org.jboss.resteasy.api.validation.ResteasyViolationException in project resteasy by resteasy.
the class ResteasyViolationExceptionRepresentationTest method testReturnValues.
/**
* @tpTestDetails Check correct number of return value violations.
* @tpPassCrit Violation count should be correct according to resource definition.
* @tpSince RESTEasy 3.0.16
*/
@Test
@OperateOnDeployment(TEST_RETURN_VALUES)
public void testReturnValues() throws Exception {
// Valid native constraint
ViolationExceptionObject foo = new ViolationExceptionObject("a");
Response response = client.target(generateURL("/native", TEST_RETURN_VALUES)).request().post(Entity.entity(foo, "application/foo"));
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals("Server send wrong content", foo, response.readEntity(ViolationExceptionObject.class));
// Valid imposed constraint
foo = new ViolationExceptionObject("abcde");
response = client.target(generateURL("/imposed", TEST_RETURN_VALUES)).request().post(Entity.entity(foo, "application/foo"));
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
response.bufferEntity();
Assert.assertEquals("Server send wrong content", foo, response.readEntity(ViolationExceptionObject.class));
// Valid native and imposed constraints.
foo = new ViolationExceptionObject("abc");
response = client.target(generateURL("/nativeAndImposed", TEST_RETURN_VALUES)).request().post(Entity.entity(foo, "application/foo"));
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals("Server send wrong content", foo, response.readEntity(ViolationExceptionObject.class));
// Invalid native constraint
response = client.target(generateURL("/native", TEST_RETURN_VALUES)).request().post(Entity.entity(new ViolationExceptionObject("abcdef"), "application/foo"));
Assert.assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, response.getStatus());
String header = response.getStringHeaders().getFirst(Validation.VALIDATION_HEADER);
Assert.assertNotNull("Header of response should not be null", header);
Assert.assertTrue("Validation header is not correct", Boolean.valueOf(header));
Object entity = response.readEntity(String.class);
logger.info("Entity from response: " + entity);
ResteasyViolationException e = new ResteasyViolationExceptionImpl(String.class.cast(entity));
logger.info("Received exception: " + e.toString());
TestUtil.countViolations(e, 1, 0, 0, 0, 1);
ResteasyConstraintViolation cv = e.getReturnValueViolations().iterator().next();
Assert.assertEquals("Exception has wrong message", cv.getMessage(), "s must have length: 1 <= length <= 3");
Assert.assertEquals("Exception has wrong value", "Foo[abcdef]", cv.getValue());
// Invalid imposed constraint
response = client.target(generateURL("/imposed", TEST_RETURN_VALUES)).request().post(Entity.entity(new ViolationExceptionObject("abcdef"), "application/foo"));
Assert.assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, response.getStatus());
header = response.getStringHeaders().getFirst(Validation.VALIDATION_HEADER);
Assert.assertNotNull("Header of response should not be null", header);
Assert.assertTrue("Validation header is not correct", Boolean.valueOf(header));
entity = response.readEntity(String.class);
logger.info("Entity from response: " + entity);
e = new ResteasyViolationExceptionImpl(String.class.cast(entity));
TestUtil.countViolations(e, 1, 0, 0, 0, 1);
cv = e.getReturnValueViolations().iterator().next();
Assert.assertEquals("Exception has wrong message", cv.getMessage(), "s must have length: 3 <= length <= 5");
Assert.assertEquals("Exception has wrong value", "Foo[abcdef]", cv.getValue());
// Invalid native and imposed constraints
response = client.target(generateURL("/nativeAndImposed", TEST_RETURN_VALUES)).request().post(Entity.entity(new ViolationExceptionObject("abcdef"), "application/foo"));
Assert.assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, response.getStatus());
header = response.getStringHeaders().getFirst(Validation.VALIDATION_HEADER);
Assert.assertNotNull("Header of response should not be null", header);
Assert.assertTrue("Validation header is not correct", Boolean.valueOf(header));
entity = response.readEntity(String.class);
logger.info("Entity from response: " + entity);
e = new ResteasyViolationExceptionImpl(String.class.cast(entity));
TestUtil.countViolations(e, 2, 0, 0, 0, 2);
Iterator<ResteasyConstraintViolation> it = e.getReturnValueViolations().iterator();
ResteasyConstraintViolation cv1 = it.next();
ResteasyConstraintViolation cv2 = it.next();
if (!cv1.toString().contains("1")) {
ResteasyConstraintViolation temp = cv1;
cv1 = cv2;
cv2 = temp;
}
Assert.assertEquals("Exception has wrong message", cv1.getMessage(), "s must have length: 1 <= length <= 3");
Assert.assertEquals("Exception has wrong value", "Foo[abcdef]", cv1.getValue());
Assert.assertEquals("Exception has wrong message", cv2.getMessage(), "s must have length: 3 <= length <= 5");
Assert.assertEquals("Exception has wrong value", "Foo[abcdef]", cv2.getValue());
}
use of org.jboss.resteasy.api.validation.ResteasyViolationException in project resteasy by resteasy.
the class EmptyArrayValidationTest method testEmptyArray.
/**
* @tpTestDetails Verify validation of empty array doesn't throw ArrayIndexOutOfBoundsException.
* @tpSince RESTEasy 4.6.0
*/
@Test
public void testEmptyArray() throws Exception {
Client client = ClientBuilder.newClient();
EmptyArrayValidationFoo foo = new EmptyArrayValidationFoo(new Object[] {});
Response response = client.target(generateURL("/emptyarray")).request().post(Entity.entity(foo, MediaType.APPLICATION_JSON), Response.class);
Object header = response.getHeaderString(Validation.VALIDATION_HEADER);
Assert.assertTrue("Header has wrong format", header instanceof String);
Assert.assertTrue("Header has wrong format", Boolean.valueOf(String.class.cast(header)));
String answer = response.readEntity(String.class);
assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
ResteasyViolationException e = new ResteasyViolationExceptionImpl(String.class.cast(answer));
TestUtil.countViolations(e, 1, 0, 0, 1, 0);
}
use of org.jboss.resteasy.api.validation.ResteasyViolationException in project resteasy by resteasy.
the class ValidationSuppressPathTestBase method doTestInputViolations.
public void doTestInputViolations(String fieldPath, String propertyPath, String classPath, String... parameterPaths) throws Exception {
ValidationCoreFoo foo = new ValidationCoreFoo("p");
Response response = client.target(PortProviderUtil.generateURL("/all/a/z", "Validation-test")).request().post(Entity.entity(foo, "application/foo"));
Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
String header = response.getHeaderString(Validation.VALIDATION_HEADER);
Assert.assertNotNull("Validation header is missing", header);
Assert.assertTrue("Wrong validation header", Boolean.valueOf(header));
String entity = response.readEntity(String.class);
ResteasyViolationException e = new ResteasyViolationExceptionImpl(entity);
TestUtil.countViolations(e, 4, 2, 1, 1, 0);
Assert.assertNotNull(WRONG_ERROR_MSG, TestUtil.getViolationByPath(e.getPropertyViolations(), fieldPath));
Assert.assertNotNull(WRONG_ERROR_MSG, TestUtil.getViolationByPath(e.getPropertyViolations(), propertyPath));
ResteasyConstraintViolation violation = e.getClassViolations().iterator().next();
Assert.assertEquals(WRONG_ERROR_MSG, classPath, violation.getPath());
violation = e.getParameterViolations().iterator().next();
Assert.assertTrue(WRONG_ERROR_MSG + parameterPaths, Arrays.asList(parameterPaths).contains(violation.getPath()));
response.close();
}
Aggregations