|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 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.test.autoconfigure.data.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 | + |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 27 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration; |
| 28 | +import org.springframework.boot.testsupport.container.OpenLdapContainer; |
| 29 | +import org.springframework.boot.testsupport.container.TestImage; |
| 30 | +import org.springframework.context.ApplicationContext; |
| 31 | +import org.springframework.ldap.core.AttributesMapper; |
| 32 | +import org.springframework.ldap.core.LdapTemplate; |
| 33 | +import org.springframework.ldap.query.LdapQueryBuilder; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration; |
| 37 | + |
| 38 | +/** |
| 39 | + * Sample test for {@link DataLdapTest @DataLdapTest}. |
| 40 | + * |
| 41 | + * @author Eddú Meléndez |
| 42 | + */ |
| 43 | +@DataLdapTest |
| 44 | +@Testcontainers(disabledWithoutDocker = true) |
| 45 | +public class DataLdapTestIntegrationTests { |
| 46 | + |
| 47 | + @Container |
| 48 | + @ServiceConnection |
| 49 | + static final OpenLdapContainer openLdap = TestImage.container(OpenLdapContainer.class).withEnv("LDAP_TLS", "false"); |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private ApplicationContext applicationContext; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private LdapTemplate ldapTemplate; |
| 56 | + |
| 57 | + @Test |
| 58 | + void connectionCanBeMadeToLdapContainer() { |
| 59 | + List<String> cn = this.ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("dcObject"), |
| 60 | + (AttributesMapper<String>) (attributes) -> attributes.get("dc").get().toString()); |
| 61 | + assertThat(cn).hasSize(1); |
| 62 | + assertThat(cn.get(0)).isEqualTo("example"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void serviceConnectionAutoConfigurationWasImported() { |
| 67 | + assertThat(this.applicationContext).has(importedAutoConfiguration(ServiceConnectionAutoConfiguration.class)); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments