LdapMapper { public Entry toEntry(Object obj, String baseDn) { Preconditions.checkNotNull(obj, "Cannot map null object in %s", baseDn); final Class<?> clazz = obj.getClass(); log.trace("Mapping {} {} as Entry in {}", clazz.getName(), obj, baseDn); final DefaultEntry entry = new DefaultEntry(); LdapEntry entryAnn = clazz.getAnnotation(LdapEntry.class); if (entryAnn == null) { throw new LdapMappingException(clazz.getName() + " must be annotated with @LdapEntry"); } final String[] objectClasses = entryAnn.objectClasses(); log.trace("{} maps to objectClasses: {}", clazz.getName(), objectClasses); try { entry.add("objectClass", objectClasses); Class<?> currentClass = clazz; while (currentClass != null) { mapFromProperties(obj, baseDn, currentClass, entry); currentClass = currentClass.getSuperclass(); if (currentClass == Object.class || currentClass == EObjectImpl.class) break; } return entry; } catch (LdapException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new LdapMappingException(e, "Error mapping " + clazz.getName()); } } 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 mapsEntryWithProperAttributeTypes() throws LdapInvalidAttributeValueException { final LdapMapper<Person> mapper = new LdapMapper<>(); Person hendy = new Person("hendy", "hendy.irawan", "Hendy", "Irawan", "hendy@soluvas.com"); log.info("Input Person: {}", hendy); Entry entry = mapper.toEntry(hendy, "ou=users,dc=aksimata,dc=com"); log.info("Output Entry: {}", entry); assertTrue("mapped Entry should contain AttributeType objectClass", entry.containsAttribute("objectClass")); assertTrue("mapped Entry should contain AttributeType uid", entry.containsAttribute("uid")); assertTrue("mapped Entry should contain AttributeType uniqueIdentifier", entry.containsAttribute("uniqueIdentifier")); assertTrue("mapped Entry should contain AttributeType gn", entry.containsAttribute("gn")); assertTrue("mapped Entry should contain AttributeType sn", entry.containsAttribute("sn")); assertTrue("mapped Entry should contain AttributeType cn", entry.containsAttribute("cn")); assertTrue("mapped Entry should contain AttributeType mail", entry.containsAttribute("mail")); }
@Test public void mapsSimpleClassWithProperAlias() throws LdapInvalidAttributeValueException { final LdapMapper<Person> mapper = new LdapMapper<>(); Person hendy = new Person("hendy", "hendy.irawan", "Hendy", "Irawan", "hendy@soluvas.com"); log.info("Input Person: {}", hendy); Entry entry = mapper.toEntry(hendy, "ou=users,dc=aksimata,dc=com"); log.info("Output Entry: {}", entry); assertEquals("Hendy", entry.get("gn").getString()); }
@Test public void mapsLongToEntry() throws LdapInvalidAttributeValueException { final LdapMapper<PersonWithLong> mapper = new LdapMapper<>(); final PersonWithLong hendy = new PersonWithLong("hendy", "hendy.irawan", "Hendy", "Irawan", "male"); hendy.setEmployeeNumber(123L); log.info("Input Person: {}", hendy); final Entry entry = mapper.toEntry(hendy, "ou=users,dc=aksimata,dc=com"); log.info("Output Entry: {}", entry); assertEquals("123", entry.get("employeeNumber").getString()); }
@Test public void mapsBigDecimalToEntry() throws LdapInvalidAttributeValueException { final LdapMapper<PersonWithBigDecimal> mapper = new LdapMapper<>(); final PersonWithBigDecimal hendy = new PersonWithBigDecimal("hendy", "hendy.irawan", "Hendy", "Irawan", "male"); hendy.setDebitBalance(new BigDecimal("123.4567")); log.info("Input Person: {}", hendy); final Entry entry = mapper.toEntry(hendy, "ou=users,dc=aksimata,dc=com"); log.info("Output Entry: {}", entry); assertEquals("123.4567", entry.get("debitBalance").getString()); }
@Test public void mapsEmptyCollectionToNothing() throws LdapInvalidAttributeValueException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final Person hendy = new Person("hendy", "hendy.irawan", "Hendy", "Irawan", "hendy@soluvas.com"); hendy.setEmails(ImmutableSet.<String>of()); log.info("Input Person: {}", hendy); final Entry entry = mapper.toEntry(hendy, "ou=users,dc=aksimata,dc=com"); log.info("Output Entry: {}", entry); assertFalse(entry.containsAttribute("mail")); }
@Test public void canMapToDateTime() throws LdapException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final SocialPerson hendy = new SocialPerson("hendy", "hendy.irawan", "Hendy", "Irawan"); final DateTimeZone wib = DateTimeZone.forID("Asia/Jakarta"); hendy.setBirthDate(new DateTime(1983, 12, 14, 0, 0, wib)); log.info("Input Person: {}", hendy); Entry entry = mapper.toEntry(hendy, "ou=users,dc=aksimata,dc=com"); log.info("Output Entry: {}", entry); assertEquals("19831213170000.000Z", entry.get("birthDate").getString()); }
@Test public void canMapToCurrencyUnit() throws LdapException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final SocialPerson hendy = new SocialPerson("hendy", "hendy", "Hendy", "Irawan"); hendy.setCurrency(Monetary.getCurrency("IDR")); log.info("Input Person: {}", hendy); final Entry personEntry = mapper.toEntry(hendy, "ou=users"); assertEquals("IDR", personEntry.get("currency").getString()); }
@Test public void canMapToBooleanTrue() throws LdapException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final SocialPerson hendy = new SocialPerson("hendy", "hendy", "Hendy", "Irawan"); hendy.setNewsletterSubscriptionEnabled(true); log.info("Input Person: {}", hendy); final Entry personEntry = mapper.toEntry(hendy, "ou=users"); assertEquals("TRUE", personEntry.get("newsletterSubscriptionEnabled").getString()); }
@Test public void canMapToBooleanFalse() throws LdapException { final LdapMapper<SocialPerson> mapper = new LdapMapper<>(); final SocialPerson hendy = new SocialPerson("hendy", "hendy", "Hendy", "Irawan"); hendy.setNewsletterSubscriptionEnabled(false); log.info("Input Person: {}", hendy); final Entry personEntry = mapper.toEntry(hendy, "ou=users"); assertEquals("FALSE", personEntry.get("newsletterSubscriptionEnabled").getString()); }
@Test public void cantMapSaldo() throws LdapException { final LdapMapper<SocialPerson> ldapMapper = new LdapMapper<>(); final SocialPerson person = new SocialPerson("hendy", "hendy", "Hendy", "Irawan"); person.setDebitBalance(new BigDecimal(5000000)); person.setDebitCurrency(Monetary.getCurrency("IDR")); log.info("Input Person: {}", person); final Entry personEntry = ldapMapper.toEntry(person, "ou=users"); assertEquals(new BigDecimal(5000000), new BigDecimal(personEntry.get("debitBalance").getString())); }