/** * Resolve validation context for provided target class. * @see #addClassChecks(Class) */ public static ValidationContext resolveFor(final Class<?> target) { ValidationContext vc = new ValidationContext(); vc.addClassChecks(target); return vc; }
protected void prepareValidator() { if (vtor == null) { vtor = new Vtor(); } }
/** * Adds all checks from provided list. */ public void addAll(final List<Check> checkList) { for (Check check : checkList) { add(check); } }
@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); }
/** * Validates action. Profiles are reset after the invocation. * @return <code>true</code> if validation is successful, otherwise returns <code>false</code> */ protected boolean validateAction(final String... profiles) { prepareValidator(); vtor.useProfiles(profiles); vtor.validate(this); vtor.resetProfiles(); List<Violation> violations = vtor.getViolations(); return violations == null; }
/** * Adds action violation. */ protected void addViolation(final String name, final Object invalidValue) { prepareValidator(); vtor.addViolation(new Violation(name, this, invalidValue)); }
@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}"); }
/** * Validate object using context from the annotations. */ public List<Violation> validate(final Object target) { return validate(ValidationContext.resolveFor(target.getClass()), target); }
@BeforeEach void setup() { //given vtor = new Vtor(); vtor.resetProfiles(); }
protected Check createCheckWithProfile(String name, String profile, ValidationConstraint constraint) { Check check = new Check(name, constraint); check.setProfiles(profile); return check; }
/** * Validates provided context and value withing this constraint content. */ public void validateWithin(final ValidationContext vctx, final Object value) { vtor.validate(vctx, value, name); } }
/** * Returns validation violations or <code>null</code> if validation was successful. */ public List<Violation> violations() { if (vtor == null) { return null; } return vtor.getViolations(); }
/** * Invokes validation on inner context. Always returns <code>true</code> since * inner context violations will be appended to provided validator. */ @Override public boolean isValid(final ValidationConstraintContext vcc, final Object value) { if (value == null) { return true; } vcc.validateWithin(targetValidationContext, value); return true; } }
protected ValidationContext mockValidationContext(Map<String, List<Check>> constraints) { ValidationContext res = new ValidationContext(); res.map.putAll(constraints); return res; } }
/** * 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; }
@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()); }
/** * @see #validate(ValidationContext, Object, String) */ public List<Violation> validate(final ValidationContext vctx, final Object target) { return validate(vctx, target, null); }
/** * Static constructor for fluent usage. */ public static Vtor create() { return new Vtor(); }
@BeforeEach void setup() { vtor = new Vtor(); }