@Test void testAddViolation_withNullValue() throws Exception { Vtor vtor = new Vtor(); vtor.addViolation(null); assertNull(vtor.getViolations()); }
@Test void testAsJoyAction() { Vtor vtor = new Vtor(); vtor.useProfiles(Vtor.DEFAULT_PROFILE, "register"); vtor.validate(new Too()); List<Violation> violations = vtor.getViolations(); assertNull(violations); }
/** * 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; }
@BeforeEach void setup() { //given vtor = new Vtor(); vtor.resetProfiles(); }
@Test void testProfiles() { Vtor vtor = new Vtor(); vtor.setValidateAllProfilesByDefault(true); Zoo zoo = new Zoo(); vtor.validate(zoo); List<Violation> vlist = vtor.getViolations(); assertEquals(3, vlist.size()); vtor.resetViolations(); vtor.useProfile("default"); vtor.validate(zoo); vlist = vtor.getViolations(); assertEquals(1, vlist.size()); vtor.resetViolations(); vtor.useProfile("p2"); vtor.validate(zoo); vlist = vtor.getViolations(); assertEquals(2, vlist.size()); vtor.resetViolations(); vtor.useProfile("p1"); vtor.validate(zoo); vlist = vtor.getViolations(); assertEquals(3, vlist.size()); vtor.resetViolations(); vtor.resetProfiles();
@Test void testTime() { Vtor vtor = new Vtor(); vtor.validate(new Tad()); assertTrue(vtor.hasViolations()); List<Violation> v = vtor.getViolations(); assertEquals(3, v.size()); assertEquals(TimeAfterConstraint.class, v.get(0).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()); } }
/** * 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 testHasViolations() throws Exception { Vtor vtor = new Vtor(); assertFalse(vtor.hasViolations(), "method must return false when violations list is empty"); vtor.addViolation(mock(Violation.class)); assertTrue(vtor.hasViolations(), "method must return true when add some violation"); }
/** * Validates provided context and value withing this constraint content. */ public void validateWithin(final ValidationContext vctx, final Object value) { vtor.validate(vctx, value, name); } }
@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()); }
@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")); }
@Test void testUseProfiles_withTwoDifferentValues() throws Exception { //given Vtor vtor = new Vtor(); vtor.useProfiles("testProfile1", "testProfile2"); assertEquals(2, vtor.enabledProfiles.size()); ArrayList<String> enabledProfileList = new ArrayList<>(vtor.enabledProfiles); assertTrue(enabledProfileList.contains("testProfile1"), "first element must be equal to first added profile"); assertTrue(enabledProfileList.contains("testProfile2"), "second element must be equal to second added profile"); //when vtor.useProfile(null); //then assertEquals(2, vtor.enabledProfiles.size()); }
@Test void testUseProfiles_withNullValue() throws Exception { Vtor vtor = new Vtor(); vtor.useProfiles(null); assertNull(vtor.enabledProfiles); }
@Test void testValidateAllProfilesByDefault() throws Exception { Vtor vtor = new Vtor(); vtor.setValidateAllProfilesByDefault(true); assertTrue(vtor.isValidateAllProfilesByDefault()); }
protected void prepareValidator() { if (vtor == null) { vtor = new 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)); } } } }
@Test void testAllProfiles() { assertTrue(vtor.matchProfiles(new String[]{Vtor.ALL_PROFILES})); assertFalse(vtor.matchProfiles(new String[]{Vtor.ALL_PROFILES, "someProfile"})); }
/** * Adds action violation. */ protected void addViolation(final String name, final Object invalidValue) { prepareValidator(); vtor.addViolation(new Violation(name, this, invalidValue)); }