@Test public void testValidTransaction() { Transaction trx = new Transaction(1L, 10D, "test", null); BeanPropertyBindingResult result = new BeanPropertyBindingResult(trx, "transaction"); ValidationUtils.invokeValidator(validator, trx, result); assertThat(result.hasErrors(), equalTo(false)); }
@Test public void testTransactionIdInvalid() { Transaction trx = new Transaction(null, 10D, "test", null); BeanPropertyBindingResult result = new BeanPropertyBindingResult(trx, "transaction"); ValidationUtils.invokeValidator(validator, trx, result); assertThat(result.hasErrors(), equalTo(true)); assertThat(result.getErrorCount(), equalTo(1)); }
private boolean doValidate(PersistentEntity entity, Object o) { Validator v = datastore.getMappingContext().getEntityValidator(entity); if (v == null) { return true; } final Object skipValidation = datastore.getCurrentSession().getAttribute(o, SKIP_VALIDATION_ATTRIBUTE); if ((skipValidation instanceof Boolean) && (Boolean) skipValidation) { return true; } BeanPropertyBindingResult result = new BeanPropertyBindingResult(o, o.getClass().getName()); v.validate(o, result); if (result.hasErrors()) { onErrors(o, result); return false; } return true; }
void validateCompanies(List<VacancyTemplate> vacancyTemplates) { List<Company> companies = new ArrayList<>(); Set<String> companyCaptions = new HashSet<String>(); vacancyTemplates.forEach( vacancyTemplate -> companyCaptions.add(vacancyTemplate.getCompanyCaption())); companyCaptions.forEach( s -> { Company company = companyService.findByCaption(s); if (company == null) { company = new Company(); company.setCaption(s); BeanPropertyBindingResult result = new BeanPropertyBindingResult(company, s); ValidationUtils.invokeValidator(validator, company, result); if (result.hasErrors()) { List<ObjectError> errors = result.getAllErrors(); StringBuilder builder = new StringBuilder(); builder.append( String.format( "Validate company '%s' . No of validation errors: %s", s, errors.size())); errors.forEach( objectError -> { builder.append("\n"); builder.append(objectError.getDefaultMessage()); }); log.error(builder.toString()); } else { companies.add(company); } } }); saveAllCompanies(companies); }
List<Vacancy> validateVacancies(List<VacancyTemplate> vacancyTemplates) { Set<String> vacancyURLs = new HashSet<>(); List<Vacancy> vacancies = new ArrayList<>(); for (VacancyTemplate vt : vacancyTemplates) { Company company = companyService.findByCaption(vt.getCompanyCaption()); if (company == null) log.error( "Job missing: '{}'. Reason: company '{}' not found", vt.getVacancyCaption(), vt.getCompanyCaption()); else if (!vacancyURLs.contains(vt.getVacancyUrl())) { String pathSegment; try { pathSegment = UriUtils.encodePathSegment(vt.getVacancyUrl(), WebUtils.DEFAULT_CHARACTER_ENCODING); } catch (UnsupportedEncodingException uee) { log.error(uee.toString()); continue; } vacancyURLs.add(pathSegment); Vacancy vacancy = new Vacancy(); vacancy.setId(pathSegment); vacancy.setCaption(vt.getVacancyCaption()); vacancy.setUrl(vt.getVacancyUrl()); vacancy.setPriceMax(vt.getPriceMax()); vacancy.setPriceMin(vt.getPriceMin()); vacancy.setRegistrationDate(vt.getRegistrationDate()); vacancy.setCurrency(currencyService.findByCaption(vt.getCurrencyCaption())); vacancy.setSite(vt.getSite()); vacancy.setCity(vt.getCity()); vacancy.setCompany(company); BeanPropertyBindingResult result = new BeanPropertyBindingResult(vacancy, vacancy.getCaption()); ValidationUtils.invokeValidator(validator, vacancy, result); if (result.hasErrors()) { List<ObjectError> errors = result.getAllErrors(); StringBuilder builder = new StringBuilder(); builder.append( String.format( "Validate vacancy '%s' . No of validation errors: %s", vacancy.getCaption(), errors.size())); errors.forEach( objectError -> { builder.append("\n"); builder.append(objectError.getDefaultMessage()); }); log.error(builder.toString()); } else { vacancies.add(vacancy); } } } return vacancies; }