DefaultSyncContext implements SyncContext { protected void syncProperties(@NotNull ExternalIdentity ext, @NotNull Authorizable auth, @NotNull Map<String, String> mapping) throws RepositoryException { Map<String, ?> properties = ext.getProperties(); for (Map.Entry<String, String> entry : mapping.entrySet()) { String relPath = entry.getKey(); String name = entry.getValue(); Object obj = properties.get(name); if (obj == null) { int nameLen = name.length(); if (nameLen > 1 && name.charAt(0) == '"' && name.charAt(nameLen-1) == '"') { auth.setProperty(relPath, valueFactory.createValue(name.substring(1, nameLen - 1))); } else { auth.removeProperty(relPath); } } else { if (obj instanceof Collection) { auth.setProperty(relPath, createValues((Collection) obj)); } else if (obj instanceof byte[] || obj instanceof char[]) { auth.setProperty(relPath, createValue(obj)); } else if (obj instanceof Object[]) { auth.setProperty(relPath, createValues(Arrays.asList((Object[]) obj))); } else { auth.setProperty(relPath, createValue(obj)); } } } } 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 public void testSyncProperties() throws Exception { ExternalUser externalUser = idp.getUser(TestIdentityProvider.ID_SECOND_USER); Authorizable a = syncCtx.createUser(externalUser); Map<String, String> mapping = new HashMap<>(); Map<String, ?> extProps = externalUser.getProperties(); for (String propName : extProps.keySet()) { mapping.put(propName, propName); } syncCtx.syncProperties(externalUser, a, mapping); for (String propName : extProps.keySet()) { assertTrue(a.hasProperty(propName)); Object obj = extProps.get(propName); Value[] vs = a.getProperty(propName); if (vs.length == 1) { assertEquals(syncCtx.createValue(obj), a.getProperty(propName)[0]); } else { Value[] expected = (obj instanceof Collection) ? syncCtx.createValues((Collection) obj) : syncCtx.createValues(Arrays.asList((Object[]) obj)); assertArrayEquals(expected, a.getProperty(propName)); } } }