Search in sources :

Example 1 with ResteasyViolationExceptionImpl

use of org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl in project resteasy by resteasy.

the class ValidationCoreTest method testReturnValues.

/**
 * @tpTestDetails Test native, imposed and both validation of return values. Also test negative scenarios.
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testReturnValues() throws Exception {
    ValidationCoreFoo foo = new ValidationCoreFoo("a");
    Assert.assertNotNull(client);
    Response response = client.target(generateURL("/return/native")).request().post(Entity.entity(foo, "application/foo"));
    // Valid native constraint
    Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
    Assert.assertEquals(RESPONSE_ERROR_MSG, foo, response.readEntity(ValidationCoreFoo.class));
    response.close();
    // Valid imposed constraint
    foo = new ValidationCoreFoo("abcde");
    response = client.target(generateURL("/return/imposed")).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();
    // Valid native and imposed constraints.
    foo = new ValidationCoreFoo("abc");
    response = client.target(generateURL("/return/nativeAndImposed")).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 native constraint
        response = client.target(generateURL("/return/native")).request().post(Entity.entity(new ValidationCoreFoo("abcdef"), "application/foo"));
        String entity = response.readEntity(String.class);
        Assert.assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, response.getStatus());
        String header = response.getHeaderString(Validation.VALIDATION_HEADER);
        Assert.assertNotNull("Validation header is missing", header);
        Assert.assertTrue("Wrong validation header", Boolean.valueOf(header));
        ResteasyViolationException e = new ResteasyViolationExceptionImpl(entity);
        ResteasyConstraintViolation violation = e.getReturnValueViolations().iterator().next();
        Assert.assertTrue(WRONG_ERROR_MSG, violation.getMessage().equals("s must have length: 1 <= length <= 3"));
        Assert.assertEquals(WRONG_ERROR_MSG, "ValidationCoreFoo[abcdef]", violation.getValue());
        response.close();
    }
    {
        // Invalid imposed constraint
        response = client.target(generateURL("/return/imposed")).request().post(Entity.entity(new ValidationCoreFoo("abcdef"), "application/foo"));
        Assert.assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, 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);
        ViolationReport r = new ViolationReport(entity);
        TestUtil.countViolations(r, 0, 0, 0, 1);
        ResteasyConstraintViolation violation = r.getReturnValueViolations().iterator().next();
        Assert.assertTrue(WRONG_ERROR_MSG, violation.getMessage().equals("s must have length: 3 <= length <= 5"));
        Assert.assertEquals(WRONG_ERROR_MSG, "ValidationCoreFoo[abcdef]", violation.getValue());
        response.close();
    }
    {
        // Invalid native and imposed constraints
        response = client.target(generateURL("/return/nativeAndImposed")).request().post(Entity.entity(new ValidationCoreFoo("abcdef"), "application/foo"));
        Assert.assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, 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);
        ViolationReport r = new ViolationReport(entity);
        TestUtil.countViolations(r, 0, 0, 0, 2);
        Iterator<ResteasyConstraintViolation> it = r.getReturnValueViolations().iterator();
        ResteasyConstraintViolation cv1 = it.next();
        ResteasyConstraintViolation cv2 = it.next();
        if (cv1.getMessage().indexOf('1') < 0) {
            ResteasyConstraintViolation temp = cv1;
            cv1 = cv2;
            cv2 = temp;
        }
        Assert.assertTrue(WRONG_ERROR_MSG, cv1.getMessage().equals("s must have length: 1 <= length <= 3"));
        Assert.assertEquals(WRONG_ERROR_MSG, "ValidationCoreFoo[abcdef]", cv1.getValue());
        Assert.assertTrue(WRONG_ERROR_MSG, cv2.getMessage().equals("s must have length: 3 <= length <= 5"));
        Assert.assertEquals(WRONG_ERROR_MSG, "ValidationCoreFoo[abcdef]", cv2.getValue());
    }
}
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) Iterator(java.util.Iterator) ViolationReport(org.jboss.resteasy.api.validation.ViolationReport) ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) Test(org.junit.Test)

Example 2 with ResteasyViolationExceptionImpl

use of org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl in project resteasy by resteasy.

the class ValidationSuppressPathTestBase method doTestReturnValueViolations.

public void doTestReturnValueViolations(String returnValuePath) throws Exception {
    Response response = client.target(PortProviderUtil.generateURL("/return/native", "Validation-test")).request().post(Entity.entity(new ValidationCoreFoo("abcdef"), "application/foo"));
    Assert.assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, 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);
    ResteasyConstraintViolation violation = e.getReturnValueViolations().iterator().next();
    Assert.assertEquals(WRONG_ERROR_MSG, returnValuePath, 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)

Example 3 with ResteasyViolationExceptionImpl

use of org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl in project resteasy by resteasy.

the class GetterReturnValueValidatedTest method testReturnValues.

/**
 * @tpTestDetails Validation of getter return value is expected because of specific validation.xml file.
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testReturnValues() throws Exception {
    Response response = client.target(generateURL("/get")).request().get();
    response.close();
    response = client.target(generateURL("/set")).request().get();
    Assert.assertEquals(HttpResponseCodes.SC_NO_CONTENT, response.getStatus());
    response.close();
    // Valid native constraint
    response = client.target(generateURL("/get")).request().get();
    Assert.assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, 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, 1, 0, 0, 0, 1);
    response.close();
}
Also used : Response(jakarta.ws.rs.core.Response) ResteasyViolationExceptionImpl(org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) Test(org.junit.Test)

Example 4 with ResteasyViolationExceptionImpl

use of org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl in project resteasy by resteasy.

the class PathSuppressionTest method doTestReturnValueViolations.

public void doTestReturnValueViolations(String suppress, String returnValuePath) throws Exception {
    Response response = client.target(PortProviderUtil.generateURL("/all/aa/bbb/cccc", "RESTEASY-945-" + suppress)).request().get();
    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_INTERNAL_SERVER_ERROR, response.getStatus());
    ViolationReport report = new ViolationReport(new ResteasyViolationExceptionImpl(String.class.cast(answer)));
    TestUtil.countViolations(report, 0, 0, 0, 1);
    ResteasyConstraintViolation violation = report.getReturnValueViolations().iterator().next();
    Assert.assertEquals("Expected validation error is not in response", returnValuePath, violation.getPath());
    response.close();
}
Also used : Response(jakarta.ws.rs.core.Response) ResteasyViolationExceptionImpl(org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl) ViolationReport(org.jboss.resteasy.api.validation.ViolationReport) ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation)

Example 5 with ResteasyViolationExceptionImpl

use of org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl in project resteasy by resteasy.

the class ResteasyViolationExceptionRepresentationTest method testViolationsBeforeReturnValue.

/**
 * @tpTestDetails Check correct number of violations before return in resource.
 * @tpPassCrit Violation count should be correct according to resource definition.
 * @tpSince RESTEasy 3.0.16
 */
