Starting in memory ldap server for JUnit 5 tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'org.springframework.boot' | |
apply plugin: 'io.spring.dependency-management' | |
dependencies { | |
compile 'com.unboundid:unboundid-ldapsdk:5.1.0' | |
compile 'org.junit.jupiter:junit-jupiter:5.6.2' | |
compile 'org.springframework.boot:spring-boot-starter-parent:2.3.0' | |
compile 'org.springframework.boot:spring-boot-starter-data-jpa' | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch.keller.test.extension; | |
import com.unboundid.ldap.listener.InMemoryDirectoryServer; | |
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; | |
import com.unboundid.ldap.listener.InMemoryListenerConfig; | |
import com.unboundid.ldif.LDIFReader; | |
import org.junit.jupiter.api.extension.AfterAllCallback; | |
import org.junit.jupiter.api.extension.BeforeAllCallback; | |
import org.junit.jupiter.api.extension.ExtensionContext; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.util.SocketUtils; | |
import java.io.InputStream; | |
public class LdapExtension implements BeforeAllCallback, AfterAllCallback { | |
public final int port; | |
private static final Logger LOG = LoggerFactory.getLogger(LdapExtension.class); | |
private InMemoryDirectoryServer server; | |
public LdapExtension() { | |
this.port = SocketUtils.findAvailableTcpPort(); | |
} | |
public LdapExtension(int port) { | |
this.port = port; | |
} | |
@Override | |
public void beforeAll(final ExtensionContext context) throws Exception { | |
try (final InputStream inputStream = getClass().getResourceAsStream("/my.ldif")) { | |
LOG.info("LDAP server starting..."); | |
final InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("o=myorganization,c=com"); | |
config.setSchema(null); // must be set or initialization fails with LDAPException | |
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port)); | |
final LDIFReader reader = new LDIFReader(inputStream); | |
server = new InMemoryDirectoryServer(config); | |
server.importFromLDIF(true, reader); | |
server.startListening(); | |
LOG.info("LDAP server started. Listen on port " + server.getListenPort()); | |
} | |
} | |
@Override | |
public void afterAll(final ExtensionContext context) { | |
server.shutDown(true); | |
} | |
public int getPort() { | |
return port; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.ldap.core.LdapTemplate; | |
@SpringBootTest | |
public class MyTest { | |
@RegisterExtension | |
static LdapExtension ldapExtension = new LdapExtension(); | |
@DynamicPropertySource | |
static void ldapProperties(DynamicPropertyRegistry registry) { | |
registry.add("ldap.url", () -> "ldap://localhost:" + ldapExtension.getPort()); | |
} | |
@Autowired | |
private LdapTemplate ldapTemplate; | |
@Test | |
public doTest() { | |
// Use ldapTemplate | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment