@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()); }
Vtor vtor = new Vtor(); vtor.setValidateAllProfilesByDefault(false); vtor.useProfile("default"); Too too = new Too(); vtor.useProfile("p1"); vtor.validate(too); vlist = vtor.getViolations(); vtor.useProfile("p2"); vtor.validate(too); vlist = vtor.getViolations(); vtor.useProfile("p3"); vtor.validate(too); vlist = vtor.getViolations();
@Test void testUseProfile_withNullValue() throws Exception { Vtor vtor = new Vtor(); vtor.useProfile(null); assertNull(vtor.enabledProfiles); }
@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 testDefaultProfile() { //when vtor.useProfile(Vtor.DEFAULT_PROFILE); //then assertTrue(vtor.matchProfiles(null), "result must be true when match null value"); assertTrue(vtor.matchProfiles(new String[]{"testProfile2", "", "testProfile3"}), "result must be true when match a list with empty profile"); assertFalse(vtor.matchProfiles(new String[]{"testProfile2", "testProfile3"}), "result must be false when match a list without empty profile"); }
@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()); }
vtor.useProfile("default"); vtor.validate(zoo); vlist = vtor.getViolations(); vtor.useProfile("p2"); vtor.validate(zoo); vlist = vtor.getViolations(); vtor.useProfile("p1"); vtor.validate(zoo); vlist = vtor.getViolations(); vtor.useProfile("p1"); vtor.validate(zoo); vlist = vtor.getViolations(); vtor.useProfile("p2"); vtor.validate(zoo); vlist = vtor.getViolations();
@Test void testMatchProfilesAgainstSomeProfile() { //when vtor.useProfile("testProfile1"); //then assertFalse(vtor.matchProfiles(null), "result must be false when match null value"); assertFalse(vtor.matchProfiles(new String[]{"testProfile2", "testProfile3"}), "result must be false when match list of profiles without any assigned profiles"); assertTrue(vtor.matchProfiles(new String[]{"testProfile1", "testProfile2", "testProfile3"}), "result must be true when match list of profiles with one assigned profile"); assertTrue(vtor.matchProfiles(new String[]{"testProfile1", "testProfile2", "testProfile3"}), "result must be true when match list of profiles with one assigned profile"); assertTrue(vtor.matchProfiles(new String[]{"testProfile1", "testProfile2", "testProfile3"}), "result must be true when match list of profiles with one assigned profile"); assertTrue(vtor.matchProfiles(new String[]{"testProfile1", "testProfile2", "testProfile3"}), "result must be true when match list of profiles with one assigned profile"); assertTrue(vtor.matchProfiles(new String[]{"testProfile2", "testProfile1", "testProfile3"}), "result must be true when match unordered list of profiles with one assigned profile"); assertFalse(vtor.matchProfiles(new String[]{"+testProfile2", "testProfile1", "testProfile3"}), "result must be false when match a list of profiles with one wrong mandatory profile"); assertFalse(vtor.matchProfiles(new String[]{"testProfile2", "-testProfile1", "testProfile3"}), "result must be false when match a list of profiles with one assigned profile which was marked as optional"); }