Search in sources :

Example 21 with ConstraintViolation

use of javax.validation.ConstraintViolation in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testEmbedded.

@Test
public void testEmbedded() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    PowerSupply ps = new PowerSupply();
    screen.setPowerSupply(ps);
    Button button = new Button();
    button.setName(null);
    button.setSize(3);
    screen.setStopButton(button);
    try {
        s.persist(screen);
        s.flush();
        fail("@NotNull on embedded property is not evaluated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        ConstraintViolation<?> cv = e.getConstraintViolations().iterator().next();
        assertEquals(Screen.class, cv.getRootBeanClass());
        // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though
        assertEquals("stopButton.name", cv.getPropertyPath().toString());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 22 with ConstraintViolation

use of javax.validation.ConstraintViolation in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testCollectionAssocNotValidated.

@Test
public void testCollectionAssocNotValidated() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    screen.setStopButton(new Button());
    screen.getStopButton().setName("STOOOOOP");
    PowerSupply ps = new PowerSupply();
    screen.setPowerSupply(ps);
    Color c = new Color();
    c.setName("Blue");
    s.persist(c);
    c.setName(null);
    screen.getDisplayColors().add(c);
    try {
        s.persist(screen);
        s.flush();
        fail("Associated objects should not be validated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(Color.class, constraintViolation.getRootBeanClass());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 23 with ConstraintViolation

use of javax.validation.ConstraintViolation in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testEmbeddedCollection.

@Test
public void testEmbeddedCollection() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    PowerSupply ps = new PowerSupply();
    screen.setPowerSupply(ps);
    DisplayConnector conn = new DisplayConnector();
    conn.setNumber(0);
    screen.getConnectors().add(conn);
    try {
        s.persist(screen);
        s.flush();
        fail("Collection of embedded objects should be validated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(Screen.class, constraintViolation.getRootBeanClass());
        // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though
        assertEquals("connectors[].number", constraintViolation.getPropertyPath().toString());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 24 with ConstraintViolation

use of javax.validation.ConstraintViolation in project fb-botmill by BotMill.

the class FbBotMillBean method validate.

/**
	 * Validates the {@link FbBotMillResponse}.
	 *
	 * @param response
	 *            the response
	 * @return true if the response is valid, false otherwise.
	 */
protected boolean validate(FbBotMillResponse response) {
    // If validations are not enabled, returns true.
    if (!FbBotMillContext.getInstance().isValidationEnabled()) {
        return true;
    }
    boolean valid = true;
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<FbBotMillResponse>> violations = validator.validate(response);
    for (ConstraintViolation<FbBotMillResponse> v : violations) {
        valid = false;
        logger.error("FbBotMillResponse validation error. Message: [{}] Value: [{}], Class: [{}], Field: [{}]", v.getMessage(), v.getInvalidValue(), v.getRootBean(), v.getPropertyPath());
    }
    if (valid == false) {
        // Sends the constraint violations through the callback.
        List<FbBotMillMonitor> registeredMonitors = FbBotMillContext.getInstance().getRegisteredMonitors();
        for (FbBotMillMonitor monitor : registeredMonitors) {
            monitor.onValidationError(response, violations);
        }
    }
    return valid;
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) FbBotMillResponse(co.aurasphere.botmill.fb.model.outcoming.FbBotMillResponse) ConstraintViolation(javax.validation.ConstraintViolation) FbBotMillMonitor(co.aurasphere.botmill.fb.support.FbBotMillMonitor) Validator(javax.validation.Validator)

Example 25 with ConstraintViolation

use of javax.validation.ConstraintViolation in project ORCID-Source by ORCID.

the class ManagePasswordOptionsValidationFormTest method testPasswordFormMissingData.

@Test
public void testPasswordFormMissingData() {
    ManagePasswordOptionsForm form = new ManagePasswordOptionsForm();
    form.setPassword("");
    form.setRetypedPassword("");
    Set<ConstraintViolation<ManagePasswordOptionsForm>> violations = validator.validate(form);
    Map<String, String> allErrorValues = retrieveErrorKeyAndMessage(violations);
    String password = allErrorValues.get("password");
    String confirmedPassword = allErrorValues.get("retypedPassword");
    String securityQuestion = allErrorValues.get("securityQuestionId");
    String securityAnswer = allErrorValues.get("securityQuestionAnswer");
    String digits = allErrorValues.get("verificationNumber");
    assertEquals("Passwords must be 8 or more characters and contain at least 1 number and at least 1 alpha character or symbol", password);
    assertEquals("The confirm password field is invalid.", confirmedPassword);
    assertEquals("Please select a security question.", securityQuestion);
    assertEquals("Please provide an answer to your security question.", securityAnswer);
    assertEquals("Please enter a 4 digit verification number", digits);
}
Also used : ManagePasswordOptionsForm(org.orcid.frontend.web.forms.ManagePasswordOptionsForm) ConstraintViolation(javax.validation.ConstraintViolation) 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