@Test public void testValidateEmptyRequiredFields() { SystemCodes obj = new SystemCodes(); obj.setKey(""); obj.setCategory(""); obj.setValue(""); BindException errors = new BindException(obj, "systemCodes"); Mockito.when(systemCodesService.isDuplicateKey(obj)).thenReturn(false); systemCodesValidator.validate(obj, errors); Mockito.verify(systemCodesService).isDuplicateKey(obj); assertTrue(errors.hasErrors()); assertEquals(3, errors.getFieldErrors().size()); FieldError keyError = errors.getFieldError("key"); assertNotNull(keyError); FieldError categoryError = errors.getFieldError("category"); assertNotNull(categoryError); FieldError valueError = errors.getFieldError("value"); assertNotNull(valueError); }
@Override public void validate(Object target, Errors errors) { SystemCodes systemCodes = (SystemCodes) target; ValidationUtils.rejectIfEmpty(errors, "category", "error.required", new Object[] { "Category" }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "key", "error.required", new Object[] { "Key" }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "value", "error.required", new Object[] { "Value" }); if(systemCodesService.isDuplicateKey(systemCodes)){ errors.reject("error.duplicate-key", new Object[]{"\""+systemCodes.getKey()+"\"","key"}, "\""+systemCodes.getKey() +"\" already exists. Please try a different key."); } if(!systemCodes.isParentValid()) { errors.reject("error.parent-invalid", new Object[]{"\"" + systemCodes.getKey() + "\"", "\"" + systemCodes.getParent().getKey() + "\""}, "\"" + systemCodes.getKey() + "\" conflicts with \"" + systemCodes.getParent().getKey() + "\". Please try a different one."); } if(!StringUtil.isEmpty(systemCodes.getParentString())) { SystemCodes parent = systemCodesService.findByKey(systemCodes.getParentString()); if(parent == null) { errors.reject("error.parent-does-not-exist",new Object[]{systemCodes.getParentString()}, systemCodes.getParentString() +" does not exist, please choose a valid Parent."); } } }
SystemCodesValidator implements Validator { @Override public void validate(Object target, Errors errors) { SystemCodes systemCodes = (SystemCodes) target; ValidationUtils.rejectIfEmpty(errors, "category", "error.required", new Object[] { "Category" }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "key", "error.required", new Object[] { "Key" }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "value", "error.required", new Object[] { "Value" }); if(systemCodesService.isDuplicateKey(systemCodes)){ errors.reject("error.duplicate-key", new Object[]{"\""+systemCodes.getKey()+"\"","key"}, "\""+systemCodes.getKey() +"\" already exists. Please try a different key."); } if(!systemCodes.isParentValid()) { errors.reject("error.parent-invalid", new Object[]{"\"" + systemCodes.getKey() + "\"", "\"" + systemCodes.getParent().getKey() + "\""}, "\"" + systemCodes.getKey() + "\" conflicts with \"" + systemCodes.getParent().getKey() + "\". Please try a different one."); } if(!StringUtil.isEmpty(systemCodes.getParentString())) { SystemCodes parent = systemCodesService.findByKey(systemCodes.getParentString()); if(parent == null) { errors.reject("error.parent-does-not-exist",new Object[]{systemCodes.getParentString()}, systemCodes.getParentString() +" does not exist, please choose a valid Parent."); } } } }
SystemCodesValidator implements Validator { @Override public void validate(Object target, Errors errors) { SystemCodes systemCodes = (SystemCodes) target; ValidationUtils.rejectIfEmpty(errors, "category", "error.required", new Object[] { "Category" }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "key", "error.required", new Object[] { "Key" }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "value", "error.required", new Object[] { "Value" }); if(systemCodesService.isDuplicateKey(systemCodes)){ errors.reject("error.duplicate-key", new Object[]{"\""+systemCodes.getKey()+"\"","key"}, "\""+systemCodes.getKey() +"\" already exists. Please try a different key."); } if(!systemCodes.isParentValid()) { errors.reject("error.parent-invalid", new Object[]{"\"" + systemCodes.getKey() + "\"", "\"" + systemCodes.getParent().getKey() + "\""}, "\"" + systemCodes.getKey() + "\" conflicts with \"" + systemCodes.getParent().getKey() + "\". Please try a different one."); } if(!StringUtil.isEmpty(systemCodes.getParentString())) { SystemCodes parent = systemCodesService.findByKey(systemCodes.getParentString()); if(parent == null) { errors.reject("error.parent-does-not-exist",new Object[]{systemCodes.getParentString()}, systemCodes.getParentString() +" does not exist, please choose a valid Parent."); } } }  }
SystemCodesValidator implements Validator { @Override public void validate(Object target, Errors errors) { SystemCodes systemCodes = (SystemCodes) target; ValidationUtils.rejectIfEmpty(errors, "category", "error.required", new Object[] { "Category" }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "key", "error.required", new Object[] { "Key" }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "value", "error.required", new Object[] { "Value" }); if(systemCodesService.isDuplicateKey(systemCodes)){ errors.reject("error.duplicate-key", new Object[]{"\""+systemCodes.getKey()+"\"","key"}, "\""+systemCodes.getKey() +"\" already exists. Please try a different key."); } if(!systemCodes.isParentValid()) { errors.reject("error.parent-invalid", new Object[]{"\"" + systemCodes.getKey() + "\"", "\"" + systemCodes.getParent().getKey() + "\""}, "\"" + systemCodes.getKey() + "\" conflicts with \"" + systemCodes.getParent().getKey() + "\". Please try a different one."); } if(!StringUtil.isEmpty(systemCodes.getParentString())) { SystemCodes parent = systemCodesService.findByKey(systemCodes.getParentString()); if(parent == null) { errors.reject("error.parent-does-not-exist",new Object[]{systemCodes.getParentString()}, systemCodes.getParentString() +" does not exist, please choose a valid Parent."); } } }  boolean supports(Class<?> clazz); @Override void validate(Object target, Errors errors); }
SystemCodesValidator implements Validator { @Override public void validate(Object target, Errors errors) { SystemCodes systemCodes = (SystemCodes) target; ValidationUtils.rejectIfEmpty(errors, "category", "error.required", new Object[] { "Category" }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "key", "error.required", new Object[] { "Key" }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "value", "error.required", new Object[] { "Value" }); if(systemCodesService.isDuplicateKey(systemCodes)){ errors.reject("error.duplicate-key", new Object[]{"\""+systemCodes.getKey()+"\"","key"}, "\""+systemCodes.getKey() +"\" already exists. Please try a different key."); } if(!systemCodes.isParentValid()) { errors.reject("error.parent-invalid", new Object[]{"\"" + systemCodes.getKey() + "\"", "\"" + systemCodes.getParent().getKey() + "\""}, "\"" + systemCodes.getKey() + "\" conflicts with \"" + systemCodes.getParent().getKey() + "\". Please try a different one."); } if(!StringUtil.isEmpty(systemCodes.getParentString())) { SystemCodes parent = systemCodesService.findByKey(systemCodes.getParentString()); if(parent == null) { errors.reject("error.parent-does-not-exist",new Object[]{systemCodes.getParentString()}, systemCodes.getParentString() +" does not exist, please choose a valid Parent."); } } }  boolean supports(Class<?> clazz); @Override void validate(Object target, Errors errors);  }