DefaultSyncHandler implements SyncHandler { @NotNull @Override public Iterator<SyncedIdentity> listIdentities(@NotNull UserManager userManager) throws RepositoryException { final Iterator<Authorizable> iter = userManager.findAuthorizables(DefaultSyncContext.REP_EXTERNAL_ID, null); return new AbstractLazyIterator<SyncedIdentity>() { @Override protected SyncedIdentity getNext() { while (iter.hasNext()) { try { SyncedIdentity id = DefaultSyncContext.createSyncedIdentity(iter.next()); if (id != null && id.getExternalIdRef() != null) { return id; } } catch (RepositoryException e) { log.error("Error while fetching authorizables", e); break; } } return null; } }; } @SuppressWarnings("UnusedDeclaration")  DefaultSyncHandler();  DefaultSyncHandler(DefaultSyncConfig config); @NotNull @Override String getName(); @NotNull @Override SyncContext createContext(@NotNull ExternalIdentityProvider idp, @NotNull UserManager userManager,
                                     @NotNull ValueFactory valueFactory); @Override SyncedIdentity findIdentity(@NotNull UserManager userManager, @NotNull String id); @Override boolean requiresSync(@NotNull SyncedIdentity identity); @NotNull @Override Iterator<SyncedIdentity> listIdentities(@NotNull UserManager userManager);  }
@Test public void testListIdentitiesBeforeSync() throws Exception { Iterator<SyncedIdentity> identities = syncHandler.listIdentities(userManager); if (identities.hasNext()) { SyncedIdentity si = identities.next(); fail("Sync handler returned unexpected identity: " + si); } }
@Test public void testListIdentitiesAfterSync() throws Exception { sync(USER_ID, false); Set<String> expected = Sets.newHashSet(USER_ID); for (ExternalIdentityRef extRef : idp.getUser(USER_ID).getDeclaredGroups()) { expected.add(extRef.getId()); } Iterator<SyncedIdentity> identities = syncHandler.listIdentities(userManager); while (identities.hasNext()) { SyncedIdentity si = identities.next(); if (expected.contains(si.getId())) { expected.remove(si.getId()); assertNotNull(si.getExternalIdRef()); } else { fail("Sync handler returned unexpected identity: " + si); } } assertTrue(expected.isEmpty()); }
@Test public void testListIdentitiesIgnoresLocal() throws Exception { sync(USER_ID, false); Iterator<SyncedIdentity> identities = syncHandler.listIdentities(userManager); while (identities.hasNext()) { SyncedIdentity si = identities.next(); ExternalIdentityRef ref = si.getExternalIdRef(); assertNotNull(ref); assertNotNull(ref.getProviderName()); } }
@Test public void testListIdentitiesIgnoresMissingExternalIdRef() throws Exception { Iterator<Authorizable> it = Iterators.singletonIterator(getTestUser()); UserManager um = mock(UserManager.class); when(um.findAuthorizables(DefaultSyncContext.REP_EXTERNAL_ID, null)).thenReturn(it); Iterator<SyncedIdentity> identities = syncHandler.listIdentities(um); assertFalse(identities.hasNext()); }
@Test public void testListIdentitiesIgnoresNull() throws Exception { Iterator<Authorizable> it = Iterators.singletonIterator(null); UserManager um = mock(UserManager.class); when(um.findAuthorizables(DefaultSyncContext.REP_EXTERNAL_ID, null)).thenReturn(it); Iterator<SyncedIdentity> identities = syncHandler.listIdentities(um); assertFalse(identities.hasNext()); }