@Test
@OperateOnDeployment(TEST_VIOLATIONS_BEFORE_RETURN_VALUE)
public void testViolationsBeforeReturnValue() throws Exception {
    // Valid
    ViolationExceptionObject foo = new ViolationExceptionObject("pqrs");
    Response response = client.target(generateURL("/abc/wxyz/unused/unused", TEST_VIOLATIONS_BEFORE_RETURN_VALUE)).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: Should have 1 each of field, property, class, and parameter violations,
    // and no return value violations.
    foo = new ViolationExceptionObject("p");
    response = client.target(generateURL("/a/z/unused/unused", TEST_VIOLATIONS_BEFORE_RETURN_VALUE)).request().post(Entity.entity(foo, "application/foo"));
    logger.info("response: " + response);
    Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
    Object entity = response.readEntity(String.class);
    logger.info("entity: " + entity);
    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));
    ResteasyViolationException e = new ResteasyViolationExceptionImpl(String.class.cast(entity));
    logger.info("exception: " + e.toString());
    TestUtil.countViolations(e, 4, 2, 1, 1, 0);
    ResteasyConstraintViolation violation = TestUtil.getViolationByMessage(e.getPropertyViolations(), "size must be between 2 and 4");
    Assert.assertNotNull("Exception has wrong message", violation);
    Assert.assertEquals("Exception has wrong value", "a", violation.getValue());
    violation = TestUtil.getViolationByMessage(e.getPropertyViolations(), "size must be between 3 and 5");
    Assert.assertNotNull("Exception has wrong message", violation);
    Assert.assertEquals("Exception has wrong value", "z", violation.getValue());
    ResteasyConstraintViolation cv = e.getClassViolations().iterator().next();
    Assert.assertEquals("Exception has wrong message", "Concatenation of s and t must have length > 5", cv.getMessage());
    logger.info("value: " + cv.getValue());
    Assert.assertTrue("Exception has wrong value", cv.getValue().startsWith("org.jboss.resteasy.test.validation.resource.ViolationExceptionResourceWithFiveViolations@"));
    cv = e.getParameterViolations().iterator().next();
    Assert.assertEquals("Exception has wrong message", "s must have length: 3 <= length <= 5", cv.getMessage());
    Assert.assertEquals("Exception has wrong value", "Foo[p]", cv.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)

Aggregations

ResteasyViolationExceptionImpl (org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl)16 Response (jakarta.ws.rs.core.Response)15 ResteasyConstraintViolation (org.jboss.resteasy.api.validation.ResteasyConstraintViolation)14 ResteasyViolationException (org.jboss.resteasy.api.validation.ResteasyViolationException)14 Test (org.junit.Test)12 ValidationCoreFoo (org.jboss.resteasy.test.validation.resource.ValidationCoreFoo)4 OperateOnDeployment (org.jboss.arquillian.container.test.api.OperateOnDeployment)3 ViolationReport (org.jboss.resteasy.api.validation.ViolationReport)3 WebTarget (jakarta.ws.rs.client.WebTarget)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 Client (jakarta.ws.rs.client.Client)1 Invocation (jakarta.ws.rs.client.Invocation)1 Iterator (java.util.Iterator)1 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)1 ClientResponse (org.jboss.resteasy.client.jaxrs.internal.ClientResponse)1 EmptyArrayValidationFoo (org.jboss.resteasy.test.validation.resource.EmptyArrayValidationFoo)1