@Test public void testValidateCreatingNewUser() { BaseUser obj = new BaseUser(); UserCredential credential = new UserCredential(); obj.setCredential(credential); BindException errors = new BindException(obj, "baseUser"); obj.setFirstName("First"); obj.setLastName("Last"); credential.setUsername("flast"); credential.setNewPassword("123456"); credential.setPassword("123456"); credential.setConfirmPassword("123456"); obj.setEmailAddress("test@test.com"); Mockito.when(userService.loadByUsername("flast")).thenReturn(null); Mockito.when(userService.loadByEmailAddress("test@test.com")).thenReturn(null); Set<UserGroup> groups = new HashSet<UserGroup>(); groups.add(new UserGroup()); obj.setGroups(groups); userValidator.validate(obj, errors); assertFalse(errors.hasErrors()); }
public void validate(Object object, Errors e) { BaseUser user = (BaseUser) object; ValidationUtils.rejectIfEmptyOrWhitespace(e, "firstName", "error.required", new Object[]{"First Name"},"First name is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "lastName", "error.required", new Object[]{"Last Name"},"Last name is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.username", "error.required", new Object[]{"Username"},"Username is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "groups", "error.required.at-least-one", new Object[]{"Groups"},"At least one Usergroup is required."); if (isDuplicateUsername(user)) { e.rejectValue("username", "error.duplicate-field", new Object[]{user.getCredential().getUsername(), "username"}, "User name already exists."); } ValidationUtils.rejectIfEmptyOrWhitespace(e, "emailAddress", "error.required", new Object[]{"Email Address"},"Email address is required."); if(!StringUtil.isEmpty(user.getEmailAddress())){ if (!ValidatorUtil.isEmail(user.getEmailAddress())) { e.rejectValue("emailAddress", "error.invalid-email-address",new Object[]{user.getEmailAddress()},"Email Address is invalid."); } if (isDuplicateEmail(user)) { e.rejectValue("emailAddress","error.duplicate-field", new Object[]{user.getEmailAddress(),"email"}, "Email address already exists."); } } if (user.getIsNew()) { ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.newPassword", "error.required", new Object[]{"Password"},"Password is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.confirmPassword", "error.required", new Object[]{"Confirm Password"},"Confirm password is required."); } if (!StringUtil.isEmpty(user.getCredential().getNewPassword()) && !StringUtil.isEmpty(user.getCredential().getConfirmPassword()) && !user.getCredential().getNewPassword().equals(user.getCredential().getConfirmPassword())) { e.rejectValue("credential.confirmPassword","error.password-confirmation-did-not-match", "Password confirmation did not match."); } }
UserValidator implements Validator { public void validate(Object object, Errors e) { BaseUser user = (BaseUser) object; ValidationUtils.rejectIfEmptyOrWhitespace(e, "firstName", "error.required", new Object[]{"First Name"},"First name is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "lastName", "error.required", new Object[]{"Last Name"},"Last name is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.username", "error.required", new Object[]{"Username"},"Username is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "groups", "error.required.at-least-one", new Object[]{"Groups"},"At least one Usergroup is required."); if (isDuplicateUsername(user)) { e.rejectValue("username", "error.duplicate-field", new Object[]{user.getCredential().getUsername(), "username"}, "User name already exists."); } ValidationUtils.rejectIfEmptyOrWhitespace(e, "emailAddress", "error.required", new Object[]{"Email Address"},"Email address is required."); if(!StringUtil.isEmpty(user.getEmailAddress())){ if (!ValidatorUtil.isEmail(user.getEmailAddress())) { e.rejectValue("emailAddress", "error.invalid-email-address",new Object[]{user.getEmailAddress()},"Email Address is invalid."); } if (isDuplicateEmail(user)) { e.rejectValue("emailAddress","error.duplicate-field", new Object[]{user.getEmailAddress(),"email"}, "Email address already exists."); } } if (user.getIsNew()) { ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.newPassword", "error.required", new Object[]{"Password"},"Password is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.confirmPassword", "error.required", new Object[]{"Confirm Password"},"Confirm password is required."); } if (!StringUtil.isEmpty(user.getCredential().getNewPassword()) && !StringUtil.isEmpty(user.getCredential().getConfirmPassword()) && !user.getCredential().getNewPassword().equals(user.getCredential().getConfirmPassword())) { e.rejectValue("credential.confirmPassword","error.password-confirmation-did-not-match", "Password confirmation did not match."); } } }
UserValidator implements Validator { public void validate(Object object, Errors e) { BaseUser user = (BaseUser) object; ValidationUtils.rejectIfEmptyOrWhitespace(e, "firstName", "error.required", new Object[]{"First Name"},"First name is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "lastName", "error.required", new Object[]{"Last Name"},"Last name is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.username", "error.required", new Object[]{"Username"},"Username is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "groups", "error.required.at-least-one", new Object[]{"Groups"},"At least one Usergroup is required."); if (isDuplicateUsername(user)) { e.rejectValue("username", "error.duplicate-field", new Object[]{user.getCredential().getUsername(), "username"}, "User name already exists."); } ValidationUtils.rejectIfEmptyOrWhitespace(e, "emailAddress", "error.required", new Object[]{"Email Address"},"Email address is required."); if(!StringUtil.isEmpty(user.getEmailAddress())){ if (!ValidatorUtil.isEmail(user.getEmailAddress())) { e.rejectValue("emailAddress", "error.invalid-email-address",new Object[]{user.getEmailAddress()},"Email Address is invalid."); } if (isDuplicateEmail(user)) { e.rejectValue("emailAddress","error.duplicate-field", new Object[]{user.getEmailAddress(),"email"}, "Email address already exists."); } } if (user.getIsNew()) { ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.newPassword", "error.required", new Object[]{"Password"},"Password is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.confirmPassword", "error.required", new Object[]{"Confirm Password"},"Confirm password is required."); } if (!StringUtil.isEmpty(user.getCredential().getNewPassword()) && !StringUtil.isEmpty(user.getCredential().getConfirmPassword()) && !user.getCredential().getNewPassword().equals(user.getCredential().getConfirmPassword())) { e.rejectValue("credential.confirmPassword","error.password-confirmation-did-not-match", "Password confirmation did not match."); } }  }
UserValidator implements Validator { public void validate(Object object, Errors e) { BaseUser user = (BaseUser) object; ValidationUtils.rejectIfEmptyOrWhitespace(e, "firstName", "error.required", new Object[]{"First Name"},"First name is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "lastName", "error.required", new Object[]{"Last Name"},"Last name is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.username", "error.required", new Object[]{"Username"},"Username is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "groups", "error.required.at-least-one", new Object[]{"Groups"},"At least one Usergroup is required."); if (isDuplicateUsername(user)) { e.rejectValue("username", "error.duplicate-field", new Object[]{user.getCredential().getUsername(), "username"}, "User name already exists."); } ValidationUtils.rejectIfEmptyOrWhitespace(e, "emailAddress", "error.required", new Object[]{"Email Address"},"Email address is required."); if(!StringUtil.isEmpty(user.getEmailAddress())){ if (!ValidatorUtil.isEmail(user.getEmailAddress())) { e.rejectValue("emailAddress", "error.invalid-email-address",new Object[]{user.getEmailAddress()},"Email Address is invalid."); } if (isDuplicateEmail(user)) { e.rejectValue("emailAddress","error.duplicate-field", new Object[]{user.getEmailAddress(),"email"}, "Email address already exists."); } } if (user.getIsNew()) { ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.newPassword", "error.required", new Object[]{"Password"},"Password is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.confirmPassword", "error.required", new Object[]{"Confirm Password"},"Confirm password is required."); } if (!StringUtil.isEmpty(user.getCredential().getNewPassword()) && !StringUtil.isEmpty(user.getCredential().getConfirmPassword()) && !user.getCredential().getNewPassword().equals(user.getCredential().getConfirmPassword())) { e.rejectValue("credential.confirmPassword","error.password-confirmation-did-not-match", "Password confirmation did not match."); } }  @SuppressWarnings("rawtypes") boolean supports(Class clazz); void validate(Object object, Errors e); }
UserValidator implements Validator { public void validate(Object object, Errors e) { BaseUser user = (BaseUser) object; ValidationUtils.rejectIfEmptyOrWhitespace(e, "firstName", "error.required", new Object[]{"First Name"},"First name is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "lastName", "error.required", new Object[]{"Last Name"},"Last name is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.username", "error.required", new Object[]{"Username"},"Username is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "groups", "error.required.at-least-one", new Object[]{"Groups"},"At least one Usergroup is required."); if (isDuplicateUsername(user)) { e.rejectValue("username", "error.duplicate-field", new Object[]{user.getCredential().getUsername(), "username"}, "User name already exists."); } ValidationUtils.rejectIfEmptyOrWhitespace(e, "emailAddress", "error.required", new Object[]{"Email Address"},"Email address is required."); if(!StringUtil.isEmpty(user.getEmailAddress())){ if (!ValidatorUtil.isEmail(user.getEmailAddress())) { e.rejectValue("emailAddress", "error.invalid-email-address",new Object[]{user.getEmailAddress()},"Email Address is invalid."); } if (isDuplicateEmail(user)) { e.rejectValue("emailAddress","error.duplicate-field", new Object[]{user.getEmailAddress(),"email"}, "Email address already exists."); } } if (user.getIsNew()) { ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.newPassword", "error.required", new Object[]{"Password"},"Password is required."); ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.confirmPassword", "error.required", new Object[]{"Confirm Password"},"Confirm password is required."); } if (!StringUtil.isEmpty(user.getCredential().getNewPassword()) && !StringUtil.isEmpty(user.getCredential().getConfirmPassword()) && !user.getCredential().getNewPassword().equals(user.getCredential().getConfirmPassword())) { e.rejectValue("credential.confirmPassword","error.password-confirmation-did-not-match", "Password confirmation did not match."); } }  @SuppressWarnings("rawtypes") boolean supports(Class clazz); void validate(Object object, Errors e);  }