// Path: spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/document/MongoDbFactoryCreator.java
// public class MongoDbFactoryCreator extends AbstractServiceConnectorCreator<MongoDbFactory, MongoServiceInfo> {
// 	@Override
// 	public MongoDbFactory create(MongoServiceInfo serviceInfo, ServiceConnectorConfig config) {
// 		try {
// 			MongoClientOptions.Builder mongoOptionsToUse = getMongoOptions((MongoDbFactoryConfig) config);
// 
// 			SimpleMongoDbFactory mongoDbFactory = createMongoDbFactory(serviceInfo, mongoOptionsToUse);
// 
// 			return configure(mongoDbFactory, (MongoDbFactoryConfig) config);
// 		} catch (UnknownHostException e) {
// 			throw new ServiceConnectorCreationException(e);
// 		} catch (MongoException e) {
// 			throw new ServiceConnectorCreationException(e);
// 		}
// 	}
// 
// 	private SimpleMongoDbFactory createMongoDbFactory(MongoServiceInfo serviceInfo, MongoClientOptions.Builder mongoOptionsToUse) throws UnknownHostException {
// 		MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri(), mongoOptionsToUse);
// 		MongoClient mongo = new MongoClient(mongoClientURI);
// 		return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase());
// 	}
// 
// 	private MongoClientOptions.Builder getMongoOptions(MongoDbFactoryConfig config) {
// 		MongoClientOptions.Builder builder = MongoClientOptions.builder();
// 
// 		if (config != null) {
// 			if (config.getConnectionsPerHost() != null) {
// 				builder.connectionsPerHost(config.getConnectionsPerHost());
// 			}
// 			if (config.getMaxWaitTime() != null) {
// 				builder.maxWaitTime(config.getMaxWaitTime());
// 			}
// 			if (config.getWriteConcern() != null) {
// 				builder.writeConcern(new WriteConcern(config.getWriteConcern()));
// 			}
// 		}
// 
// 		return builder;
// 	}
// 
// 	public SimpleMongoDbFactory configure(SimpleMongoDbFactory mongoDbFactory, MongoDbFactoryConfig config) {
// 		if (config != null && config.getWriteConcern() != null) {
// 			WriteConcern writeConcern = WriteConcern.valueOf(config.getWriteConcern());
// 			if (writeConcern != null) {
// 				mongoDbFactory.setWriteConcern(writeConcern);
// 			}
// 		}
// 		return mongoDbFactory;
// 	}
// }


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.lang.reflect.Method;
import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.common.MongoServiceInfo;
import org.springframework.cloud.service.document.MongoDbFactoryCreator;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
package org.springframework.cloud.service.mongo;





/**
 * Test cases for Mongo service connector creators.
 *
 * @author Ramnivas Laddad
 * @author Chris Schaefer
 */
public class MongoServiceConnectorCreatorTest {
	private static final String TEST_HOST = "10.20.30.40";
	private static final String TEST_HOST_1 = "10.20.30.41";
	private static final String TEST_HOST_2 = "10.20.30.42";
	private static final int TEST_PORT = 1234;
	private static final int TEST_PORT_DEFAULT = 27017;
	private static final String TEST_USERNAME = "myuser";
	private static final String TEST_PASSWORD = "mypass";
	private static final String TEST_DB = "mydb";
	private static final String MONGODB_SCHEME = "mongodb";
	private static final String[] TEST_HOSTS = new String[] { TEST_HOST, TEST_HOST_1, TEST_HOST_2 };

	private MongoDbFactoryCreator testCreator = new MongoDbFactoryCreator();

	@Test
	public void cloudMongoCreationNoConfig() throws Exception {
		MongoServiceInfo serviceInfo = new MongoServiceInfo("id", TEST_HOST, TEST_PORT, TEST_USERNAME, TEST_PASSWORD, TEST_DB);

		MongoDbFactory mongoDbFactory = testCreator.create(serviceInfo, null);
		assertNotNull(mongoDbFactory);

		MongoClient mongoClient = getMongoClientField(mongoDbFactory);

		MongoCredential credentials = mongoClient.getCredentialsList().get(0);

		List<ServerAddress> addresses = extractServerAddresses(mongoClient);
		assertEquals(1, addresses.size());

		ServerAddress address = addresses.get(0);

		assertEquals(serviceInfo.getHost(), address.getHost());
		assertEquals(serviceInfo.getPort(), address.getPort());
		assertEquals(serviceInfo.getUserName(), credentials.getUserName());
		assertNotNull(credentials.getPassword());

		// Don't do connector.getDatabase().getName() as that will try to initiate the connection
		assertEquals(serviceInfo.getDatabase(), ReflectionTestUtils.getField(mongoDbFactory, "databaseName"));
	}

