/** * Creates an array of JCR values based on the type. * @param propValues the given values * @return and array of JCR values * @throws RepositoryException if an error occurs */ @Nullable protected Value[] createValues(@NotNull Collection<?> propValues) throws RepositoryException { List<Value> values = new ArrayList<>(); for (Object obj : propValues) { Value v = createValue(obj); if (v != null) { values.add(v); } } return values.toArray(new Value[0]); }
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));
@Test public void testSyncProperties() throws Exception { ExternalUser externalUser = idp.getUser(TestIdentityProvider.ID_SECOND_USER); Authorizable a = syncCtx.createUser(externalUser); // create exact mapping 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)); } } }
@Test public void testCreateValueNull() throws Exception { assertNull(syncCtx.createValue(null)); }
@Test public void testSyncPropertiesRemapped() throws Exception { ExternalUser externalUser = idp.getUser(TestIdentityProvider.ID_SECOND_USER); Authorizable a = syncCtx.createUser(externalUser); // create exact mapping Map<String, String> mapping = new HashMap<>(); Map<String, ?> extProps = externalUser.getProperties(); for (String propName : extProps.keySet()) { mapping.put("a/"+propName, propName); } syncCtx.syncProperties(externalUser, a, mapping); for (String propName : extProps.keySet()) { String relPath = "a/" + propName; assertTrue(a.hasProperty(relPath)); Object obj = extProps.get(propName); Value[] vs = a.getProperty(relPath); if (vs.length == 1) { assertEquals(syncCtx.createValue(obj), a.getProperty(relPath)[0]); } else { Value[] expected = (obj instanceof Collection) ? syncCtx.createValues((Collection) obj) : syncCtx.createValues(Arrays.asList((Object[]) obj)); assertArrayEquals(expected, a.getProperty(relPath)); } } }
@Test public void testCreateValueDate() throws Exception { Date d = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(d); Value v = syncCtx.createValue(cal); assertNotNull(v); assertEquals(PropertyType.DATE, v.getType()); Value v2 = syncCtx.createValue(d); assertNotNull(v2); assertEquals(PropertyType.DATE, v2.getType()); assertEquals(v, v2); }
@Test public void testCreateValueString() throws Exception { Value v = syncCtx.createValue("s"); assertNotNull(v); assertEquals(PropertyType.STRING, v.getType()); assertEquals("s", v.getString()); v = syncCtx.createValue(new char[] {'s'}); assertNotNull(v); assertEquals(PropertyType.STRING, v.getType()); assertEquals("s", v.getString()); Object o = new TestIdentityProvider.ForeignExternalUser(); v = syncCtx.createValue(o); assertNotNull(v); assertEquals(PropertyType.STRING, v.getType()); assertEquals(o.toString(), v.getString()); }
@Test public void testCreateValueDecimal() throws Exception { BigDecimal dec = new BigDecimal(123); Value v = syncCtx.createValue(dec); assertNotNull(v); assertEquals(PropertyType.DECIMAL, v.getType()); assertEquals(dec, v.getDecimal()); }
@Test public void testCreateValueDouble() throws Exception { Value v = syncCtx.createValue(Double.valueOf(1.1)); assertNotNull(v); assertEquals(PropertyType.DOUBLE, v.getType()); assertEquals(1.1, v.getDouble(), 0); v = syncCtx.createValue(Float.NaN); assertNotNull(v); assertEquals(PropertyType.DOUBLE, v.getType()); assertEquals(Float.NaN, v.getDouble(), 0); }
@Test public void testCreateValueFromBytesArray() throws Exception { byte[] bytes = new byte[]{'a', 'b'}; ByteArrayInputStream is = new ByteArrayInputStream(bytes); Binary binary = valueFactory.createBinary(is); Value v = syncCtx.createValue(bytes); assertNotNull(v); assertEquals(PropertyType.BINARY, v.getType()); assertEquals(binary, v.getBinary()); }
/** * @see <a href="https://issues.apache.org/jira/browse/OAK-4231">OAK-4231</a> */ @Test public void testCreateValueFromBinary() throws Exception { byte[] bytes = new byte[]{'a', 'b'}; ByteArrayInputStream is = new ByteArrayInputStream(bytes); Binary binary = valueFactory.createBinary(is); Value v = syncCtx.createValue(binary); assertNotNull(v); assertEquals(PropertyType.BINARY, v.getType()); assertEquals(binary, v.getBinary()); }
/** * @see <a href="https://issues.apache.org/jira/browse/OAK-4231">OAK-4231</a> */ @Test public void testCreateValueFromInputStream() throws Exception { byte[] bytes = new byte[]{'a', 'b'}; ByteArrayInputStream is = new ByteArrayInputStream(bytes); Binary binary = valueFactory.createBinary(is); Value v = syncCtx.createValue(is); assertNotNull(v); assertEquals(PropertyType.BINARY, v.getType()); assertEquals(binary, v.getBinary()); }
@Test public void testCreateValueLong() throws Exception { Value v = syncCtx.createValue(Long.MAX_VALUE); assertNotNull(v); assertEquals(PropertyType.LONG, v.getType()); assertEquals(Long.MAX_VALUE, v.getLong()); v = syncCtx.createValue(Integer.valueOf(23)); assertNotNull(v); assertEquals(PropertyType.LONG, v.getType()); assertEquals(23, v.getLong()); v = syncCtx.createValue(Short.MIN_VALUE); assertNotNull(v); assertEquals(PropertyType.LONG, v.getType()); assertEquals(Short.MIN_VALUE, v.getLong()); v = syncCtx.createValue(Byte.MAX_VALUE); assertNotNull(v); assertEquals(PropertyType.LONG, v.getType()); assertEquals(Byte.MAX_VALUE, v.getLong()); }
@Test public void testCreateValueBoolean() throws Exception { Value v = syncCtx.createValue(true); assertNotNull(v); assertEquals(PropertyType.BOOLEAN, v.getType()); assertEquals(true, v.getBoolean()); }