Search in sources :

Example 1 with CandidateSync

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

the class ExtSourcesManagerBlImpl method getCandidate.

@Override
public CandidateSync getCandidate(PerunSession perunSession, Map<String, String> subjectData, ExtSource source, String login) {
    if (login == null || login.isEmpty())
        throw new InternalErrorException("Login can't be empty or null.");
    if (subjectData == null || subjectData.isEmpty())
        throw new InternalErrorException("Subject data can't be null or empty, at least login there must exists.");
    // New Canddate
    CandidateSync candidateSync = new CandidateSync();
    // Prepare userExtSource object
    UserExtSource userExtSource = new UserExtSource();
    userExtSource.setExtSource(source);
    userExtSource.setLogin(login);
    // If first name of candidate is not in format of name, set null instead
    candidateSync.setFirstName(subjectData.get("firstName"));
    if (candidateSync.getFirstName() != null) {
        Matcher name = namePattern.matcher(candidateSync.getFirstName());
        if (!name.matches())
            candidateSync.setFirstName(null);
    }
    // If last name of candidate is not in format of name, set null instead
    candidateSync.setLastName(subjectData.get("lastName"));
    if (candidateSync.getLastName() != null) {
        Matcher name = namePattern.matcher(candidateSync.getLastName());
        if (!name.matches())
            candidateSync.setLastName(null);
    }
    candidateSync.setMiddleName(subjectData.get("middleName"));
    candidateSync.setTitleAfter(subjectData.get("titleAfter"));
    candidateSync.setTitleBefore(subjectData.get("titleBefore"));
    // Set service user
    if (subjectData.get("isServiceUser") == null) {
        candidateSync.setServiceUser(false);
    } else {
        String isServiceUser = subjectData.get("isServiceUser");
        candidateSync.setServiceUser(isServiceUser.equals("true"));
    }
    // Set sponsored user
    if (subjectData.get("isSponsoredUser") == null) {
        candidateSync.setSponsoredUser(false);
    } else {
        String isSponsoredUser = subjectData.get("isSponsoredUser");
        candidateSync.setSponsoredUser(isSponsoredUser.equals("true"));
    }
    // Filter attributes
    Map<String, String> attributes = new HashMap<>();
    for (String attrName : subjectData.keySet()) {
        // FIXME volat metody z attributesManagera nez kontrolovat na zacatek jmena
        if (attrName.startsWith(AttributesManager.NS_MEMBER_ATTR) || attrName.startsWith(AttributesManager.NS_USER_ATTR)) {
            attributes.put(attrName, subjectData.get(attrName));
        }
    }
    candidateSync.setRichUserExtSource(new RichUserExtSource(userExtSource, new ArrayList<>()));
    candidateSync.setAdditionalRichUserExtSources(Utils.extractAdditionalUserExtSources(perunSession, subjectData));
    candidateSync.setAttributes(attributes);
    return candidateSync;
}
Also used : RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) CandidateSync(cz.metacentrum.perun.core.api.CandidateSync) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 2 with CandidateSync

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

the class GroupAndGroupStructureSynchronizationIntegrationTest method synchronizeGroupUpdateExistingMemberStatus.

@Test
public void synchronizeGroupUpdateExistingMemberStatus() throws Exception {
    System.out.println(CLASS_NAME + "synchronizeGroupUpdateExistingMemberStatus");
    when(extSourceManagerBl.getExtSourceByName(sess, ExtSourcesManager.EXTSOURCE_NAME_PERUN)).thenReturn(extSourceForUserCreation);
    Attribute attr = attributesManagerBl.getAttribute(sess, group, GroupsManager.GROUPEXTSOURCE_ATTRNAME);
    attr.setValue(extSource.getName());
    attributesManagerBl.setAttribute(sess, group, attr);
    List<Map<String, String>> subjects = new ArrayList<>();
    Map<String, String> attributes = new HashMap<>();
    attributes.put("login", "metodej");
    subjects.add(attributes);
    Candidate candidate = setUpCandidate();
    member = perun.getMembersManagerBl().createMemberSync(sess, vo, candidate);
    perun.getMembersManagerBl().setStatus(sess, member, Status.DISABLED);
    groupsManagerBl.addMember(sess, group, member);
    when(extSourceManagerBl.getCandidate(sess, attributes, (ExtSourceLdap) essa, "metodej")).thenReturn(new CandidateSync(candidate));
    when(essa.getGroupSubjects(anyMap())).thenReturn(subjects);
    assertEquals(Status.DISABLED, groupsManagerBl.getGroupMembers(sess, group).get(0).getStatus());
    groupsManagerBl.synchronizeGroup(sess, group);
    assertEquals(Status.VALID, groupsManagerBl.getGroupMembers(sess, group).get(0).getStatus());
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) Attribute(cz.metacentrum.perun.core.api.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CandidateSync(cz.metacentrum.perun.core.api.CandidateSync) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) LinkedHashMap(java.util.LinkedHashMap) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 3 with CandidateSync

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

