### Question:
GroupValidator extends BeanValidator { @Override public void validate(ValidationContext validationContext) { Group group = (Group) validationContext.getBindContext().getValidatorArg("group"); String oldName = group.getName(); String newName = (String) validationContext.getProperty().getValue(); boolean beanValidationFailed = beanValidationFails(validationContext, new Group(newName)); if (!beanValidationFailed && nameWasChanged(newName, oldName)) { checkForUniqueness(validationContext, newName); } } GroupValidator(GroupService groupService); @Override void validate(ValidationContext validationContext);  }### Answer:
@Test public void tooLongNameIsInvalid() { String longName = RandomStringUtils.randomAlphanumeric(255) + "name"; givenBindContextReturnsGroupAndNewName(longName, createGroupWithIdAndName(0, "")); validator.validate(context); verify(context).setInvalid(); }
@Test public void emptyGroupNameIsInvalid() { givenBindContextReturnsGroupAndNewName("", createGroupWithIdAndName(0, "")); validator.validate(context); verify(context).setInvalid(); }
@Test public void duplicatedNameIsInvalid() throws Exception { Group group1 = createGroupWithId(1); Group group2 = createGroupWithId(2); group1.setName("group1"); group2.setName("someGroup"); givenBindContextReturnsGroupAndNewName(group1.getName(), group2); storeGroupsInMockedDb(group1); validator.validate(context); verify(context).setInvalid(); }
@Test public void duplicatedGroupNameWithAnotherCaseIsInvalid() throws Exception { Group group1 = createGroupWithId(1); Group group2 = createGroupWithId(2); group1.setName("group1"); givenBindContextReturnsGroupAndNewName("GroUp1", group2); storeGroupsInMockedDb(group1); validator.validate(context); verify(context).setInvalid(); }
@Test public void duplicatedGroupNamesWithAnotherCaseAndTwoWordsIsInvalid() throws Exception { Group group1 = createGroupWithIdAndName(1, "Group test"); Group group2 = createGroupWithIdAndName(2, "Some name"); givenBindContextReturnsGroupAndNewName(group1.getName().toLowerCase(), group2); storeGroupsInMockedDb(group1); validator.validate(context); verify(context).setInvalid(); }
@Test public void changeGroupNameToTheSameIsValid() throws Exception { Group group = createGroupWithIdAndName(1, "myGroup"); givenBindContextReturnsGroupAndNewName(group.getName(), group); storeGroupsInMockedDb(group); validator.validate(context); verify(context, never()).setInvalid(); }
@Test public void changeGroupNameFromNullIsValid() throws Exception { Group group = createGroupWithIdAndName(1, null); givenBindContextReturnsGroupAndNewName("new group", group); validator.validate(context); verify(context, never()).setInvalid(); }
@Test public void changeGroupNameFromNullToDuplicatedIsInvalid() throws Exception { Group group = createGroupWithIdAndName(1, null); Group group2 = createGroupWithIdAndName(2, "myGroup"); givenBindContextReturnsGroupAndNewName(group2.getName(), group); storeGroupsInMockedDb(group2); validator.validate(context); verify(context).setInvalid(); }
@Test public void changeGroupNameToTheSameWithSpacesIsValid() throws Exception { Group group = createGroupWithIdAndName(1, "myGroup"); givenBindContextReturnsGroupAndNewName(" " + group.getName() + " ", group); storeGroupsInMockedDb(group); validator.validate(context); verify(context, never()).setInvalid(); }
@Test public void twoUniqueNamesAreValid() throws Exception { Group group1 = createGroupWithId(1); Group group2 = createGroupWithId(2); group1.setName("group1"); givenBindContextReturnsGroupAndNewName("group2", group2); storeGroupsInMockedDb(group1); validator.validate(context); verify(context, never()).setInvalid(); }