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