userTO.getMemberships().clear();
userTO.getDerivedAttributes().clear();
userTO.getVirtualAttributes().clear();
userTO.addDerivedAttribute(attributeTO("csvuserid", null));
MembershipTO memb12 = new MembershipTO();
memb12.setRoleId(12L);
memb12.addAttribute(attributeTO("postalAddress", "postalAddress"));
userTO.addMembership(memb12);
MembershipTO memb13 = new MembershipTO();
memb13.setRoleId(13L);
userTO.addMembership(memb13);
userTO.addResource(RESOURCE_NAME_LDAP);
UserTO actual = createUser(userTO);
assertNotNull(actual);
assertEquals(2, actual.getMemberships().size());
ConnObjectTO connObjectTO = readConnectorObject(RESOURCE_NAME_LDAP, actual.getId());
assertNotNull(connObjectTO);
AttributeTO postalAddress = connObjectTO.getAttributeMap().get("postalAddress");
assertNotNull(postalAddress);
assertEquals(1, postalAddress.getValues().size());
assertEquals("postalAddress", postalAddress.getValues().get(0));
AttributeTO title = connObjectTO.getAttributeMap().get("title");
assertNotNull(title);
assertEquals(2, title.getValues().size());
assertTrue(title.getValues().contains("r12") && title.getValues().contains("r13"));
// -----------------------------------
// Remove the first membership and check for membership attr propagation and role attr propagation
// -----------------------------------
UserMod userMod = new UserMod();
userMod.setId(actual.getId());
MembershipTO membershipTO = actual.getMemberships().get(0).getRoleId() == 12L
? actual.getMemberships().get(0)
: actual.getMemberships().get(1);
userMod.addMembershipToBeRemoved(membershipTO.getId());
actual = userService.update(userMod.getId(), userMod);
assertNotNull(actual);
assertEquals(1, actual.getMemberships().size());