package fr.synchrotron.soleil.ica.ci.lib.mongodb.util;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.ServerAddress;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

/**
 * @author Gregory Boissinot
 */
public class BasicMongoDBDataSourceTest {

    @Test
    public void testDefault() {
        BasicMongoDBDataSource mongoDBDataSource = new BasicMongoDBDataSource();
        final DB mongoDB = mongoDBDataSource.getMongoDB();
        assertNotNull(mongoDB);
        assertEquals(BasicMongoDBDataSource.DEFAULT_MONGODB_DBNAME, mongoDB.getName());
        final Mongo mongo = mongoDB.getMongo();
        assertNotNull(mongo);
        final ServerAddress address = mongo.getAddress();
        assertNotNull(address);
        assertEquals(BasicMongoDBDataSource.DEFAULT_MONGODB_HOST, address.getHost());
        assertEquals(BasicMongoDBDataSource.DEFAULT_MONGODB_PORT, address.getPort());
    }

    @Test
    public void testInstanceWith2Parameters() {
        final String localhost = "localhost";
        final int port = 2000;
        BasicMongoDBDataSource mongoDBDataSource = new BasicMongoDBDataSource(localhost, port);
        final DB mongoDB = mongoDBDataSource.getMongoDB();
        assertNotNull(mongoDB);
        assertEquals(BasicMongoDBDataSource.DEFAULT_MONGODB_DBNAME, mongoDB.getName());
        final Mongo mongo = mongoDB.getMongo();
        assertNotNull(mongo);
        final ServerAddress address = mongo.getAddress();
        assertNotNull(address);
        assertEquals(localhost, address.getHost());
        assertEquals(port, address.getPort());
    }

    @Test
    public void testInstanceWith3Parameters() {
        final String dbName = "myDbName";
        final String localhost = "localhost";
        final int port = 2000;
        BasicMongoDBDataSource mongoDBDataSource = new BasicMongoDBDataSource(localhost, port, dbName);
        final DB mongoDB = mongoDBDataSource.getMongoDB();
        assertNotNull(mongoDB);
        assertEquals(dbName, mongoDB.getName());
        final Mongo mongo = mongoDB.getMongo();
        assertNotNull(mongo);
        final ServerAddress address = mongo.getAddress();
        assertNotNull(address);
        assertEquals(localhost, address.getHost());
        assertEquals(port, address.getPort());
    }

    @Test
    public void testInstanceWithListMongoInstancesAsParameters() {
        MongoDBInstance mongoDBInstance1 = new MongoDBInstance("localhost", 27001);
        MongoDBInstance mongoDBInstance2 = new MongoDBInstance("localhost", 27002);
        final String dbName = "myDbName";
        BasicMongoDBDataSource mongoDBDataSource =
                new BasicMongoDBDataSource(Arrays.asList(mongoDBInstance1, mongoDBInstance2), dbName);
        final DB mongoDB = mongoDBDataSource.getMongoDB();
        assertNotNull(mongoDB);
        assertEquals(dbName, mongoDB.getName());
        final Mongo mongo = mongoDB.getMongo();
        assertNotNull(mongo);

        final List<ServerAddress> serverAddressList = mongo.getAllAddress();
        assertNotNull(serverAddressList);
        assertEquals(2, serverAddressList.size());

        ServerAddress serverAddress1 = serverAddressList.get(0);
        assertEquals(mongoDBInstance1.getHost(), serverAddress1.getHost());
        assertEquals(mongoDBInstance1.getPort(), serverAddress1.getPort());

        ServerAddress serverAddress2 = serverAddressList.get(1);
        assertEquals(mongoDBInstance2.getHost(), serverAddress2.getHost());
        assertEquals(mongoDBInstance2.getPort(), serverAddress2.getPort());
    }

}

@@ -152,24 +152,90 @@
 unit.Test;%0A%0A
+import java.net.UnknownHostException;%0Aimport java.util.ArrayList;%0A
 import java.
@@ -455,16 +455,558 @@
 Test %7B%0A%0A
+    @Test(expected = MongoDBException.class)%0A    public void testUnknownHost() %7B%0A        new BasicMongoDBDataSource(%22unknownHost%22, 27001);%0A    %7D%0A%0A    @Test(expected = MongoDBException.class)%0A    public void testUnknownListHost() throws UnknownHostException %7B%0A        List%3CMongoDBInstance%3E serverAddresses = new ArrayList%3CMongoDBInstance%3E();%0A        serverAddresses.add(new MongoDBInstance(%22localhost%22, 27001));%0A        serverAddresses.add(new MongoDBInstance(%22unknowHost%22, 27002));%0A        new BasicMongoDBDataSource(serverAddresses);%0A    %7D%0A%0A
     @Tes
@@ -3114,210 +3114,895 @@
     
-MongoDBInstance mongoDBInstance1 = new MongoDBInstance(%22localhost%22, 27001);%0A        MongoDBInstance mongoDBInstance2 = new MongoDBInstance(%22localhost%22, 27002);%0A        final String dbName = %22myDb
+initInstanceWithListMongoInstancesAsParameters(null);%0A    %7D%0A%0A    @Test%0A    public void testInstanceWithListMongoInstancesAsParametersWithDbName() %7B%0A        initInstanceWithListMongoInstancesAsParameters(%22myDbName%22);%0A    %7D%0A%0A    private void initInstanceWithListMongoInstancesAsParameters(String dbName) %7B%0A        MongoDBInstance mongoDBInstance1 = new MongoDBInstance(%22localhost%22, 27001);%0A        MongoDBInstance mongoDBInstance2 = new MongoDBInstance(%22localhost%22, 27002);%0A%0A        DB mongoDB = null;%0A        if (dbName == null) %7B%0A            BasicMongoDBDataSource mongoDBDataSource =%0A                    new BasicMongoDBDataSource(Arrays.asList(mongoDBInstance1, mongoDBInstance2));%0A            mongoDB = mongoDBDataSource.getMongoDB();%0A            assertNotNull(mongoDB);%0A            assertEquals(BasicMongoDBDataSource.DEFAULT_MONGODB_DBNAME, mongoDB.get
 Name
-%22
+())
 ;%0A
+        %7D else %7B%0A    
     
@@ -4040,32 +4040,36 @@
 oDBDataSource =%0A
+    
                 
@@ -4159,32 +4159,27 @@
 e);%0A        
-final DB
+   
  mongoDB = m
@@ -4209,32 +4209,36 @@
 goDB();%0A        
+    
 assertNotNull(mo
@@ -4245,32 +4245,36 @@
 ngoDB);%0A        
+   
+ 
 assertEquals(dbN
@@ -4290,32 +4290,42 @@
 oDB.getName());%0A
+        %7D%0A
         final Mo

Add unit tests