Migrate LDAP tests to use ApplicationContextRunner

This commit is contained in:
Eddú Meléndez
2018-01-24 21:40:48 -05:00
committed by Phillip Webb
parent d0a2613241
commit 44ad630de3

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@@ -21,14 +21,14 @@ import com.unboundid.ldap.sdk.BindResult;
import com.unboundid.ldap.sdk.DN;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration;
import org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -44,121 +44,127 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class EmbeddedLdapAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@Before
public void setup() {
this.context = new AnnotationConfigApplicationContext();
}
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(EmbeddedLdapAutoConfiguration.class));
@Test
public void testSetDefaultPort() {
load("spring.ldap.embedded.port:1234",
"spring.ldap.embedded.base-dn:dc=spring,dc=org");
InMemoryDirectoryServer server = this.context
.getBean(InMemoryDirectoryServer.class);
assertThat(server.getListenPort()).isEqualTo(1234);
this.contextRunner.withPropertyValues("spring.ldap.embedded.port:1234",
"spring.ldap.embedded.base-dn:dc=spring,dc=org").run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
assertThat(server.getListenPort()).isEqualTo(1234);
});
}
@Test
public void testRandomPortWithEnvironment() {
load("spring.ldap.embedded.base-dn:dc=spring,dc=org");
InMemoryDirectoryServer server = this.context
.getBean(InMemoryDirectoryServer.class);
assertThat(server.getListenPort()).isEqualTo(this.context.getEnvironment()
.getProperty("local.ldap.port", Integer.class));
this.contextRunner
.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org")
.run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
assertThat(server.getListenPort()).isEqualTo(context.getEnvironment()
.getProperty("local.ldap.port", Integer.class));
});
}
@Test
public void testRandomPortWithValueAnnotation() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.ldap.embedded.base-dn:dc=spring,dc=org")
.applyTo(this.context);
this.context.register(EmbeddedLdapAutoConfiguration.class,
.applyTo(context);
context.register(EmbeddedLdapAutoConfiguration.class,
LdapClientConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
LDAPConnection connection = this.context.getBean(LDAPConnection.class);
assertThat(connection.getConnectedPort()).isEqualTo(this.context.getEnvironment()
.getProperty("local.ldap.port", Integer.class));
context.refresh();
LDAPConnection connection = context.getBean(LDAPConnection.class);
assertThat(connection.getConnectedPort()).isEqualTo(
context.getEnvironment().getProperty("local.ldap.port", Integer.class));
}
@Test
public void testSetCredentials() throws LDAPException {
load("spring.ldap.embedded.base-dn:dc=spring,dc=org",
"spring.ldap.embedded.credential.username:uid=root",
"spring.ldap.embedded.credential.password:boot");
InMemoryDirectoryServer server = this.context
.getBean(InMemoryDirectoryServer.class);
BindResult result = server.bind("uid=root", "boot");
assertThat(result).isNotNull();
public void testSetCredentials() {
this.contextRunner
.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org",
"spring.ldap.embedded.credential.username:uid=root",
"spring.ldap.embedded.credential.password:boot")
.run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
BindResult result = server.bind("uid=root", "boot");
assertThat(result).isNotNull();
});
}
@Test
public void testSetPartitionSuffix() throws LDAPException {
load("spring.ldap.embedded.base-dn:dc=spring,dc=org");
InMemoryDirectoryServer server = this.context
.getBean(InMemoryDirectoryServer.class);
assertThat(server.getBaseDNs()).containsExactly(new DN("dc=spring,dc=org"));
public void testSetPartitionSuffix() {
this.contextRunner
.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org")
.run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
assertThat(server.getBaseDNs())
.containsExactly(new DN("dc=spring,dc=org"));
});
}
@Test
public void testSetLdifFile() throws LDAPException {
load("spring.ldap.embedded.base-dn:dc=spring,dc=org");
InMemoryDirectoryServer server = this.context
.getBean(InMemoryDirectoryServer.class);
assertThat(server.countEntriesBelow("ou=company1,c=Sweden,dc=spring,dc=org"))
.isEqualTo(5);
public void testSetLdifFile() {
this.contextRunner
.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org")
.run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
assertThat(server
.countEntriesBelow("ou=company1,c=Sweden,dc=spring,dc=org"))
.isEqualTo(5);
});
}
@Test
public void testQueryEmbeddedLdap() {
TestPropertyValues.of("spring.ldap.embedded.base-dn:dc=spring,dc=org")
.applyTo(this.context);
this.context.register(EmbeddedLdapAutoConfiguration.class,
LdapAutoConfiguration.class, LdapDataAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(LdapTemplate.class).length)
.isEqualTo(1);
LdapTemplate ldapTemplate = this.context.getBean(LdapTemplate.class);
assertThat(ldapTemplate.list("ou=company1,c=Sweden,dc=spring,dc=org")).hasSize(4);
this.contextRunner
.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org")
.withConfiguration(AutoConfigurations.of(LdapAutoConfiguration.class,
LdapDataAutoConfiguration.class))
.run(context -> {
assertThat(context.getBeanNamesForType(LdapTemplate.class).length)
.isEqualTo(1);
LdapTemplate ldapTemplate = context.getBean(LdapTemplate.class);
assertThat(ldapTemplate.list("ou=company1,c=Sweden,dc=spring,dc=org"))
.hasSize(4);
});
}
@Test
public void testDisableSchemaValidation() throws LDAPException {
load("spring.ldap.embedded.validation.enabled:false",
"spring.ldap.embedded.base-dn:dc=spring,dc=org");
InMemoryDirectoryServer server = this.context
.getBean(InMemoryDirectoryServer.class);
assertThat(server.getSchema()).isNull();
public void testDisableSchemaValidation() {
this.contextRunner
.withPropertyValues("spring.ldap.embedded.validation.enabled:false",
"spring.ldap.embedded.base-dn:dc=spring,dc=org")
.run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
assertThat(server.getSchema()).isNull();
});
}
@Test
public void testCustomSchemaValidation() throws LDAPException {
load("spring.ldap.embedded.validation.schema:classpath:custom-schema.ldif",
public void testCustomSchemaValidation() {
this.contextRunner.withPropertyValues(
"spring.ldap.embedded.validation.schema:classpath:custom-schema.ldif",
"spring.ldap.embedded.ldif:classpath:custom-schema-sample.ldif",
"spring.ldap.embedded.base-dn:dc=spring,dc=org");
InMemoryDirectoryServer server = this.context
.getBean(InMemoryDirectoryServer.class);
"spring.ldap.embedded.base-dn:dc=spring,dc=org").run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
assertThat(server.getSchema().getObjectClass("exampleAuxiliaryClass"))
.isNotNull();
assertThat(server.getSchema().getAttributeType("exampleAttributeName"))
.isNotNull();
}
private void load(String... properties) {
TestPropertyValues.of(properties).applyTo(this.context);
this.context.register(EmbeddedLdapAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(server.getSchema().getObjectClass("exampleAuxiliaryClass"))
.isNotNull();
assertThat(
server.getSchema().getAttributeType("exampleAttributeName"))
.isNotNull();
});
}
@Configuration