/*
 * Copyright 2013 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 com.gopivotal.cloudfoundry.test.core;

import com.mongodb.DB;
import com.mongodb.ServerAddress;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.stereotype.Component;

@Component
public class MongoDbUtils extends AbstractServiceUtils<MongoDbFactory> {

    public String checkAccess(MongoDbFactory mongoDbFactory) {
        try {
            DB db = mongoDbFactory.getDb();
            db.getMongo();
            return "ok";
        } catch (Exception e) {
            return "failed with " + e.getMessage();
        }
    }

    public String getUrl(MongoDbFactory mongoDbFactory) {
        DB mongoDb = mongoDbFactory.getDb();

        if (mongoDb != null) {
            ServerAddress serverAddress = mongoDb.getMongo().getAddress();
            String host = serverAddress.getHost();
            int port = serverAddress.getPort();
            String db = mongoDb.getName();

            return String.format("mongodb://%s:%d/%s", host, port, db);
        }

        return "mongodb://fake.connection";
    }

}

@@ -657,16 +657,40 @@
 .core;%0A%0A
+import java.util.List;%0A%0A
 import c
@@ -1334,24 +1334,25 @@
  != null) %7B%0A
+%0A
             
@@ -1351,16 +1351,78 @@
         
+StringBuilder builder = new StringBuilder();%0A            List%3C
 ServerAd
@@ -1426,16 +1426,17 @@
 rAddress
+%3E
  serverA
@@ -1441,16 +1441,18 @@
 rAddress
+es
  = mongo
@@ -1469,16 +1469,19 @@
 o().getA
+llA
 ddress()
@@ -1498,21 +1498,47 @@
     
-String host =
+builder.append(getServerString(mongoDb,
  ser
@@ -1551,20 +1551,22 @@
 ress
+es
 .get
-Host(
+(0))
 );%0A
+%0A
     
@@ -1577,213 +1577,542 @@
     
-int port = serverAddress.getPort();%0A            String db = mongoDb.getName();%0A%0A            return String.format(%22mongodb://%25s:%25d/%25s%22, host, port, db);%0A        %7D%0A%0A        return %22mongodb://fake.connection%22
+for (int i = 1; i %3C serverAddresses.size(); i++) %7B%0A                builder.append(%22, %22 + getServerString(mongoDb, serverAddresses.get(i)));%0A            %7D%0A%0A            return builder.toString();%0A        %7D%0A%0A        return %22mongodb://fake.connection%22;%0A    %7D%0A%0A    private String getServerString(DB mongoDb, ServerAddress serverAddress) %7B%0A        String host = serverAddress.getHost();%0A        int port = serverAddress.getPort();%0A        String name = mongoDb.getName();%0A        return String.format(%22mongodb://%25s:%25d/%25s%22, host, port, name)
 ;%0A  

Support MongoDB replica sets