@Test void testValidateFldHasChecker_isValidFalse() throws Exception { //given ValidationConstraint testCheck1Constraint = mock(ValidationConstraint.class); //ValidationConstraint.isValid always returns false when(testCheck1Constraint.isValid(any(ValidationConstraintContext.class), any())).thenReturn(false); Map<String, List<Check>> constraints = createValidateFldHasCheckerTestConstraints(testCheck1Constraint); //when validate an object with field testField List<Violation> violations = new Vtor().validate(mockValidationContext(constraints), new ValidateTestObject("testValue"), "testField"); //then //isValid for ValidationConstraint mast be invoked verify(testCheck1Constraint).isValid(any(ValidationConstraintContext.class), eq("testValue")); assertEquals(1, violations.size()); }
throw new VtorException("Invalid constraint: " + constraintClass.getClass().getName(), ex); vc.configure(annotation); Check check = new Check(targetName, vc); copyDefaultCheckProperties(check, annotation);
@Test void testValidateFldHasChecker_isValidTrue() throws Exception { //given ValidationConstraint testCheck1Constraint = mock(ValidationConstraint.class); //ValidationConstraint.isValid always returns false when(testCheck1Constraint.isValid(any(ValidationConstraintContext.class), any())).thenReturn(true); Map<String, List<Check>> constraints = createValidateFldHasCheckerTestConstraints(testCheck1Constraint); //when validate an object with field testField List<Violation> validate = new Vtor().validate(mockValidationContext(constraints), new ValidateTestObject("testValue"), "testField"); //then //isValid for ValidationConstraint mast be invoked verify(testCheck1Constraint).isValid(any(ValidationConstraintContext.class), eq("testValue")); assertNull(validate); }
/** * Collect annotations for some target. */ @SuppressWarnings({"unchecked"}) protected void collectAnnotationChecks(List<Check> annChecks, Class targetType, String targetName, Annotation[] annotations) { for (Annotation annotation : annotations) { Constraint c = annotation.annotationType().getAnnotation(Constraint.class); if (c == null) { continue; } Class<? extends ValidationConstraint> constraintClass = c.value(); ValidationConstraint vc; try { vc = newConstraint(constraintClass, targetType); } catch (Exception ex) { throw new VtorException("Unable to create constraint: " + constraintClass.getClass().getName(), ex); } vc.configure(annotation); Check check = new Check(targetName, vc); copyDefaultCheckProperties(check, annotation); annChecks.add(check); } }
/** * 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 testValidateCheckForDifferentProfile() throws Exception { //given a list of constraints with different profiles, one check has same profile as Vtor Vtor vtor = new Vtor(); vtor.useProfile("profil1"); Map<String, List<Check>> constraints = new HashMap<>(); ValidationConstraint testCheck1Constraint = mock(ValidationConstraint.class); ValidationConstraint testCheck2Constraint = mock(ValidationConstraint.class); Check ch1 = createCheckWithProfile("check1", "profil1", testCheck1Constraint); Check ch2 = createCheckWithProfile("check2", "profil2", testCheck2Constraint); List<Check> checks = new ArrayList<>(); checks.add(ch1); checks.add(ch2); constraints.put("testField", checks); //when List<Violation> violations = vtor.validate(mockValidationContext(constraints), new ValidateTestObject("testValue"), "testField"); //then assertEquals(1, violations.size()); assertEquals("profil1", violations.get(0).getCheck().getProfiles()[0]); verify(testCheck1Constraint).isValid(any(ValidationConstraintContext.class), eq("testValue")); verify(testCheck2Constraint, never()).isValid(any(ValidationConstraintContext.class), eq("testValue")); }
@Test void testValidateCheckSeverity() throws Exception { //given Vtor vtor = new Vtor(); vtor.setSeverity(10); Map<String, List<Check>> constraints = new HashMap<>(); ValidationConstraint testCheck1Constraint = mock(ValidationConstraint.class); ValidationConstraint testCheck2Constraint = mock(ValidationConstraint.class); Check ch1 = new Check("check1", testCheck1Constraint); ch1.setSeverity(15); Check ch2 = new Check("check2", testCheck2Constraint); ch2.setSeverity(5); List<Check> checks = new ArrayList<>(); checks.add(ch1); checks.add(ch2); constraints.put("testField", checks); //when List<Violation> violations = vtor.validate(mockValidationContext(constraints), new ValidateTestObject("testValue"), "testField"); //then assertEquals(1, violations.size()); assertEquals(ch1, violations.get(0).getCheck()); verify(testCheck1Constraint).isValid(any(ValidationConstraintContext.class), eq("testValue")); verify(testCheck2Constraint, never()).isValid(any(ValidationConstraintContext.class), eq("testValue")); }
/** * 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)); } } } }