/**
   * Set-up an in-memory LDAP server
   *
   * @see
   *     https://www.unboundid.com/products/ldap-sdk/docs/javadoc/com/unboundid/ldap/listener/InMemoryDirectoryServer.html
   * @throws Exception
   */
  @BeforeClass
  public static void setUp() throws Exception {
    String baseDir = String.format("%s/etc/ldap", System.getProperty("user.dir"));
    String ldifTestFile = String.format("%s/test.ldif", baseDir);
    InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    config.addAdditionalBindCredentials(BIND_DN, BIND_PASSWORD);

    // StartTLS factory
    InMemoryListenerConfig ldapFactory =
        InMemoryListenerConfig.createLDAPConfig(
            "LDAP", // Listener name
            InetAddress.getLocalHost(),
            0, // Listen port (0 = automatically choose an available port)
            null);
    config.setListenerConfigs(ldapFactory);

    Schema ns = Schema.getSchema(String.format("%s/test.schema.ldif", baseDir));
    config.setSchema(ns);

    ldapServer = new InMemoryDirectoryServer(config);
    ldapServer.importFromLDIF(true, ldifTestFile);
    ldapServer.startListening();

    Configuration cfg = org.informea.odata.config.Configuration.getInstance();

    LDAPConfiguration ldapCfg = new LDAPConfiguration();
    ldapCfg.setHost(ldapServer.getListenAddress().getHostAddress());
    ldapCfg.setPort(ldapServer.getListenPort());
    ldapCfg.setBindDN(BIND_DN);
    ldapCfg.setPassword(BIND_PASSWORD);
    ldapCfg.setUserBaseDN("ou=People,dc=example,dc=com");
    ldapCfg.setUsersQueryFilter("uid=*");

    cfg.setLDAPConfiguration(ldapCfg);
  }
  private InMemoryDirectoryServerConfig createInMemoryServerConfiguration() {
    try {
      final InMemoryDirectoryServerConfig inMemoryDirectoryServerConfig =
          new InMemoryDirectoryServerConfig(domainDsnArray());

      if (bindCredentials != null) {
        this.authenticationConfiguration =
            new AuthenticationConfiguration(bindDSN, bindCredentials);
        inMemoryDirectoryServerConfig.addAdditionalBindCredentials(bindDSN, bindCredentials);
      }

      final InMemoryListenerConfig listenerConfig =
          InMemoryListenerConfig.createLDAPConfig(
              LDAP_SERVER_LISTENER_NAME, bindAddress, bindPort, null);
      inMemoryDirectoryServerConfig.setListenerConfigs(listenerConfig);
      for (Schema s : customSchema().asSet()) {
        inMemoryDirectoryServerConfig.setSchema(s);
      }
      return inMemoryDirectoryServerConfig;
    } catch (LDAPException e) {
      throw new IllegalStateException(
          "Could not create configuration for the in-memory LDAP instance due to an exception", e);
    }
  }