@Override public void afterPropertiesSet() throws Exception { SpringValidatorAdapter springValidatorAdapter = new SpringValidatorAdapter(this.validator); SpringValidator<T> springValidator = new SpringValidator<>(); springValidator.setValidator(springValidatorAdapter); springValidator.afterPropertiesSet(); setValidator(springValidator); super.afterPropertiesSet(); } }
/** * Validate the item and return it unmodified * * @return the input item * @throws ValidationException if validation fails */ @Override public T process(T item) throws ValidationException { try { validator.validate(item); } catch (ValidationException e) { if (filter) { return null; // filter the item } else { throw e; // skip the item } } return item; }
@Bean public BeanValidatingItemProcessor<Person> itemValidator() throws Exception { BeanValidatingItemProcessor<Person> validator = new BeanValidatingItemProcessor<>(); validator.setFilter(true); validator.afterPropertiesSet(); return validator; }
private String processFailedValidation(ValidatingItemProcessor<String> tested) { validator.validate(ITEM); when(validator).thenThrow(new ValidationException("invalid item")); return tested.process(ITEM); } }
@Override public Exception getException(String msg) throws Exception { return new ValidationException(msg); }
@Test public void testSuccessfulValidation() throws Exception { ValidatingItemProcessor<String> tested = new ValidatingItemProcessor<>(validator); validator.validate(ITEM); assertSame(ITEM, tested.process(ITEM)); }
@Test public void testFailedValidation_Filter() throws Exception { ValidatingItemProcessor<String> tested = new ValidatingItemProcessor<>(validator); tested.setFilter(true); assertNull(processFailedValidation(tested)); }
@Test(expected = ValidationException.class) public void testInvalidObjectValidation() throws Exception { // given BeanValidatingItemProcessor<Foo> validatingItemProcessor = new BeanValidatingItemProcessor<>(); validatingItemProcessor.afterPropertiesSet(); Foo foo = new Foo(""); // when validatingItemProcessor.process(foo); // then // expected exception }
/** * Validator property is not set */ @Test(expected = IllegalArgumentException.class) public void testNullValidator() throws Exception { validator.setValidator(null); validator.afterPropertiesSet(); }
@Before public void setUp() throws Exception { mockValidator = new MockSpringValidator(); validator.setValidator(mockValidator); }
@Test(expected = ValidationException.class) public void testFailedValidation() throws Exception { ValidatingItemProcessor<String> tested = new ValidatingItemProcessor<>(validator); processFailedValidation(tested); }
/** * @return string of field errors followed by global errors. */ private String errorsToString(Errors errors) { StringBuilder builder = new StringBuilder(); appendCollection(errors.getFieldErrors(), builder); appendCollection(errors.getGlobalErrors(), builder); return builder.toString(); }
/** * Typical failed validation - {@link ValidationException} is thrown */ @Test(expected = BindException.class) public void testValidateFailureWithErrors() throws Exception { try { validator.validate(MockSpringValidator.REJECT_VALUE); } catch (ValidationException e) { throw (BindException) e.getCause(); } }
/** * Typical failed validation - message contains the item and names of * invalid fields. */ @Test public void testValidateFailureWithFields() { try { validator.validate(MockSpringValidator.REJECT_MULTI_VALUE); fail("exception should have been thrown on invalid value"); } catch (ValidationException expected) { assertTrue("message should contain the item#toString() value", expected.getMessage().contains( "TestBeanToString")); assertTrue("message should contain names of the invalid fields", expected.getMessage().contains("foo")); assertTrue("message should contain names of the invalid fields", expected.getMessage().contains("bar")); } }
/** * Typical successful validation - no exception is thrown. */ @Test public void testValidateSuccessfully() { validator.validate(MockSpringValidator.ACCEPT_VALUE); }
@Override public Exception getException(String msg, Throwable t) throws Exception { return new ValidationException(msg, t); }
@Test public void testValidObjectValidation() throws Exception { // given BeanValidatingItemProcessor<Foo> validatingItemProcessor = new BeanValidatingItemProcessor<>(); validatingItemProcessor.afterPropertiesSet(); Foo foo = new Foo("foo"); // when Foo processed = validatingItemProcessor.process(foo); // then Assert.assertNotNull(processed); }
/** * Typical failed validation - {@link ValidationException} is thrown */ @Test(expected = ValidationException.class) public void testValidateFailure() { validator.validate(MockSpringValidator.REJECT_VALUE); }
@Override public Trade process(Trade item) throws Exception { if ((failedItem == null && index++ == failure) || (failedItem != null && failedItem.equals(item))) { failedItem = item; throw new ValidationException("Some bad data for " + failedItem); } return item; } }
/** * Validator does not know how to validate object of the given class */ @Test(expected = ValidationException.class) public void testValidateUnsupportedType() { validator.validate(Integer.valueOf(1)); // only strings are supported }