From 4b77eadf8d00c330b4fe5d8af32c01bc30c872f8 Mon Sep 17 00:00:00 2001 From: sofyenne Date: Thu, 7 Sep 2023 12:26:06 +0100 Subject: [PATCH] update ipmortUserProfile method and add the unit test --- exo.core.component.organization.api/pom.xml | 5 + .../IDMExternalStoreImportService.java | 32 ++++- .../IDMExternalStoreImportServiceTest.java | 123 ++++++++++++++++++ 3 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 exo.core.component.organization.api/src/test/java/org/exoplatform/services/organization/api/IDMExternalStoreImportServiceTest.java diff --git a/exo.core.component.organization.api/pom.xml b/exo.core.component.organization.api/pom.xml index c3cd53a45..19b57b991 100644 --- a/exo.core.component.organization.api/pom.xml +++ b/exo.core.component.organization.api/pom.xml @@ -79,6 +79,11 @@ + + org.mockito + mockito-inline + test + diff --git a/exo.core.component.organization.api/src/main/java/org/exoplatform/services/organization/externalstore/IDMExternalStoreImportService.java b/exo.core.component.organization.api/src/main/java/org/exoplatform/services/organization/externalstore/IDMExternalStoreImportService.java index 8233357ec..e14d8b65f 100644 --- a/exo.core.component.organization.api/src/main/java/org/exoplatform/services/organization/externalstore/IDMExternalStoreImportService.java +++ b/exo.core.component.organization.api/src/main/java/org/exoplatform/services/organization/externalstore/IDMExternalStoreImportService.java @@ -776,13 +776,35 @@ private UserProfile importUserProfile(String username, boolean deleted) throws E LOG.trace("Remove from internal store deleted user profile '{}' from external store", username); organizationService.getUserProfileHandler().removeUserProfile(username, true); } + UserProfile externalUserProfile = externalStoreService.getEntity(IDMEntityType.USER_PROFILE, username); - Map userInfoMap = externalUserProfile.getUserInfoMap(); - if (!userInfoMap.containsKey("username")) { - userInfoMap.put("username", username); + + if (externalUserProfile != null && externalUserProfile.getUserInfoMap() != null + && !externalUserProfile.getUserInfoMap().isEmpty()) { + Map externalUserInfoMap = externalUserProfile.getUserInfoMap(); + Map internalUserInfoMap = (internalUserProfile == null + || internalUserProfile.getUserInfoMap() == null) ? new HashMap<>() : internalUserProfile.getUserInfoMap(); + boolean isModified = internalUserInfoMap.isEmpty(); + if (!isModified) { + Set externalUserProfileAttributes = externalUserInfoMap.keySet(); + for (String externalUserProfileAttribute : externalUserProfileAttributes) { + isModified |= !internalUserInfoMap.containsKey(externalUserProfileAttribute) + || !Objects.equals(externalUserInfoMap.get(externalUserProfileAttribute), + internalUserInfoMap.get(externalUserProfileAttribute)); + if (isModified) { + break; + } + } + } + + if (isModified) { + if (!externalUserInfoMap.containsKey("username")) { + externalUserInfoMap.put("username", username); + } + listenerService.broadcast(IDMExternalStoreService.USER_PROFILE_ADDED_FROM_EXTERNAL_STORE, this, externalUserInfoMap); + } } - listenerService.broadcast(IDMExternalStoreService.USER_PROFILE_ADDED_FROM_EXTERNAL_STORE, this, userInfoMap); - return null; + return internalUserProfile; } private User importUser(String username, boolean deleted, boolean updateModified, boolean updateDeleted) throws Exception { diff --git a/exo.core.component.organization.api/src/test/java/org/exoplatform/services/organization/api/IDMExternalStoreImportServiceTest.java b/exo.core.component.organization.api/src/test/java/org/exoplatform/services/organization/api/IDMExternalStoreImportServiceTest.java new file mode 100644 index 000000000..8a790a794 --- /dev/null +++ b/exo.core.component.organization.api/src/test/java/org/exoplatform/services/organization/api/IDMExternalStoreImportServiceTest.java @@ -0,0 +1,123 @@ +/* + * This file is part of the Meeds project (https://meeds.io/). + * + * Copyright (C) 2023 Meeds Association contact@meeds.io + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.exoplatform.services.organization.api; + +import static org.mockito.Mockito.*; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import org.exoplatform.container.ExoContainer; +import org.exoplatform.container.xml.InitParams; +import org.exoplatform.services.listener.ListenerService; +import org.exoplatform.services.organization.OrganizationService; +import org.exoplatform.services.organization.UserProfile; +import org.exoplatform.services.organization.UserProfileHandler; +import org.exoplatform.services.organization.externalstore.IDMExternalStoreImportService; +import org.exoplatform.services.organization.externalstore.IDMExternalStoreService; +import org.exoplatform.services.organization.externalstore.IDMQueueService; +import org.exoplatform.services.organization.externalstore.model.IDMEntityType; +import org.exoplatform.services.scheduler.JobSchedulerService; + +@RunWith(MockitoJUnitRunner.class) +public class IDMExternalStoreImportServiceTest { + + @Mock + ExoContainer container; + @Mock + JobSchedulerService jobSchedulerService; + @Mock + IDMQueueService idmQueueService; + @Mock + InitParams initParams; + @Mock + private OrganizationService organizationService; + @Mock + private IDMExternalStoreService idmExternalStoreService; + @Mock + private ListenerService listenerService; + private IDMExternalStoreImportService idmExternalStoreImportService; + + @Before + public void setUp() { + this.idmExternalStoreImportService = new IDMExternalStoreImportService(container, + organizationService, + listenerService, + idmExternalStoreService, + jobSchedulerService, + idmQueueService, + initParams); + } + + @Test + public void importUserProfileTest() throws Exception { + String userName = "john"; + UserProfile internalUserProfile = mock(UserProfile.class); + UserProfileHandler userProfileHandler = mock(UserProfileHandler.class); + when(organizationService.getUserProfileHandler()).thenReturn(userProfileHandler); + when(userProfileHandler.findUserProfileByName(userName)).thenReturn(null); + UserProfile externalUserProfile = mock(UserProfile.class); + when(idmExternalStoreService.getEntity(IDMEntityType.USER_PROFILE, userName)).thenReturn(externalUserProfile); + when(externalUserProfile.getUserName()).thenReturn(userName); + when(externalUserProfile.getUserInfoMap()).thenReturn(new HashMap<>()); + when(idmExternalStoreService.isEntityPresent(IDMEntityType.USER_PROFILE, userName)).thenReturn(true); + // + idmExternalStoreImportService.importEntityToInternalStore(IDMEntityType.USER_PROFILE, userName, false, false); + verify(listenerService, times(0)).broadcast(eq(IDMExternalStoreService.USER_PROFILE_ADDED_FROM_EXTERNAL_STORE), + anyObject(), + argThat(param -> param instanceof HashMap)); + + Map propertiesMap = new HashMap<>(); + propertiesMap.put("propertyKey", "propertyValue"); + when(userProfileHandler.findUserProfileByName(userName)).thenReturn(internalUserProfile); + when(internalUserProfile.getUserInfoMap()).thenReturn(propertiesMap); + when(externalUserProfile.getUserInfoMap()).thenReturn(propertiesMap); + // + idmExternalStoreImportService.importEntityToInternalStore(IDMEntityType.USER_PROFILE, userName, false, false); + verify(listenerService, times(0)).broadcast(eq(IDMExternalStoreService.USER_PROFILE_ADDED_FROM_EXTERNAL_STORE), + anyObject(), + argThat(param -> param instanceof HashMap)); + + when(internalUserProfile.getUserInfoMap()).thenReturn(new HashMap<>()); + // + idmExternalStoreImportService.importEntityToInternalStore(IDMEntityType.USER_PROFILE, userName, false, false); + verify(listenerService, times(1)).broadcast(eq(IDMExternalStoreService.USER_PROFILE_ADDED_FROM_EXTERNAL_STORE), + anyObject(), + argThat(param -> param instanceof HashMap)); + + Map updatedPropertyMap = new HashMap<>(); + updatedPropertyMap.put("propertyKey", "updatedPropertyValue"); + when(externalUserProfile.getUserInfoMap()).thenReturn(updatedPropertyMap); + when(internalUserProfile.getUserInfoMap()).thenReturn(propertiesMap); + // + idmExternalStoreImportService.importEntityToInternalStore(IDMEntityType.USER_PROFILE, userName, false, false); + verify(listenerService, atLeast(1)).broadcast(eq(IDMExternalStoreService.USER_PROFILE_ADDED_FROM_EXTERNAL_STORE), + anyObject(), + argThat(param -> param instanceof HashMap)); + + } + +}