Delegatee { @NotNull String[] syncExternalUsers(@NotNull String[] externalIds) { ResultMessages messages = new ResultMessages(); context.setForceGroupSync(true).setForceUserSync(true); List<SyncResult> results = new ArrayList<>(batchSize); for (String externalId : externalIds) { ExternalIdentityRef ref = ExternalIdentityRef.fromString(externalId); if (!idp.getName().equals(ref.getProviderName())) { results.add(new DefaultSyncResultImpl(new DefaultSyncedIdentity(ref.getId(), ref, false, -1), SyncResult.Status.FOREIGN)); } else { try { ExternalIdentity id = idp.getIdentity(ref); if (id != null) { results = syncUser(id, results, messages); } else { results.add(new DefaultSyncResultImpl( new DefaultSyncedIdentity("", ref, false, -1), SyncResult.Status.NO_SUCH_IDENTITY )); } } catch (ExternalIdentityException e) { log.warn("error while fetching the external identity {}", externalId, e); results.add(new ErrorSyncResult(ref, e)); } } } commit(messages, results, NO_BATCH_SIZE); return messages.getMessages(); } private  Delegatee(@NotNull SyncHandler handler, @NotNull ExternalIdentityProvider idp,
                      @NotNull ContentSession systemSession, @NotNull SecurityProvider securityProvider, int batchSize);   }
@Test public void testSyncNonExistingExternalUserSaveError() throws Exception { Root r = preventRootCommit(delegatee);; String[] result = delegatee.syncExternalUsers(new String[] {new ExternalIdentityRef("nonExisting", idp.getName()).getString()}); assertResultMessages(result, "", "nsi"); assertFalse(r.hasPendingChanges()); }
@Test public void testSyncForeignExternalUserSaveError() throws Exception { Root r = preventRootCommit(delegatee);; String[] result = delegatee.syncExternalUsers(new String[] {new ExternalIdentityRef(ID_TEST_USER, foreignIDP.getName()).getString()}); assertResultMessages(result, ID_TEST_USER, "for"); assertFalse(r.hasPendingChanges()); }
@Test public void testSyncThrowingExternalUserSaveError() throws Exception { Root r = preventRootCommit(delegatee);; String[] result = delegatee.syncExternalUsers(new String[] {new ExternalIdentityRef(TestIdentityProvider.ID_EXCEPTION, idp.getName()).getString()}); assertResultMessages(result, TestIdentityProvider.ID_EXCEPTION, "ERR"); assertFalse(r.hasPendingChanges()); }
@Test public void testSyncExternalUsersSaveError() throws Exception { Root r = preventRootCommit(delegatee);; List<String> externalIds = new ArrayList<>(); for (String id : TEST_IDS) { externalIds.add(new ExternalIdentityRef(id, idp.getName()).getString()); } String[] result = delegatee.syncExternalUsers(externalIds.toArray(new String[0])); assertResultMessages(result, ImmutableMap.of( ID_TEST_USER, "ERR", TestIdentityProvider.ID_SECOND_USER, "ERR", TestIdentityProvider.ID_WILDCARD_USER, "ERR")); assertFalse(r.hasPendingChanges()); }
@Test public void testSyncExternalUsersGeneratesNullIdentity() throws Exception { SyncContext ctx = mock(SyncContext.class); when(ctx.sync(any(ExternalIdentity.class))).thenReturn(new DefaultSyncResultImpl(null, SyncResult.Status.NOP)); when(ctx.setForceGroupSync(anyBoolean())).thenReturn(ctx); SyncHandler syncHandler = when(mock(SyncHandler.class).createContext(any(ExternalIdentityProvider.class), any(UserManager.class), any(ValueFactory.class))).thenReturn(ctx).getMock(); Delegatee d = createDelegatee(new TestIdentityProvider(), syncHandler); ExternalIdentityRef ref = new ExternalIdentityRef(ID_TEST_USER, DEFAULT_IDP_NAME); String[] res = d.syncExternalUsers(new String[] {ref.getString()}); assertResultMessages(res, ID_TEST_USER, "nsi"); }