DynamicSyncContext extends DefaultSyncContext { @NotNull @Override public SyncResult sync(@NotNull ExternalIdentity identity) throws SyncException { if (identity instanceof ExternalUser) { return super.sync(identity); } else if (identity instanceof ExternalGroup) { try { Group group = getAuthorizable(identity, Group.class); if (group != null) { return syncGroup((ExternalGroup) identity, group); } else { ExternalIdentityRef ref = identity.getExternalId(); log.debug("ExternalGroup {}: Not synchronized as authorizable Group into the repository.", ref.getString()); SyncResult.Status status = (isSameIDP(ref)) ? SyncResult.Status.NOP : SyncResult.Status.FOREIGN; return new DefaultSyncResultImpl(new DefaultSyncedIdentity(identity.getId(), ref, true, -1), status); } } catch (RepositoryException e) { throw new SyncException(e); } } else { throw new IllegalArgumentException("identity must be user or group but was: " + identity); } } DynamicSyncContext(@NotNull DefaultSyncConfig config,
                              @NotNull ExternalIdentityProvider idp,
                              @NotNull UserManager userManager,
                              @NotNull ValueFactory valueFactory); @NotNull @Override SyncResult sync(@NotNull ExternalIdentity identity);  }
@Test(expected = IllegalArgumentException.class) public void testSyncExternalIdentity() throws Exception { syncContext.sync(new TestIdentityProvider.TestIdentity()); }
@Test public void testSyncExternalUser() throws Exception { ExternalUser externalUser = idp.getUser(USER_ID); sync(externalUser, SyncResult.Status.ADD); assertNotNull(userManager.getAuthorizable(USER_ID)); }
@Test public void testSyncExternalUserDepth0() throws Exception { syncConfig.user().setMembershipNestingDepth(0); ExternalUser externalUser = idp.getUser(USER_ID); sync(externalUser, SyncResult.Status.ADD); Tree tree = r.getTree(userManager.getAuthorizable(USER_ID).getPath()); PropertyState extPrincipalNames = tree.getProperty(REP_EXTERNAL_PRINCIPAL_NAMES); assertNotNull(extPrincipalNames); assertEquals(0, extPrincipalNames.count()); }
@Test public void testSyncExternalUserDepth1() throws Exception { syncConfig.user().setMembershipNestingDepth(1); ExternalUser externalUser = idp.getUser(USER_ID); sync(externalUser, SyncResult.Status.ADD); Tree tree = r.getTree(userManager.getAuthorizable(USER_ID).getPath()); PropertyState extPrincipalNames = tree.getProperty(REP_EXTERNAL_PRINCIPAL_NAMES); assertNotNull(extPrincipalNames); Set<String> pNames = Sets.newHashSet(extPrincipalNames.getValue(Type.STRINGS)); for (ExternalIdentityRef ref : externalUser.getDeclaredGroups()) { assertTrue(pNames.remove(idp.getIdentity(ref).getPrincipalName())); } assertTrue(pNames.isEmpty()); }
@Test public void testSyncExternalUserDepthInfinite() throws Exception { syncConfig.user().setMembershipNestingDepth(Long.MAX_VALUE); ExternalUser externalUser = idp.getUser(USER_ID); sync(externalUser, SyncResult.Status.ADD); Tree tree = r.getTree(userManager.getAuthorizable(USER_ID).getPath()); PropertyState extPrincipalNames = tree.getProperty(REP_EXTERNAL_PRINCIPAL_NAMES); assertNotNull(extPrincipalNames); Set<String> pNames = Sets.newHashSet(extPrincipalNames.getValue(Type.STRINGS)); Set<String> expected = Sets.newHashSet(); collectGroupPrincipals(expected, externalUser.getDeclaredGroups(), Long.MAX_VALUE); assertEquals(expected, pNames); }
@Test public void testSyncExternalUserExistingGroups() throws Exception { syncConfig.user().setMembershipNestingDepth(1); ExternalUser externalUser = idp.getUser(USER_ID); DefaultSyncContext ctx = new DefaultSyncContext(syncConfig, idp, userManager, valueFactory); ctx.sync(externalUser); ctx.close(); Authorizable a = userManager.getAuthorizable(USER_ID); assertSyncedMembership(userManager, a, externalUser); syncContext.setForceUserSync(true); syncConfig.user().setMembershipExpirationTime(-1); syncContext.sync(externalUser); Tree t = r.getTree(a.getPath()); assertFalse(t.hasProperty(REP_EXTERNAL_PRINCIPAL_NAMES)); assertSyncedMembership(userManager, a, externalUser); }
@Test public void testSyncExternalGroup() throws Exception { ExternalGroup gr = idp.listGroups().next(); syncContext.sync(gr); assertNull(userManager.getAuthorizable(gr.getId())); assertFalse(r.hasPendingChanges()); }
@Test public void testSyncExternalGroupVerifyStatus() throws Exception { ExternalGroup gr = idp.listGroups().next(); SyncResult result = syncContext.sync(gr); assertEquals(SyncResult.Status.NOP, result.getStatus()); result = syncContext.sync(gr); assertEquals(SyncResult.Status.NOP, result.getStatus()); syncContext.setForceGroupSync(true); result = syncContext.sync(gr); assertEquals(SyncResult.Status.NOP, result.getStatus()); }
@Test public void testSyncExternalGroupExisting() throws Exception { ExternalGroup externalGroup = idp.listGroups().next(); SyncContext ctx = new DefaultSyncContext(syncConfig, idp, userManager, valueFactory); ctx.sync(externalGroup); ctx.close(); syncContext.setForceGroupSync(true); SyncResult result = syncContext.sync(externalGroup); assertSame(SyncResult.Status.UPDATE, result.getStatus()); }
@Test public void testSyncForeignExternalGroup() throws Exception { ExternalGroup foreign = new TestIdentityProvider.ForeignExternalGroup(); SyncResult res = syncContext.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(r.hasPendingChanges()); }
@Test public void testSyncUserByIdUpdate() throws Exception { ExternalIdentity externalId = idp.listUsers().next(); Authorizable a = userManager.createUser(externalId.getId(), null); a.setProperty(DefaultSyncContext.REP_EXTERNAL_ID, valueFactory.createValue(externalId.getExternalId().getString())); syncContext.setForceUserSync(true); SyncResult result = syncContext.sync(externalId.getId()); assertEquals(SyncResult.Status.UPDATE, result.getStatus()); Tree t = r.getTree(a.getPath()); assertTrue(t.hasProperty(REP_EXTERNAL_PRINCIPAL_NAMES)); }
@Test public void testSyncUserIdExistingGroups() throws Exception { ExternalUser externalUser = idp.getUser(USER_ID); DefaultSyncContext ctx = new DefaultSyncContext(syncConfig, idp, userManager, valueFactory); ctx.sync(externalUser); ctx.close(); Authorizable user = userManager.getAuthorizable(externalUser.getId()); for (ExternalIdentityRef ref : externalUser.getDeclaredGroups()) { Group gr = userManager.getAuthorizable(ref.getId(), Group.class); assertTrue(gr.isMember(user)); } syncContext.setForceUserSync(true); syncContext.sync(externalUser.getId()); Authorizable a = userManager.getAuthorizable(USER_ID); Tree t = r.getTree(a.getPath()); assertFalse(t.hasProperty(REP_EXTERNAL_PRINCIPAL_NAMES)); assertSyncedMembership(userManager, a, externalUser); }
@Test public void testSyncMembershipWithForeignGroups() throws Exception { TestIdentityProvider.TestUser testuser = (TestIdentityProvider.TestUser) idp.getUser(ID_TEST_USER); Set<ExternalIdentityRef> sameIdpGroups = ImmutableSet.copyOf(testuser.getDeclaredGroups()); TestIdentityProvider.ForeignExternalGroup foreignGroup = new TestIdentityProvider.ForeignExternalGroup(); testuser.withGroups(foreignGroup.getExternalId()); assertFalse(Iterables.elementsEqual(sameIdpGroups, testuser.getDeclaredGroups())); sync(testuser, SyncResult.Status.ADD); Authorizable a = userManager.getAuthorizable(ID_TEST_USER); assertTrue(a.hasProperty(REP_EXTERNAL_PRINCIPAL_NAMES)); Value[] extPrincipalNames = a.getProperty(REP_EXTERNAL_PRINCIPAL_NAMES); assertEquals(Iterables.size(sameIdpGroups), extPrincipalNames.length); for (Value v : extPrincipalNames) { assertNotEquals(foreignGroup.getPrincipalName(), v.getString()); } }
@Test public void testSyncMembershipWithUserRef() throws Exception { TestIdentityProvider.TestUser testuser = (TestIdentityProvider.TestUser) idp.getUser(ID_TEST_USER); Set<ExternalIdentityRef> groupRefs = ImmutableSet.copyOf(testuser.getDeclaredGroups()); ExternalUser second = idp.getUser(ID_SECOND_USER); testuser.withGroups(second.getExternalId()); assertFalse(Iterables.elementsEqual(groupRefs, testuser.getDeclaredGroups())); sync(testuser, SyncResult.Status.ADD); Authorizable a = userManager.getAuthorizable(ID_TEST_USER); assertTrue(a.hasProperty(REP_EXTERNAL_PRINCIPAL_NAMES)); Value[] extPrincipalNames = a.getProperty(REP_EXTERNAL_PRINCIPAL_NAMES); assertEquals(Iterables.size(groupRefs), extPrincipalNames.length); for (Value v : extPrincipalNames) { assertNotEquals(second.getPrincipalName(), v.getString()); } }
@Test public void testAutoMembership() throws Exception { Group gr = userManager.createGroup("group" + UUID.randomUUID()); r.commit(); syncConfig.user().setAutoMembership(gr.getID(), "non-existing-group"); SyncResult result = syncContext.sync(idp.getUser(USER_ID)); assertSame(SyncResult.Status.ADD, result.getStatus()); User u = userManager.getAuthorizable(USER_ID, User.class); assertFalse(gr.isDeclaredMember(u)); assertFalse(gr.isMember(u)); }