Merge pull request #41325 from eddumelendez

* pr/41325:
  Add support for ServiceConnection in DataLdapTest

Closes gh-41325
This commit is contained in:
Stéphane Nicoll 2024-07-16 16:49:07 +02:00
commit dc0204b68a
2 changed files with 72 additions and 1 deletions

View File

@ -0,0 +1,70 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.data.ldap;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.boot.testsupport.container.OpenLdapContainer;
import org.springframework.boot.testsupport.container.TestImage;
import org.springframework.context.ApplicationContext;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.query.LdapQueryBuilder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
* Sample test for {@link DataLdapTest @DataLdapTest}.
*
* @author Eddú Meléndez
*/
@DataLdapTest
@Testcontainers(disabledWithoutDocker = true)
public class DataLdapTestIntegrationTests {
@Container
@ServiceConnection
static final OpenLdapContainer openLdap = TestImage.container(OpenLdapContainer.class).withEnv("LDAP_TLS", "false");
@Autowired
private ApplicationContext applicationContext;
@Autowired
private LdapTemplate ldapTemplate;
@Test
void connectionCanBeMadeToLdapContainer() {
List<String> cn = this.ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("dcObject"),
(AttributesMapper<String>) (attributes) -> attributes.get("dc").get().toString());
assertThat(cn).hasSize(1);
assertThat(cn.get(0)).isEqualTo("example");
}
@Test
void serviceConnectionAutoConfigurationWasImported() {
assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class));
}
}

View File

@ -2,3 +2,4 @@
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration
optional:org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration