Search in sources :

Example 6 with ResteasyViolationException

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();
    }
}
Also used : Response(jakarta.ws.rs.core.Response) ResteasyViolationExceptionImpl(org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) WebTarget(jakarta.ws.rs.client.WebTarget) ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) MultipleWarSumConstraint(org.jboss.resteasy.test.validation.cdi.resource.MultipleWarSumConstraint) Test(org.junit.Test)

Example 7 with ResteasyViolationException

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();
}
Also used : Response(jakarta.ws.rs.core.Response) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ResteasyViolationExceptionImpl(org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) Test(org.junit.Test)

Example 8 with ResteasyViolationException

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);
}
Also used : GroupDefinitionException(jakarta.validation.GroupDefinitionException) ConstraintDeclarationException(jakarta.validation.ConstraintDeclarationException) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ConstraintDeclarationException(jakarta.validation.ConstraintDeclarationException) ConstraintDefinitionException(jakarta.validation.ConstraintDefinitionException) ValidationException(jakarta.validation.ValidationException) GroupDefinitionException(jakarta.validation.GroupDefinitionException) ConstraintDefinitionException(jakarta.validation.ConstraintDefinitionException)

Example 9 with ResteasyViolationException

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;
}
Also used : Set(java.util.Set) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ConstraintViolationException(jakarta.validation.ConstraintViolationException)

Example 10 with ResteasyViolationException

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();
}
Also used : Response(jakarta.ws.rs.core.Response) ResteasyViolationExceptionImpl(org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl) ValidationCoreFoo(org.jboss.resteasy.test.validation.resource.ValidationCoreFoo) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) Test(org.junit.Test)

Aggregations

ResteasyViolationException (org.jboss.resteasy.api.validation.ResteasyViolationException)18 ResteasyViolationExceptionImpl (org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl)14 Response (jakarta.ws.rs.core.Response)13 ResteasyConstraintViolation (org.jboss.resteasy.api.validation.ResteasyConstraintViolation)12 Test (org.junit.Test)12 ValidationCoreFoo (org.jboss.resteasy.test.validation.resource.ValidationCoreFoo)4 OperateOnDeployment (org.jboss.arquillian.container.test.api.OperateOnDeployment)3 WebTarget (jakarta.ws.rs.client.WebTarget)2 ValidationException (javax.validation.ValidationException)2 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)2 MultipleWarSumConstraint (org.jboss.resteasy.test.validation.cdi.resource.MultipleWarSumConstraint)2 ViolationExceptionObject (org.jboss.resteasy.test.validation.resource.ViolationExceptionObject)2 ConstraintDeclarationException (jakarta.validation.ConstraintDeclarationException)1 ConstraintDefinitionException (jakarta.validation.ConstraintDefinitionException)1 ConstraintViolationException (jakarta.validation.ConstraintViolationException)1 GroupDefinitionException (jakarta.validation.GroupDefinitionException)1 ValidationException (jakarta.validation.ValidationException)1 Client (jakarta.ws.rs.client.Client)1 Invocation (jakarta.ws.rs.client.Invocation)1 Iterator (java.util.Iterator)1