protected Check createCheckWithProfile(String name, String profile, ValidationConstraint constraint) { Check check = new Check(name, constraint); check.setProfiles(profile); return check; }
/** * Adds validation checks. */ public void add(final Check check) { String name = check.getName(); List<Check> list = map.computeIfAbsent(name, k -> new ArrayList<>()); list.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(); }
/** * Copies default properties from annotation to the check. */ protected void copyDefaultCheckProperties(final Check destCheck, final Annotation annotation) { Integer severity = (Integer) ClassUtil.readAnnotationValue(annotation, ANN_SEVERITY); destCheck.setSeverity(severity.intValue()); String[] profiles = (String[]) ClassUtil.readAnnotationValue(annotation, ANN_PROFILES); destCheck.setProfiles(profiles); String message = (String) ClassUtil.readAnnotationValue(annotation, ANN_MESSAGE); destCheck.setMessage(message); }
@Test void testManualConfig() { ValidationContext vctx = new ValidationContext(); vctx.add(new Check("string", new MinLengthConstraint(2))); Vtor vtor = new Vtor(); vtor.validate(vctx, new Foo()); assertTrue(vtor.hasViolations()); List<Violation> vlist = vtor.getViolations(); assertFalse(vlist.isEmpty()); assertEquals(1, vlist.size()); Violation v = vlist.get(0); assertEquals("string", v.getName()); assertEquals("1", v.getInvalidValue()); assertEquals("string", v.getCheck().getName()); assertNull(v.getCheck().getMessage()); assertEquals(MinLengthConstraint.class, v.getConstraint().getClass()); // new context that contains previous ValidationContext vctx2 = new ValidationContext(); vctx2.add(new Check("foo", new AssertValidConstraint(vctx))); vtor = new Vtor(); vtor.validate(vctx2, new Boo()); vlist = vtor.getViolations(); assertFalse(vlist.isEmpty()); assertEquals(1, vlist.size()); v = vlist.get(0); assertEquals("foo.string", v.getName()); assertEquals("1", v.getInvalidValue()); assertEquals("string", v.getCheck().getName()); assertEquals(MinLengthConstraint.class, v.getConstraint().getClass()); }
@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")); }
private Map<String, List<Check>> createValidateFldHasCheckerTestConstraints(ValidationConstraint testCheck1Constraint) { Map<String, List<Check>> constraints = new HashMap<>(); List<Check> checks = new ArrayList<>(); checks.add(new Check("testCheck1", testCheck1Constraint)); constraints.put("testField", checks); return constraints; }
@Test void testManualAddViolation() { ValidationContext vctx = new ValidationContext(); vctx.add(new Check("string", new MinLengthConstraint(2))); Vtor vtor = new Vtor(); Foo foo = new Foo(); vtor.validate(vctx, foo); vtor.addViolation(new Violation("number", foo, null)); List<Violation> vlist = vtor.getViolations(); assertFalse(vlist.isEmpty()); assertEquals(2, vlist.size()); Violation v = vlist.get(0); assertEquals("string", v.getName()); assertEquals("1", v.getInvalidValue()); assertEquals("string", v.getCheck().getName()); assertEquals(MinLengthConstraint.class, v.getConstraint().getClass()); v = vlist.get(1); assertEquals("number", v.getName()); assertNull(v.getInvalidValue()); assertNull(v.getCheck()); assertNull(v.getConstraint()); } }
/** * Copies default properties from annotation to the check. */ protected void copyDefaultCheckProperties(Check destCheck, Annotation annotation) { Integer severity = (Integer) ReflectUtil.readAnnotationValue(annotation, ANN_SEVERITY); destCheck.setSeverity(severity.intValue()); String[] profiles = (String[]) ReflectUtil.readAnnotationValue(annotation, ANN_PROFILES); destCheck.setProfiles(profiles); }
@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")); }
/** * Creates new violation. * @param name violation name inside of validation context * @param validatedObject object that is validated * @param invalidValue invalid value that is cause of violation * @param check {@link Check check} that made validation. */ public Violation(final String name, final Object validatedObject, final Object invalidValue, final Check check) { this.name = name; this.validatedObject = validatedObject; this.invalidValue = invalidValue; this.check = check; this.constraint = check != null ? check.getConstraint() : null; }
Check check = new Check(targetName, vc); copyDefaultCheckProperties(check, annotation); annChecks.add(check);
/** * Creates new violation. * @param name violation name inside of validation context * @param validatedObject object that is validated * @param invalidValue invalid value that is cause of violation * @param check {@link Check check} that made validation. */ public Violation(String name, Object validatedObject, Object invalidValue, Check check) { this.name = name; this.validatedObject = validatedObject; this.invalidValue = invalidValue; this.check = check; this.constraint = check != null ? check.getConstraint() : null; }
/** * 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)); } } } }
@Test void testToString() throws Exception { //given Object validatedObject = new Object(); Object invalidValue = new Object(); Violation violation = new Violation("niceViolation", validatedObject, invalidValue, new Check("niceCheck", new TestValidationConstraint())); //when String toString = violation.toString(); //then assertEquals(toString, "Violation{niceViolation:jodd.vtor.ViolationTest$TestValidationConstraint}"); }
/** * Adds validation checks. */ public void add(Check check) { String name = check.getName(); List<Check> list = map.get(name); if (list == null) { list = new LinkedList<Check>(); map.put(name, list); } list.add(check); }
@Test void testConstructor2() throws Exception { //given Object validatedObject = new Object(); Object invalidValue = new Object(); ValidationConstraint constr = mock(ValidationConstraint.class); Check niceCheck = new Check("niceCheck", constr); //when Violation violation = new Violation("niceViolation", validatedObject, invalidValue, niceCheck); //then assertEquals("niceViolation", violation.getName()); assertEquals(validatedObject, violation.getValidatedObject()); assertEquals(invalidValue, violation.getInvalidValue()); assertEquals(niceCheck, violation.getCheck()); assertEquals(constr, violation.getConstraint()); }
/** * 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); } }