DefaultSyncContext implements SyncContext { @NotNull @Override public SyncResult sync(@NotNull ExternalIdentity identity) throws SyncException { ExternalIdentityRef ref = identity.getExternalId(); if (!isSameIDP(ref)) { boolean isGroup = (identity instanceof ExternalGroup); return new DefaultSyncResultImpl(new DefaultSyncedIdentity(identity.getId(), ref, isGroup, -1), SyncResult.Status.FOREIGN); } try { DebugTimer timer = new DebugTimer(); DefaultSyncResultImpl ret; boolean created = false; if (identity instanceof ExternalUser) { User user = getAuthorizable(identity, User.class); timer.mark("find"); if (user == null) { user = createUser((ExternalUser) identity); timer.mark("create"); created = true; } ret = syncUser((ExternalUser) identity, user); timer.mark("sync"); } else if (identity instanceof ExternalGroup) { Group group = getAuthorizable(identity, Group.class); timer.mark("find"); if (group == null) { group = createGroup((ExternalGroup) identity); timer.mark("create"); created = true; } ret = syncGroup((ExternalGroup) identity, group); timer.mark("sync"); } else { throw new IllegalArgumentException("identity must be user or group but was: " + identity); } debug("sync({}) -> {} {}", ref.getString(), identity.getId(), timer.getString()); if (created) { ret.setStatus(SyncResult.Status.ADD); } return ret; } catch (RepositoryException e) { throw new SyncException(e); } } DefaultSyncContext(@NotNull DefaultSyncConfig config, @NotNull ExternalIdentityProvider idp, @NotNull UserManager userManager, @NotNull ValueFactory valueFactory); @Nullable static DefaultSyncedIdentity createSyncedIdentity(@Nullable Authorizable auth); @Nullable static ExternalIdentityRef getIdentityRef(@Nullable Authorizable auth); static String joinPaths(String... paths); @Override void close(); @Override boolean isKeepMissing(); @NotNull @Override SyncContext setKeepMissing(boolean keepMissing); @Override boolean isForceUserSync(); @NotNull @Override SyncContext setForceUserSync(boolean forceUserSync); @Override boolean isForceGroupSync(); @Override @NotNull SyncContext setForceGroupSync(boolean forceGroupSync); @NotNull @Override SyncResult sync(@NotNull ExternalIdentity identity); @NotNull @Override SyncResult sync(@NotNull String id); static final String REP_EXTERNAL_ID; static final String REP_LAST_SYNCED; }
@Test(expected = IllegalArgumentException.class) public void testSyncInvalidExternalIdentity() throws Exception { syncCtx.sync(new TestIdentityProvider.TestIdentity()); }
@Test public void testSyncForeignExternalUser() throws Exception { ExternalIdentity foreign = new TestIdentityProvider.ForeignExternalUser(); SyncResult res = syncCtx.sync(foreign); assertNotNull(res); assertSame(SyncResult.Status.FOREIGN, res.getStatus()); SyncedIdentity si = res.getIdentity(); assertNotNull(si); assertEquals(foreign.getId(), si.getId()); ExternalIdentityRef ref = si.getExternalIdRef(); assertNotNull(ref); assertEquals(foreign.getExternalId(), ref); assertFalse(si.isGroup()); assertEquals(-1, si.lastSynced()); assertFalse(root.hasPendingChanges()); }
@Test public void testSyncForeignExternalGroup() throws Exception { ExternalIdentity foreign = new TestIdentityProvider.ForeignExternalGroup(); SyncResult res = syncCtx.sync(foreign); assertNotNull(res); assertSame(SyncResult.Status.FOREIGN, res.getStatus()); SyncedIdentity si = res.getIdentity(); assertNotNull(si); assertEquals(foreign.getId(), si.getId()); ExternalIdentityRef ref = si.getExternalIdRef(); assertNotNull(ref); assertEquals(foreign.getExternalId(), ref); assertTrue(si.isGroup()); assertEquals(-1, si.lastSynced()); assertFalse(root.hasPendingChanges()); }
@Test public void testSyncByForeignUserId() throws Exception { SyncResult result = syncCtx.sync(getTestUser().getID()); assertEquals(SyncResult.Status.FOREIGN, result.getStatus()); SyncedIdentity si = result.getIdentity(); assertNotNull(si); assertNull(si.getExternalIdRef()); assertFalse(si.isGroup()); }
@Test public void testSyncByForeignGroupId() throws Exception { SyncResult result = syncCtx.sync(createTestGroup().getID()); assertEquals(SyncResult.Status.FOREIGN, result.getStatus()); SyncedIdentity si = result.getIdentity(); assertNotNull(si); assertNull(si.getExternalIdRef()); assertTrue(si.isGroup()); }
@Test public void testSyncAutoMembershipListsNonExistingGroup() throws Exception { syncConfig.user().setAutoMembership("nonExistingGroup"); SyncResult result = syncCtx.sync(idp.listUsers().next()); assertEquals(SyncResult.Status.ADD, result.getStatus()); }
@Test public void testSyncAutoMembershipListsUser() throws Exception { syncConfig.user().setAutoMembership(getTestUser().getID()); syncCtx.sync(idp.listUsers().next()); }