Search in sources :

Example 1 with ViolationReport

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

the class NonCDIValidatorFactoryTest method testValidate.

// ////////////////////////////////////////////////////////////////////////////
@Test
public void testValidate() throws Exception {
    Invocation.Builder request = client.target("http://localhost:8081/test/validate/x").request();
    Response response = request.get();
    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);
    ViolationReport r = new ViolationReport(entity);
    TestUtil.countViolations(r, 0, 0, 1, 0);
    client.close();
}
Also used : Response(jakarta.ws.rs.core.Response) Invocation(jakarta.ws.rs.client.Invocation) ViolationReport(org.jboss.resteasy.api.validation.ViolationReport) AbstractBootstrapTest(org.jboss.resteasy.embedded.test.AbstractBootstrapTest) Test(org.junit.Test)

Example 2 with ViolationReport

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

the class ValidationComplexTest method testCrossParameterConstraint.

/**
 * @tpTestDetails Validation by cross-parameter constraints
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testCrossParameterConstraint() throws Exception {
    // Valid
    Response response = client.target(generateURL(BASIC_DEPLOYMENT, "/2/3", ValidationComplexSubResourceWithCrossParameterConstraint.class.getSimpleName())).request().post(Entity.text(new String()));
    Assert.assertEquals(HttpResponseCodes.SC_NO_CONTENT, response.getStatus());
    response.close();
    // Invalid
    response = client.target(generateURL(BASIC_DEPLOYMENT, "/5/7", ValidationComplexSubResourceWithCrossParameterConstraint.class.getSimpleName())).request().post(Entity.text(new String()));
    Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
    String header = response.getHeaderString(Validation.VALIDATION_HEADER);
    Assert.assertNotNull("Missing validation header", header);
    Assert.assertTrue("Wrong validation header", Boolean.valueOf(header));
    Object entity = response.readEntity(String.class);
    logger.info("entity: " + entity);
    ViolationReport r = new ViolationReport(String.class.cast(entity));
    TestUtil.countViolations(r, 0, 0, 1, 0);
    ResteasyConstraintViolation violation = r.getParameterViolations().iterator().next();
    logger.info("violation: " + violation);
    Assert.assertEquals(WRONG_ERROR_MSG, "Parameters must total <= 7", violation.getMessage());
    logger.info("violation value: " + violation.getValue());
    Assert.assertEquals(RESPONSE_ERROR_MSG, "[5, 7]", violation.getValue());
    response.close();
}
Also used : Response(jakarta.ws.rs.core.Response) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ViolationReport(org.jboss.resteasy.api.validation.ViolationReport) ValidationComplexOneString(org.jboss.resteasy.test.validation.resource.ValidationComplexOneString) ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) ValidationComplexSubResourceWithCrossParameterConstraint(org.jboss.resteasy.test.validation.resource.ValidationComplexSubResourceWithCrossParameterConstraint) Test(org.junit.Test)

Example 3 with ViolationReport

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

the class ValidationComplexTest method testLocators.

/**
 * @tpTestDetails Locators validation
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testLocators() throws Exception {
    // Sub-resource locator returns resource with valid field.
    Response response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/validField", ValidationComplexResourceWithSubLocators.class.getSimpleName())).request().post(Entity.text(new String()));
    Assert.assertEquals(HttpResponseCodes.SC_NO_CONTENT, response.getStatus());
    response.close();
    // Sub-resource locator returns resource with invalid field.
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/invalidField", ValidationComplexResourceWithSubLocators.class.getSimpleName())).request().post(Entity.text(new String()));
    Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
    String entity = response.readEntity(String.class);
    ViolationReport r = new ViolationReport(entity);
    TestUtil.countViolations(r, 1, 0, 0, 0);
    ResteasyConstraintViolation cv = r.getPropertyViolations().iterator().next();
    Assert.assertEquals(WRONG_ERROR_MSG, "size must be between 2 and 4", cv.getMessage());
    Assert.assertEquals(RESPONSE_ERROR_MSG, "abcde", cv.getValue());
    response.close();
    // Sub-resource locator returns resource with valid property.
    // Note: The resource ValidationComplexResourceWithProperty has a @PathParam annotation used by a setter,
    // but it is not used when ValidationComplexResourceWithProperty is used a sub-resource.  Hence "unused".
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/property/abc/unused", ValidationComplexResourceWithSubLocators.class.getSimpleName())).request().post(Entity.text(new String()));
    Assert.assertEquals(HttpResponseCodes.SC_NO_CONTENT, response.getStatus());
    response.close();
    // Sub-resource locator returns resource with invalid property.
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/property/abcdef/unused", ValidationComplexResourceWithSubLocators.class.getSimpleName())).request().post(Entity.text(new String()));
    Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
    entity = response.readEntity(String.class);
    r = new ViolationReport(entity);
    TestUtil.countViolations(r, 1, 0, 0, 0);
    cv = r.getPropertyViolations().iterator().next();
    Assert.assertEquals(WRONG_ERROR_MSG, "size must be between 2 and 4", cv.getMessage());
    Assert.assertEquals(RESPONSE_ERROR_MSG, "abcdef", cv.getValue());
    response.close();
    // Valid
    ValidationComplexFoo foo = new ValidationComplexFoo("pqrs");
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/everything/abc/wxyz/unused/unused", ValidationComplexResourceWithSubLocators.class.getSimpleName())).request().post(Entity.entity(foo, "application/foo"));
    Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
    Assert.assertEquals(RESPONSE_ERROR_MSG, foo, response.readEntity(ValidationComplexFoo.class));
    response.close();
    // Invalid: Should have 1 each of field, property, class, and parameter violations,and no return value violations.
    // Note: expect warning because ValidationComplexResourceWithAllFivePotentialViolations is being used a sub-resource and it has an injectible field:
    // WARN org.jboss.resteasy.core.ResourceLocator - Field s of subresource org.jboss.resteasy.test.validation.ValidationComplexTest$ValidationComplexResourceWithAllFivePotentialViolations will not be injected according to spec
    foo = new ValidationComplexFoo("p");
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/everything/a/z/unused/unused", ValidationComplexResourceWithSubLocators.class.getSimpleName())).request().post(Entity.entity(foo, "application/foo"));
    Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
    entity = response.readEntity(String.class);
    r = new ViolationReport(String.class.cast(entity));
    TestUtil.countViolations(r, 2, 1, 1, 0);
    ResteasyConstraintViolation violation = TestUtil.getViolationByMessage(r.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(r.getPropertyViolations(), "size must be between 3 and 5");
    Assert.assertNotNull(WRONG_ERROR_MSG, violation);
    Assert.assertEquals(WRONG_ERROR_MSG, "z", violation.getValue());
    cv = r.getClassViolations().iterator().next();
    Assert.assertEquals(WRONG_ERROR_MSG, "Concatenation of s and t must have length > 5", cv.getMessage());
    Assert.assertTrue(RESPONSE_ERROR_MSG, cv.getValue().startsWith("org.jboss.resteasy.test.validation.resource.ValidationComplexResourceWithAllFivePotentialViolations@"));
    response.close();
    // Sub-sub-resource locator returns resource with valid property.
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/locator/sublocator/abc", ValidationComplexResourceWithSubLocators.class.getSimpleName())).request().post(Entity.text(new String()));
    Assert.assertEquals(HttpResponseCodes.SC_NO_CONTENT, response.getStatus());
    response.close();
    // Sub-resource locator returns resource with invalid property.
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/locator/sublocator/abcdef", ValidationComplexResourceWithSubLocators.class.getSimpleName())).request().post(Entity.text(new String()));
    Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
    entity = response.readEntity(String.class);
    r = new ViolationReport(String.class.cast(entity));
    TestUtil.countViolations(r, 1, 0, 0, 0);
    cv = r.getPropertyViolations().iterator().next();
    Assert.assertEquals(WRONG_ERROR_MSG, "size must be between 2 and 3", cv.getMessage());
    Assert.assertEquals(RESPONSE_ERROR_MSG, "abcdef", cv.getValue());
    response.close();
}
Also used : Response(jakarta.ws.rs.core.Response) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ValidationComplexFoo(org.jboss.resteasy.test.validation.resource.ValidationComplexFoo) ValidationComplexResourceWithSubLocators(org.jboss.resteasy.test.validation.resource.ValidationComplexResourceWithSubLocators) ViolationReport(org.jboss.resteasy.api.validation.ViolationReport) ValidationComplexOneString(org.jboss.resteasy.test.validation.resource.ValidationComplexOneString) ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) Test(org.junit.Test)

Example 4 with ViolationReport

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

the class ValidationComplexTest method testProxy.

/**
 * @tpTestDetails Validation with client proxies
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testProxy() throws Exception {
    // Valid
    ValidationComplexProxyInterface client = this.client.target(generateURL(BASIC_DEPLOYMENT, "/", ValidationComplexProxyResource.class.getSimpleName())).proxy(ValidationComplexProxyInterface.class);
    client.s("abcd");
    String result = client.g();
    Assert.assertEquals("abcd", result);
    // Invalid
    client.s("abcde");
    try {
        client.g();
    } catch (InternalServerErrorException e) {
        Response response = e.getResponse();
        logger.info("status: " + response.getStatus());
        String header = response.getHeaderString(Validation.VALIDATION_HEADER);
        Assert.assertNotNull("Missing validation header", header);
        Assert.assertTrue("Wrong validation header", Boolean.valueOf(header));
        Object entity = response.readEntity(String.class);
        logger.info("entity: " + entity);
        ViolationReport r = new ViolationReport(String.class.cast(entity));
        TestUtil.countViolations(r, 0, 0, 0, 1);
        ResteasyConstraintViolation violation = r.getReturnValueViolations().iterator().next();
        logger.info("violation: " + violation);
        Assert.assertEquals(WRONG_ERROR_MSG, "size must be between 2 and 4", violation.getMessage());
        Assert.assertEquals(RESPONSE_ERROR_MSG, "abcde", violation.getValue());
        response.close();
    } catch (Exception e) {
        throw new RuntimeException("expected InternalServerErrorException", e);
    }
}
Also used : Response(jakarta.ws.rs.core.Response) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ValidationComplexProxyResource(org.jboss.resteasy.test.validation.resource.ValidationComplexProxyResource) ValidationComplexProxyInterface(org.jboss.resteasy.test.validation.resource.ValidationComplexProxyInterface) InternalServerErrorException(jakarta.ws.rs.InternalServerErrorException) ViolationReport(org.jboss.resteasy.api.validation.ViolationReport) ValidationComplexOneString(org.jboss.resteasy.test.validation.resource.ValidationComplexOneString) ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) InternalServerErrorException(jakarta.ws.rs.InternalServerErrorException) Test(org.junit.Test)

Example 5 with ViolationReport

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

the class ValidationComplexTest method testFieldAndProperty.

/**
 * @tpTestDetails Field and property validation test
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testFieldAndProperty() throws Exception {
    // Valid
    Response response = client.target(generateURL(BASIC_DEPLOYMENT, "/abc/wxyz", ValidationComplexResourceWithFieldAndProperty.class.getSimpleName())).request().post(Entity.text(new String()));
    Assert.assertEquals(HttpResponseCodes.SC_NO_CONTENT, response.getStatus());
    response.close();
    // Invalid
    response = client.target(generateURL(BASIC_DEPLOYMENT, "/a/uvwxyz", ValidationComplexResourceWithFieldAndProperty.class.getSimpleName())).request().post(Entity.text(new String()));
    Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
    String entity = response.readEntity(String.class);
    ViolationReport r = new ViolationReport(entity);
    TestUtil.countViolations(r, 2, 0, 0, 0);
    ResteasyConstraintViolation cv = TestUtil.getViolationByMessage(r.getPropertyViolations(), "size must be between 2 and 4");
    Assert.assertNotNull(WRONG_ERROR_MSG, cv);
    Assert.assertEquals(RESPONSE_ERROR_MSG, "a", cv.getValue());
    cv = TestUtil.getViolationByMessage(r.getPropertyViolations(), "size must be between 3 and 5");
    Assert.assertNotNull(WRONG_ERROR_MSG, cv);
    Assert.assertEquals(RESPONSE_ERROR_MSG, "uvwxyz", cv.getValue());
    response.close();
}
Also used : Response(jakarta.ws.rs.core.Response) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ValidationComplexResourceWithFieldAndProperty(org.jboss.resteasy.test.validation.resource.ValidationComplexResourceWithFieldAndProperty) ViolationReport(org.jboss.resteasy.api.validation.ViolationReport) ValidationComplexOneString(org.jboss.resteasy.test.validation.resource.ValidationComplexOneString) ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) Test(org.junit.Test)

Aggregations

ViolationReport (org.jboss.resteasy.api.validation.ViolationReport)46 Test (org.junit.Test)37 Response (jakarta.ws.rs.core.Response)36 ResteasyConstraintViolation (org.jboss.resteasy.api.validation.ResteasyConstraintViolation)29 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)16 ValidationComplexOneString (org.jboss.resteasy.test.validation.resource.ValidationComplexOneString)16 Invocation (jakarta.ws.rs.client.Invocation)10 WebTarget (jakarta.ws.rs.client.WebTarget)9 ClientResponse (org.jboss.resteasy.client.jaxrs.internal.ClientResponse)8 ClientBuilder (jakarta.ws.rs.client.ClientBuilder)5 ValidationComplexFoo (org.jboss.resteasy.test.validation.resource.ValidationComplexFoo)5 Builder (jakarta.ws.rs.client.Invocation.Builder)4 ResteasyViolationExceptionImpl (org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl)3 ValidationCoreFoo (org.jboss.resteasy.test.validation.resource.ValidationCoreFoo)3 Iterator (java.util.Iterator)2 ApplicationScopeMyDto (org.jboss.resteasy.test.validation.cdi.resource.ApplicationScopeMyDto)2 ValidationComplexResourceWithAllFivePotentialViolations (org.jboss.resteasy.test.validation.resource.ValidationComplexResourceWithAllFivePotentialViolations)2 ValidationComplexResourceWithClassConstraint (org.jboss.resteasy.test.validation.resource.ValidationComplexResourceWithClassConstraint)2 ValidationComplexSubResourceWithCrossParameterConstraint (org.jboss.resteasy.test.validation.resource.ValidationComplexSubResourceWithCrossParameterConstraint)2 ValidationFoo (org.jboss.resteasy.test.validation.resource.ValidationFoo)2