@Test
public void testIsReservedGroup() throws Exception {

    UserGroup ug1 = _repo.save(newUserGroup());
    UserGroup ug2 = _repo.save(newUserGroup());
    UserGroup ug3 = _repo.save(newUserGroup());
    UserGroup ug4 = _repo.save(newUserGroup());
    Map<ReservedGroup, Integer> normalIds = new HashMap<ReservedGroup, Integer>();
    // Set all reserveGroup ids so that they are not the the same as those in the other groups
    for (ReservedGroup reservedGroup : ReservedGroup.values()) {
        normalIds.put(reservedGroup, reservedGroup.getId());
        setReservedGroupId(ug1.getId().getGroupId() - 10 + reservedGroup.getId(), reservedGroup);
    }
    try {
        final int groupId = ug3.getId().getGroupId();
        final ReservedGroup group = ReservedGroup.intranet;
        setReservedGroupId(groupId, group);
        setReservedGroupId(groupId, group);
        setReservedGroupId(groupId, group);
        setReservedGroupId(groupId, group);
        List<Integer> found = _repo.findGroupIds(isReservedGroup(true));
        assertFalse(found.contains(ug1.getId().getGroupId()));
        assertFalse(found.contains(ug2.getId().getGroupId()));
        assertTrue(found.contains(groupId));
        assertFalse(found.contains(ug4.getId().getGroupId()));
        found = _repo.findGroupIds(isReservedGroup(false));
        assertTrue(found.contains(ug1.getId().getGroupId()));
        assertTrue(found.contains(ug2.getId().getGroupId()));
        assertFalse(found.contains(groupId));
        assertTrue(found.contains(ug4.getId().getGroupId()));
    } finally {
        for (ReservedGroup reservedGroup : ReservedGroup.values()) {
            setReservedGroupId(normalIds.get(reservedGroup), reservedGroup);
        }
    }
}