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