LdapMapper { public T fromEntry(Entry entry, Class<? extends T> entityClass) { if (entry.getDn() == null) throw new LdapMappingException("LDAP Entry DN cannot be null"); try { final T bean = entityClass.newInstance(); Class<?> currentClass = entityClass; while (currentClass != null) { mapToProperties(entry, currentClass, bean); currentClass = currentClass.getSuperclass(); if (currentClass == Object.class) break; } return bean; } catch (InstantiationException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (IllegalAccessException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (InvocationTargetException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (LdapInvalidAttributeValueException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (NoSuchMethodException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } } LdapMapper(); LdapMapping getMapping(final Class<?> clazz); Entry toEntry(Object obj, String baseDn); Set<String> getAttributeIds(Class<?> clazz); T fromEntry(Entry entry, Class<? extends T> entityClass); String getDn(final String rdnValue, final Class<?> clazz, final String baseDn); @Nonnull String getDn(final Object obj, final String baseDn); @Nullable String getRdnValue(final Object obj); ModifyRequest createModifyRequest(final Entry existingEntry, final T existing, final T up);  }
@Test public void mapsEntryToSimpleClass() throws LdapInvalidAttributeValueException, LdapInvalidDnException { final LdapMapper<Person> mapper = new LdapMapper<>(); final Entry entry = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); entry.put("objectClass", "organizationalPerson", "extensibleObject"); entry.put("uid", "hendy"); entry.put("uniqueIdentifier", "hendy.irawan"); entry.put("gn", "Hendy"); entry.put("sn", "Irawan"); entry.put("cn", "Hendy Irawan"); entry.put("mail", "hendy@soluvas.com"); log.info("Input Entry: {}", entry); final Person person = mapper.fromEntry(entry, Person.class); log.info("Output Person: {}", person); assertEquals("hendy", person.getId()); assertEquals("hendy.irawan", person.getSlug()); assertEquals("Hendy", person.getFirstName()); assertEquals("Irawan", person.getLastName()); assertEquals("Hendy Irawan", person.getName()); assertEquals(ImmutableSet.of("hendy@soluvas.com"), person.getEmails()); }
@Test public void mapsUserPasswordCorrectly() throws LdapInvalidAttributeValueException, LdapInvalidDnException { final LdapMapper<Person> mapper = new LdapMapper<>(); final Entry entry = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); entry.put("objectClass", "organizationalPerson", "extensibleObject"); entry.put("uid", "hendy"); final String password = "{SSHA}BacKnhFVjpSunHYgivCVPAzcavAZZe9QFtd51A=="; entry.put("userPassword", password.getBytes()); log.info("Input Entry: {}", entry); final Person person = mapper.fromEntry(entry, Person.class); log.info("Output Person: {}", person); assertEquals(password, person.getPassword()); }
@Test public void mapsMultiToSimpleClass() throws LdapInvalidAttributeValueException, LdapInvalidDnException { final LdapMapper<Person> mapper = new LdapMapper<>(); final Entry entry = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); entry.put("objectClass", "organizationalPerson", "extensibleObject"); entry.put("uid", "hendy"); entry.put("uniqueIdentifier", "hendy.irawan"); entry.put("gn", "Hendy"); entry.put("sn", "Irawan"); entry.put("cn", "Hendy Irawan"); entry.put("mail", "hendy@soluvas.com", "ceefour666@gmail.com", "hendy@bippo.co.id"); log.info("Input Entry: {}", entry); final Person person = mapper.fromEntry(entry, Person.class); log.info("Output Person: {}", person); assertEquals("hendy", person.getId()); assertEquals("hendy.irawan", person.getSlug()); assertEquals("Hendy", person.getFirstName()); assertEquals("Irawan", person.getLastName()); assertEquals("Hendy Irawan", person.getName()); assertEquals(ImmutableSet.of("hendy@soluvas.com", "ceefour666@gmail.com", "hendy@bippo.co.id"), person.getEmails()); }
@Test public void mapsEntryToSubclass() throws LdapInvalidAttributeValueException, LdapInvalidDnException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final Entry entry = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); entry.put("objectClass", "organizationalPerson", "extensibleObject", "socialPerson", "facebookObject", "twitterObject"); entry.put("uid", "hendy"); entry.put("uniqueIdentifier", "hendy.irawan"); entry.put("gn", "Hendy"); entry.put("sn", "Irawan"); entry.put("cn", "Hendy Irawan"); entry.put("mail", "hendy@soluvas.com", "ceefour666@gmail.com", "hendy@bippo.co.id"); entry.put("photoId", "hendy"); entry.put("fbId", "596326671"); entry.put("fbUser", "ceefour"); entry.put("twitterId", "384154265"); entry.put("twitterScreenName", "hendyirawan"); log.info("Input Entry: {}", entry); final SocialPerson person = mapper.fromEntry(entry, SocialPerson.class); log.info("Output Person: {}", person); assertEquals("hendy", person.getId()); assertEquals("hendy.irawan", person.getSlug()); assertEquals("Hendy", person.getFirstName()); assertEquals("Irawan", person.getLastName()); assertEquals("Hendy Irawan", person.getName()); assertEquals(ImmutableSet.of("hendy@soluvas.com", "ceefour666@gmail.com", "hendy@bippo.co.id"), person.getEmails()); assertEquals("hendy", person.getPhotoId()); assertEquals((Long)596326671L, person.getFacebookId()); assertEquals("ceefour", person.getFacebookUsername()); assertEquals((Long)384154265L, person.getTwitterId()); assertEquals("hendyirawan", person.getTwitterScreenName()); }
@Test public void mapsAttributeToLong() throws LdapInvalidAttributeValueException, LdapInvalidDnException { final LdapMapper<PersonWithLong> mapper = new LdapMapper<>(); final Entry entry = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); entry.put("objectClass", "organizationalPerson", "extensibleObject", "socialPerson", "facebookObject", "twitterObject"); entry.put("uid", "hendy"); entry.put("uniqueIdentifier", "hendy.irawan"); entry.put("gn", "Hendy"); entry.put("sn", "Irawan"); entry.put("cn", "Hendy Irawan"); entry.put("mail", "hendy@soluvas.com", "ceefour666@gmail.com", "hendy@bippo.co.id"); entry.put("employeeNumber", "123"); log.info("Input Entry: {}", entry); final PersonWithLong person = mapper.fromEntry(entry, PersonWithLong.class); log.info("Output Person: {}", person); assertEquals((Long)123L, person.getEmployeeNumber()); }
@Test public void mapsAttributeToBigDecimal() throws LdapInvalidAttributeValueException, LdapInvalidDnException { final LdapMapper<PersonWithBigDecimal> mapper = new LdapMapper<>(); final Entry entry = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); entry.put("objectClass", "organizationalPerson", "extensibleObject", "socialPerson", "facebookObject", "twitterObject"); entry.put("uid", "hendy"); entry.put("uniqueIdentifier", "hendy.irawan"); entry.put("gn", "Hendy"); entry.put("sn", "Irawan"); entry.put("cn", "Hendy Irawan"); entry.put("mail", "hendy@soluvas.com", "ceefour666@gmail.com", "hendy@bippo.co.id"); entry.put("debitBalance", "123.4567"); log.info("Input Entry: {}", entry); final PersonWithBigDecimal person = mapper.fromEntry(entry, PersonWithBigDecimal.class); log.info("Output Person: {}", person); assertEquals(new BigDecimal("123.4567"), person.getDebitBalance()); }
@Test public void mapsAttributeAlias() throws LdapInvalidAttributeValueException, LdapInvalidDnException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final Entry entry = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); entry.put("objectClass", "organizationalPerson", "extensibleObject", "socialPerson", "facebookObject", "twitterObject"); entry.put("uid", "hendy"); entry.put("uniqueIdentifier", "hendy.irawan"); entry.put("givenName", "Hendy"); entry.put("sn", "Irawan"); entry.put("cn", "Hendy Irawan"); entry.put("mail", "hendy@soluvas.com", "ceefour666@gmail.com", "hendy@bippo.co.id"); entry.put("magentoId", "123"); log.info("Input Entry: {}", entry); final SocialPerson person = mapper.fromEntry(entry, SocialPerson.class); log.info("Output Person: {}", person); assertEquals("Hendy", person.getFirstName()); }
@Test public void canMapFromDateToDateTime() throws LdapException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final Entry existing = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); existing.put("objectClass", "organizationalPerson", "extensibleObject", "inetOrgPerson", "uidObject"); existing.put("uid", "hendy"); existing.put("cn", "Hendy Irawan"); existing.put("birthDate", "19831213170000Z"); log.info("Input Entry: {}", existing); final SocialPerson person = mapper.fromEntry(existing, SocialPerson.class); final DateTimeZone wib = DateTimeZone.forID("Asia/Jakarta"); assertEquals(new DateTime(1983, 12, 14, 0, 0, wib), person.getBirthDate()); }
@Test public void canMapFromCurrencyUnit() throws LdapException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final Entry existing = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); existing.put("objectClass", "organizationalPerson", "extensibleObject", "inetOrgPerson", "uidObject"); existing.put("uid", "hendy"); existing.put("cn", "Hendy Irawan"); existing.put("currency", "IDR"); log.info("Input Entry: {}", existing); final SocialPerson person = mapper.fromEntry(existing, SocialPerson.class); final CurrencyUnit idr = Monetary.getCurrency("IDR"); assertEquals(idr, person.getCurrency()); }
@Test public void canMapFromBooleanTrue() throws LdapException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final Entry existing = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); existing.put("objectClass", "organizationalPerson", "extensibleObject", "inetOrgPerson", "uidObject"); existing.put("uid", "hendy"); existing.put("cn", "Hendy Irawan"); existing.put("newsletterSubscriptionEnabled", "TRUE"); log.info("Input Entry: {}", existing); final SocialPerson person = mapper.fromEntry(existing, SocialPerson.class); assertEquals(true, person.getNewsletterSubscriptionEnabled()); }
@Test public void canMapFromBooleanTrue2() throws LdapException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final Entry existing = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); existing.put("objectClass", "organizationalPerson", "extensibleObject", "inetOrgPerson", "uidObject"); existing.put("uid", "hendy"); existing.put("cn", "Hendy Irawan"); existing.put("newsletterSubscriptionEnabled", "true"); log.info("Input Entry: {}", existing); final SocialPerson person = mapper.fromEntry(existing, SocialPerson.class); assertEquals(true, person.getNewsletterSubscriptionEnabled()); }
@Test public void canMapFromBooleanFalse() throws LdapException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final Entry existing = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); existing.put("objectClass", "organizationalPerson", "extensibleObject", "inetOrgPerson", "uidObject"); existing.put("uid", "hendy"); existing.put("cn", "Hendy Irawan"); existing.put("newsletterSubscriptionEnabled", "FALSE"); log.info("Input Entry: {}", existing); final SocialPerson person = mapper.fromEntry(existing, SocialPerson.class); assertEquals(false, person.getNewsletterSubscriptionEnabled()); }
@Test public void canMapFromBooleanFalse2() throws LdapException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final Entry existing = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); existing.put("objectClass", "organizationalPerson", "extensibleObject", "inetOrgPerson", "uidObject"); existing.put("uid", "hendy"); existing.put("cn", "Hendy Irawan"); existing.put("newsletterSubscriptionEnabled", "false"); log.info("Input Entry: {}", existing); final SocialPerson person = mapper.fromEntry(existing, SocialPerson.class); assertEquals(false, person.getNewsletterSubscriptionEnabled()); }