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 index a55b16a4..d7ee73db 100644 --- 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 @@ -19,8 +19,10 @@ */ package org.exoplatform.services.organization.api; +import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.*; +import java.net.URL; import java.util.HashMap; import java.util.Map; @@ -30,55 +32,63 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; -import org.exoplatform.container.ExoContainer; +import org.exoplatform.container.StandaloneContainer; import org.exoplatform.container.xml.InitParams; import org.exoplatform.services.listener.ListenerService; +import org.exoplatform.services.organization.BaseOrganizationService; 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; +import org.exoplatform.services.organization.impl.UserProfileImpl; @RunWith(MockitoJUnitRunner.class) public class IDMExternalStoreImportServiceTest { @Mock - ExoContainer container; - @Mock - JobSchedulerService jobSchedulerService; - @Mock - IDMQueueService idmQueueService; - @Mock - InitParams initParams; - @Mock - private OrganizationService organizationService; + private InitParams initParams; + @Mock private IDMExternalStoreService idmExternalStoreService; + @Mock private ListenerService listenerService; + private IDMExternalStoreImportService idmExternalStoreImportService; + private OrganizationService organizationService; + + private UserProfileHandler profileHandler; + @Before - public void setUp() { + public void setUp() throws Exception { + URL containerConfURL = TestUserHandler.class.getResource("/conf/standalone/test-configuration.xml"); + assertNotNull(containerConfURL); + + String containerConf = containerConfURL.toString(); + StandaloneContainer.addConfigurationURL(containerConf); + StandaloneContainer container = StandaloneContainer.getInstance(); + + organizationService = + (BaseOrganizationService) container.getComponentInstance(org.exoplatform.services.organization.OrganizationService.class); + assertNotNull(organizationService); + + profileHandler = organizationService.getUserProfileHandler(); this.idmExternalStoreImportService = new IDMExternalStoreImportService(container, organizationService, listenerService, idmExternalStoreService, - jobSchedulerService, - idmQueueService, + null, + null, 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); + profileHandler.createUserProfileInstance(); UserProfile externalUserProfile = mock(UserProfile.class); when(idmExternalStoreService.getEntity(IDMEntityType.USER_PROFILE, userName)).thenReturn(externalUserProfile); when(externalUserProfile.getUserInfoMap()).thenReturn(new HashMap<>()); @@ -89,34 +99,39 @@ public void importUserProfileTest() throws Exception { 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); + when(externalUserProfile.getUserInfoMap()).thenReturn(new HashMap<>()); // 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<>()); + Map propertiesMap = new HashMap<>(); + propertiesMap.put("propertyKey", "propertyValue"); + when(externalUserProfile.getUserInfoMap()).thenReturn(propertiesMap); // 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)); + UserProfile userProfile = new UserProfileImpl(); + userProfile.setUserName(userName); + userProfile.setUserInfoMap(propertiesMap); + profileHandler.saveUserProfile(userProfile, false); + // + idmExternalStoreImportService.importEntityToInternalStore(IDMEntityType.USER_PROFILE, userName, false, false); + verify(listenerService, atLeast(0)).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)); - } }