Search in sources :

Example 1 with ResteasyConstraintViolation

use of org.jboss.resteasy.api.validation.ResteasyConstraintViolation 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 2 with ResteasyConstraintViolation

use of org.jboss.resteasy.api.validation.ResteasyConstraintViolation 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 3 with ResteasyConstraintViolation

use of org.jboss.resteasy.api.validation.ResteasyConstraintViolation 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 4 with ResteasyConstraintViolation

use of org.jboss.resteasy.api.validation.ResteasyConstraintViolation 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)

Example 5 with ResteasyConstraintViolation

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

the class ValidationComplexTest method testReturnValues.

/**
 * @tpTestDetails Test return value validation
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testReturnValues() throws Exception {
    // Valid native constraint
    ValidationComplexFoo foo = new ValidationComplexFoo("a");
    Response response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/native", ValidationComplexResourceWithReturnValues.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();
    // Valid imposed constraint
    foo = new ValidationComplexFoo("abcde");
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/imposed", ValidationComplexResourceWithReturnValues.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();
    // Valid native and imposed constraints.
    foo = new ValidationComplexFoo("abc");
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/nativeAndImposed", ValidationComplexResourceWithReturnValues.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 native constraint
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/native", ValidationComplexResourceWithReturnValues.class.getSimpleName())).request().post(Entity.entity(new ValidationComplexFoo("abcdef"), "application/foo"));
    Assert.assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, response.getStatus());
    Object entity = response.readEntity(String.class);
    ViolationReport r = new ViolationReport(String.class.cast(entity));
    TestUtil.countViolations(r, 0, 0, 0, 1);
    ResteasyConstraintViolation cv = r.getReturnValueViolations().iterator().next();
    Assert.assertTrue(WRONG_ERROR_MSG, cv.getMessage().equals("s must have length: 1 <= length <= 3"));
    Assert.assertEquals(RESPONSE_ERROR_MSG, "ValidationComplexFoo[abcdef]", cv.getValue());
    response.close();
    // Invalid imposed constraint
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/imposed", ValidationComplexResourceWithReturnValues.class.getSimpleName())).request().post(Entity.entity(new ValidationComplexFoo("abcdef"), "application/foo"));
    Assert.assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, response.getStatus());
    entity = response.readEntity(String.class);
    r = new ViolationReport(String.class.cast(entity));
    TestUtil.countViolations(r, 0, 0, 0, 1);
    cv = r.getReturnValueViolations().iterator().next();
    Assert.assertTrue(WRONG_ERROR_MSG, cv.getMessage().equals("s must have length: 3 <= length <= 5"));
    Assert.assertEquals(RESPONSE_ERROR_MSG, "ValidationComplexFoo[abcdef]", cv.getValue());
    response.close();
    // Invalid native and imposed constraints
    response = client.target(generateURL(CUSTOM_OBJECT_DEPLOYMENT, "/nativeAndImposed", ValidationComplexResourceWithReturnValues.class.getSimpleName())).request().post(Entity.entity(new ValidationComplexFoo("abcdef"), "application/foo"));
    Assert.assertEquals(HttpResponseCodes.SC_INTERNAL_SERVER_ERROR, response.getStatus());
    entity = response.readEntity(String.class);
    r = new ViolationReport(String.class.cast(entity));
    TestUtil.countViolations(r, 0, 0, 0, 2);
    Iterator<ResteasyConstraintViolation> it = r.getReturnValueViolations().iterator();
    ResteasyConstraintViolation cv1 = it.next();
    ResteasyConstraintViolation cv2 = it.next();
    if (cv1.toString().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(RESPONSE_ERROR_MSG, "ValidationComplexFoo[abcdef]", cv1.getValue());
    Assert.assertTrue(WRONG_ERROR_MSG, cv2.getMessage().equals("s must have length: 3 <= length <= 5"));
    Assert.assertEquals(RESPONSE_ERROR_MSG, "ValidationComplexFoo[abcdef]", cv2.getValue());
    response.close();
}
Also used : Response(jakarta.ws.rs.core.Response) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ValidationComplexFoo(org.jboss.resteasy.test.validation.resource.ValidationComplexFoo) ValidationComplexResourceWithReturnValues(org.jboss.resteasy.test.validation.resource.ValidationComplexResourceWithReturnValues) 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

ResteasyConstraintViolation (org.jboss.resteasy.api.validation.ResteasyConstraintViolation)42 Test (org.junit.Test)35 Response (jakarta.ws.rs.core.Response)33 ViolationReport (org.jboss.resteasy.api.validation.ViolationReport)29 ValidationComplexOneString (org.jboss.resteasy.test.validation.resource.ValidationComplexOneString)17 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)16 ResteasyViolationExceptionImpl (org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl)14 ResteasyViolationException (org.jboss.resteasy.api.validation.ResteasyViolationException)12 Invocation (jakarta.ws.rs.client.Invocation)7 ClientResponse (org.jboss.resteasy.client.jaxrs.internal.ClientResponse)7 ValidationCoreFoo (org.jboss.resteasy.test.validation.resource.ValidationCoreFoo)6 ValidationComplexFoo (org.jboss.resteasy.test.validation.resource.ValidationComplexFoo)5 WebTarget (jakarta.ws.rs.client.WebTarget)4 OperateOnDeployment (org.jboss.arquillian.container.test.api.OperateOnDeployment)3 Iterator (java.util.Iterator)2 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)2 MultipleWarSumConstraint (org.jboss.resteasy.test.validation.cdi.resource.MultipleWarSumConstraint)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