the class GroupAndGroupStructureSynchronizationIntegrationTest method synchronizeGroupUpdateFirstNameOfMember.

@Test
public void synchronizeGroupUpdateFirstNameOfMember() throws Exception {
    System.out.println(CLASS_NAME + "synchronizeGroupUpdateFirstNameOfMember");
    when(extSourceManagerBl.getExtSourceByName(sess, ExtSourcesManager.EXTSOURCE_NAME_PERUN)).thenReturn(extSourceForUserCreation);
    Attribute attr = attributesManagerBl.getAttribute(sess, group, GroupsManager.GROUPEXTSOURCE_ATTRNAME);
    attr.setValue(extSource.getName());
    attributesManagerBl.setAttribute(sess, group, attr);
    List<Map<String, String>> subjects = new ArrayList<>();
    Map<String, String> attributes = new HashMap<>();
    attributes.put("login", "metodej");
    subjects.add(attributes);
    Candidate candidate = setUpCandidate();
    member = perun.getMembersManagerBl().createMemberSync(sess, vo, candidate);
    groupsManagerBl.addMember(sess, group, member);
    candidate.setFirstName("metodej");
    Attribute attribute = perun.getAttributesManagerBl().getAttribute(sess, perun.getUsersManagerBl().getUserByMember(sess, member), AttributesManager.NS_USER_ATTR_CORE + ":firstName");
    Map<String, String> candidateAttrs = new HashMap<>();
    candidateAttrs.put(attribute.getName(), attribute.valueAsString());
    candidate.setAttributes(candidateAttrs);
    Map<String, String> map = new HashMap<>();
    map.put("overwriteUserAttributes", attribute.getName());
    when(extSourceManagerBl.getCandidate(sess, attributes, (ExtSourceLdap) essa, "metodej")).thenReturn(new CandidateSync(candidate));
    when(essa.getGroupSubjects(anyMap())).thenReturn(subjects);
    doReturn(map).when(extSourceManagerBl).getAttributes((ExtSourceLdap) essa);
    assertNotEquals(candidate.getFirstName(), perun.getUsersManagerBl().getUserByMember(sess, member).getFirstName());
    groupsManagerBl.synchronizeGroup(sess, group);
    assertEquals(candidate.getFirstName(), perun.getUsersManagerBl().getUserByMember(sess, member).getFirstName());
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) Attribute(cz.metacentrum.perun.core.api.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CandidateSync(cz.metacentrum.perun.core.api.CandidateSync) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) LinkedHashMap(java.util.LinkedHashMap) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 4 with CandidateSync

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

the class GroupAndGroupStructureSynchronizationIntegrationTest method synchronizeGroupMergeMemberAttributeValue.