	@Test
// Path: spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/document/MongoDbFactoryCreator.java
// public class MongoDbFactoryCreator extends AbstractServiceConnectorCreator<MongoDbFactory, MongoServiceInfo> {
// 	@Override
// 	public MongoDbFactory create(MongoServiceInfo serviceInfo, ServiceConnectorConfig config) {
// 		try {
// 			MongoClientOptions.Builder mongoOptionsToUse = getMongoOptions((MongoDbFactoryConfig) config);
// 
// 			SimpleMongoDbFactory mongoDbFactory = createMongoDbFactory(serviceInfo, mongoOptionsToUse);
// 
// 			return configure(mongoDbFactory, (MongoDbFactoryConfig) config);
// 		} catch (UnknownHostException e) {
// 			throw new ServiceConnectorCreationException(e);
// 		} catch (MongoException e) {
// 			throw new ServiceConnectorCreationException(e);
// 		}
// 	}
// 
// 	private SimpleMongoDbFactory createMongoDbFactory(MongoServiceInfo serviceInfo, MongoClientOptions.Builder mongoOptionsToUse) throws UnknownHostException {
// 		MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri(), mongoOptionsToUse);
// 		MongoClient mongo = new MongoClient(mongoClientURI);
// 		return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase());
// 	}
// 
// 	private MongoClientOptions.Builder getMongoOptions(MongoDbFactoryConfig config) {
// 		MongoClientOptions.Builder builder = MongoClientOptions.builder();
// 
// 		if (config != null) {
// 			if (config.getConnectionsPerHost() != null) {
// 				builder.connectionsPerHost(config.getConnectionsPerHost());
// 			}
// 			if (config.getMaxWaitTime() != null) {
// 				builder.maxWaitTime(config.getMaxWaitTime());
// 			}
// 			if (config.getWriteConcern() != null) {
// 				builder.writeConcern(new WriteConcern(config.getWriteConcern()));
// 			}
// 		}
// 
// 		return builder;
// 	}
// 
// 	public SimpleMongoDbFactory configure(SimpleMongoDbFactory mongoDbFactory, MongoDbFactoryConfig config) {
// 		if (config != null && config.getWriteConcern() != null) {
// 			WriteConcern writeConcern = WriteConcern.valueOf(config.getWriteConcern());
// 			if (writeConcern != null) {
// 				mongoDbFactory.setWriteConcern(writeConcern);
// 			}
// 		}
// 		return mongoDbFactory;
// 	}
// }


// Path: spring-cloud-spring-service-connector/src/test/java/org/springframework/cloud/service/mongo/MongoServiceConnectorCreatorTest.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.lang.reflect.Method;
import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.common.MongoServiceInfo;
import org.springframework.cloud.service.document.MongoDbFactoryCreator;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

package org.springframework.cloud.service.mongo;





/**
 * Test cases for Mongo service connector creators.
 *
 * @author Ramnivas Laddad
 * @author Chris Schaefer
 */
public class MongoServiceConnectorCreatorTest {
	private static final String TEST_HOST = "10.20.30.40";
	private static final String TEST_HOST_1 = "10.20.30.41";
	private static final String TEST_HOST_2 = "10.20.30.42";
	private static final int TEST_PORT = 1234;
	private static final int TEST_PORT_DEFAULT = 27017;
	private static final String TEST_USERNAME = "myuser";
	private static final String TEST_PASSWORD = "mypass";
	private static final String TEST_DB = "mydb";
	private static final String MONGODB_SCHEME = "mongodb";
	private static final String[] TEST_HOSTS = new String[] { TEST_HOST, TEST_HOST_1, TEST_HOST_2 };

	private MongoDbFactoryCreator testCreator = new MongoDbFactoryCreator();

	@Test
	public void cloudMongoCreationNoConfig() throws Exception {
		MongoServiceInfo serviceInfo = new MongoServiceInfo("id", TEST_HOST, TEST_PORT, TEST_USERNAME, TEST_PASSWORD, TEST_DB);

		MongoDbFactory mongoDbFactory = testCreator.create(serviceInfo, null);
		assertNotNull(mongoDbFactory);

		MongoClient mongoClient = getMongoClientField(mongoDbFactory);

		MongoCredential credentials = mongoClient.getCredentialsList().get(0);

		List<ServerAddress> addresses = extractServerAddresses(mongoClient);
		assertEquals(1, addresses.size());

		ServerAddress address = addresses.get(0);

		assertEquals(serviceInfo.getHost(), address.getHost());
		assertEquals(serviceInfo.getPort(), address.getPort());
		assertEquals(serviceInfo.getUserName(), credentials.getUserName());
		assertNotNull(credentials.getPassword());

		// Don't do connector.getDatabase().getName() as that will try to initiate the connection
		assertEquals(serviceInfo.getDatabase(), ReflectionTestUtils.getField(mongoDbFactory, "databaseName"));
	}

	@Test
	public void cloudMongoCreationWithMultipleHostsByUri() throws Exception {