/*
 * Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
 *
 * 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.jongo.util;

import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;
import com.mongodb.client.MongoDatabase;
import de.flapdoodle.embed.mongo.Command;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.*;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.config.IRuntimeConfig;
import de.flapdoodle.embed.process.config.io.ProcessOutput;
import de.flapdoodle.embed.process.extract.UserTempNaming;
import de.flapdoodle.embed.process.io.IStreamProcessor;
import de.flapdoodle.embed.process.io.NullProcessor;
import de.flapdoodle.embed.process.runtime.Network;

import java.net.UnknownHostException;

public class MongoResource {

    public DB getDb(String dbname) {
        return getInstance().getDB(dbname);
    }

    public MongoDatabase getDatabase(String dbname) {
        return getInstance().getDatabase(dbname);
    }

    public MongoClient getInstance() {
        String isLocal = System.getProperty("jongo.test.mongo.local");
        if (isLocal != null && isLocal.equals("true")) {
            return LocalMongo.instance;
        } else {
            return EmbeddedMongo.instance;
        }
    }

    /**
     * Launches an embedded Mongod server instance in a separate process.
     *
     * @author Alexandre Dutra
     */
    private static class EmbeddedMongo {

        private static MongoClient instance = getInstance();

        private static MongoClient getInstance() {
            try {
                Command mongoD = Command.MongoD;
                int port = RandomPortNumberGenerator.pickAvailableRandomEphemeralPortNumber();

                ArtifactStoreBuilder artifactStoreBuilder = new ArtifactStoreBuilder();
                artifactStoreBuilder.defaults(mongoD);
                artifactStoreBuilder.executableNaming(new UserTempNaming());

                IStreamProcessor output = new NullProcessor();
                ProcessOutput processOutput = new ProcessOutput(output, output, output);

                IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
                        .defaults(mongoD)
                        .processOutput(processOutput)
                        .artifactStore(artifactStoreBuilder.build())
                        .build();

                Net network = new Net(port, Network.localhostIsIPv6());
                IMongodConfig mongodConfig = new MongodConfigBuilder()
                        .version(getVersion())
                        .cmdOptions(new MongoCmdOptionsBuilder().useStorageEngine("ephemeralForTest").build())
                        .net(network)
                        .build();

                MongodStarter.getInstance(runtimeConfig).prepare(mongodConfig).start();

                return createClient(port);

            } catch (Exception e) {
                throw new RuntimeException("Failed to initialize Embedded Mongo instance: " + e, e);
            }
        }

        private static Version.Main getVersion() {
            String version = System.getProperty("jongo.test.db.version");
            if (version == null) {
                return Version.Main.PRODUCTION;
            }
            return Version.Main.valueOf("V" + version.replaceAll("\\.", "_"));
        }
    }

    private static class LocalMongo {

        private static MongoClient instance = getInstance();

        private static MongoClient getInstance() {
            try {
                return createClient(27017);
            } catch (Exception e) {
                throw new RuntimeException("Failed to initialize local Mongo instance: " + e, e);
            }
        }
    }

    private static MongoClient createClient(int port) throws UnknownHostException {
        MongoClient mongo = new MongoClient("127.0.0.1", port);
        mongo.setWriteConcern(WriteConcern.SAFE);
        return mongo;
    }
}

@@ -1346,23 +1346,263 @@
 ess.
-runtime.Network
+io.directories.FixedPath;%0Aimport de.flapdoodle.embed.process.io.directories.IDirectory;%0Aimport de.flapdoodle.embed.process.io.directories.UserHome;%0Aimport de.flapdoodle.embed.process.runtime.Network;%0Aimport de.flapdoodle.embed.process.store.IArtifactStore
 ;%0A%0Ai
@@ -2618,116 +2618,370 @@
     
-ArtifactStoreBuilder artifactStoreBuilder = new ArtifactStoreBuilder();%0A                artifactStoreBuilder
+de.flapdoodle.embed.process.config.store.DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder()%0A                        .defaultsForCommand(mongoD)%0A                        .defaults()%0A                        .artifactStorePath(getMongoPath());%0A%0A                IArtifactStore artifactStore = new ArtifactStoreBuilder()%0A                        
 .def
@@ -2993,17 +2993,16 @@
 (mongoD)
-;
 %0A       
@@ -3010,36 +3010,24 @@
         
-artifactStoreBuilder
+        
 .executa
@@ -3057,16 +3057,106 @@
 aming())
+%0A                        .download(downloadConfigBuilder)%0A                        .build()
 ;%0A%0A     
@@ -3529,23 +3529,8 @@
 tore
-Builder.build()
 )%0A  
@@ -3566,80 +3566,8 @@
 );%0A%0A
-                Net network = new Net(port, Network.localhostIsIPv6());%0A
     
@@ -3822,21 +3822,54 @@
  .net(ne
-twork
+w Net(port, Network.localhostIsIPv6())
 )%0A      
@@ -4188,24 +4188,292 @@
 %0A        %7D%0A%0A
+        private static IDirectory getMongoPath() %7B%0A            String path = System.getProperty(%22jongo.test.db.path%22);%0A            if (path == null) %7B%0A                return new UserHome(%22.embedmongo%22);%0A            %7D%0A            return new FixedPath(path);%0A        %7D%0A%0A
         priv

Allow MongoResource to download MongoDb into a custom path