@Test
public void synchronizeGroupMergeMemberAttributeValue() throws Exception {
    System.out.println(CLASS_NAME + "synchronizeGroupMergeMemberAttributeValue");
    when(extSourceManagerBl.getExtSourceByName(sess, ExtSourcesManager.EXTSOURCE_NAME_PERUN)).thenReturn(extSourceForUserCreation);
    Attribute attr = attributesManagerBl.getAttribute(sess, group, GroupsManager.GROUPEXTSOURCE_ATTRNAME);
    attr.setValue(extSource.getName());
    attributesManagerBl.setAttribute(sess, group, attr);
    List<Map<String, String>> subjects = new ArrayList<>();
    Map<String, String> attributes = new HashMap<>();
    attributes.put("login", "metodej");
    subjects.add(attributes);
    Candidate candidate = setUpCandidate();
    member = perun.getMembersManagerBl().createMemberSync(sess, vo, candidate);
    groupsManagerBl.addMember(sess, group, member);
    candidate.setFirstName("metodej");
    Attribute attribute = new Attribute();
    String namespace = "member-test-unique-attribute:specialNamespace";
    attribute.setNamespace(AttributesManager.NS_MEMBER_ATTR_DEF);
    attribute.setFriendlyName(namespace + "1");
    attribute.setType(ArrayList.class.getName());
    attribute.setValue(Stream.of("value1", "value2").collect(Collectors.toList()));
    assertNotNull("unable to create member attribute", attributesManagerBl.createAttribute(sess, attribute));
    Map<String, String> candidateAttrs = new HashMap<>();
    candidateAttrs.put(attribute.getName(), "value2,value3");
    candidate.setAttributes(candidateAttrs);
    attributesManagerBl.setAttribute(sess, member, attribute);
    Map<String, String> map = new HashMap<>();
    map.put("mergeMemberAttributes", attribute.getName());
    when(extSourceManagerBl.getCandidate(sess, attributes, (ExtSourceLdap) essa, "metodej")).thenReturn(new CandidateSync(candidate));
    when(essa.getGroupSubjects(anyMap())).thenReturn(subjects);
    doReturn(map).when(extSourceManagerBl).getAttributes((ExtSourceLdap) essa);
    assertEquals(2, attributesManagerBl.getAttribute(sess, member, attribute.getName()).valueAsList().size());
    groupsManagerBl.synchronizeGroup(sess, group);
    assertEquals(3, attributesManagerBl.getAttribute(sess, member, attribute.getName()).valueAsList().size());
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) Attribute(cz.metacentrum.perun.core.api.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CandidateSync(cz.metacentrum.perun.core.api.CandidateSync) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) LinkedHashMap(java.util.LinkedHashMap) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 5 with CandidateSync

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

the class GroupAndGroupStructureSynchronizationIntegrationTest method synchronizeGroupAddMissingMemberWhileCandidateAlreadyMember.

@Test
public void synchronizeGroupAddMissingMemberWhileCandidateAlreadyMember() throws Exception {
    System.out.println(CLASS_NAME + "synchronizeGroupAddMissingMemberWhileCandidateAlreadyMember");
    when(extSourceManagerBl.getExtSourceByName(sess, ExtSourcesManager.EXTSOURCE_NAME_PERUN)).thenReturn(extSourceForUserCreation);
    Attribute attr = attributesManagerBl.getAttribute(sess, group, GroupsManager.GROUPEXTSOURCE_ATTRNAME);
    attr.setValue(extSource.getName());
    attributesManagerBl.setAttribute(sess, group, attr);
    List<Map<String, String>> subjects = new ArrayList<>();
    Map<String, String> attributes = new HashMap<>();
    attributes.put("login", "metodej");
    subjects.add(attributes);
    Candidate candidate = setUpCandidate();
    member = perun.getMembersManagerBl().createMemberSync(sess, vo, candidate);
    when(extSourceManagerBl.getCandidate(sess, attributes, (ExtSourceLdap) essa, "metodej")).thenReturn(new CandidateSync(candidate));
    when(essa.getGroupSubjects(anyMap())).thenReturn(subjects);
    assertEquals(0, groupsManagerBl.getGroupMembers(sess, group).size());
    groupsManagerBl.synchronizeGroup(sess, group);
    assertEquals(1, groupsManagerBl.getGroupMembers(sess, group).size());
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) Attribute(cz.metacentrum.perun.core.api.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CandidateSync(cz.metacentrum.perun.core.api.CandidateSync) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) LinkedHashMap(java.util.LinkedHashMap) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Aggregations

CandidateSync (cz.metacentrum.perun.core.api.CandidateSync)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)6 Attribute (cz.metacentrum.perun.core.api.Attribute)6 Candidate (cz.metacentrum.perun.core.api.Candidate)6 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 Test (org.junit.Test)6 ArgumentMatchers.anyMap (org.mockito.ArgumentMatchers.anyMap)6 RichUserExtSource (cz.metacentrum.perun.core.api.RichUserExtSource)1 User (cz.metacentrum.perun.core.api.User)1 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 Matcher (java.util.regex.Matcher)1