use of org.jboss.resteasy.api.validation.ResteasyViolationException in project resteasy by resteasy.
the class MultipleWarTest method testReturnValueInvalid.
/**
* @tpTestDetails Check validation of invalid return value
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testReturnValueInvalid() throws Exception {
WebTarget request1 = client.target(generateURL("1/test/5/7/9"));
WebTarget request2 = client.target(generateURL("2/test/5/7/9"));
Response response;
for (int i = 1; i < 2; i++) {
response = request1.request().get();
String answer = response.readEntity(String.class);
assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, response.getStatus());
ResteasyViolationException e = new ResteasyViolationExceptionImpl(String.class.cast(answer));
TestUtil.countViolations(e, 1, 0, 0, 0, 1);
ResteasyConstraintViolation cv = e.getReturnValueViolations().iterator().next();
Assert.assertTrue(WRONG_ERROR_MSG, cv.getMessage().equals("must be less than or equal to 0"));
response.close();
response = request2.request().get();
answer = response.readEntity(String.class);
assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, response.getStatus());
e = new ResteasyViolationExceptionImpl(String.class.cast(answer));
TestUtil.countViolations(e, 1, 0, 0, 0, 1);
cv = e.getReturnValueViolations().iterator().next();
Assert.assertTrue(WRONG_ERROR_MSG, cv.getMessage().equals("must be less than or equal to 0"));
response.close();
}
}
use of org.jboss.resteasy.api.validation.ResteasyViolationException in project resteasy by resteasy.
the class ValidationSessionBeanTest method testInvalidParam.
@Test
public void testInvalidParam() throws Exception {
ResteasyClient client = (ResteasyClient) ClientBuilder.newClient();
Response response = client.target(generateURL("/test/resource")).queryParam("param", "abc").request().get();
String answer = response.readEntity(String.class);
assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
ResteasyViolationException e = new ResteasyViolationExceptionImpl(String.class.cast(answer));
int c = e.getViolations().size();
Assert.assertTrue(c == 1 || c == 2);
TestUtil.countViolations(e, c, 0, 0, c, 0);
ResteasyConstraintViolation cv = e.getParameterViolations().iterator().next();
Assert.assertTrue("Expected validation error is not in response", cv.getMessage().startsWith("size must be between 4 and"));
Assert.assertTrue("Expected validation error is not in response", answer.contains("size must be between 4 and"));
response.close();
client.close();
}
use of org.jboss.resteasy.api.validation.ResteasyViolationException in project resteasy by resteasy.
the class ResteasyViolationExceptionMapper method toResponse.
public Response toResponse(ValidationException exception) {
if (exception instanceof ConstraintDefinitionException) {
return buildResponse(unwrapException(exception), MediaType.TEXT_PLAIN, Status.INTERNAL_SERVER_ERROR);
}
if (exception instanceof ConstraintDeclarationException) {
return buildResponse(unwrapException(exception), MediaType.TEXT_PLAIN, Status.INTERNAL_SERVER_ERROR);
}
if (exception instanceof GroupDefinitionException) {
return buildResponse(unwrapException(exception), MediaType.TEXT_PLAIN, Status.INTERNAL_SERVER_ERROR);
}
if (exception instanceof ResteasyViolationException) {
ResteasyViolationException resteasyViolationException = ResteasyViolationException.class.cast(exception);
Exception e = resteasyViolationException.getException();
if (e != null) {
return buildResponse(unwrapException(e), MediaType.TEXT_PLAIN, Status.INTERNAL_SERVER_ERROR);
} else if (resteasyViolationException.getReturnValueViolations().size() == 0) {
return buildViolationReportResponse(resteasyViolationException, Status.BAD_REQUEST);
} else {
return buildViolationReportResponse(resteasyViolationException, Status.INTERNAL_SERVER_ERROR);
}
}
return buildResponse(unwrapException(exception), MediaType.TEXT_PLAIN, Status.INTERNAL_SERVER_ERROR);
}
use of org.jboss.resteasy.api.validation.ResteasyViolationException in project resteasy by resteasy.
the class GeneralValidatorImpl method checkForConstraintViolations.
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void checkForConstraintViolations(HttpRequest request, Exception e) {
Throwable t = e;
while (t != null && !(t instanceof ConstraintViolationException)) {
t = t.getCause();
}
if (t instanceof ResteasyViolationException) {
throw ResteasyViolationException.class.cast(t);
}
if (t instanceof ConstraintViolationException) {
SimpleViolationsContainer violationsContainer = getViolationsContainer(request, null);
ConstraintViolationException cve = ConstraintViolationException.class.cast(t);
Set cvs = cve.getConstraintViolations();
violationsContainer.addViolations(cvs);
if (violationsContainer.size() > 0) {
throw new ResteasyViolationExceptionImpl(violationsContainer, request.getHttpHeaders().getAcceptableMediaTypes());
}
}
return;
}
use of org.jboss.resteasy.api.validation.ResteasyViolationException in project resteasy by resteasy.
the class ExecutableValidationDisabledTest method testViolationsBeforeReturnValue.
/**
* @tpTestDetails Test disabled validation before return value evaluation.
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testViolationsBeforeReturnValue() throws Exception {
// Valid
ValidationCoreFoo foo = new ValidationCoreFoo("pqrs");
Response response = client.target(generateURL("/all/abc/wxyz")).request().post(Entity.entity(foo, "application/foo"));
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals(RESPONSE_ERROR_MSG, foo, response.readEntity(ValidationCoreFoo.class));
response.close();
// Invalid: Should have 1 each of field, property, class, and parameter violations,
// and no return value violations.
// BUT EXECUTABLE VALIDATION IS DISABLE. There will be no parameter violation.
response = client.target(generateURL("/all/a/z")).request().post(Entity.entity(foo, "application/foo"));
Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
String header = response.getHeaderString(Validation.VALIDATION_HEADER);
Assert.assertNotNull("Missing validation header", header);
Assert.assertTrue("Wrong value of validation header", Boolean.valueOf(header));
String entity = response.readEntity(String.class);
ResteasyViolationException e = new ResteasyViolationExceptionImpl(entity);
TestUtil.countViolations(e, 3, 2, 1, 0, 0);
ResteasyConstraintViolation violation = TestUtil.getViolationByMessage(e.getPropertyViolations(), "size must be between 2 and 4");
Assert.assertNotNull(WRONG_ERROR_MSG, violation);
Assert.assertEquals(WRONG_ERROR_MSG, "a", violation.getValue());
violation = TestUtil.getViolationByMessage(e.getPropertyViolations(), "size must be between 3 and 5");
Assert.assertNotNull(WRONG_ERROR_MSG, violation);
Assert.assertEquals(WRONG_ERROR_MSG, "z", violation.getValue());
violation = e.getClassViolations().iterator().next();
Assert.assertEquals(WRONG_ERROR_MSG, "Concatenation of s and t must have length > 5", violation.getMessage());
Assert.assertTrue(WRONG_ERROR_MSG, violation.getValue().startsWith("org.jboss.resteasy.test.validation.resource.ValidationCoreResourceWithAllViolationTypes@"));
response.close();
}
Aggregations