@Test
 public void testCountPrimaryEntities() {
   LDAPDataProvider dp = new LDAPDataProvider();
   int c = dp.countPrimaryEntities(null, null);
   assertEquals(12, c);
   dp.closeResources();
 }
  @Test
  public void testFromSearchResultEntry() throws Exception {
    LDAPDataProvider dp = new LDAPDataProvider();
    Configuration cfg = Configuration.getInstance();

    LDAPConnection conn = ldapServer.getConnection();
    SearchResultEntry item =
        conn.searchForEntry(
            cfg.getLDAPConfiguration().getUserBaseDN(), SearchScope.SUB, "uid=bjensen");

    List<Treaty> t =
        new ArrayList<Treaty>() {
          {
            add(Treaty.CMS);
            add(Treaty.AEWA);
          }
        };

    LDAPConfiguration ldap = cfg.getLDAPConfiguration();
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_PREFIX, "personalTitle");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_COUNTRY, "c");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_FIRST_NAME, "givenName");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_LAST_NAME, "sn");
    ldap.setMapping(
        LDAPConfiguration.LDAP_MAPPING_ADDRESS, "Address: ${registeredAddress} ${postalCode}");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_DEPARTMENT, "ou");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_EMAIL, "mail");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_FAX, "facsimileTelephoneNumber");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_INSTITUTION, "o");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_PHONE, "telephoneNumber");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_POSITION, "title");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_UPDATED, "roomNumber");
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_TREATIES, "carLicense");
    cfg.save();

    IContact c = dp.fromSearchResultEntry(item);

    assertEquals("Address: 5th Avenue, NY 12345", c.getAddress());
    assertEquals("bjensen", c.getId());
    assertEquals("RO", c.getCountry());
    assertEquals("Software Development", c.getDepartment());
    assertEquals("*****@*****.**", c.getEmail());
    assertEquals("+1 408 555 1992", c.getFax());
    assertEquals("Barbara", c.getFirstName());
    assertEquals("Eau de Web", c.getInstitution());
    assertEquals("Jensen", c.getLastName());
    assertEquals("+1 408 555 1862", c.getPhoneNumber());
    assertEquals("Web Developer", c.getPosition());
    assertEquals("H.E.", c.getPrefix());
    assertEquals(new Short((short) 1), c.getProtocolVersion());
    assertEquals(t, c.getTreaties());

    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+2"));
    cal.set(2006, 11, 25, 16, 45, 22);
    cal.set(Calendar.MILLISECOND, 0);
    Date d = cal.getTime();
    Date updated = c.getUpdated();
    assertEquals(updated, d);
  }
 @Test
 public void testOpenResources() {
   LDAPDataProvider dp = new LDAPDataProvider();
   dp.openResources();
   LDAPConnection conn = dp.getConnection();
   assertNotNull(conn);
   assertTrue(conn.isConnected());
   dp.closeResources();
 }
  @Test
  public void testGetFieldValue() throws Exception {
    LDAPConnection conn = ldapServer.getConnection();
    SearchResultEntry item =
        conn.searchForEntry("ou=People,dc=example,dc=com", SearchScope.SUB, "uid=bjensen");
    assertNotNull(item);

    LDAPDataProvider dp = new LDAPDataProvider();
    assertEquals("12345", dp.getFieldValue(item, "postalCode"));

    assertEquals("valid12345", dp.getFieldValue(item, "valid${postalCode}"));

    assertEquals("invalidAttribute", dp.getFieldValue(item, "invalidAttribute"));

    assertEquals("BJ", dp.getFieldValue(item, "${initials}"));
    assertEquals(" BJ ", dp.getFieldValue(item, " ${initials} "));
    assertEquals(" BJ arbitrary string", dp.getFieldValue(item, " ${initials} arbitrary string"));

    assertEquals(
        " BJ Web Developer arbitrary string",
        dp.getFieldValue(item, " ${initials} ${title} arbitrary string"));
    assertEquals(
        " BJWeb Developerarbitrary string",
        dp.getFieldValue(item, " ${initials}${title}arbitrary string"));
  }
  @Test
  public void testGetPrimaryEntity() {
    Configuration cfg = Configuration.getInstance();
    LDAPConfiguration ldap = cfg.getLDAPConfiguration();
    ldap.setMapping(LDAPConfiguration.LDAP_MAPPING_ID, "uid");

    LDAPDataProvider dp = new LDAPDataProvider();
    IContact row = (IContact) dp.getPrimaryEntity(null, "bjensen");
    assertNotNull(row);
    assertEquals("bjensen", row.getId());
  }
  @Test(expected = InvalidValueException.class)
  public void testFromSearchResultEntryMissingId() throws Exception {
    LDAPDataProvider dp = new LDAPDataProvider();
    Configuration cfg = Configuration.getInstance();

    LDAPConnection conn = ldapServer.getConnection();
    SearchResultEntry item =
        conn.searchForEntry(
            cfg.getLDAPConfiguration().getUserBaseDN(), SearchScope.SUB, "uid=bjensen");
    cfg.getLDAPConfiguration().setMapping(LDAPConfiguration.LDAP_MAPPING_ID, "unknown");
    dp.fromSearchResultEntry(item);
  }
  @Test
  public void testGetPrimaryEntitiesNegativeStart() {
    LDAPConfiguration ldapCfg = Configuration.getInstance().getLDAPConfiguration();
    ldapCfg.setMapping(LDAPConfiguration.LDAP_MAPPING_ID, "uid");

    LDAPDataProvider dp = new LDAPDataProvider();
    users = dp.getPrimaryEntities(null, null, -1, 5, null);
    assertEquals(5, users.size());
    first = users.get(0);
    last = users.get(users.size() - 1);
    assertEquals("bjensen", first.getId());
    assertEquals("ahall", last.getId());
  }
  @Test
  public void testGetPrimaryEntitiesBiggerPage() {
    LDAPConfiguration ldapCfg = Configuration.getInstance().getLDAPConfiguration();
    ldapCfg.setMapping(LDAPConfiguration.LDAP_MAPPING_ID, "uid");

    LDAPDataProvider dp = new LDAPDataProvider();
    users = dp.getPrimaryEntities(null, null, 0, 20, null);
    assertEquals(12, users.size());
    first = users.get(0);
    last = users.get(users.size() - 1);
    assertEquals("bjensen", first.getId());
    assertEquals("ashelton", last.getId());
  }
  @Test
  public void testGetPrimaryEntitiesMaxPageSize() {
    Configuration cfg = Configuration.getInstance();
    LDAPConfiguration ldapCfg = Configuration.getInstance().getLDAPConfiguration();
    ldapCfg.setMapping(LDAPConfiguration.LDAP_MAPPING_ID, "uid");
    ldapCfg.setMaxPageSize(8);

    LDAPDataProvider dp = new LDAPDataProvider();
    users = dp.getPrimaryEntities(null, null, 0, 10000, null);
    assertEquals(8, users.size());
    first = users.get(0);
    last = users.get(users.size() - 1);
    assertEquals("bjensen", first.getId());
    assertEquals("ajensen", last.getId());
    // Reset to default
    cfg.getLDAPConfiguration().setMaxPageSize(0);
  }
  @Test
  public void testGetPrimaryEntitiesNegativePageSize() {
    Configuration cfg = Configuration.getInstance();
    LDAPConfiguration ldapCfg = cfg.getLDAPConfiguration();
    ldapCfg.setMaxPageSize(5);
    ldapCfg.setMapping(LDAPConfiguration.LDAP_MAPPING_ID, "uid");

    LDAPDataProvider dp = new LDAPDataProvider();
    users = dp.getPrimaryEntities(null, null, 10, -1, null);
    assertEquals(2, users.size());
    first = users.get(0);
    last = users.get(users.size() - 1);
    assertEquals("alutz", first.getId());
    assertEquals("ashelton", last.getId());
    // Reset to default
    cfg.getLDAPConfiguration().setMaxPageSize(0);
  }
  @Test
  public void testGetPageSize() {
    Configuration cfg = Configuration.getInstance();
    cfg.getLDAPConfiguration().setMaxPageSize(0);
    LDAPDataProvider dp = new LDAPDataProvider();
    assertEquals(10, dp.getPageSize(10));
    assertEquals(100, dp.getPageSize(-1));
    assertEquals(100, dp.getPageSize(100));
    assertEquals(100, dp.getPageSize(10000));

    cfg.getLDAPConfiguration().setMaxPageSize(200);
    assertEquals(10, dp.getPageSize(10));
    assertEquals(200, dp.getPageSize(-1));
    assertEquals(200, dp.getPageSize(200));
    assertEquals(200, dp.getPageSize(10000));
  }
  @Test(expected = RuntimeException.class)
  public void testOpenResourcesException() {
    // Test invalid connection
    LDAPConfiguration ldap = new LDAPConfiguration();
    ldap.setHost("invalid");
    ldap.setPort(-1);
    Configuration cfg = Configuration.getInstance();
    cfg.setLDAPConfiguration(ldap);

    LDAPDataProvider dp = new LDAPDataProvider();
    try {
      dp.openResources();
      assertNull(dp.getConnection());
    } finally {
      // Put back the correct configuration
      ldap.setHost(ldapServer.getListenAddress().getHostAddress());
      ldap.setPort(ldapServer.getListenPort());
      ldap.setBindDN(BIND_DN);
      ldap.setPassword(BIND_PASSWORD);
      ldap.setUserBaseDN("ou=People,dc=example,dc=com");
      ldap.setUsersQueryFilter("uid=*");
      cfg.setLDAPConfiguration(ldap);
    }
  }
 @Test
 public void testGetEntity() {
   LDAPDataProvider dp = new LDAPDataProvider();
   assertNull(dp.getEntity(null, null));
 }
 @Test(expected = InvalidValueException.class)
 public void testFromSearchResultEntryNull() throws Exception {
   LDAPDataProvider dp = new LDAPDataProvider();
   assertNull(dp.fromSearchResultEntry(null));
 }