fix up path validation

This commit is contained in:
Mengyi Zhou 2015-07-08 13:58:11 +08:00
parent f26d94b355
commit 0939b5db56
2 changed files with 17 additions and 6 deletions

View file

@ -32,7 +32,7 @@ public class DefaultGroupValidator implements GroupValidator {
throw new ValidationException("Group with null value cannot be persisted.");
}
if (!validateGroupSlbs(group))
throw new ValidationException("Virtual server cannot be found.");
throw new ValidationException("Virtual server has invalid data.");
}
@Override
@ -65,14 +65,13 @@ public class DefaultGroupValidator implements GroupValidator {
groupPaths.add(vs.getId() + gs.getPath());
}
for (Long virtualServerId : virtualServerIds) {
Set<String> paths = new HashSet<>();
for (GroupSlb groupSlb : slbRepository.listGroupSlbsByVirtualServer(virtualServerId)) {
if (groupSlb.getGroupId().equals(group.getId()))
continue;
if (paths.contains(groupSlb.getPath()))
if (groupPaths.contains(virtualServerId + groupSlb.getPath()))
return false;
else
paths.add(groupSlb.getPath());
groupPaths.add(virtualServerId + groupSlb.getPath());
}
}
return true;

View file

@ -200,9 +200,21 @@ public class ValidationTest extends AbstractSpringTest {
}
group = new Group().setName("testDuplicatePath").setAppId("000000")
.addGroupSlb(new GroupSlb().setSlbId(1L).setPath("/test").setVirtualServer(slb.getVirtualServers().get(0)))
.addGroupSlb(new GroupSlb().setSlbId(1L).setPath("/test1").setVirtualServer(slb.getVirtualServers().get(0)));
.addGroupSlb(new GroupSlb().setSlbId(1L).setPath("/test").setVirtualServer(slb.getVirtualServers().get(0)));
groupRepository.add(group);
group = new Group().setName("testDuplicatePath1").setAppId("000000")
.addGroupSlb(new GroupSlb().setSlbId(1L).setPath("/test").setVirtualServer(slb.getVirtualServers().get(0)));
try {
groupRepository.add(group);
Assert.assertTrue(false);
} catch (Exception e) {
Assert.assertTrue(e instanceof ValidationException);
System.out.println("Expected cause: duplicate path was found; real cause: " + e.getMessage());
}
group = groupRepository.get("testDuplicatePath");
groupRepository.update(group);
}
private Slb prepareSlb() throws Exception {