/*
 * Copyright (c) 2002-2017 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This product is licensed to you under the Apache License, Version 2.0 (the "License").
 * You may not use this product except in compliance with the License.
 *
 * This product may include a number of subcomponents with
 * separate copyright notices and license terms. Your use of the source
 * code for these subcomponents is subject to the terms and
 *  conditions of the subcomponent's license, as noted in the LICENSE file.
 */

package org.neo4j.ogm.testutil;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.BeforeClass;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.ogm.config.ClasspathConfigurationSource;
import org.neo4j.ogm.config.Configuration;
import org.neo4j.ogm.driver.DriverManager;
import org.neo4j.ogm.drivers.bolt.driver.BoltDriver;
import org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver;
import org.neo4j.ogm.drivers.http.driver.HttpDriver;


/**
 * @author Vince Bickers
 * @author Mark Angrish
 */
public class MultiDriverTestClass {

	private static TestServer testServer;
	private static File graphStore;
	private static Configuration.Builder baseConfiguration;

	static {
        testServer = new TestServer(true, true, 5);
        graphStore = createTemporaryGraphStore();

        String configFileName = System.getenv("ogm.properties");
        if (configFileName == null) {
            configFileName = System.getProperty("ogm.properties");
        }
        if (configFileName == null) {
            configFileName = "ogm.properties";
        }
        baseConfiguration= new Configuration.Builder(new ClasspathConfigurationSource(configFileName));
    }

	@BeforeClass
	public static void setupMultiDriverTestEnvironment() {

		if (baseConfiguration.build().getDriverClassName().equals(HttpDriver.class.getCanonicalName())) {
			baseConfiguration.uri(testServer.getUri()).credentials(testServer.getUsername(), testServer.getPassword());
		} else if (baseConfiguration.build().getDriverClassName().equals(BoltDriver.class.getCanonicalName())) {
			baseConfiguration.uri(testServer.getUri()).credentials(testServer.getUsername(), testServer.getPassword());
		} else {
			baseConfiguration.uri(graphStore.toURI().toString()).build();
		}
	}

	public static Configuration.Builder getBaseConfiguration() {
		return Configuration.Builder.copy(baseConfiguration);
	}

	public static GraphDatabaseService getGraphDatabaseService() {
		// if using an embedded config, return the db from the driver
		if (baseConfiguration.build().getURI().startsWith("file")) {
			return ((EmbeddedDriver) DriverManager.getDriver()).getGraphDatabaseService();
		}
		// else (bolt, http), return just a test server (not really used except for indices ?)
		return testServer.getGraphDatabaseService();
	}

	private static File createTemporaryGraphStore() {
		try {
			Path path = Files.createTempDirectory("graph.db");
			File f = path.toFile();
			f.deleteOnExit();
			return f;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}

@@ -1285,110 +1285,8 @@
 ic %7B
-%0A        testServer = new TestServer(true, true, 5);%0A        graphStore = createTemporaryGraphStore();
 %0A%0A  
@@ -1583,16 +1583,17 @@
 guration
+ 
 = new Co
@@ -1667,88 +1667,17 @@
 ));%0A
+%0A
     
-%7D%0A%0A%09@BeforeClass%0A%09public static void setupMultiDriverTestEnvironment() %7B%0A%0A%09%09
+    
 if (
@@ -1774,128 +1774,395 @@
 ) %7B%0A
-%09%09%09baseConfiguration.uri(testServer.getUri()).credentials(testServer.getUsername(), testServer.getPassword());%0A%09%09%7D else 
+            testServer = new TestServer(true, false, 5);%0A        %7D else if (baseConfiguration.build().getDriverClassName().equals(BoltDriver.class.getCanonicalName())) %7B%0A            testServer = new TestServer(true, true, 5);%0A        %7D else %7B%0A            graphStore = createTemporaryGraphStore();%0A        %7D%0A    %7D%0A%0A%09@BeforeClass%0A%09public static void setupMultiDriverTestEnvironment() %7B%0A%0A%09%09
 if (
@@ -2207,36 +2207,40 @@
 ssName().equals(
-Bolt
+Embedded
 Driver.class.get
@@ -2259,19 +2259,119 @@
 e())) %7B%0A
-%09%09%09
+            baseConfiguration.uri(graphStore.toURI().toString()).build();%0A        %7D else %7B%0A            
 baseConf
@@ -2474,84 +2474,8 @@
 ));%0A
-%09%09%7D else %7B%0A%09%09%09baseConfiguration.uri(graphStore.toURI().toString()).build();%0A
 %09%09%7D%0A

Make tests pick configuration from env parameters