Search in sources :

Example 6 with MemberCandidate

use of cz.metacentrum.perun.core.api.MemberCandidate in project perun by CESNET.

the class VosManagerEntryIntegrationTest method getCompleteCandidatesFromGroup.

@Test
public void getCompleteCandidatesFromGroup() throws Exception {
    System.out.println(CLASS_NAME + "getCompleteCandidatesFromGroup");
    // create group and vo
    myVo = perun.getVosManagerBl().createVo(sess, myVo);
    Group group = new Group("testGroup", "testingGroup");
    Group returnedGroup = perun.getGroupsManager().createGroup(sess, myVo, group);
    // prepare second extSource
    ExtSource extSource = new ExtSource("testExtSource", ExtSourcesManager.EXTSOURCE_INTERNAL);
    extSource = perun.getExtSourcesManagerBl().createExtSource(sess, extSource, null);
    // prepare users to be returned by getCompleteCandidates
    Candidate candidate = prepareCandidateWithExtSource("Jan", es);
    User userToContain1 = perun.getUsersManagerBl().getRichUser(sess, perun.getUsersManagerBl().createUser(sess, candidate));
    candidate = prepareCandidateWithExtSource("Jana", es);
    User userToContain2 = perun.getUsersManagerBl().getRichUser(sess, perun.getUsersManagerBl().createUser(sess, candidate));
    candidate = prepareCandidateWithExtSource("Josef", es);
    User userNotToContain1 = perun.getUsersManagerBl().getRichUser(sess, perun.getUsersManagerBl().createUser(sess, candidate));
    candidate = prepareCandidateWithExtSource("Jan", extSource);
    RichUser userNotToContain2 = perun.getUsersManagerBl().getRichUser(sess, perun.getUsersManagerBl().createUser(sess, candidate));
    candidate = prepareCandidateWithExtSource("Jana", extSource);
    RichUser userToContain3 = perun.getUsersManagerBl().getRichUser(sess, perun.getUsersManagerBl().createUser(sess, candidate));
    Member member = perun.getMembersManagerBl().createMember(sess, myVo, candidate);
    List<MemberCandidate> completeCandidates = perun.getVosManagerBl().getCompleteCandidates(sess, myVo, returnedGroup, null, "jan", Arrays.asList(es));
    List<RichUser> usersOfCompleteCandidates = completeCandidates.stream().map(MemberCandidate::getRichUser).collect(Collectors.toList());
    assertEquals("Three users should have been returned.", 3, usersOfCompleteCandidates.size());
    assertTrue("User should've been returned.", usersOfCompleteCandidates.contains(userToContain1));
    assertTrue("User should've been returned.", usersOfCompleteCandidates.contains(userToContain2));
    assertTrue("User should've been returned.", usersOfCompleteCandidates.contains(userToContain3));
    assertFalse("User shouldn't have been returned.", usersOfCompleteCandidates.contains(userNotToContain1));
    assertFalse("User shouldn't have been returned.", usersOfCompleteCandidates.contains(userNotToContain2));
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) MemberCandidate(cz.metacentrum.perun.core.api.MemberCandidate) Group(cz.metacentrum.perun.core.api.Group) RichUser(cz.metacentrum.perun.core.api.RichUser) User(cz.metacentrum.perun.core.api.User) MemberCandidate(cz.metacentrum.perun.core.api.MemberCandidate) RichUser(cz.metacentrum.perun.core.api.RichUser) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Member(cz.metacentrum.perun.core.api.Member) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 7 with MemberCandidate

use of cz.metacentrum.perun.core.api.MemberCandidate in project perun by CESNET.

the class VosManagerBlImpl method createMemberCandidates.

/**
 * Creates MemberCandidates for given RichUsers, vo, group and candidates. If the given group is not null
 * then to all members who are in this group is assigned the sourceGroupId of the given group.
 * The given group can be null.
 *
 * @param sess session
 * @param users users
 * @param group group
 * @param candidates candidates
 * @param attrNames names of attributes that will be returned
 * @return list of MemberCandidates for given RichUsers, group and candidates
 * @throws InternalErrorException internal error
 */
public List<MemberCandidate> createMemberCandidates(PerunSession sess, List<RichUser> users, Vo vo, Group group, List<Candidate> candidates, List<String> attrNames) {
    List<MemberCandidate> memberCandidates = new ArrayList<>();
    Set<Integer> allUsersIds = new HashSet<>();
    int userId;
    // try to find matching RichUser for candidates
    for (Candidate candidate : candidates) {
        MemberCandidate mc = new MemberCandidate();
        try {
            User user = getPerunBl().getUsersManagerBl().getUserByUserExtSources(sess, candidate.getUserExtSources());
            userId = user.getId();
            // check if user already exists in the list
            if (!allUsersIds.contains(userId)) {
                RichUser richUser = getPerunBl().getUsersManagerBl().convertUserToRichUserWithAttributesByNames(sess, user, attrNames);
                mc.setRichUser(richUser);
                memberCandidates.add(mc);
            }
            allUsersIds.add(userId);
        } catch (UserNotExistsException ignored) {
            // no matching user was found
            mc.setCandidate(candidate);
            memberCandidates.add(mc);
        }
    }
    List<RichUser> foundRichUsers = memberCandidates.stream().map(MemberCandidate::getRichUser).collect(Collectors.toList());
    // create MemberCandidates for RichUsers without candidate
    for (RichUser richUser : users) {
        if (!foundRichUsers.contains(richUser)) {
            MemberCandidate mc = new MemberCandidate();
            mc.setRichUser(richUser);
            memberCandidates.add(mc);
        }
    }
    // try to find member for MemberCandidates with not null RichUser
    for (MemberCandidate memberCandidate : memberCandidates) {
        if (memberCandidate.getRichUser() != null) {
            Member member = null;
            try {
                member = getPerunBl().getMembersManagerBl().getMemberByUser(sess, vo, memberCandidate.getRichUser());
                if (group != null) {
                    member = getPerunBl().getGroupsManagerBl().getGroupMemberById(sess, group, member.getId());
                }
            } catch (MemberNotExistsException ignored) {
            // no matching VO member was found
            } catch (NotGroupMemberException e) {
            // not matching Group member was found
            }
            // put null or matching member
            memberCandidate.setMember(member);
        }
    }
    return memberCandidates;
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) MemberCandidate(cz.metacentrum.perun.core.api.MemberCandidate) User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) RichUser(cz.metacentrum.perun.core.api.RichUser) ArrayList(java.util.ArrayList) NotGroupMemberException(cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException) MemberCandidate(cz.metacentrum.perun.core.api.MemberCandidate) Member(cz.metacentrum.perun.core.api.Member) HashSet(java.util.HashSet)

Aggregations

MemberCandidate (cz.metacentrum.perun.core.api.MemberCandidate)7 RichUser (cz.metacentrum.perun.core.api.RichUser)5 User (cz.metacentrum.perun.core.api.User)5 Test (org.junit.Test)4 Candidate (cz.metacentrum.perun.core.api.Candidate)3 ExtSource (cz.metacentrum.perun.core.api.ExtSource)3 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)3 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)2 BanOnVo (cz.metacentrum.perun.core.api.BanOnVo)2 Group (cz.metacentrum.perun.core.api.Group)2 Member (cz.metacentrum.perun.core.api.Member)2 Vo (cz.metacentrum.perun.core.api.Vo)2 ArrayList (java.util.ArrayList)2 MemberNotExistsException (cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException)1 NotGroupMemberException (cz.metacentrum.perun.core.api.exceptions.NotGroupMemberException)1 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)1 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)1 HashSet (java.util.HashSet)1