Search in sources :

Example 41 with ConstraintViolation

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();
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Validator(javax.validation.Validator) Test(org.junit.Test)

Example 42 with ConstraintViolation

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());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ArrayList(java.util.ArrayList) Validator(javax.validation.Validator) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 43 with ConstraintViolation

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());
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) MessageInterpolator(javax.validation.MessageInterpolator) Test(org.junit.Test)

Example 44 with ConstraintViolation

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());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 45 with ConstraintViolation

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"));
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Aggregations

ConstraintViolation (javax.validation.ConstraintViolation)95 Test (org.junit.Test)78 Validator (javax.validation.Validator)19 ConstraintViolationException (javax.validation.ConstraintViolationException)12 SMSMessage (org.apache.camel.component.cm.client.SMSMessage)12 ChangePasswordForm (org.orcid.frontend.web.forms.ChangePasswordForm)11 CmsDocumentBlobSegment (gov.ca.cwds.data.persistence.cms.CmsDocumentBlobSegment)10 ValidatorFactory (javax.validation.ValidatorFactory)9 ManagePasswordOptionsForm (org.orcid.frontend.web.forms.ManagePasswordOptionsForm)8 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 CMConfiguration (org.apache.camel.component.cm.CMConfiguration)6 Set (java.util.Set)5 InitialContext (javax.naming.InitialContext)5 Session (org.hibernate.Session)5 Transaction (org.hibernate.Transaction)5 Method (java.lang.reflect.Method)4 ExecutableValidator (javax.validation.executable.ExecutableValidator)4 CamelExecutionException (org.apache.camel.CamelExecutionException)4 Exchange (org.apache.camel.Exchange)4