@Test
public void testSetRoleMemberExpiration() {

    Authority savedAuthority = zms.userAuthority;
    zms.userAuthority = null;
    // with authority null we always get no changes
    Authority authority = Mockito.mock(Authority.class);
    Date testDate = new Date();
    Mockito.when(authority.getDateAttribute("user.john", "elevated-clearance"))
            .thenReturn(testDate);
    Mockito.when(authority.getDateAttribute("user.jane", "elevated-clearance"))
            .thenReturn(testDate);
    Mockito.when(authority.getDateAttribute("user.joe", "elevated-clearance"))
            .thenReturn(null);
    Set<String> dateAttrSet = new HashSet<>();
    dateAttrSet.add("elevated-clearance");
    Mockito.when(authority.dateAttributesSupported()).thenReturn(dateAttrSet);
    zms.userAuthority = authority;
    // add a role with an elevated clearance option
    final String domainName = "userexpirydomain";
    TopLevelDomain dom1 = createTopLevelDomainObject(domainName,
            "Test Domain1", "testOrg", "user.user1");
    zms.postTopLevelDomain(mockDomRsrcCtx, auditRef, dom1);
    final String roleName = "audit-role";
    Role role = createRoleObject(domainName, roleName, null, null);
    role.setUserAuthorityExpiration("elevated-clearance");
    zms.putRole(mockDomRsrcCtx, domainName, roleName, auditRef, role);
    // add a valid member who should get the expiry date
    Membership mbr = new Membership().setMemberName("user.john");
    zms.putMembership(mockDomRsrcCtx, domainName, roleName, "user.john", auditRef, mbr);
    Membership mbrResult = zms.getMembership(mockDomRsrcCtx, domainName, roleName, "user.john", null);
    assertNotNull(mbrResult);
    assertEquals(mbrResult.getMemberName(), "user.john");
    assertNotNull(mbrResult.getExpiration());
    assertEquals(mbrResult.getExpiration().millis(), testDate.getTime());
    // user with no expiry
    mbr = new Membership().setMemberName("user.joe");
    try {
        zms.putMembership(mockDomRsrcCtx, domainName, roleName, "user.joe", auditRef, mbr);
        fail();
    } catch (ResourceException ex) {
        assertTrue(ex.getMessage().contains("User does not have required user authority expiry configured"));
    }
    // service user should be added ok since service user is not processed
    // by user authority
    mbr = new Membership().setMemberName("userexpirydomain.api");
    zms.putMembership(mockDomRsrcCtx, domainName, roleName, "userexpirydomain.api", auditRef, mbr);
    mbrResult = zms.getMembership(mockDomRsrcCtx, domainName, roleName, "userexpirydomain.api", null);
    assertNotNull(mbrResult);
    zms.userAuthority = savedAuthority;
    zms.deleteTopLevelDomain(mockDomRsrcCtx, domainName, auditRef);
}