/** * {@inheritDoc} */ @Override public void propertyValidationFailed(SingleProperty<?> property, PropertyValidation propertyValidation) { if (propertyValidation.hasErrors()) { validationMap.put(property, propertyValidation); correctUpdateMap.remove(property); } updatePage(); }
/** * Validates all properties in this configuration returning the map of containing the properties * that have validation errors. * * @return Map of properties with {@link PropertyValidation} containing errors. */ public Map<AbstractProperty, PropertyValidation> validate() { Map<AbstractProperty, PropertyValidation> validationMap = new HashMap<>(); for (AbstractProperty property : getAllProperties()) { PropertyValidation propertyValidation = property.validate(); if (propertyValidation.hasErrors()) { validationMap.put(property, propertyValidation); } } return validationMap; }
/** * Returns message for the {@link PropertyValidation}. * * @return Returns message for the {@link PropertyValidation}. */ public String getMessage() { StringBuilder stringBuilder = new StringBuilder(); if (this.hasErrors()) { stringBuilder.append("Property "); stringBuilder.append(this.getProperty().getName()); stringBuilder.append(" can not be validated. Validation test failed because of the following errors:\n"); for (ValidationError validationError : this.getErrors()) { stringBuilder.append("||- "); stringBuilder.append(validationError.getMessage()); } } else { stringBuilder.append("Property "); stringBuilder.append(this.getProperty().getName()); stringBuilder.append("validated without errors."); } return stringBuilder.toString(); }
/** * Validates with the group validators this property, based on the changes of the single * properties reported by update list. * * @param propertyUpdates * Information about updates. * @throws PropertyValidationException * If validation fails. */ public void validateForPropertiesUpdate(Collection<IPropertyUpdate<?>> propertyUpdates) throws PropertyValidationException { PropertyValidation propertyValidation = PropertyValidation.createFor(this); for (IGroupedProperyValidator groupedProperyValidator : validators) { groupedProperyValidator.validateForPropertyUpdates(this, propertyUpdates, propertyValidation); } // if has errors raise exception, otherwise create property update if (propertyValidation.hasErrors()) { throw new PropertyValidationException(propertyValidation); } }
@Test public void validate() { when(propertyValidation1.hasErrors()).thenReturn(true); when(property1.validate()).thenReturn(propertyValidation1); when(propertyValidation2.hasErrors()).thenReturn(false); when(property2.validate()).thenReturn(propertyValidation2); Map<AbstractProperty, PropertyValidation> validateMap = configuration.validate(); assertThat(validateMap, hasEntry(property1, propertyValidation1)); assertThat(validateMap, not(hasKey(property2))); } }
return !propertyValidation.hasErrors();
/** * Validates the update value against the property and if validation passes returns the * {@link AbstractPropertyUpdate} object. * * @param updateValue * Update value * @return {@link AbstractPropertyUpdate} * @throws PropertyValidationException * If validation fails. */ public AbstractPropertyUpdate<T> createAndValidatePropertyUpdate(T updateValue) throws PropertyValidationException { if (getValue().equals(updateValue)) { throw new PropertyValidationException("Update value for creating property update can not be same as current property value."); } PropertyValidation propertyValidation = this.validateForValue(updateValue); // if has errors raise exception, otherwise create property update if (propertyValidation.hasErrors()) { throw new PropertyValidationException(propertyValidation); } else { return this.createPropertyUpdate(updateValue); } }
assertThat(captor2.getValue().getProperty(), is(equalTo((AbstractProperty) singleProperty))); assertThat(propertyValidation.hasErrors(), is(true)); assertThat(propertyValidation.getErrorCount(), is(1)); assertThat(propertyValidation.getErrors(), hasSize(1));
@Test public void validationFailsOnSingleProperty() { GroupedProperty groupedProperty = new GroupedProperty(); groupedProperty.addSingleProperty(singleProperty1); groupedProperty.addSingleProperty(singleProperty2); groupedProperty.addValidator(validator1); groupedProperty.addValidator(validator2); doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); PropertyValidation propertyValidation = (PropertyValidation) args[0]; propertyValidation.addValidationError(new ValidationError()); return null; } }).when(singleProperty1).validate(Matchers.<PropertyValidation> anyObject()); PropertyValidation propertyValidation = groupedProperty.validate(); assertThat(propertyValidation.hasErrors(), is(true)); assertThat(propertyValidation.getErrorCount(), is(1)); verify(singleProperty1, times(1)).validate(propertyValidation); verify(singleProperty2, times(1)).validate(propertyValidation); verify(validator1, times(1)).validate(groupedProperty, propertyValidation); verify(validator2, times(1)).validate(groupedProperty, propertyValidation); verifyNoMoreInteractions(singleProperty1, singleProperty2, validator1, validator2); }
assertThat(propertyValidation.hasErrors(), is(true)); assertThat(propertyValidation.getErrorCount(), is(2));