Merge branch 'KON-43-Synchronisation-Interne-Personen-mit-LDAP' of ssh://git.eclipse.org:29418/openk-usermodules/org.eclipse.openk-usermodules.contactBaseData.backend into KON-43-Synchronisation-Interne-Personen-mit-LDAP
diff --git a/src/main/java/org/eclipse/openk/contactbasedata/controller/LdapController.java b/src/main/java/org/eclipse/openk/contactbasedata/controller/LdapController.java
index e5746aa..8ceaaaa 100644
--- a/src/main/java/org/eclipse/openk/contactbasedata/controller/LdapController.java
+++ b/src/main/java/org/eclipse/openk/contactbasedata/controller/LdapController.java
@@ -18,7 +18,7 @@
 import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
 import lombok.extern.log4j.Log4j2;
-import org.eclipse.openk.contactbasedata.service.LdapUserService;
+import org.eclipse.openk.contactbasedata.service.LdapService;
 import org.eclipse.openk.contactbasedata.viewmodel.LdapUser;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
@@ -36,14 +36,14 @@
 public class LdapController {
 
     @Autowired
-    private LdapUserService ldapUserService;
+    private LdapService ldapService;
 
     @ApiOperation(value = "Ermitteln der UserModules vom Auth'n'Auth-Service")
     @ApiResponses(value = {@ApiResponse(code = 200, message = "Erfolgreich durchgeführt")})
     @ResponseStatus(HttpStatus.OK)
     @GetMapping( "/users")
     public List<LdapUser> getLdapUser() {
-        return ldapUserService.getAllLdapUsers();
+        return ldapService.getAllLdapUsers();
     }
 
     @ApiOperation(value = "Synchronisierung der User anhand des LDAPs")
@@ -51,7 +51,7 @@
     @ResponseStatus(HttpStatus.OK)
     @GetMapping( "/sync")
     public ResponseEntity<Object> syncLdapUser() {
-        ldapUserService.synchronizeLDAP();
+        ldapService.synchronizeLDAP();
         return ResponseEntity.ok().build();
     }
 
diff --git a/src/main/java/org/eclipse/openk/contactbasedata/service/LdapService.java b/src/main/java/org/eclipse/openk/contactbasedata/service/LdapService.java
index 791a5ac..8636830 100644
--- a/src/main/java/org/eclipse/openk/contactbasedata/service/LdapService.java
+++ b/src/main/java/org/eclipse/openk/contactbasedata/service/LdapService.java
@@ -1,104 +1,191 @@
-/*
- *******************************************************************************
- * Copyright (c) 2019 Contributors to the Eclipse Foundation
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information regarding copyright ownership.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v. 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0.
- *
- * SPDX-License-Identifier: EPL-2.0
- *******************************************************************************
-*/
 package org.eclipse.openk.contactbasedata.service;
-
-import org.eclipse.openk.contactbasedata.api.AuthNAuthApi;
+import lombok.extern.log4j.Log4j2;
+import org.apache.commons.lang.StringUtils;
+import org.eclipse.openk.contactbasedata.exceptions.NotFoundException;
+import org.eclipse.openk.contactbasedata.model.RefCommunicationType;
+import org.eclipse.openk.contactbasedata.model.TblCommunication;
+import org.eclipse.openk.contactbasedata.model.TblInternalPerson;
+import org.eclipse.openk.contactbasedata.repository.CommunicationRepository;
+import org.eclipse.openk.contactbasedata.repository.CommunicationTypeRepository;
+import org.eclipse.openk.contactbasedata.repository.InternalPersonRepository;
+import org.eclipse.openk.contactbasedata.service.util.LdapUserAttributesMapper;
+import org.eclipse.openk.contactbasedata.viewmodel.LdapUser;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.env.Environment;
-import org.springframework.ldap.core.*;
-import org.springframework.ldap.support.LdapNameBuilder;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.ldap.core.LdapTemplate;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
-import javax.naming.Name;
-import java.util.List;
+import java.util.*;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
+import static org.springframework.ldap.query.LdapQueryBuilder.query;
+
+@Log4j2
 @Service
 public class LdapService {
 
-    @Autowired
-    InternalPersonService internalPersonService;
+    @Value("${ldap.attribute-mapping.uid}")
+    private String uid;
+
+    @Value("${ldap.db-id-mapping.telephone-number-id}")
+    private Long telephoneNumberId;
+
+    @Value("${ldap.db-id-mapping.mail-id}")
+    private Long mailId;
 
     @Autowired
-    private AuthNAuthApi authNAuthApi;
+    LdapUserAttributesMapper ldapUserAttributesMapper;
 
     @Autowired
-    private Environment env;
-
-    @Autowired
-    private ContextSource contextSource;
+    private InternalPersonRepository internalPersonRepository;
 
     @Autowired
     private LdapTemplate ldapTemplate;
 
-    public void authenticate(final String username, final String password) {
-        contextSource.getContext("cn=" + username + ",ou=users," + env.getRequiredProperty("ldap.partitionSuffix"), password);
+    @Autowired
+    private CommunicationTypeRepository communicationTypeRepository;
+
+    @Autowired
+    CommunicationRepository communicationRepository;
+
+    /**
+     * Retrieves all the LdapUsers in the ldap server, using {@link LdapUserAttributesMapper}
+     * @return list of LdapUsers
+     */
+    public List<LdapUser> getAllLdapUsers() {
+        return ldapTemplate.search(query()
+                .where("objectclass").is("person"), ldapUserAttributesMapper);
     }
 
-    public List<String> search(final String username) {
-        return ldapTemplate.search(
-                "ou=users",
-                "cn=" + username,
-                (AttributesMapper<String>) attrs -> (String) attrs
-                        .get("cn")
-                        .get());
+    /**
+     * Retrieves the distinct the LdapUsers from the ldap server, using {@link LdapUserAttributesMapper}
+     * @return ldapUsers
+     */
+    public List<LdapUser> getLdapUserByUID(String uid) {
+        return ldapTemplate.search(query()
+                .where("objectclass").is("person").and(this.uid).is(uid), ldapUserAttributesMapper);
     }
 
-    public void create(final String username) {
-        Name dn = LdapNameBuilder
-                .newInstance()
-                .add("ou", "users")
-                .add("cn", username)
-                .build();
-        DirContextAdapter context = new DirContextAdapter(dn);
+    @Transactional
+    public void synchronizeLDAP() {
+        List<TblInternalPerson> internalPersonList = internalPersonRepository.findByUidNotNull();
 
-        context.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
-        context.setAttributeValue("cn", username);
-        context.setAttributeValue("sn", username);
+        Map<String, TblInternalPerson> uidToInternalPersonMap = internalPersonList.stream().collect(
+                Collectors.toMap(TblInternalPerson::getUid, Function.identity()));
 
-        ldapTemplate.bind(context);
-    }
+        List<LdapUser> allFoundLdapUsers = new ArrayList<>();
+        List<TblInternalPerson> allNotExistingLdapUsers = new ArrayList<>();
+        findExistingAndNotExistingLdapUsers(internalPersonList, allFoundLdapUsers, allNotExistingLdapUsers);
 
-    public void modify(final String username, final String password) {
-        Name dn = LdapNameBuilder
-                .newInstance()
-                .add("ou", "users")
-                .add("cn", username)
-                .build();
-        DirContextOperations context = ldapTemplate.lookupContext(dn);
+        List<TblInternalPerson> internalPersonListSynchronized = new ArrayList<>();
 
-        context.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
-        context.setAttributeValue("cn", username);
-        context.setAttributeValue("sn", username);
-        //context.setAttributeValue("userPassword", digestSHA(password));
+        RefCommunicationType refCommunicationTypeMail = getRefCommunicationType(mailId);
+        RefCommunicationType refCommunicationTypePhone = getRefCommunicationType(telephoneNumberId);
 
-        ldapTemplate.modifyAttributes(context);
-    }
-
-    /*private String digestSHA(final String password) {
-        String base64;
-        try {
-            MessageDigest digest = MessageDigest.getInstance("SHA");
-            digest.update(password.getBytes());
-            base64 = Base64
-                    .getEncoder()
-                    .encodeToString(digest.digest());
-        } catch (NoSuchAlgorithmException e) {
-            throw new RuntimeException(e);
+        for (LdapUser ldapUser : allFoundLdapUsers) {
+            TblInternalPerson tblInternalPerson = uidToInternalPersonMap.get(ldapUser.getUid());
+            if (tblInternalPerson == null) continue;
+            boolean attributesChanged = mapLdapUserToInternaPerson(tblInternalPerson, ldapUser, refCommunicationTypeMail, refCommunicationTypePhone);
+            if (attributesChanged) {
+                internalPersonListSynchronized.add(tblInternalPerson);
+            }
         }
-        return "{SHA}" + base64;
-    }*/
+
+        //Update all not found users notes with the sync error message
+        internalPersonRepository.saveAll(allNotExistingLdapUsers);
+
+        //Update all found Users with the synchronized LDAP data
+        internalPersonRepository.saveAll(internalPersonListSynchronized);
+    }
+
+    private RefCommunicationType getRefCommunicationType(Long refCommunicationTypeId) {
+        return communicationTypeRepository.findAll().stream()
+                .filter(x -> x.getId().equals(refCommunicationTypeId))
+                .findFirst()
+                .orElseThrow(NotFoundException::new);
+    }
+
+    private void findExistingAndNotExistingLdapUsers(List<TblInternalPerson> internalPersonList, List<LdapUser> allFoundLdapUsers, List<TblInternalPerson> allNotExistingLdapUsers) {
+        for (TblInternalPerson tblInternalPerson : internalPersonList) {
+            String uidTblInternalPerson = tblInternalPerson.getUid();
+            List<LdapUser> ldapUserByUIDResult = getLdapUserByUID(uidTblInternalPerson);
+
+            if(ldapUserByUIDResult.isEmpty()){
+                String errorMsg = String.format("[LDAP Sync Error] User with Uid: [ %s ] can not be found in LDAP", uidTblInternalPerson);
+                setNoteAndLogSyncError(allNotExistingLdapUsers, tblInternalPerson, errorMsg);
+            }
+            else if(ldapUserByUIDResult.size() > 1){
+                String errorMsg = String.format("[LDAP Sync Error] More than one result for UID: [ %s ] in LDAP", uidTblInternalPerson);
+                setNoteAndLogSyncError(allNotExistingLdapUsers, tblInternalPerson, errorMsg);
+            }
+            else {
+                LdapUser ldapUser = ldapUserByUIDResult.get(0);
+                allFoundLdapUsers.add(ldapUser);
+            }
+        }
+    }
+
+    private void setNoteAndLogSyncError(List<TblInternalPerson> allNotExistingLdapUsers, TblInternalPerson tblInternalPerson, String errorMsg) {
+        log.error(errorMsg);
+        String note = tblInternalPerson.getContact().getNote();
+        if (note == null) note = "";
+        if (!note.contains(errorMsg)) {
+            note = note + System.lineSeparator() + errorMsg;
+            tblInternalPerson.getContact().setNote(note);
+            allNotExistingLdapUsers.add(tblInternalPerson);
+        }
+    }
+
+    private boolean mapLdapUserToInternaPerson(TblInternalPerson tblInternalPerson, LdapUser ldapUser, RefCommunicationType refCommunicationTypeMail, RefCommunicationType refCommunicationTypePhone) {
+        boolean attributesChanged = false;
+
+        attributesChanged = isCommunicationDataChangedAndSync(tblInternalPerson, ldapUser.getMail(), refCommunicationTypeMail, attributesChanged, mailId);
+        attributesChanged = isCommunicationDataChangedAndSync(tblInternalPerson, ldapUser.getTelephoneNumber(), refCommunicationTypePhone, attributesChanged, telephoneNumberId);
+
+        if (!Objects.equals(tblInternalPerson.getFirstName(), ldapUser.getFirstName())
+                || !Objects.equals(tblInternalPerson.getLastName(), ldapUser.getLastName())
+                || !Objects.equals(tblInternalPerson.getTitle(), ldapUser.getTitle())
+                || !Objects.equals(tblInternalPerson.getDepartment(), ldapUser.getDepartment())) {
+            attributesChanged = true;
+            tblInternalPerson.setFirstName(ldapUser.getFirstName());
+            tblInternalPerson.setLastName(ldapUser.getLastName());
+            tblInternalPerson.setTitle(ldapUser.getTitle());
+            tblInternalPerson.setDepartment(ldapUser.getDepartment());
+        }
+        return attributesChanged;
+    }
+
+    private boolean isCommunicationDataChangedAndSync(TblInternalPerson tblInternalPerson, String communicationdata, RefCommunicationType refCommunicationType, boolean attributesChanged, Long communicationTypeId) {
+        List<TblCommunication> tblCommunicationList = tblInternalPerson.getContact().getCommunications();
+
+        Optional<TblCommunication> optionalTblCommunication = tblCommunicationList.stream()
+                .filter(tblCommunication -> tblCommunication.getRefCommunicationType().getId().equals(communicationTypeId))
+                .findFirst();
+
+        if (optionalTblCommunication.isPresent()) {
+            if (!Objects.equals(optionalTblCommunication.get().getCommunicationData(), communicationdata)){
+                optionalTblCommunication.get().setCommunicationData(communicationdata);
+                attributesChanged = true;
+            }
+        } else {
+            attributesChanged = createAndSaveNewTblCommunication(tblInternalPerson, communicationdata, refCommunicationType, attributesChanged);
+        }
+        return attributesChanged;
+    }
+
+    private boolean createAndSaveNewTblCommunication(TblInternalPerson tblInternalPerson, String communicationData, RefCommunicationType refCommunicationType, boolean attributesChanged) {
+        if (StringUtils.isEmpty(communicationData)) return attributesChanged;
+        TblCommunication tblCommunication = new TblCommunication();
+        tblCommunication.setUuid(UUID.randomUUID());
+        tblCommunication.setTblContact(tblInternalPerson.getContact());
+        tblCommunication.setRefCommunicationType(refCommunicationType);
+        tblCommunication.setCommunicationData(communicationData);
+        communicationRepository.save(tblCommunication);
+        tblInternalPerson.getContact().getCommunications().add(tblCommunication);
+        return true;
+    }
 
 
 }
diff --git a/src/main/java/org/eclipse/openk/contactbasedata/service/LdapUserService.java b/src/main/java/org/eclipse/openk/contactbasedata/service/LdapUserService.java
deleted file mode 100644
index 4f2f1f6..0000000
--- a/src/main/java/org/eclipse/openk/contactbasedata/service/LdapUserService.java
+++ /dev/null
@@ -1,193 +0,0 @@
-package org.eclipse.openk.contactbasedata.service;
-import lombok.extern.log4j.Log4j2;
-import org.apache.commons.lang.StringUtils;
-import org.eclipse.openk.contactbasedata.exceptions.NotFoundException;
-import org.eclipse.openk.contactbasedata.model.RefCommunicationType;
-import org.eclipse.openk.contactbasedata.model.TblCommunication;
-import org.eclipse.openk.contactbasedata.model.TblInternalPerson;
-import org.eclipse.openk.contactbasedata.repository.CommunicationRepository;
-import org.eclipse.openk.contactbasedata.repository.CommunicationTypeRepository;
-import org.eclipse.openk.contactbasedata.repository.InternalPersonRepository;
-import org.eclipse.openk.contactbasedata.service.util.LdapUserAttributesMapper;
-import org.eclipse.openk.contactbasedata.viewmodel.LdapUser;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.ldap.core.LdapTemplate;
-import org.springframework.security.access.annotation.Secured;
-import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.util.*;
-import java.util.function.Function;
-import java.util.stream.Collectors;
-
-import static org.springframework.ldap.query.LdapQueryBuilder.query;
-
-@Log4j2
-@Service
-public class LdapUserService {
-
-    @Value("${ldap.attribute-mapping.uid}")
-    private String uid;
-
-    @Value("${ldap.db-id-mapping.telephone-number-id}")
-    private Long telephoneNumberId;
-
-    @Value("${ldap.db-id-mapping.mail-id}")
-    private Long mailId;
-
-    @Autowired
-    LdapUserAttributesMapper ldapUserAttributesMapper;
-
-    @Autowired
-    private InternalPersonRepository internalPersonRepository;
-
-    @Autowired
-    private LdapTemplate ldapTemplate;
-
-    @Autowired
-    private CommunicationTypeRepository communicationTypeRepository;
-
-    @Autowired
-    CommunicationRepository communicationRepository;
-
-    /**
-     * Retrieves all the LdapUsers in the ldap server, using {@link LdapUserAttributesMapper}
-     * @return list of LdapUsers
-     */
-    @Secured({"ROLE_KON-WRITER", "ROLE_KON-ADMIN"})
-    public List<LdapUser> getAllLdapUsers() {
-        return ldapTemplate.search(query()
-                .where("objectclass").is("person"), ldapUserAttributesMapper);
-    }
-
-    /**
-     * Retrieves the distinct the LdapUsers from the ldap server, using {@link LdapUserAttributesMapper}
-     * @return ldapUsers
-     */
-    public List<LdapUser> getLdapUserByUID(String uid) {
-        return ldapTemplate.search(query()
-                .where("objectclass").is("person").and(this.uid).is(uid), ldapUserAttributesMapper);
-    }
-
-    @Transactional
-    @Secured("ROLE_KON-ADMIN")
-    public void synchronizeLDAP() {
-        List<TblInternalPerson> internalPersonList = internalPersonRepository.findByUidNotNull();
-
-        Map<String, TblInternalPerson> uidToInternalPersonMap = internalPersonList.stream().collect(
-                Collectors.toMap(TblInternalPerson::getUid, Function.identity()));
-
-        List<LdapUser> allFoundLdapUsers = new ArrayList<>();
-        List<TblInternalPerson> internalPersonListSynchronized = new ArrayList<>();
-        findExistingAndNotExistingLdapUsers(internalPersonList, allFoundLdapUsers, internalPersonListSynchronized);
-
-        RefCommunicationType refCommunicationTypeMail = getRefCommunicationType(mailId);
-        RefCommunicationType refCommunicationTypePhone = getRefCommunicationType(telephoneNumberId);
-
-        for (LdapUser ldapUser : allFoundLdapUsers) {
-            TblInternalPerson tblInternalPerson = uidToInternalPersonMap.get(ldapUser.getUid());
-            if (tblInternalPerson != null) {
-                boolean attributesChanged = mapLdapUserToInternalPerson(tblInternalPerson, ldapUser, refCommunicationTypeMail, refCommunicationTypePhone);
-                if (attributesChanged) {
-                    internalPersonListSynchronized.add(tblInternalPerson);
-                }
-            }
-        }
-
-        //Update all not found users notes with the sync error message
-        //Update all found Users with the synchronized LDAP data
-        internalPersonRepository.saveAll(internalPersonListSynchronized);
-    }
-
-    private RefCommunicationType getRefCommunicationType(Long refCommunicationTypeId) {
-        return communicationTypeRepository.findAll().stream()
-                .filter(x -> x.getId().equals(refCommunicationTypeId))
-                .findFirst()
-                .orElseThrow(NotFoundException::new);
-    }
-
-    private void findExistingAndNotExistingLdapUsers(List<TblInternalPerson> internalPersonList, List<LdapUser> allFoundLdapUsers, List<TblInternalPerson> allNotExistingLdapUsers) {
-        for (TblInternalPerson tblInternalPerson : internalPersonList) {
-            String uidTblInternalPerson = tblInternalPerson.getUid();
-            List<LdapUser> ldapUserByUIDResult = getLdapUserByUID(uidTblInternalPerson);
-
-            if(ldapUserByUIDResult.size() == 0){
-                String errorMsg = String.format("[LDAP Sync Error] User with Uid: [ %s ] can not be found in LDAP", uidTblInternalPerson);
-                setNoteAndLogSyncError(allNotExistingLdapUsers, tblInternalPerson, errorMsg);
-                continue;
-            }
-
-            if(ldapUserByUIDResult.size() > 1){
-                String errorMsg = String.format("[LDAP Sync Error] More than one result for UID: [ %s ] in LDAP", uidTblInternalPerson);
-                setNoteAndLogSyncError(allNotExistingLdapUsers, tblInternalPerson, errorMsg);
-                continue;
-            }
-
-            LdapUser ldapUser = ldapUserByUIDResult.get(0);
-            allFoundLdapUsers.add(ldapUser);
-        }
-    }
-
-    private void setNoteAndLogSyncError(List<TblInternalPerson> allNotExistingLdapUsers, TblInternalPerson tblInternalPerson, String errorMsg) {
-        log.error(errorMsg);
-        String note = tblInternalPerson.getContact().getNote();
-        if (note == null) note = "";
-        if (!note.contains(errorMsg)) {
-            note = note + System.lineSeparator() + errorMsg;
-            tblInternalPerson.getContact().setNote(note);
-            allNotExistingLdapUsers.add(tblInternalPerson);
-        }
-    }
-
-    private boolean mapLdapUserToInternalPerson(TblInternalPerson tblInternalPerson, LdapUser ldapUser, RefCommunicationType refCommunicationTypeMail, RefCommunicationType refCommunicationTypePhone) {
-        boolean attributesChanged = false;
-
-        attributesChanged = isCommunicationDataChangedAndSync(tblInternalPerson, ldapUser.getMail(), refCommunicationTypeMail, attributesChanged, mailId);
-        attributesChanged = isCommunicationDataChangedAndSync(tblInternalPerson, ldapUser.getTelephoneNumber(), refCommunicationTypePhone, attributesChanged, telephoneNumberId);
-
-        if (!Objects.equals(tblInternalPerson.getFirstName(), ldapUser.getFirstName())
-                || !Objects.equals(tblInternalPerson.getLastName(), ldapUser.getLastName())
-                || !Objects.equals(tblInternalPerson.getTitle(), ldapUser.getTitle())
-                || !Objects.equals(tblInternalPerson.getDepartment(), ldapUser.getDepartment())) {
-            attributesChanged = true;
-            tblInternalPerson.setFirstName(ldapUser.getFirstName());
-            tblInternalPerson.setLastName(ldapUser.getLastName());
-            tblInternalPerson.setTitle(ldapUser.getTitle());
-            tblInternalPerson.setDepartment(ldapUser.getDepartment());
-        }
-        return attributesChanged;
-    }
-
-    private boolean isCommunicationDataChangedAndSync(TblInternalPerson tblInternalPerson, String communicationdata, RefCommunicationType refCommunicationType, boolean attributesChanged, Long communicationTypeId) {
-        List<TblCommunication> tblCommunicationList = tblInternalPerson.getContact().getCommunications();
-
-        Optional<TblCommunication> optionalTblCommunication = tblCommunicationList.stream()
-                .filter(tblCommunication -> tblCommunication.getRefCommunicationType().getId().equals(communicationTypeId))
-                .findFirst();
-
-        if (optionalTblCommunication.isPresent()) {
-            if (!Objects.equals(optionalTblCommunication.get().getCommunicationData(), communicationdata)){
-                optionalTblCommunication.get().setCommunicationData(communicationdata);
-                attributesChanged = true;
-            }
-        } else {
-            attributesChanged = createAndSaveNewTblCommunication(tblInternalPerson, communicationdata, refCommunicationType, attributesChanged);
-        }
-        return attributesChanged;
-    }
-
-    private boolean createAndSaveNewTblCommunication(TblInternalPerson tblInternalPerson, String communicationData, RefCommunicationType refCommunicationType, boolean attributesChanged) {
-        if (StringUtils.isEmpty(communicationData)) return attributesChanged;
-        TblCommunication tblCommunication = new TblCommunication();
-        tblCommunication.setUuid(UUID.randomUUID());
-        tblCommunication.setTblContact(tblInternalPerson.getContact());
-        tblCommunication.setRefCommunicationType(refCommunicationType);
-        tblCommunication.setCommunicationData(communicationData);
-        communicationRepository.save(tblCommunication);
-        tblInternalPerson.getContact().getCommunications().add(tblCommunication);
-        return true;
-    }
-
-
-}
diff --git a/src/main/java/org/eclipse/openk/contactbasedata/service/SchedulingService.java b/src/main/java/org/eclipse/openk/contactbasedata/service/SchedulingService.java
index 702e3ec..1ffa3c5 100644
--- a/src/main/java/org/eclipse/openk/contactbasedata/service/SchedulingService.java
+++ b/src/main/java/org/eclipse/openk/contactbasedata/service/SchedulingService.java
@@ -5,19 +5,17 @@
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
-import java.time.LocalDate;
-
 @Log4j2
 @Service
 public class SchedulingService {
 
     @Autowired
-    LdapUserService ldapUserService;
+    LdapService ldapService;
 
     @Scheduled(cron = "${ldap.scheduling.cron-expression}")
     public void scheduleTaskSynchronize() {
         log.info("Executing scheduled task: Synchronizing Users");
-        ldapUserService.synchronizeLDAP();
+        ldapService.synchronizeLDAP();
         log.info("Finished scheduled task: Synchronizing Users");
     }
 }
diff --git a/src/main/java/org/eclipse/openk/contactbasedata/service/util/LdapUserAttributesMapper.java b/src/main/java/org/eclipse/openk/contactbasedata/service/util/LdapUserAttributesMapper.java
index 033f5bf..e856921 100644
--- a/src/main/java/org/eclipse/openk/contactbasedata/service/util/LdapUserAttributesMapper.java
+++ b/src/main/java/org/eclipse/openk/contactbasedata/service/util/LdapUserAttributesMapper.java
@@ -53,10 +53,6 @@
     @Value("${ldap.attribute-mapping.telephone-number}")
     private String telephoneNumber;
 
-    @Value("${ldap.attribute-mapping.password}")
-    private String password;
-
-
     @Override
     public LdapUser mapFromAttributes(Attributes attributes) throws NamingException {
         LdapUser ldapUser = new LdapUser();
@@ -93,10 +89,6 @@
         if (attribute != null) {
             ldapUser.setMail((String) attribute.get());
         }
-        attribute = attributes.get(password);
-        if (attribute != null) {
-            ldapUser.setPassword((byte[]) attribute.get());
-        }
 
         return ldapUser;
     }
diff --git a/src/test/java/org/eclipse/openk/contactbasedata/config/TestConfiguration.java b/src/test/java/org/eclipse/openk/contactbasedata/config/TestConfiguration.java
index b12bcfb..7056b3a 100644
--- a/src/test/java/org/eclipse/openk/contactbasedata/config/TestConfiguration.java
+++ b/src/test/java/org/eclipse/openk/contactbasedata/config/TestConfiguration.java
@@ -20,8 +20,10 @@
 import org.eclipse.openk.contactbasedata.service.util.LdapUserAttributesMapper;
 import org.springframework.boot.autoconfigure.domain.EntityScan;
 import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
+import org.springframework.boot.test.mock.mockito.MockBean;
 import org.springframework.context.annotation.Bean;
 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
+import org.springframework.ldap.core.LdapTemplate;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.TestPropertySource;
 
@@ -75,6 +77,9 @@
     @Bean
     LdapUserAttributesMapper ldapUserAttributesMapper() { return new LdapUserAttributesMapper(); };
 
+    @MockBean
+    private LdapTemplate ldapTemplate;
+
     @Bean
     public VersionService myVersionService() {
         return new VersionService();
@@ -123,5 +128,5 @@
     public ContactAnonymizerService myContactAnonymizerService() { return new ContactAnonymizerService(); }
 
     @Bean
-    public LdapUserService myLdapUserService() { return new LdapUserService(); }
+    public LdapService myLdapService() { return new LdapService(); }
 }
diff --git a/src/test/java/org/eclipse/openk/contactbasedata/service/LdapUserServiceTest.java b/src/test/java/org/eclipse/openk/contactbasedata/service/LdapUserServiceTest.java
index 78290e3..8325524 100644
--- a/src/test/java/org/eclipse/openk/contactbasedata/service/LdapUserServiceTest.java
+++ b/src/test/java/org/eclipse/openk/contactbasedata/service/LdapUserServiceTest.java
@@ -24,6 +24,7 @@
 import org.eclipse.openk.contactbasedata.support.MockDataHelper;
 import org.eclipse.openk.contactbasedata.viewmodel.LdapUser;
 import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
 import org.mockito.stubbing.Answer;
 import org.powermock.reflect.Whitebox;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -39,6 +40,7 @@
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.when;
 
 @DataJpaTest
@@ -48,9 +50,9 @@
     private static final long MAIL_ID = 1L;
     private static final long TEL_ID = 2L;
 
-    @Qualifier("myLdapUserService")
+    @Qualifier("myLdapService")
     @Autowired
-    private LdapUserService ldapUserService;
+    private LdapService ldapService;
 
     @Autowired
     private LdapUserAttributesMapper ldapUserAttributesMapper;
@@ -64,11 +66,12 @@
     @MockBean
     private CommunicationTypeRepository communicationTypeRepository;
 
-    @MockBean
+    @Autowired
     private LdapTemplate ldapTemplate;
 
     @Test
     public void shouldSynchronizeLDAPRunThrou1() {
+        when(internalPersonRepository.saveAll(any( List.class ))).thenReturn(Lists.emptyList());
         List<TblInternalPerson> internalPeople = MockDataHelper.mockTblInternalPersonPage().getContent();
         when( internalPersonRepository.findByUidNotNull() ).thenReturn(internalPeople);
 
@@ -80,8 +83,8 @@
         when( ldapTemplate.search(any(LdapQuery.class), any(LdapUserAttributesMapper.class))).thenReturn( ldapUserList );
 
         List<RefCommunicationType> refCommunicationTypes = MockDataHelper.mockRefCommunicationTypeList();
-        Whitebox.setInternalState(ldapUserService, "mailId", MAIL_ID);
-        Whitebox.setInternalState(ldapUserService, "telephoneNumberId", TEL_ID);
+        Whitebox.setInternalState(ldapService, "mailId", MAIL_ID);
+        Whitebox.setInternalState(ldapService, "telephoneNumberId", TEL_ID);
         refCommunicationTypes.get(1).setId(TEL_ID);
         refCommunicationTypes.get(2).setId(MAIL_ID);
         when( communicationTypeRepository.findAll()).thenReturn(refCommunicationTypes);
@@ -93,11 +96,14 @@
                 });
 
 
-        ldapUserService.synchronizeLDAP();
+        ldapService.synchronizeLDAP();
+        Mockito.verify(internalPersonRepository, times(2)).saveAll(any(List.class));
     }
 
     @Test
     public void shouldSynchronizeLDAPRunThrou2() {
+
+        when(internalPersonRepository.saveAll(any( List.class ))).thenReturn(Lists.emptyList());
         List<TblInternalPerson> internalPeople = MockDataHelper.mockTblInternalPersonPage().getContent();
         when( internalPersonRepository.findByUidNotNull() ).thenReturn(internalPeople);
 
@@ -105,13 +111,15 @@
         when( ldapTemplate.search(any(LdapQuery.class), any(LdapUserAttributesMapper.class))).thenReturn( Lists.emptyList() );
 
         List<RefCommunicationType> refCommunicationTypes = MockDataHelper.mockRefCommunicationTypeList();
-        Whitebox.setInternalState(ldapUserService, "mailId", MAIL_ID);
-        Whitebox.setInternalState(ldapUserService, "telephoneNumberId", TEL_ID);
+        Whitebox.setInternalState(ldapService, "mailId", MAIL_ID);
+        Whitebox.setInternalState(ldapService, "telephoneNumberId", TEL_ID);
         refCommunicationTypes.get(1).setId(TEL_ID);
         refCommunicationTypes.get(2).setId(MAIL_ID);
         when( communicationTypeRepository.findAll()).thenReturn(refCommunicationTypes);
 
-        ldapUserService.synchronizeLDAP();
+        ldapService.synchronizeLDAP();
+        Mockito.verify(internalPersonRepository, times(2)).saveAll(any(List.class));
+
     }
 
     @Test
@@ -127,7 +135,7 @@
         // users are always found
         when( ldapTemplate.search(any(LdapQuery.class), any(LdapUserAttributesMapper.class))).thenReturn( ldapUserList );
 
-        Whitebox.invokeMethod(ldapUserService, "findExistingAndNotExistingLdapUsers",
+        Whitebox.invokeMethod(ldapService, "findExistingAndNotExistingLdapUsers",
                 internalPeople, ldapUsers, allNotExistingLdapUsers);
 
         assertEquals( internalPeople.size(), ldapUsers.size());
@@ -144,7 +152,7 @@
         // users are not found
         when( ldapTemplate.search(any(LdapQuery.class), any(LdapUserAttributesMapper.class))).thenReturn( Lists.emptyList() );
 
-        Whitebox.invokeMethod(ldapUserService, "findExistingAndNotExistingLdapUsers",
+        Whitebox.invokeMethod(ldapService, "findExistingAndNotExistingLdapUsers",
                 internalPeople, ldapUsers, allNotExistingLdapUsers);
 
         assertEquals( 0, ldapUsers.size());
@@ -165,7 +173,7 @@
         // too much users are found
         when( ldapTemplate.search(any(LdapQuery.class), any(LdapUserAttributesMapper.class))).thenReturn( ldapUserList );
 
-        Whitebox.invokeMethod(ldapUserService, "findExistingAndNotExistingLdapUsers",
+        Whitebox.invokeMethod(ldapService, "findExistingAndNotExistingLdapUsers",
                 internalPeople, ldapUsers, allNotExistingLdapUsers);
 
         assertEquals( 0, ldapUsers.size());