private static void append(@NotNull List<String> list, @NotNull SyncResult r) { if (r instanceof ErrorSyncResult) { ((ErrorSyncResult) r).append(list); } else { append(list, r.getIdentity(), getOperationFromStatus(r.getStatus()), null); } }
private void sync(@NotNull ExternalIdentity externalIdentity, @NotNull SyncResult.Status expectedStatus) throws Exception { SyncResult result = syncContext.sync(externalIdentity); assertSame(expectedStatus, result.getStatus()); r.commit(); }
@NotNull private List<SyncResult> syncUser(@NotNull ExternalIdentity id, @NotNull List<SyncResult> results, @NotNull List<String> list) { try { SyncResult r = context.sync(id); if (r.getIdentity() == null) { r = new DefaultSyncResultImpl( new DefaultSyncedIdentity(id.getId(), id.getExternalId(), false, -1), SyncResult.Status.NO_SUCH_IDENTITY ); log.warn("sync failed. {}", r.getIdentity()); } else { log.info("synced {}", r.getIdentity()); } results.add(r); } catch (SyncException e) { log.error(ERROR_SYNC_USER, id, e); results.add(new ErrorSyncResult(id.getExternalId(), e)); } return commit(list, results, batchSize); }
/** * Test utility method to synchronize the given identity into the repository. * This is intended to simplify those tests that require a given user/group * to be synchronized before executing the test. * * @param externalIdentity The external identity to be synchronized. * @throws Exception */ private void sync(@NotNull ExternalIdentity externalIdentity) throws Exception { SyncResult result = syncCtx.sync(externalIdentity); assertSame(SyncResult.Status.ADD, result.getStatus()); root.commit(); }
@Test public void testGetIdentityFromNull() { SyncResult res = new DefaultSyncResultImpl(null, SyncResult.Status.NOP); assertNull(res.getIdentity()); }
private static void append(@NotNull List<String> list, @NotNull List<SyncResult> results, @NotNull Exception e) { for (SyncResult result : results) { if (result instanceof ErrorSyncResult) { ((ErrorSyncResult) result).append(list); } else { SyncResult.Status st = result.getStatus(); switch (st) { case ADD: case DELETE: case UPDATE: case ENABLE: case DISABLE: append(list, result.getIdentity(), e); break; default: append(list, result); } } } }
@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 testUniqueConstraintNonUserNode() throws Exception { try { SyncResult res = syncCtx.sync(idp.getUser(USER_ID)); Tree nonUserTree = r.getTree("/"); nonUserTree.setProperty(DefaultSyncContext.REP_EXTERNAL_ID, res.getIdentity().getExternalIdRef().getString()); r.commit(); fail("Duplicate value for rep:externalId must be detected in the default setup."); } catch (CommitFailedException e) { // success: verify nature of the exception assertTrue(e.isConstraintViolation()); assertEquals(30, e.getCode()); } finally { r.refresh(); } } }
private void sync(@NotNull String id, boolean isGroup) throws Exception { ctx = sh.createContext(idp, userManager, valueFactory); ExternalIdentity exIdentity = (isGroup) ? idp.getGroup(id) : idp.getUser(id); assertNotNull(exIdentity); SyncResult res = ctx.sync(exIdentity); assertEquals(idp.getName(), res.getIdentity().getExternalIdRef().getProviderName()); assertSame(SyncResult.Status.ADD, res.getStatus()); r.commit(); }
@Test public void testSyncExternalGroup() throws Exception { ExternalGroup gr = idp.listGroups().next(); assertNotNull(gr); SyncResult result = syncCtx.sync(gr); assertEquals(SyncResult.Status.ADD, result.getStatus()); result = syncCtx.sync(gr); assertEquals(SyncResult.Status.NOP, result.getStatus()); syncCtx.setForceGroupSync(true); result = syncCtx.sync(gr); assertEquals(SyncResult.Status.UPDATE, result.getStatus()); }
@Test public void testUniqueConstraint() throws Exception { SyncResult res = syncCtx.sync(idp.getUser(USER_ID)); try { Tree t = r.getTree(getTestUser().getPath()); t.setProperty(DefaultSyncContext.REP_EXTERNAL_ID, res.getIdentity().getExternalIdRef().getString()); r.commit(); fail("Duplicate value for rep:externalId must be detected in the default setup."); } catch (CommitFailedException e) { // success: verify nature of the exception assertTrue(e.isConstraintViolation()); assertEquals(30, e.getCode()); } finally { r.refresh(); } }
@Test public void testSyncAutoMembership() throws Exception { Group gr = createTestGroup(); syncConfig.user().setAutoMembership(gr.getID()); SyncResult result = syncCtx.sync(idp.listUsers().next()); assertEquals(SyncResult.Status.ADD, result.getStatus()); Authorizable a = userManager.getAuthorizable(result.getIdentity().getId()); assertTrue(gr.isDeclaredMember(a)); }
@Test public void testSyncExternalUser() throws Exception { ExternalUser user = idp.listUsers().next(); assertNotNull(user); SyncResult result = syncCtx.sync(user); assertEquals(SyncResult.Status.ADD, result.getStatus()); result = syncCtx.sync(user); assertEquals(SyncResult.Status.NOP, result.getStatus()); syncCtx.setForceUserSync(true); result = syncCtx.sync(user); assertEquals(SyncResult.Status.UPDATE, result.getStatus()); }
@Test public void testUniqueConstraintSubsequentCommit() throws Exception { SyncResult res = syncCtx.sync(idp.getUser(USER_ID)); r.commit(); try { Tree t = r.getTree(getTestUser().getPath()); t.setProperty(DefaultSyncContext.REP_EXTERNAL_ID, res.getIdentity().getExternalIdRef().getString()); r.commit(); fail("Duplicate value for rep:externalId must be detected in the default setup."); } catch (CommitFailedException e) { // success: verify nature of the exception assertTrue(e.isConstraintViolation()); assertEquals(30, e.getCode()); } finally { r.refresh(); } }
@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 testSyncAutoMembershipListsNonExistingGroup() throws Exception { syncConfig.user().setAutoMembership("nonExistingGroup"); SyncResult result = syncCtx.sync(idp.listUsers().next()); assertEquals(SyncResult.Status.ADD, result.getStatus()); }
@Test public void testSyncGroupsForeign() throws Exception { // sync user from foreign IDP into the repository SyncResult res = sync(foreignIDP, "a", true); assertNotNull(getUserManager().getAuthorizable("a")); assertEquals(foreignIDP.getGroup("a").getExternalId(), res.getIdentity().getExternalIdRef()); // syncUsers with testIDP must detect the foreign status String[] result = syncMBean.syncUsers(new String[]{"a"}, false); assertResultMessages(result, "a", "for"); assertNotNull(getUserManager().getAuthorizable("a")); // same expected with 'purge' set to true result = syncMBean.syncUsers(new String[] {"a"}, true); assertResultMessages(result, "a", "for"); assertNotNull(getUserManager().getAuthorizable("a")); }
@Test public void testSyncExternalForeignLocalUser() throws Exception { ExternalUser external = idp.listUsers().next(); syncCtx.sync(external); User u = userManager.getAuthorizable(external.getId(), User.class); setExternalID(u, "differentIDP"); SyncResult result = syncCtx.sync(external); assertEquals(SyncResult.Status.FOREIGN, result.getStatus()); SyncedIdentity si = result.getIdentity(); assertNotNull(si); assertEquals(external.getExternalId(), si.getExternalIdRef()); }
@Test public void testSyncGroupById() throws Exception { ExternalIdentity externalId = idp.listGroups().next(); // no initial sync -> sync-by-id doesn't succeed SyncResult result = syncCtx.sync(externalId.getId()); assertEquals(SyncResult.Status.NO_SUCH_AUTHORIZABLE, result.getStatus()); // force sync syncCtx.sync(externalId); // try again syncCtx.setForceGroupSync(true); result = syncCtx.sync(externalId.getId()); assertEquals(SyncResult.Status.UPDATE, result.getStatus()); }
@Test public void testSyncUsersForeign() throws Exception { // sync user from foreign IDP into the repository SyncResult res = sync(foreignIDP, TestIdentityProvider.ID_TEST_USER, false); assertNotNull(getUserManager().getAuthorizable(TestIdentityProvider.ID_TEST_USER)); assertEquals(foreignIDP.getUser(TestIdentityProvider.ID_TEST_USER).getExternalId(), res.getIdentity().getExternalIdRef()); // syncUsers with testIDP must detect the foreign status String[] result = syncMBean.syncUsers(new String[]{TestIdentityProvider.ID_TEST_USER}, false); assertResultMessages(result, TestIdentityProvider.ID_TEST_USER, "for"); assertNotNull(getUserManager().getAuthorizable(TestIdentityProvider.ID_TEST_USER)); // same expected with 'purge' set to true result = syncMBean.syncUsers(new String[] {TestIdentityProvider.ID_TEST_USER}, true); assertResultMessages(result, TestIdentityProvider.ID_TEST_USER, "for"); assertNotNull(getUserManager().getAuthorizable(TestIdentityProvider.ID_TEST_USER)); }