/*
 * Copyright 2012-2016 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.mongo;

import java.net.UnknownHostException;
import java.util.List;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.junit.Test;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;

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

/**
 * Tests for {@link MongoProperties}.
 *
 * @author Phillip Webb
 * @author Andy Wilkinson
 */
public class MongoPropertiesTests {

	@Test
	public void canBindCharArrayPassword() {
		// gh-1572
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		EnvironmentTestUtils.addEnvironment(context, "spring.data.mongodb.password:word");
		context.register(Conf.class);
		context.refresh();
		MongoProperties properties = context.getBean(MongoProperties.class);
		assertThat(properties.getPassword()).isEqualTo("word".toCharArray());
	}

	@Test
	public void portCanBeCustomized() throws UnknownHostException {
		MongoProperties properties = new MongoProperties();
		properties.setPort(12345);
		MongoClient client = properties.createMongoClient(null, null);
		List<ServerAddress> allAddresses = client.getAllAddress();
		assertThat(allAddresses).hasSize(1);
		assertServerAddress(allAddresses.get(0), "localhost", 12345);
	}

	@Test
	public void hostCanBeCustomized() throws UnknownHostException {
		MongoProperties properties = new MongoProperties();
		properties.setHost("mongo.example.com");
		MongoClient client = properties.createMongoClient(null, null);
		List<ServerAddress> allAddresses = client.getAllAddress();
		assertThat(allAddresses).hasSize(1);
		assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017);
	}

	@Test
	public void credentialsCanBeCustomized() throws UnknownHostException {
		MongoProperties properties = new MongoProperties();
		properties.setUsername("user");
		properties.setPassword("secret".toCharArray());
		MongoClient client = properties.createMongoClient(null, null);
		assertMongoCredential(client.getCredentialsList().get(0), "user", "secret",
				"test");
	}

	@Test
	public void databaseCanBeCustomized() throws UnknownHostException {
		MongoProperties properties = new MongoProperties();
		properties.setDatabase("foo");
		properties.setUsername("user");
		properties.setPassword("secret".toCharArray());
		MongoClient client = properties.createMongoClient(null, null);
		assertMongoCredential(client.getCredentialsList().get(0), "user", "secret",
				"foo");
	}

	@Test
	public void authenticationDatabaseCanBeCustomized() throws UnknownHostException {
		MongoProperties properties = new MongoProperties();
		properties.setAuthenticationDatabase("foo");
		properties.setUsername("user");
		properties.setPassword("secret".toCharArray());
		MongoClient client = properties.createMongoClient(null, null);
		assertMongoCredential(client.getCredentialsList().get(0), "user", "secret",
				"foo");
	}

	@Test
	public void uriCanBeCustomized() throws UnknownHostException {
		MongoProperties properties = new MongoProperties();
		properties.setUri("mongodb://user:secret@mongo1.example.com:12345,"
				+ "mongo2.example.com:23456/test");
		MongoClient client = properties.createMongoClient(null, null);
		List<ServerAddress> allAddresses = client.getAllAddress();
		assertThat(allAddresses).hasSize(2);
		assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
		assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
		List<MongoCredential> credentialsList = client.getCredentialsList();
		assertThat(credentialsList).hasSize(1);
		assertMongoCredential(credentialsList.get(0), "user", "secret", "test");
	}

	private void assertServerAddress(ServerAddress serverAddress, String expectedHost,
			int expectedPort) {
		assertThat(serverAddress.getHost()).isEqualTo(expectedHost);
		assertThat(serverAddress.getPort()).isEqualTo(expectedPort);
	}

	private void assertMongoCredential(MongoCredential credentials,
			String expectedUsername, String expectedPassword, String expectedSource) {
		assertThat(credentials.getUserName()).isEqualTo(expectedUsername);
		assertThat(credentials.getPassword()).isEqualTo(expectedPassword.toCharArray());
		assertThat(credentials.getSource()).isEqualTo(expectedSource);
	}

	@Configuration
	@EnableConfigurationProperties(MongoProperties.class)
	static class Conf {

	}

}

@@ -834,16 +834,102 @@
 ddress;%0A
+import com.mongodb.connection.Cluster;%0Aimport com.mongodb.connection.ClusterSettings;%0A
 import o
@@ -939,24 +939,24 @@
 junit.Test;%0A
-
 %0Aimport org.
@@ -1232,16 +1232,74 @@
 uration;
+%0Aimport org.springframework.test.util.ReflectionTestUtils;
 %0A%0Aimport
@@ -2180,37 +2180,45 @@
 esses = 
-client.getAllAddress(
+extractServerAddresses(client
 );%0A%09%09ass
@@ -2590,37 +2590,45 @@
 esses = 
-client.getAllAddress(
+extractServerAddresses(client
 );%0A%09%09ass
@@ -4295,29 +4295,37 @@
 s = 
-client.getAllAddress(
+extractServerAddresses(client
 );%0A%09
@@ -4613,32 +4613,32 @@
 st).hasSize(1);%0A
-
 %09%09assertMongoCre
@@ -4696,24 +4696,379 @@
 test%22);%0A%09%7D%0A%0A
+%09private List%3CServerAddress%3E extractServerAddresses(MongoClient client) %7B%0A%09%09Cluster cluster = (Cluster) ReflectionTestUtils.getField(client, %22cluster%22);%0A%09%09ClusterSettings clusterSettings = (ClusterSettings) ReflectionTestUtils%0A%09%09%09%09.getField(cluster, %22settings%22);%0A%09%09List%3CServerAddress%3E allAddresses = clusterSettings.getHosts();%0A%09%09return allAddresses;%0A%09%7D%0A%0A
 %09private voi

Fix MongoPropertiesTests following upgrade to Mongo 3.2 (6f5bd2e)