@Test
public void testPutGroupMembership() {

    final String domainName = "put-group-mbr";
    final String groupName = "group1";
    Mockito.when(mockDomRsrcCtx.getApiName())
            .thenReturn("putserviceidentity")
            .thenReturn("posttopleveldomain")
            .thenReturn("posttopleveldomain")
            .thenReturn("putserviceidentity")
            .thenReturn("postsubdomain")
            .thenReturn("putgroup").thenReturn("putgroup").thenReturn("putgroup").thenReturn("putgroup") // called 4 times in group api
            .thenReturn("putgroupmembership");
    TestAuditLogger alogger = new TestAuditLogger();
    ZMSImpl zmsImpl = getZmsImpl(alogger);
    TopLevelDomain dom1 = createTopLevelDomainObject(domainName, "Test Domain1", "testOrg", "user.user1");
    zmsImpl.postTopLevelDomain(mockDomRsrcCtx, auditRef, dom1);
    TopLevelDomain dom2 = createTopLevelDomainObject("coretech", "Test Domain2", "testOrg", adminUser);
    zmsImpl.postTopLevelDomain(mockDomRsrcCtx, auditRef, dom2);
    ServiceIdentity service = createServiceObject("coretech", "storage",
            "http://localhost", "/usr/bin/java", "root", "users", "host1");
    zms.putServiceIdentity(mockDomRsrcCtx, "coretech", "storage", auditRef, service);
    SubDomain subDom2 = createSubDomainObject("storage", "coretech", "Test Domain2", "testOrg", adminUser);
    zmsImpl.postSubDomain(mockDomRsrcCtx, "coretech", auditRef, subDom2);
    Group group1 = createGroupObject(domainName, groupName, "user.joe", "user.jane");
    zmsImpl.putGroup(mockDomRsrcCtx, domainName, groupName, auditRef, group1);
    GroupMembership mbr = generateGroupMembership(groupName, "user.doe");
    zmsImpl.putGroupMembership(mockDomRsrcCtx, domainName, groupName, "user.doe", auditRef, mbr);
    // check audit log msg for putGroup
    boolean foundError = false;
    List<String> aLogMsgs = alogger.getLogMsgList();
    System.err.println("testPutGroupMembership: Number of lines: " + aLogMsgs.size());
    for (String msg: aLogMsgs) {
        if (!msg.contains("WHAT-api=(putgroupmembership)")) {
            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 = generateGroupMembership(groupName, "coretech.storage");
    zmsImpl.putGroupMembership(mockDomRsrcCtx, domainName, groupName, "coretech.storage", auditRef, mbr);
    Group group = zmsImpl.getGroup(mockDomRsrcCtx, domainName, groupName, false, false);
    assertNotNull(group);
    List<GroupMember> members = group.getGroupMembers();
    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");
    checkGroupMember(checkList, members);
    foundError = false;
    System.err.println("testGroupPutMembership: now Number of lines: " + aLogMsgs.size());
    for (String msg: aLogMsgs) {
        if (!msg.contains("WHAT-api=(putgroupmembership)")) {
            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 = generateGroupMembership(groupName, "user.joe");
    zmsImpl.putGroupMembership(mockDomRsrcCtx, domainName, groupName, "user.joe", auditRef, mbr);
    // invalid user with exception
    mbr = generateGroupMembership("group1", "user.john");
    try {
        zmsImpl.putGroupMembership(mockDomRsrcCtx, domainName, groupName, "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, domainName, auditRef);
}