@Test
public void testUpdateUserAuthorityExpiryGroupMember() {

    Authority savedAuthority = zms.dbService.zmsConfig.getUserAuthority();
    Authority authority = Mockito.mock(Authority.class);
    Date currentDate = new Date();
    Timestamp authorityDate = Timestamp.fromDate(currentDate);
    Mockito.when(authority.getDateAttribute("user.john", "elevated-clearance"))
            .thenReturn(currentDate);
    Mockito.when(authority.getDateAttribute("user.jane", "elevated-clearance"))
            .thenReturn(currentDate);
    Mockito.when(authority.getDateAttribute("user.joe", "elevated-clearance"))
            .thenReturn(null);
    zms.dbService.zmsConfig.setUserAuthority(authority);
    // service users are not processed
    GroupMember groupMember = new GroupMember().setMemberName("sports.api");
    assertFalse(zms.dbService.updateUserAuthorityExpiry(groupMember, "elevated-clearance"));
    // user.joe - no expiry setting
    groupMember = new GroupMember().setMemberName("user.joe");
    assertTrue(zms.dbService.updateUserAuthorityExpiry(groupMember, "elevated-clearance"));
    assertNotNull(groupMember.getExpiration());
    // we'll change if the expiry date is in the future
    Timestamp expiryDate = Timestamp.fromMillis(System.currentTimeMillis() + 1000000);
    groupMember.setExpiration(expiryDate);
    assertTrue(zms.dbService.updateUserAuthorityExpiry(groupMember, "elevated-clearance"));
    assertNotEquals(groupMember.getExpiration(), expiryDate);
    // we will not change if the entry is already expired
    expiryDate = Timestamp.fromMillis(System.currentTimeMillis() - 1000000);
    groupMember.setExpiration(expiryDate);
    assertFalse(zms.dbService.updateUserAuthorityExpiry(groupMember, "elevated-clearance"));
    assertEquals(groupMember.getExpiration(), expiryDate);
    // now let's test a user with valid authority expiry date
    // if the user doesn't have an expiry, we'll default to the value
    // returned by the user authority
    groupMember = new GroupMember().setMemberName("user.jane");
    assertTrue(zms.dbService.updateUserAuthorityExpiry(groupMember, "elevated-clearance"));
    assertNotNull(groupMember.getExpiration());
    assertEquals(groupMember.getExpiration(), authorityDate);
    // if the value matches to our user authority value then no change
    groupMember.setExpiration(authorityDate);
    assertFalse(zms.dbService.updateUserAuthorityExpiry(groupMember, "elevated-clearance"));
    assertNotNull(groupMember.getExpiration());
    assertEquals(groupMember.getExpiration(), authorityDate);
    // if no match then we change the value
    groupMember.setExpiration(Timestamp.fromMillis(System.currentTimeMillis() - 2000000));
    assertTrue(zms.dbService.updateUserAuthorityExpiry(groupMember, "elevated-clearance"));
    assertNotNull(groupMember.getExpiration());
    assertEquals(groupMember.getExpiration(), authorityDate);
    zms.dbService.zmsConfig.setUserAuthority(savedAuthority);
}