Skip to content

Commit

Permalink
update UT
Browse files Browse the repository at this point in the history
  • Loading branch information
sofyenne committed Sep 7, 2023
1 parent d0082d5 commit 9dc22c2
Showing 1 changed file with 42 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

@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);
UserProfile internalUserProfile = profileHandler.createUserProfileInstance();
UserProfile externalUserProfile = mock(UserProfile.class);
when(idmExternalStoreService.getEntity(IDMEntityType.USER_PROFILE, userName)).thenReturn(externalUserProfile);
when(externalUserProfile.getUserInfoMap()).thenReturn(new HashMap<>());
Expand All @@ -89,34 +99,40 @@ public void importUserProfileTest() throws Exception {
anyObject(),
argThat(param -> param instanceof HashMap<?, ?>));

Map<String, String> 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<String, String> 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);
internalUserProfile = profileHandler.findUserProfileByName(userName);
//
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<String, String> 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<?, ?>));

}

}

0 comments on commit 9dc22c2

Please sign in to comment.