@Test public void validatorIsLessThan() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property"); LessValidator<Number> validator = new LessValidator<>(); when(singleProperty.parseLiteral(null)).thenReturn(11, 10, 9); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); validator.validate(singleProperty, propertyValidation); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(2)).addValidationError(Matchers.<ValidationError> anyObject()); }
/** * {@inheritDoc} */ @Override protected void compare(SingleProperty<? extends T> property, SingleProperty<? extends T> againstProperty, T value, T against, PropertyValidation propertyValidation) { if (!matches(value, against)) { List<SingleProperty<?>> properties = new ArrayList<>(2); properties.add(property); properties.add(againstProperty); ValidationError validationError = new ValidationError(properties, "Value of property '" + property.getName() + "' must be less than value of property '" + againstProperty.getName() + "'"); propertyValidation.addValidationError(validationError); } }
@Test public void validatorGroupValidation() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property1"); when(singleProperty2.getValue()).thenReturn(Integer.valueOf(20)); when(singleProperty2.getLogicalName()).thenReturn("my.property2"); Set<SingleProperty<?>> properties = new HashSet<>(); properties.add(singleProperty); properties.add(singleProperty2); when(groupedProperty.forLogicalname("my.property1")).thenReturn(singleProperty); when(groupedProperty.forLogicalname("my.property2")).thenReturn(singleProperty2); LessValidator<Number> validator = new LessValidator<>(); validator.setProperty("my.property1"); validator.setThan("my.property2"); validator.validate(groupedProperty, propertyValidation); verifyZeroInteractions(propertyValidation); // make it fail when(singleProperty.getValue()).thenReturn(Integer.valueOf(30)); validator.validate(groupedProperty, propertyValidation); verify(propertyValidation, times(1)).addValidationError(Matchers.<ValidationError> anyObject()); }
SingleProperty<Long> property4 = new LongProperty("Min rotation", "Define min rotation", "properties.rotation.min", 0L, false, false); groupedProperty.addSingleProperty(property4); LessValidator<Long> lessValidator = new LessValidator<>(); lessValidator.setProperty("properties.rotation.min"); lessValidator.setThan("properties.rotation.max"); groupedProperty.addValidator(lessValidator);
/** * {@inheritDoc} */ @Override protected void compare(SingleProperty<? extends T> property, T value, T against, PropertyValidation propertyValidation) { if (!matches(value, against)) { ValidationError validationError = new ValidationError(Collections.<SingleProperty<?>> singletonList(property), "Value of property '" + property.getName() + "' must be less than " + against); propertyValidation.addValidationError(validationError); } }