@Test
public void testApplyMembershipChanges() {

    List<RoleMember> incomingMembers = new ArrayList<>(3);
    List<RoleMember> originalMembers = new ArrayList<>(5);
    Timestamp thirtyDayExpiry = Timestamp.fromMillis(System.currentTimeMillis()
            + TimeUnit.MILLISECONDS.convert(30, TimeUnit.DAYS) + TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS));
    Timestamp currentExpiry = Timestamp.fromMillis(System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(4, TimeUnit.DAYS));
    incomingMembers.add(createRoleMember("user.user1", true, thirtyDayExpiry, true));
    incomingMembers.add(createRoleMember("user.user2", true, thirtyDayExpiry, true));
    incomingMembers.add(createRoleMember("user.user4", false, thirtyDayExpiry, true));
    Role incomingRole = new Role().setName("role1").setRoleMembers(incomingMembers);
    originalMembers.add(createRoleMember("user.user1", true, currentExpiry, true));
    originalMembers.add(createRoleMember("user.user2", true, currentExpiry, true));
    originalMembers.add(createRoleMember("user.user3", true, currentExpiry, true));
    originalMembers.add(createRoleMember("user.user4", true, currentExpiry, true));
    originalMembers.add(createRoleMember("user.user5", true, currentExpiry, true));
    Role originalRole = new Role().setName("role1").setRoleMembers(originalMembers);
    Role updatedRole = new Role().setName("role1");
    zms.dbService.applyMembershipChanges(updatedRole, originalRole, incomingRole, auditRef);
    assertEquals(updatedRole.getRoleMembers().size(), 3);
    List<String> expectedMemberNames = Arrays.asList("user.user1", "user.user2", "user.user4");
    for (RoleMember roleMember : updatedRole.getRoleMembers()) {
        assertTrue(roleMember.getApproved());
        assertEquals(roleMember.getExpiration(), thirtyDayExpiry);
        assertTrue(expectedMemberNames.contains(roleMember.getMemberName()));
        assertEquals(roleMember.getAuditRef(), auditRef);
        if (roleMember.getMemberName().equals("user.user4")) {
            assertFalse(roleMember.getActive());
        } else {
            assertTrue(roleMember.getActive());
        }
    }
    originalRole.setAuditEnabled(true);
    updatedRole.setRoleMembers(null);
    List<RoleMember> noactionMembers = zms.dbService.applyMembershipChanges(updatedRole, originalRole, incomingRole, auditRef);
    assertEquals(noactionMembers.size(), 2);
    int noActChecked = 0;
    for (RoleMember roleMember : noactionMembers) {
        switch (roleMember.getMemberName()) {
            case "user.user3":
            case "user.user5":
                noActChecked += 1;
                break;
        }
    }
    assertEquals(noActChecked, 2);
    assertEquals(updatedRole.getRoleMembers().size(), 3);
    for (RoleMember roleMember : updatedRole.getRoleMembers()) {
        assertFalse(roleMember.getApproved());
        assertEquals(roleMember.getExpiration(), thirtyDayExpiry);
        assertTrue(expectedMemberNames.contains(roleMember.getMemberName()));
        assertEquals(roleMember.getAuditRef(), auditRef);
        if (roleMember.getMemberName().equals("user.user4")) {
            assertFalse(roleMember.getActive());
        } else {
            assertTrue(roleMember.getActive());
        }
    }
}