@Test
public void testPutMembership() {

    TestAuditLogger alogger = new TestAuditLogger();
    ZMSImpl zmsImpl = getZmsImpl(alogger);
    Mockito.when(mockDomRsrcCtx.getApiName())
            .thenReturn("posttopleveldomain")
            .thenReturn("posttopleveldomain")
            .thenReturn("postsubdomain")
            .thenReturn("putrole")
            .thenReturn("putmembership");
    TopLevelDomain dom1 = createTopLevelDomainObject("MbrAddDom1",
            "Test Domain1", "testOrg", "user.user1");
    zmsImpl.postTopLevelDomain(mockDomRsrcCtx, auditRef, dom1);
    TopLevelDomain dom2 = createTopLevelDomainObject("coretech",
            "Test Domain2", "testOrg", adminUser);
    zmsImpl.postTopLevelDomain(mockDomRsrcCtx, auditRef, dom2);
    
    SubDomain subDom2 = createSubDomainObject("storage", "coretech",
            "Test Domain2", "testOrg", adminUser);
    zmsImpl.postSubDomain(mockDomRsrcCtx, "coretech", auditRef, subDom2);
    
    Role role1 = createRoleObject("MbrAddDom1", "Role1", null,
            "user.joe", "user.jane");
    zmsImpl.putRole(mockDomRsrcCtx, "MbrAddDom1", "Role1", auditRef, role1);
    
    Membership mbr = generateMembership("Role1", "user.doe");
    zmsImpl.putMembership(mockDomRsrcCtx, "MbrAddDom1", "Role1", "user.doe", auditRef, mbr);
    // check audit log msg for putRole
    boolean foundError = false;
    List<String> aLogMsgs = alogger.getLogMsgList();
    System.err.println("testPutMembership: Number of lines: " + aLogMsgs.size());
    for (String msg: aLogMsgs) {
        if (!msg.contains("WHAT-api=(putmembership)")) {
            continue;
        }
        int index = msg.indexOf("WHAT-details=(");
        assertTrue(index != -1, msg);
        int index2 = msg.indexOf("{\"member\": \"user.doe\", \"approved\": true, \"system-disabled\": 0}");
        assertTrue(index2 > index, msg);
        foundError = true;
        break;
    }
    assertTrue(foundError);
    
    aLogMsgs.clear();
    mbr = generateMembership("Role1", "coretech.storage");
    zmsImpl.putMembership(mockDomRsrcCtx, "MbrAddDom1", "Role1", "coretech.storage", auditRef, mbr);
    Role role = zmsImpl.getRole(mockDomRsrcCtx, "MbrAddDom1", "Role1", false, false, false);
    assertNotNull(role);
    List<RoleMember> members = role.getRoleMembers();
    assertNotNull(members);
    assertEquals(members.size(), 4);
    List<String> checkList = new ArrayList<>();
    checkList.add("user.joe");
    checkList.add("user.jane");
    checkList.add("user.doe");
    checkList.add("coretech.storage");
    checkRoleMember(checkList, members);
    foundError = false;
    System.err.println("testPutMembership: now Number of lines: " + aLogMsgs.size());
    for (String msg: aLogMsgs) {
        if (!msg.contains("WHAT-api=(putmembership)")) {
            continue;
        }
        int index = msg.indexOf("WHAT-details=(");
        assertTrue(index != -1, msg);
        int index2 = msg.indexOf("{\"member\": \"coretech.storage\", \"approved\": true, \"system-disabled\": 0}");
        assertTrue(index2 > index, msg);
        foundError = true;
        break;
    }
    assertTrue(foundError);
    // enable user validation for the test
    zmsImpl.userAuthority = new TestUserPrincipalAuthority();
    zmsImpl.validateUserRoleMembers = true;
    // valid users no exception
    mbr = generateMembership("role1", "user.joe");
    zmsImpl.putMembership(mockDomRsrcCtx, "MbrAddDom1", "role1", "user.joe", auditRef, mbr);
    // invalid user with exception
    mbr = generateMembership("role1", "user.john");
    try {
        zmsImpl.putMembership(mockDomRsrcCtx, "MbrAddDom1", "role1", "user.john", auditRef, mbr);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), ResourceException.BAD_REQUEST);
    }
    zmsImpl.deleteSubDomain(mockDomRsrcCtx, "coretech", "storage", auditRef);
    zmsImpl.deleteTopLevelDomain(mockDomRsrcCtx, "coretech", auditRef);
    zmsImpl.deleteTopLevelDomain(mockDomRsrcCtx,"MbrAddDom1", auditRef);
}