@Test void testValidateWithin() throws Exception { Vtor vtor = mock(Vtor.class); Object target = new Object(); ValidationConstraintContext context = new ValidationConstraintContext(vtor, target, "niceConstraint"); ValidationContext vctx = mock(ValidationContext.class); Object value = new Object(); //when context.validateWithin(vctx, value); //then verify(vtor).validate(eq(vctx), eq(value), eq("niceConstraint")); } }
/** * Performs validation of provided validation context and appends violations. */ public List<Violation> validate(final ValidationContext ctx, final Object target, final String targetName) { for (Map.Entry<String, List<Check>> entry : ctx.map.entrySet()) { String name = entry.getKey(); Object value = BeanUtil.declaredSilent.getProperty(target, name); String valueName = targetName != null ? (targetName + '.' + name) : name; // move up ValidationConstraintContext vcc = new ValidationConstraintContext(this, target, valueName); for (Check check : entry.getValue()) { String[] checkProfiles = check.getProfiles(); if (!matchProfiles(checkProfiles)) { continue; } if (check.getSeverity() < severity) { continue; } ValidationConstraint constraint = check.getConstraint(); if (!constraint.isValid(vcc, value)) { addViolation(new Violation(valueName, target, value, check)); } } } return getViolations(); }
@Test void testConstructor() throws Exception { Vtor vtor = mock(Vtor.class); Object target = new Object(); ValidationConstraintContext context = new ValidationConstraintContext(vtor, target, "niceConstraint"); assertEquals(context.getName(), "niceConstraint"); assertEquals(context.getTarget(), target); assertEquals(context.getValidator(), vtor); }
/** * Performs validation of provided validation context and appends violations. */ public void validate(ValidationContext ctx, Object target, String targetName) { for (Map.Entry<String, List<Check>> entry : ctx.map.entrySet()) { String name = entry.getKey(); Object value = BeanUtil.getDeclaredPropertySilently(target, name); String valueName = targetName != null ? (targetName + '.' + name) : name; // move up ValidationConstraintContext vcc = new ValidationConstraintContext(this, target, valueName); for (Check check : entry.getValue()) { String[] checkProfiles = check.getProfiles(); if (matchProfiles(checkProfiles) == false) { continue; } if (check.getSeverity() < severity) { continue; } ValidationConstraint constraint = check.getConstraint(); if (constraint.isValid(vcc, value) == false) { addViolation(new Violation(valueName, target, value, check)); } } } }