@Test public void testValidateEmptyRequiredFields() { BaseUser obj = new BaseUser(); UserCredential credential = new UserCredential(); obj.setCredential(credential); BindException errors = new BindException(obj, "baseUser"); obj.setFirstName(""); obj.setLastName(""); credential.setUsername(""); credential.setNewPassword(""); credential.setConfirmPassword(""); obj.setEmailAddress(""); obj.setGroups(null); userValidator.validate(obj, errors); Mockito.verify(userService, Mockito.never()).loadByUsername("flast"); Mockito.verify(userService, Mockito.never()).loadByEmailAddress("test@test.com"); assertTrue(errors.hasErrors()); assertEquals(7, errors.getFieldErrorCount()); FieldError firstNameError = errors.getFieldError("firstName"); assertNotNull(firstNameError); assertEquals("error.required", firstNameError.getCode()); FieldError lastNameError = errors.getFieldError("lastName"); assertNotNull(lastNameError); assertEquals("error.required", lastNameError.getCode()); FieldError usernameError = errors.getFieldError("credential.username"); assertNotNull(usernameError); assertEquals("error.required", usernameError.getCode()); FieldError groupsError = errors.getFieldError("groups"); assertNotNull(groupsError); assertEquals("error.required.at-least-one", groupsError.getCode()); FieldError emailAddressError = errors.getFieldError("emailAddress"); assertNotNull(emailAddressError); assertEquals("error.required", emailAddressError.getCode()); FieldError newPasswordError = errors.getFieldError("credential.newPassword"); assertNotNull(newPasswordError); assertEquals("error.required", newPasswordError.getCode()); FieldError confirmPasswordError = errors.getFieldError("credential.confirmPassword"); assertNotNull(confirmPasswordError); assertEquals("error.required", confirmPasswordError.getCode()); }
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);  }