diff options
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-44-Synchronisation-Interne-Personen-mit-Auth&Auth-ModulKON-44-Synchronisation-Interne-Personen-mit-Auth_und_Auth-Modul
# Conflicts:
# src/main/java/org/eclipse/openk/contactbasedata/config/SchedulerConfig.java
# src/main/java/org/eclipse/openk/contactbasedata/service/util/LdapUserAttributesMapper.java
7 files changed, 225 insertions, 22 deletions
diff --git a/src/main/java/org/eclipse/openk/contactbasedata/service/InitPropertyLogger.java b/src/main/java/org/eclipse/openk/contactbasedata/service/InitPropertyLogger.java index 92552b2..4399cfe 100644 --- a/src/main/java/org/eclipse/openk/contactbasedata/service/InitPropertyLogger.java +++ b/src/main/java/org/eclipse/openk/contactbasedata/service/InitPropertyLogger.java @@ -1,13 +1,10 @@ package org.eclipse.openk.contactbasedata.service; import lombok.extern.log4j.Log4j2; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.boot.env.OriginTrackedMapPropertySource; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.core.env.AbstractEnvironment; -import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.Environment; import org.springframework.core.env.MutablePropertySources; import org.springframework.stereotype.Component; 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 891a993..cc67420 100644 --- a/src/main/java/org/eclipse/openk/contactbasedata/service/LdapService.java +++ b/src/main/java/org/eclipse/openk/contactbasedata/service/LdapService.java @@ -118,20 +118,18 @@ public class LdapService { String uidTblInternalPerson = tblInternalPerson.getUid(); List<LdapUser> ldapUserByUIDResult = getLdapUserByUID(uidTblInternalPerson); - if(ldapUserByUIDResult.size() == 0){ + 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); - continue; } - - if(ldapUserByUIDResult.size() > 1){ + 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); - continue; } - - LdapUser ldapUser = ldapUserByUIDResult.get(0); - allFoundLdapUsers.add(ldapUser); + else { + LdapUser ldapUser = ldapUserByUIDResult.get(0); + allFoundLdapUsers.add(ldapUser); + } } } 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 4bd38f6..3827a14 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 @@ public class LdapUserAttributesMapper implements AttributesMapper { @Value("${ldap-sync.attribute-mapping.telephone-number}") private String telephoneNumber; - @Value("${ldap-sync.attribute-mapping.password}") - private String password; - - @Override public LdapUser mapFromAttributes(Attributes attributes) throws NamingException { LdapUser ldapUser = new LdapUser(); @@ -93,10 +89,6 @@ public class LdapUserAttributesMapper implements AttributesMapper { 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/main/resources/application.yml b/src/main/resources/application.yml index 296afa7..0c70ee9 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -34,7 +34,6 @@ ldap-sync: mail: mail department: department telephone-number: phone - password: userpassword db-id-mapping: mail-id: 1 telephone-number-id: 2 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 8d0468d..7056b3a 100644 --- a/src/test/java/org/eclipse/openk/contactbasedata/config/TestConfiguration.java +++ b/src/test/java/org/eclipse/openk/contactbasedata/config/TestConfiguration.java @@ -17,10 +17,13 @@ package org.eclipse.openk.contactbasedata.config; import org.eclipse.openk.contactbasedata.ContactBaseDataApplication; import org.eclipse.openk.contactbasedata.mapper.*; import org.eclipse.openk.contactbasedata.service.*; +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; @@ -72,6 +75,12 @@ public class TestConfiguration { AssignmentModulContactMapper assignmentModulContactMapper() { return new AssignmentModulContactMapperImpl(); } @Bean + LdapUserAttributesMapper ldapUserAttributesMapper() { return new LdapUserAttributesMapper(); }; + + @MockBean + private LdapTemplate ldapTemplate; + + @Bean public VersionService myVersionService() { return new VersionService(); } @@ -117,4 +126,7 @@ public class TestConfiguration { @Bean public ContactAnonymizerService myContactAnonymizerService() { return new ContactAnonymizerService(); } + + @Bean + public LdapService myLdapService() { return new LdapService(); } } diff --git a/src/test/java/org/eclipse/openk/contactbasedata/service/LdapServiceTest.java b/src/test/java/org/eclipse/openk/contactbasedata/service/LdapServiceTest.java new file mode 100644 index 0000000..1e03724 --- /dev/null +++ b/src/test/java/org/eclipse/openk/contactbasedata/service/LdapServiceTest.java @@ -0,0 +1,182 @@ +/* + ******************************************************************************* + * 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.assertj.core.util.Lists; +import org.eclipse.openk.contactbasedata.config.TestConfiguration; +import org.eclipse.openk.contactbasedata.model.RefCommunicationType; +import org.eclipse.openk.contactbasedata.model.TblInternalPerson; +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.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; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.query.LdapQuery; +import org.springframework.test.context.ContextConfiguration; + +import java.util.ArrayList; +import java.util.List; + +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 +@ContextConfiguration(classes = {TestConfiguration.class}) + +public class LdapServiceTest { + private static final long MAIL_ID = 1L; + private static final long TEL_ID = 2L; + + @Qualifier("myLdapService") + @Autowired + private LdapService ldapService; + + @Autowired + private LdapUserAttributesMapper ldapUserAttributesMapper; + + @MockBean + private InternalPersonService internalPersonService; + + @MockBean + private InternalPersonRepository internalPersonRepository; + + @MockBean + private CommunicationTypeRepository communicationTypeRepository; + + @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); + + List<LdapUser> ldapUserList = new ArrayList<>(1); + LdapUser ldapUser = MockDataHelper.mockLdapUser(); + ldapUser.setUid(internalPeople.get(0).getUid()); + ldapUserList.add( ldapUser ); + // users are found + when( ldapTemplate.search(any(LdapQuery.class), any(LdapUserAttributesMapper.class))).thenReturn( ldapUserList ); + + List<RefCommunicationType> refCommunicationTypes = MockDataHelper.mockRefCommunicationTypeList(); + 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); + + when(internalPersonRepository.saveAll(any( List.class))) + .then((Answer<List<TblInternalPerson>>) invocation -> { + Object[] args = invocation.getArguments(); + return (List<TblInternalPerson>) args[0]; + }); + + + 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); + + // users are not found + when( ldapTemplate.search(any(LdapQuery.class), any(LdapUserAttributesMapper.class))).thenReturn( Lists.emptyList() ); + + List<RefCommunicationType> refCommunicationTypes = MockDataHelper.mockRefCommunicationTypeList(); + 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); + + ldapService.synchronizeLDAP(); + Mockito.verify(internalPersonRepository, times(2)).saveAll(any(List.class)); + + } + + @Test + public void shouldFindExistingAndNotExistingLdapUsers1() throws Exception { + List<TblInternalPerson> internalPeople = MockDataHelper.mockTblInternalPersonPage().getContent(); + List<LdapUser> ldapUsers = new ArrayList<>(); + List<TblInternalPerson> allNotExistingLdapUsers = new ArrayList<>(); + + List<LdapUser> ldapUserList = new ArrayList<>(1); + LdapUser ldapUser = MockDataHelper.mockLdapUser(); + ldapUser.setUid(internalPeople.get(0).getUid()); + ldapUserList.add( ldapUser ); + // users are always found + when( ldapTemplate.search(any(LdapQuery.class), any(LdapUserAttributesMapper.class))).thenReturn( ldapUserList ); + + Whitebox.invokeMethod(ldapService, "findExistingAndNotExistingLdapUsers", + internalPeople, ldapUsers, allNotExistingLdapUsers); + + assertEquals( internalPeople.size(), ldapUsers.size()); + assertEquals( ldapUser, ldapUsers.get(0)); + assertEquals( ldapUser, ldapUsers.get(1)); + } + + @Test + public void shouldFindExistingAndNotExistingLdapUsers2() throws Exception { + List<TblInternalPerson> internalPeople = MockDataHelper.mockTblInternalPersonPage().getContent(); + List<LdapUser> ldapUsers = new ArrayList<>(); + List<TblInternalPerson> allNotExistingLdapUsers = new ArrayList<>(); + + // users are not found + when( ldapTemplate.search(any(LdapQuery.class), any(LdapUserAttributesMapper.class))).thenReturn( Lists.emptyList() ); + + Whitebox.invokeMethod(ldapService, "findExistingAndNotExistingLdapUsers", + internalPeople, ldapUsers, allNotExistingLdapUsers); + + assertEquals( 0, ldapUsers.size()); + } + + + @Test + public void shouldFindExistingAndNotExistingLdapUsers3() throws Exception { + List<TblInternalPerson> internalPeople = MockDataHelper.mockTblInternalPersonPage().getContent(); + List<LdapUser> ldapUsers = new ArrayList<>(); + List<TblInternalPerson> allNotExistingLdapUsers = new ArrayList<>(); + + List<LdapUser> ldapUserList = new ArrayList<>(1); + LdapUser ldapUser = MockDataHelper.mockLdapUser(); + ldapUser.setUid(internalPeople.get(0).getUid()); + ldapUserList.add( ldapUser ); + ldapUserList.add( new LdapUser()); + // too much users are found + when( ldapTemplate.search(any(LdapQuery.class), any(LdapUserAttributesMapper.class))).thenReturn( ldapUserList ); + + Whitebox.invokeMethod(ldapService, "findExistingAndNotExistingLdapUsers", + internalPeople, ldapUsers, allNotExistingLdapUsers); + + assertEquals( 0, ldapUsers.size()); + } + +} diff --git a/src/test/java/org/eclipse/openk/contactbasedata/support/MockDataHelper.java b/src/test/java/org/eclipse/openk/contactbasedata/support/MockDataHelper.java index 54e216e..bde9bac 100644 --- a/src/test/java/org/eclipse/openk/contactbasedata/support/MockDataHelper.java +++ b/src/test/java/org/eclipse/openk/contactbasedata/support/MockDataHelper.java @@ -364,8 +364,9 @@ public class MockDataHelper { ct2.setDescription("Mobil"); ct2.setType("Mobil"); RefCommunicationType ct3 = mockRefCommunicationType(); - ct3.setDescription("Pers�nlich"); - ct3.setType("Pers�nlich"); + ct3.setDescription("E-Mail"); + ct3.setType("E-Mail"); + ct3.setTypeEmail(true); List<RefCommunicationType> listCt = new ArrayList<>(); listCt.add(ct1); @@ -725,4 +726,26 @@ public class MockDataHelper { return list; } + public static LdapUser mockLdapUser() { + LdapUser ldapUser = new LdapUser(); + ldapUser.setUid( UUID.randomUUID().toString() ); + ldapUser.setFirstName("Ludwig"); + ldapUser.setLastName("Lumpensammler"); + ldapUser.setMail("ll@lumpi.de"); + + return ldapUser; + } + + public static List<LdapUser> mockLdapUsers() { + List<LdapUser> list = new ArrayList<>(); + LdapUser ldapUser2 = new LdapUser(); + ldapUser2.setUid( UUID.randomUUID().toString() ); + ldapUser2.setFirstName("Karl"); + ldapUser2.setLastName("Kleinfink"); + ldapUser2.setTelephoneNumber("99887766"); + list.add( mockLdapUser() ); + list.add( ldapUser2 ); + return list; + } + } |