Search in sources :

Example 11 with ResteasyViolationException

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

Example 12 with ResteasyViolationException

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);
}
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) OperateOnDeployment(org.jboss.arquillian.container.test.api.OperateOnDeployment) Test(org.junit.Test)

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

Example 14 with ResteasyViolationException

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

Example 15 with ResteasyViolationException

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();
}
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)

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