protected void prepareValidator() { if (vtor == null) { vtor = new Vtor(); } }
/** * Static constructor for fluent usage. */ public static Vtor create() { return new Vtor(); }
@BeforeEach void setup() { vtor = new Vtor(); }
@BeforeEach void setup() { //given vtor = new Vtor(); vtor.resetProfiles(); }
@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 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); }
@Test void testUseProfile_withTwoDifferentValues() throws Exception { //given Vtor vtor = new Vtor(); vtor.useProfile("testProfile"); assertEquals(1, vtor.enabledProfiles.size()); assertTrue(new ArrayList<>(vtor.enabledProfiles).contains("testProfile"), "first element of enabled profiles must be testProfile when use testProfile"); //when vtor.useProfile(null); //then assertEquals(1, vtor.enabledProfiles.size()); }
@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 testSetSeverity() throws Exception { Vtor vtor = new Vtor(); vtor.setSeverity(1); assertEquals(vtor.severity, 1); }
@Test void testUseProfile_withNullValue() throws Exception { Vtor vtor = new Vtor(); vtor.useProfile(null); assertNull(vtor.enabledProfiles); }
@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()); }
@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 testAddViolation_withNullValue() throws Exception { Vtor vtor = new Vtor(); vtor.addViolation(null); assertNull(vtor.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"); }
@Test void testAddViolation_withTwoDifferentValues() throws Exception { //given Vtor vtor = new Vtor(); Violation violation1 = mock(Violation.class); vtor.addViolation(violation1); Violation violation2 = mock(Violation.class); vtor.addViolation(violation2); assertEquals(2, vtor.getViolations().size()); assertEquals(violation1, vtor.getViolations().get(0)); assertEquals(violation2, vtor.getViolations().get(1)); //when vtor.addViolation(null); //then assertEquals(2, vtor.getViolations().size()); }
@Test void testAsJoyAction() { Vtor vtor = new Vtor(); vtor.useProfiles(Vtor.DEFAULT_PROFILE, "register"); vtor.validate(new Too()); List<Violation> violations = vtor.getViolations(); assertNull(violations); }
@Test void testDefaultProfiles() { Vtor vtor = new Vtor(); vtor.setValidateAllProfilesByDefault(false); Zoo zoo = new Zoo(); vtor.validate(zoo); List<Violation> vlist = vtor.getViolations(); assertEquals(1, vlist.size()); vtor.resetViolations(); vtor.useProfile("default"); vtor.validate(zoo); vlist = vtor.getViolations(); assertEquals(1, vlist.size()); assertEquals("ccc", vlist.get(0).getName()); vtor.resetViolations(); vtor.resetProfiles(); vtor.useProfile("p2"); vtor.validate(zoo); vlist = vtor.getViolations(); assertEquals(1, vlist.size()); assertEquals("bbb", vlist.get(0).getName()); }
@Test void testAnn() { Vtor vtor = new Vtor(); vtor.validate(new Woo()); List<Violation> v = vtor.getViolations(); assertEquals(1, v.size()); vtor.resetViolations(); vtor.useProfiles("p1", "p2"); vtor.validate(new Woo()); v = vtor.getViolations(); assertEquals(2, v.size()); vtor.resetViolations(); vtor.useProfiles("default", "p1", "p2"); vtor.validate(new Woo()); v = vtor.getViolations(); assertEquals(3, v.size()); }
@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()); } }