Skip to content

Commit 261ea64

Browse files
eddumelendezmhalbritter
authored andcommitted
Add ServiceConnection support for LLdapContainer (Testcontainers)
See gh-44389 Signed-off-by: Eddú Meléndez <[email protected]>
1 parent 5fac101 commit 261ea64

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/testcontainers.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ The following service connection factories are provided in the `spring-boot-test
6262
| javadoc:org.springframework.boot.autoconfigure.kafka.KafkaConnectionDetails[]
6363
| Containers of type javadoc:org.testcontainers.kafka.KafkaContainer[], javadoc:org.testcontainers.kafka.ConfluentKafkaContainer[] or javadoc:org.testcontainers.redpanda.RedpandaContainer[]
6464

65+
| javadoc:org.springframework.boot.autoconfigure.ldap.LdapConnectionDetails[]
66+
| Containers named "osixia/openldap" or of type javadoc:org.testcontainers.ldap.LLdapContainer[]
67+
6568
| javadoc:org.springframework.boot.autoconfigure.liquibase.LiquibaseConnectionDetails[]
6669
| Containers of type javadoc:{url-testcontainers-jdbc-javadoc}/org.testcontainers.containers.JdbcDatabaseContainer[]
6770

spring-boot-project/spring-boot-testcontainers/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ dependencies {
7070
optional("org.testcontainers:grafana")
7171
optional("org.testcontainers:jdbc")
7272
optional("org.testcontainers:kafka")
73+
optional("org.testcontainers:ldap")
7374
optional("org.testcontainers:mariadb")
7475
optional("org.testcontainers:mongodb")
7576
optional("org.testcontainers:mssqlserver")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.service.connection.ldap;
18+
19+
import java.util.List;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.testcontainers.junit.jupiter.Container;
23+
import org.testcontainers.junit.jupiter.Testcontainers;
24+
import org.testcontainers.ldap.LLdapContainer;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
28+
import org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration;
29+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
30+
import org.springframework.boot.testsupport.container.TestImage;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.ldap.core.AttributesMapper;
33+
import org.springframework.ldap.core.LdapTemplate;
34+
import org.springframework.ldap.query.LdapQueryBuilder;
35+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
39+
/**
40+
* Tests for {@link LLdapContainerConnectionDetailsFactory}.
41+
*
42+
* @author Eddú Meléndez
43+
*/
44+
@SpringJUnitConfig
45+
@Testcontainers(disabledWithoutDocker = true)
46+
class LLdapContainerConnectionDetailsFactoryIntegrationTests {
47+
48+
@Container
49+
@ServiceConnection
50+
static final LLdapContainer lldap = TestImage.container(LLdapContainer.class);
51+
52+
@Autowired
53+
private LdapTemplate ldapTemplate;
54+
55+
@Test
56+
void connectionCanBeMadeToLdapContainer() {
57+
List<String> cn = this.ldapTemplate.search(LdapQueryBuilder.query().where("objectClass").is("inetOrgPerson"),
58+
(AttributesMapper<String>) (attributes) -> attributes.get("cn").get().toString());
59+
assertThat(cn).singleElement().isEqualTo("Administrator");
60+
}
61+
62+
@Configuration(proxyBeanMethods = false)
63+
@ImportAutoConfiguration({ LdapAutoConfiguration.class })
64+
static class TestConfiguration {
65+
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.service.connection.ldap;
18+
19+
import org.testcontainers.ldap.LLdapContainer;
20+
21+
import org.springframework.boot.autoconfigure.ldap.LdapConnectionDetails;
22+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
23+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
24+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
25+
26+
/**
27+
* {@link ContainerConnectionDetailsFactory} to create {@link LdapConnectionDetails} from
28+
* a {@link ServiceConnection @ServiceConnection}-annotated {@link LLdapContainer}.
29+
*
30+
* @author Eddú Meléndez
31+
*/
32+
class LLdapContainerConnectionDetailsFactory
33+
extends ContainerConnectionDetailsFactory<LLdapContainer, LdapConnectionDetails> {
34+
35+
@Override
36+
protected LdapConnectionDetails getContainerConnectionDetails(ContainerConnectionSource<LLdapContainer> source) {
37+
return new LLdapContainerConnectionDetails(source);
38+
}
39+
40+
private static final class LLdapContainerConnectionDetails extends ContainerConnectionDetails<LLdapContainer>
41+
implements LdapConnectionDetails {
42+
43+
private LLdapContainerConnectionDetails(ContainerConnectionSource<LLdapContainer> source) {
44+
super(source);
45+
}
46+
47+
@Override
48+
public String[] getUrls() {
49+
return new String[] { getContainer().getLdapUrl() };
50+
}
51+
52+
@Override
53+
public String getBase() {
54+
return getContainer().getBaseDn();
55+
}
56+
57+
@Override
58+
public String getUsername() {
59+
return getContainer().getUser();
60+
}
61+
62+
@Override
63+
public String getPassword() {
64+
return getContainer().getUserPass();
65+
}
66+
67+
}
68+
69+
}

spring-boot-project/spring-boot-testcontainers/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ org.springframework.boot.testcontainers.service.connection.jdbc.JdbcContainerCon
2222
org.springframework.boot.testcontainers.service.connection.kafka.ApacheKafkaContainerConnectionDetailsFactory,\
2323
org.springframework.boot.testcontainers.service.connection.kafka.ConfluentKafkaContainerConnectionDetailsFactory,\
2424
org.springframework.boot.testcontainers.service.connection.kafka.DeprecatedConfluentKafkaContainerConnectionDetailsFactory,\
25+
org.springframework.boot.testcontainers.service.connection.ldap.LLdapContainerConnectionDetailsFactory,\
2526
org.springframework.boot.testcontainers.service.connection.ldap.OpenLdapContainerConnectionDetailsFactory,\
2627
org.springframework.boot.testcontainers.service.connection.liquibase.LiquibaseContainerConnectionDetailsFactory,\
2728
org.springframework.boot.testcontainers.service.connection.mongo.MongoContainerConnectionDetailsFactory,\

0 commit comments

Comments
 (0)