package com.zakgof.db.velvet.test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.function.Supplier;

import org.apache.commons.text.StrSubstitutor;
import org.junit.AfterClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import com.zakgof.db.velvet.IVelvetEnvironment;
import com.zakgof.db.velvet.VelvetException;
import com.zakgof.db.velvet.VelvetFactory;

@RunWith(Suite.class)

@SuiteClasses({

    PrimarySortedLinkTest.class,
    PrimarySortedLinkTest2.class,
    SortedStoreTest.class,
    KeylessTest.class,
    PutGetTest.class,
    SimpleLinkTest.class,

  /*
    StoreIndexesTest.class,
    SecondarySortedLinkTest.class,
    PerformanceTest.class,
    ConcurrentWriteTest.class,

    */
})

public abstract class VelvetTestSuite {

    protected static Supplier<IVelvetEnvironment> velvetProvider;
    private static IVelvetEnvironment env;
    private static String PATH;

    protected static void setup(String providerName) {
        try {
            PATH = Files.createTempDirectory("velvet").toString();
        } catch (IOException e) {
            throw new VelvetException(e);
        }
        velvetProvider = () -> createVelvet(providerName);
    }

    private static IVelvetEnvironment createVelvet(String providerName) {
        tearDownClass();
        if (providerName.equals("datastore")) {
            String url =  StrSubstitutor.replaceSystemProperties("velvetdb://datastore/${velvetdb.datastore.projectId}/?credentialPath=${velvetdb.datastore.credentialPath}&proxyHost=${velvetdb.datastore.proxyHost}&proxyPort=${velvetdb.datastore.proxyPort}&proxyUser=${velvetdb.datastore.proxyUser}&proxyPassword=${velvetdb.datastore.proxyPassword}");
            env = VelvetFactory.open(url);
        } else if (providerName.equals("dynamodb")) {
            String url =  StrSubstitutor.replaceSystemProperties("velvetdb://dynamodb/us-west-2?awsAccessKeyId=${velvetdb.aws.accessKeyId}&awsSecretKey=${velvetdb.aws.secretKey}");
            env = VelvetFactory.open(url);
        } else{
            new File(PATH).mkdirs();
            env = VelvetFactory.open("velvetdb://" + providerName + "/" + PATH.replace(File.separatorChar, '/'));
            // env.setSerializer(() -> new KryoSerializer());
            // env.setSerializer(() -> new ElsaVelvetSerializer());
        }
        return env;
    }

    @AfterClass
    public static void tearDownClass() {
        if (env != null) {
            env.close();
            for (File file : new File(PATH).listFiles())
                file.delete();
        }
    }
}

@@ -507,16 +507,17 @@
 sses(%7B%0A%0A
+%0A
     Prim
@@ -598,24 +598,50 @@
 Test.class,%0A
+    SimpleLinkTest.class,%0A
     KeylessT
@@ -677,35 +677,11 @@
 ss,%0A
+%0A
   
-  SimpleLinkTest.class,%0A%0A
   /*
@@ -775,40 +775,8 @@
 ss,%0A
-    ConcurrentWriteTest.class,%0A%0A
     
@@ -1589,34 +1589,24 @@
 =$%7Bvelvetdb.
-datastore.
 proxyHost%7D&p
@@ -1621,34 +1621,24 @@
 =$%7Bvelvetdb.
-datastore.
 proxyPort%7D&p
@@ -1653,34 +1653,24 @@
 =$%7Bvelvetdb.
-datastore.
 proxyUser%7D&p
@@ -1693,26 +1693,16 @@
 elvetdb.
-datastore.
 proxyPas
@@ -1989,52 +1989,319 @@
 Key%7D
-%22);%0A            env = VelvetFactory.open(url
+&proxyHost=$%7Bvelvetdb.proxyHost%7D&proxyPort=$%7Bvelvetdb.proxyPort%7D&proxyUser=$%7Bvelvetdb.proxyUser%7D&proxyPassword=$%7Bvelvetdb.proxyPassword%7D%22);%0A            env = VelvetFactory.open(url);%0A            env.execute(velvet -%3E %7B%0A                velvet.getClass().getDeclaredMethod(%22killAll%22).invoke(velvet);%0A            %7D
 );%0A 

Resolve conflicts