use of javax.validation.ConstraintViolation in project apex-core by apache.
the class LogicalPlanTest method testOperatorValidation.
@Test
public void testOperatorValidation() {
ValidationTestOperator bean = new ValidationTestOperator();
bean.stringField1 = "malhar1";
bean.intField1 = 1;
// ensure validation standalone produces expected results
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<ValidationTestOperator>> constraintViolations = validator.validate(bean);
Assert.assertEquals("" + constraintViolations, 1, constraintViolations.size());
ConstraintViolation<ValidationTestOperator> cv = constraintViolations.iterator().next();
Assert.assertEquals("", bean.intField1, cv.getInvalidValue());
Assert.assertEquals("", "intField1", cv.getPropertyPath().toString());
// ensure DAG validation produces matching results
bean = dag.addOperator("testOperator", bean);
try {
dag.validate();
Assert.fail("should throw ConstraintViolationException");
} catch (ConstraintViolationException e) {
Assert.assertEquals("violation details", constraintViolations, e.getConstraintViolations());
String expRegex = ".*ValidationTestOperator\\{name=null}, propertyPath='intField1', message='must be greater than or equal to 2',.*value=1}]";
Assert.assertThat("exception message", e.getMessage(), RegexMatcher.matches(expRegex));
}
try {
bean.intField1 = 3;
dag.validate();
Assert.fail("should throw ConstraintViolationException");
} catch (ConstraintViolationException e) {
ConstraintViolation<?> cv2 = e.getConstraintViolations().iterator().next();
Assert.assertEquals("" + e.getConstraintViolations(), 1, constraintViolations.size());
Assert.assertEquals("", false, cv2.getInvalidValue());
Assert.assertEquals("", "validConfiguration", cv2.getPropertyPath().toString());
}
bean.stringField1 = "malhar3";
// annotated getter
try {
bean.getterProperty2 = null;
dag.validate();
Assert.fail("should throw ConstraintViolationException");
} catch (ConstraintViolationException e) {
ConstraintViolation<?> cv2 = e.getConstraintViolations().iterator().next();
Assert.assertEquals("" + e.getConstraintViolations(), 1, constraintViolations.size());
Assert.assertEquals("", null, cv2.getInvalidValue());
Assert.assertEquals("", "property2", cv2.getPropertyPath().toString());
}
bean.getterProperty2 = "";
// nested property
try {
bean.nestedBean.property = null;
dag.validate();
Assert.fail("should throw ConstraintViolationException");
} catch (ConstraintViolationException e) {
ConstraintViolation<?> cv2 = e.getConstraintViolations().iterator().next();
Assert.assertEquals("" + e.getConstraintViolations(), 1, constraintViolations.size());
Assert.assertEquals("", null, cv2.getInvalidValue());
Assert.assertEquals("", "nestedBean.property", cv2.getPropertyPath().toString());
}
bean.nestedBean.property = "";
// all valid
dag.validate();
}
use of javax.validation.ConstraintViolation in project wildfly by wildfly.
the class GroupandGroupSequenceValidationTestCase method testGroupValidation.
@Test
public void testGroupValidation() throws NamingException {
Validator validator = (Validator) new InitialContext().lookup("java:comp/Validator");
// create first passenger
UserBean user1 = new UserBean("MADHUMITA", "SADHUKHAN");
user1.setEmail("madhu@gmail.com");
user1.setAddress("REDHAT Brno");
// create second passenger
UserBean user2 = new UserBean("Mickey", "Mouse");
user2.setEmail("mickey@gmail.com");
user2.setAddress("DISNEY CA USA");
List<UserBean> passengers = new ArrayList<UserBean>();
passengers.add(user1);
passengers.add(user2);
// Create a Car
Car car = new Car("CET5678", passengers);
car.setModel("SKODA Octavia");
// validate Car with default group as per GroupSequenceProvider
Set<ConstraintViolation<Car>> result = validator.validate(car);
assertEquals(1, result.size());
assertEquals("The Car has to pass the fuel test and inspection test before being driven", result.iterator().next().getMessage());
Driver driver = new Driver("Sebastian", "Vettel");
driver.setAge(25);
driver.setAddress("ABC");
result = validator.validate(car);
assertEquals(1, result.size());
assertEquals("The Car has to pass the fuel test and inspection test before being driven", result.iterator().next().getMessage());
car.setPassedVehicleInspection(true);
result = validator.validate(car);
// New group set in defaultsequence for Car as per CarGroupSequenceProvider should be implemented now
assertEquals(2, result.size());
car.setDriver(driver);
// implementing validation for group associated with associated objects of Car(in this case Driver)
Set<ConstraintViolation<Car>> driverResult = validator.validate(car, DriverChecks.class);
assertEquals(1, driverResult.size());
driver.setHasValidDrivingLicense(true);
assertEquals(0, validator.validate(car, DriverChecks.class).size());
car.setSeats(5);
car.setHasBeenPaid(true);
assertEquals(0, validator.validate(car).size());
}
use of javax.validation.ConstraintViolation in project wildfly by wildfly.
the class MessageInterpolationValidationTestCase method testCustomMessageInterpolation.
@Test
public void testCustomMessageInterpolation() {
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
assertNotNull(configuration);
final MessageInterpolator messageInterpolator = new CustomMessageInterpolator();
configuration.messageInterpolator(messageInterpolator);
ValidatorFactory factory = configuration.buildValidatorFactory();
Validator validator = factory.getValidator();
// create employee
Employee emp = new Employee();
emp.setEmail("MADHUMITA");
Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
assertEquals(CustomMessageInterpolator.MESSAGE, constraintViolations.iterator().next().getMessage());
}
use of javax.validation.ConstraintViolation in project wildfly by wildfly.
the class ScriptAssertTestCase method testScriptAssert.
@Test
public void testScriptAssert() throws NamingException, SQLException {
Validator validator = (Validator) new InitialContext().lookup("java:comp/Validator");
final Set<ConstraintViolation<ScriptAssertBean>> result = validator.validate(new ScriptAssertBean());
Assert.assertEquals(1, result.size());
}
use of javax.validation.ConstraintViolation in project wildfly by wildfly.
the class ConstraintValidationTestCase method testConstraintValidation.
@Test
public void testConstraintValidation() throws NamingException {
Validator validator = (Validator) new InitialContext().lookup("java:comp/Validator");
UserBean user1 = new UserBean("MADHUMITA", "");
user1.setEmail("madhumita_gmail");
user1.setAddress("");
final Set<ConstraintViolation<UserBean>> result = validator.validate(user1);
Iterator<ConstraintViolation<UserBean>> it = result.iterator();
String message = "";
while (it.hasNext()) {
ConstraintViolation<UserBean> cts = it.next();
String mess = cts.getMessage();
if (mess.contains("Please get a valid address")) {
message = mess;
}
}
assertEquals(3, result.size());
assertTrue(message.contains("Please get a valid address"));
}
Aggregations