<commit_before>/*
 * Copyright 2012-2017 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
 *
 *      http://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.autoconfigure.ldap.embedded;

import com.unboundid.ldap.listener.InMemoryDirectoryServer;
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.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration;
import org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.LdapTemplate;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Tests for {@link EmbeddedLdapAutoConfiguration}
 *
 * @author <NAME>
 */
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();
		}
	}

	@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);
	}

	@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));
	}

	@Test
	public void testRandomPortWithValueAnnotation() {
		TestPropertyValues.of("spring.ldap.embedded.base-dn:dc=spring,dc=org")
				.applyTo(this.context);
		this.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));
	}

	@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();
	}

	@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"));
	}

	@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);
	}

	@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);
	}

	@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();
	}

	@Test
	public void testCustomSchemaValidation() throws LDAPException {
		load("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);

		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();
	}<commit_msg>Migrate LDAP tests to use ApplicationContextRunner<commit_after>/*
 * 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.
 * You may obtain a copy of the License at
 *
 *      http://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.autoconfigure.ldap.embedded;

import com.unboundid.ldap.listener.InMemoryDirectoryServer;
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.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;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.LdapTemplate;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Tests for {@link EmbeddedLdapAutoConfiguration}
 *
 * @author <NAME>
 */
public class EmbeddedLdapAutoConfigurationTests {

	private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
			.withConfiguration(
					AutoConfigurations.of(EmbeddedLdapAutoConfiguration.class));

	@Test
	public void testSetDefaultPort() {
		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() {
		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(context);
		context.register(EmbeddedLdapAutoConfiguration.class,
				LdapClientConfiguration.class,
				PropertyPlaceholderAutoConfiguration.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() {
		this.contextRunner
				.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org",
						"spring.ldap.embedded.credential.username:uid=root",
						"spring.ldap.embedded.credential.password:<PASSWORD>")
				.run(context -> {
					InMemoryDirectoryServer server = context
							.getBean(InMemoryDirectoryServer.class);
					BindResult result = server.bind("uid=root", "boot");
					assertThat(result).isNotNull();
				});
	}

	@Test
	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() {
		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() {
		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() {
		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() {
		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").run(context -> {
					InMemoryDirectoryServer server = context
							.getBean(InMemoryDirectoryServer.class);

					assertThat(server.getSchema().getObjectClass("exampleAuxiliaryClass"))
							.isNotNull();
					assertThat(
							server.getSchema().getAttributeType("exampleAttributeName"))
									.isNotNull();
				});
	}
{'fraction_non_alphanumeric': 0.11636274429634476, 'fraction_numerical': 0.004088641753209584, 'mean_word_length': 5.271794871794872, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '426351', 'n_tokens_mistral': 4081, 'n_tokens_neox': 3606, 'n_words': 573}