Polish EmbeddedLdapServer Tests

Issue #938

Signed-off-by: etrandafir93 <emanueltrandafir1993@gmail.com>
This commit is contained in:
Emanuel Trandafir
2025-03-27 21:16:12 +02:00
committed by Josh Cummings
parent 8d44ee31a8
commit 7a0a7bdc53

View File

@@ -28,6 +28,7 @@ import javax.naming.directory.Attributes;
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
import com.unboundid.ldap.listener.InMemoryListenerConfig;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ldap.core.AttributesMapper;
@@ -40,110 +41,110 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
public class EmbeddedLdapServerTests {
@Test
public void shouldStartAndCloseServer() throws Exception {
int port = getFreePort();
assertThat(isPortOpen(port)).isFalse();
private int port;
EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port);
assertThat(isPortOpen(port)).isTrue();
server.close();
assertThat(isPortOpen(port)).isFalse();
@Before
public void setUp() throws IOException {
this.port = getFreePort();
}
@Test
public void shouldStartAndAutoCloseServer() throws Exception {
int port = getFreePort();
assertThat(isPortOpen(port)).isFalse();
public void shouldStartAndCloseServer() {
assertPortIsFree(this.port);
try (EmbeddedLdapServer ignored = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port)) {
assertThat(isPortOpen(port)).isTrue();
EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port);
assertPortIsUsed(this.port);
server.close();
assertPortIsFree(this.port);
}
@Test
public void shouldStartAndAutoCloseServer() {
assertPortIsFree(this.port);
try (EmbeddedLdapServer ignored = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se",
this.port)) {
assertPortIsUsed(this.port);
}
assertThat(isPortOpen(port)).isFalse();
assertPortIsFree(this.port);
}
@Test
public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
int port = getFreePort();
assertThat(isPortOpen(port)).isFalse();
assertPortIsFree(this.port);
LdapTestUtils.startEmbeddedServer(port, "dc=jayway,dc=se", "jayway");
assertThat(isPortOpen(port)).isTrue();
LdapTestUtils.startEmbeddedServer(this.port, "dc=jayway,dc=se", "jayway");
assertPortIsUsed(this.port);
LdapTestUtils.shutdownEmbeddedServer();
assertThat(isPortOpen(port)).isFalse();
assertPortIsFree(this.port);
}
@Test
public void startWhenNewEmbeddedServerThenException() throws Exception {
int port = getFreePort();
EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", port);
public void startWhenNewEmbeddedServerThenException() {
EmbeddedLdapServer server = EmbeddedLdapServer.newEmbeddedServer("jayway", "dc=jayway,dc=se", this.port);
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
}
@Test
public void startWhenUnstartedThenWorks() throws Exception {
int port = getFreePort();
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port));
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port));
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
try (EmbeddedLdapServer server = new EmbeddedLdapServer(ds)) {
server.start();
assertThat(isPortOpen(port)).isTrue();
assertPortIsUsed(this.port);
}
}
@Test
public void startWhenAlreadyStartedThenFails() throws Exception {
int port = getFreePort();
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=jayway,dc=se");
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port));
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", this.port));
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
try (EmbeddedLdapServer server = new EmbeddedLdapServer(ds)) {
server.start();
assertThat(isPortOpen(port)).isTrue();
assertPortIsUsed(this.port);
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(server::start);
}
}
@Test
public void shouldBuildButNotStartTheServer() throws IOException {
int port = getFreePort();
EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").port(port).build();
assertThat(isPortOpen(port)).isFalse();
public void shouldBuildButNotStartTheServer() {
EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").port(this.port).build();
assertPortIsFree(this.port);
}
@Test
public void shouldBuildTheServerWithCustomPort() throws IOException {
int port = getFreePort();
EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se").port(port);
public void shouldBuildTheServerWithCustomPort() {
EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se")
.port(this.port);
try (EmbeddedLdapServer server = serverBuilder.build()) {
server.start();
assertThat(isPortOpen(port)).isTrue();
assertPortIsUsed(this.port);
}
assertThat(isPortOpen(port)).isFalse();
assertPortIsFree(this.port);
}
@Test
public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOException {
int port = getFreePort();
String tempLogFile = Files.createTempFile("ldap-log-", ".txt").toAbsolutePath().toString();
EmbeddedLdapServer.Builder serverBuilder = EmbeddedLdapServer.withPartitionSuffix("dc=jayway,dc=se")
.port(port)
.port(this.port)
.configurationCustomizer((config) -> config.setCodeLogDetails(tempLogFile, true));
try (EmbeddedLdapServer server = serverBuilder.build()) {
server.start();
ldapTemplate("dc=jayway,dc=se", port).search(LdapQueryBuilder.query().where("objectclass").is("person"),
new AttributesMapper<>() {
public String mapFromAttributes(Attributes attrs) throws NamingException {
return (String) attrs.get("cn").get();
}
});
ldapTemplate("dc=jayway,dc=se", this.port)
.search(LdapQueryBuilder.query().where("objectclass").is("person"), new AttributesMapper<>() {
public String mapFromAttributes(Attributes attrs) throws NamingException {
return (String) attrs.get("cn").get();
}
});
}
assertThat(Path.of(tempLogFile))
@@ -151,6 +152,14 @@ public class EmbeddedLdapServerTests {
.isNotEmptyFile();
}
static void assertPortIsFree(int port) {
assertThat(isPortOpen(port)).isFalse();
}
static void assertPortIsUsed(int port) {
assertThat(isPortOpen(port)).isTrue();
}
static boolean isPortOpen(int port) {
try (Socket ignored = new Socket("localhost", port)) {
return true;