ComparativeConstraint extends AbstractMultivalueConstraint<T> { @Override public Violation evaluate(Map<Path<T,?>, Object> values) { Comparable a = (Comparable)values.get(op1Path); Comparable b = (Comparable)values.get(op2Path); if (a== null || b == null) return null; if (comparison.comp.check(a, b)) return null; return new Violation(this, a + " is not " + comparison.label + " " + b); } ComparativeConstraint(String id, Path<T,?> op1, Path<T,?> op2, Comparison comp); static ConstraintBuilder<Object[]> builderFor(final String regexDelimiter, final PathBuilder builder); @Override String typeTag(); @Override Path[] valuePaths(); @Override Violation evaluate(Map<Path<T,?>, Object> values); static final String TypeTag; static final PathParameter Path1; static final PathParameter Path2; static final ComparisonParameter CompParam; }
@Test public void testEvaluate() { ConstraintBuilder<?> builder = ComparativeConstraint.builderFor(" ", SimpleAvroPath.Builder); Map<String,String> paramsByKey = asParams( ComparativeConstraint.Path1, "birthdate", ComparativeConstraint.Path2, "deathdate", ComparativeConstraint.CompParam, "<" ); Collection<Constraint<?>> constraints = builder.constraintsFrom(null, false, paramsByKey); MultiValueConstraint<GenericRecord> mvc = (MultiValueConstraint<GenericRecord>)constraints.iterator().next(); assertNotNull(mvc); AvroValidator validator = new AvroValidator(); validator.register(mvc, ""); long now = System.currentTimeMillis(); Person person = PeopleBuilder.createPeople(1).iterator().next(); person.setBirthdate( now); person.setDeathdate( now + 1 ); Map<Path<GenericRecord, ?>, Collection<Violation>> issues = validator.validate(person); assertEquals(0, issues.size()); person.setBirthdate( now); person.setDeathdate( now - 1 ); issues = validator.validate(person); assertEquals(1, issues.size()); }