Search in sources :

Example 1 with ValidationCoreFoo

use of org.jboss.resteasy.test.validation.resource.ValidationCoreFoo 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 ValidationCoreFoo

use of org.jboss.resteasy.test.validation.resource.ValidationCoreFoo in project resteasy by resteasy.

the class ValidationCoreTest method testViolationsBeforeReturnValue.

/**
 * @tpTestDetails Test violations before returning some value.
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testViolationsBeforeReturnValue() throws Exception {
    // Valid
    ValidationCoreFoo foo = new ValidationCoreFoo("pqrs");
    Response response = client.target(generateURL("/all/abc/wxyz")).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: Should have 1 each of field, property, class, and parameter violations,
    // and no return value violations.
    foo = new ValidationCoreFoo("p");
    response = client.target(generateURL("/all/a/z")).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);
    ViolationReport r = new ViolationReport(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());
    violation = r.getClassViolations().iterator().next();
    Assert.assertEquals(WRONG_ERROR_MSG, "Concatenation of s and t must have length > 5", violation.getMessage());
    Assert.assertTrue(WRONG_ERROR_MSG, violation.getValue().startsWith("org.jboss.resteasy.test.validation.resource.ValidationCoreResourceWithAllViolationTypes@"));
    violation = r.getParameterViolations().iterator().next();
    Assert.assertEquals(WRONG_ERROR_MSG, "s must have length: 3 <= length <= 5", violation.getMessage());
    Assert.assertEquals(WRONG_ERROR_MSG, "ValidationCoreFoo[p]", violation.getValue());
    response.close();
}
Also used : Response(jakarta.ws.rs.core.Response) ValidationCoreFoo(org.jboss.resteasy.test.validation.resource.ValidationCoreFoo) ViolationReport(org.jboss.resteasy.api.validation.ViolationReport) ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) Test(org.junit.Test)

Example 3 with ValidationCoreFoo

use of org.jboss.resteasy.test.validation.resource.ValidationCoreFoo in project resteasy by resteasy.

the class ValidationJaxbTest method doRawXMLTest.

public void doRawXMLTest(MediaType mediaType, String expected, String targetURL) throws Exception {
    ValidationCoreFoo foo = new ValidationCoreFoo("p");
    Response response = client.target(targetURL).request().accept(mediaType).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 value of validation header", Boolean.valueOf(header));
    String report = response.readEntity(String.class);
    MatcherAssert.assertThat(UNEXPECTED_VALIDATION_ERROR_MSG, report, containsString(expected));
    response.close();
}
Also used : Response(jakarta.ws.rs.core.Response) ValidationCoreFoo(org.jboss.resteasy.test.validation.resource.ValidationCoreFoo) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 4 with ValidationCoreFoo

use of org.jboss.resteasy.test.validation.resource.ValidationCoreFoo 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 5 with ValidationCoreFoo

use of org.jboss.resteasy.test.validation.resource.ValidationCoreFoo in project resteasy by resteasy.

the class ExecutableValidationDisabledTest method testViolationsBeforeReturnValue.

/**
 * @tpTestDetails Test disabled validation before return value evaluation.
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testViolationsBeforeReturnValue() throws Exception {
    // Valid
    ValidationCoreFoo foo = new ValidationCoreFoo("pqrs");
    Response response = client.target(generateURL("/all/abc/wxyz")).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: Should have 1 each of field, property, class, and parameter violations,
    // and no return value violations.
    // BUT EXECUTABLE VALIDATION IS DISABLE. There will be no parameter violation.
    response = client.target(generateURL("/all/a/z")).request().post(Entity.entity(foo, "application/foo"));
    Assert.assertEquals(HttpResponseCodes.SC_BAD_REQUEST, 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, 3, 2, 1, 0, 0);
    ResteasyConstraintViolation violation = TestUtil.getViolationByMessage(e.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(e.getPropertyViolations(), "size must be between 3 and 5");
    Assert.assertNotNull(WRONG_ERROR_MSG, violation);
    Assert.assertEquals(WRONG_ERROR_MSG, "z", violation.getValue());
    violation = e.getClassViolations().iterator().next();
    Assert.assertEquals(WRONG_ERROR_MSG, "Concatenation of s and t must have length > 5", violation.getMessage());
    Assert.assertTrue(WRONG_ERROR_MSG, violation.getValue().startsWith("org.jboss.resteasy.test.validation.resource.ValidationCoreResourceWithAllViolationTypes@"));
    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) Test(org.junit.Test)

Aggregations

Response (jakarta.ws.rs.core.Response)10 ValidationCoreFoo (org.jboss.resteasy.test.validation.resource.ValidationCoreFoo)10 ResteasyConstraintViolation (org.jboss.resteasy.api.validation.ResteasyConstraintViolation)6 Test (org.junit.Test)6 ResteasyViolationException (org.jboss.resteasy.api.validation.ResteasyViolationException)4 ResteasyViolationExceptionImpl (org.jboss.resteasy.plugins.validation.ResteasyViolationExceptionImpl)4 ViolationReport (org.jboss.resteasy.api.validation.ViolationReport)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Iterator (java.util.Iterator)1