Search in sources :

Example 16 with ResteasyViolationException

use of org.jboss.resteasy.api.validation.ResteasyViolationException in project resteasy by resteasy.

the class MultipleWarTest method testInputsInvalid.

/**
 * @tpTestDetails Check validation of invalid inputs
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testInputsInvalid() throws Exception {
    WebTarget request1 = client.target(generateURL("1/test/0/0/0"));
    WebTarget request2 = client.target(generateURL("2/test/0/0/0"));
    Response response;
    for (int i = 1; i < 2; i++) {
        response = request1.request().get();
        String answer = response.readEntity(String.class);
        assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
        ResteasyViolationException e = new ResteasyViolationExceptionImpl(String.class.cast(answer));
        TestUtil.countViolations(e, 4, 2, 1, 1, 0);
        ResteasyConstraintViolation cv = TestUtil.getViolationByMessage(e.getPropertyViolations(), "must be greater than or equal to 3");
        Assert.assertNotNull(WRONG_ERROR_MSG, cv);
        cv = TestUtil.getViolationByMessage(e.getPropertyViolations(), "must be greater than or equal to 5");
        Assert.assertNotNull(WRONG_ERROR_MSG, cv);
        cv = e.getClassViolations().iterator().next();
        Assert.assertTrue(WRONG_ERROR_MSG, cv.getMessage().indexOf("org.jboss.resteasy.resteasy1058.MultipleWarSumConstraint") > 0);
        cv = e.getParameterViolations().iterator().next();
        Assert.assertTrue(WRONG_ERROR_MSG, cv.getMessage().equals("must be greater than or equal to 7"));
        response.close();
        response = request2.request().get();
        answer = response.readEntity(String.class);
        assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
        e = new ResteasyViolationExceptionImpl(String.class.cast(answer));
        TestUtil.countViolations(e, 4, 2, 1, 1, 0);
        cv = TestUtil.getViolationByMessage(e.getPropertyViolations(), "must be greater than or equal to 3");
        Assert.assertNotNull(WRONG_ERROR_MSG, cv);
        cv = TestUtil.getViolationByMessage(e.getPropertyViolations(), "must be greater than or equal to 5");
        Assert.assertNotNull(WRONG_ERROR_MSG, cv);
        cv = e.getClassViolations().iterator().next();
        Assert.assertTrue(WRONG_ERROR_MSG, cv.getMessage().indexOf("org.jboss.resteasy.resteasy1058.MultipleWarSumConstraint") > 0);
        cv = e.getParameterViolations().iterator().next();
        Assert.assertTrue(WRONG_ERROR_MSG, cv.getMessage().equals("must be greater than or equal to 7"));
        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 17 with ResteasyViolationException

use of org.jboss.resteasy.api.validation.ResteasyViolationException in project resteasy by resteasy.

the class MissingCDITest method testMissingCDIInvalid.

@Test
public void testMissingCDIInvalid() throws Exception {
    Response response = ResteasyClientBuilder.newClient().target(baseUri.toString() + "test/0").request().get();
    String entity = response.readEntity(String.class);
    assertEquals(400, response.getStatus());
    ResteasyViolationException e = new ResteasyViolationExceptionImpl(entity);
    countViolations(e, 1, 0, 0, 1, 0);
    ResteasyConstraintViolation cv = e.getParameterViolations().iterator().next();
    Assert.assertTrue(cv.getMessage().equals("must be greater than or equal to 7"));
}
Also used : Response(jakarta.ws.rs.core.Response) 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 18 with ResteasyViolationException

use of org.jboss.resteasy.api.validation.ResteasyViolationException in project nessie by projectnessie.

the class ResteasyExceptionMapper method toResponse.

@Override
public Response toResponse(ResteasyViolationException exception) {
    Exception e = exception.getException();
    if (e == null) {
        boolean returnValueViolation = !exception.getReturnValueViolations().isEmpty();
        ErrorCode errorCode = returnValueViolation ? ErrorCode.UNKNOWN : ErrorCode.BAD_REQUEST;
        return buildExceptionResponse(errorCode, exception.getMessage(), exception, // no need to send the stack trace for a validation-error
        false, b -> b.header(Validation.VALIDATION_HEADER, "true"));
    }
    return buildExceptionResponse(ErrorCode.UNKNOWN, unwrapException(exception), exception);
}
Also used : ErrorCode(org.projectnessie.error.ErrorCode) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ValidationException(javax.validation.ValidationException)

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