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; }
protected Check createCheckWithProfile(String name, String profile, ValidationConstraint constraint) { Check check = new Check(name, constraint); check.setProfiles(profile); return check; }
Check check = new Check(targetName, vc); copyDefaultCheckProperties(check, annotation); annChecks.add(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}"); }
@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")); }
@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()); }
@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 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()); } }
/** * 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); } }