package com.orientechnologies.orient.server.distributed;

import static org.junit.Assert.assertTrue;

import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.ODatabaseType;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.db.tool.ODatabaseExport;
import com.orientechnologies.orient.core.db.tool.ODatabaseImport;
import com.orientechnologies.orient.server.OServer;
import java.io.File;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DistributedDatabaseImportIT {

  private OServer server0;
  private OServer server1;
  private OServer server2;

  @Before
  public void before() throws Exception {
    server0 = OServer.startFromClasspathConfig("orientdb-simple-dserver-config-0.xml");
    server1 = OServer.startFromClasspathConfig("orientdb-simple-dserver-config-1.xml");
    server2 = OServer.startFromClasspathConfig("orientdb-simple-dserver-config-2.xml");
  }

  @Test
  public void test() throws IOException {
    final OrientDB ctx1 = server0.getContext();
    ctx1.create("import-test", ODatabaseType.PLOCAL);
    final ODatabaseSession session = ctx1.open("import-test", "admin", "admin");
    session.createClass("testa");
    final ODatabaseExport export =
        new ODatabaseExport(
            (ODatabaseDocumentInternal) session, "target/export.tar.gz", iText -> {});
    export.exportDatabase();
    export.close();
    session.close();

    ctx1.create("imported-test", ODatabaseType.PLOCAL);
    final ODatabaseSession session1 = ctx1.open("imported-test", "admin", "admin");
    final ODatabaseImport imp =
        new ODatabaseImport(
            (ODatabaseDocumentInternal) session1, "target/export.tar.gz", iText -> {});
    imp.importDatabase();
    imp.close();
    session1.close();

    final OrientDB ctx2 = server1.getContext();
    final ODatabaseSession session2 = ctx2.open("imported-test", "admin", "admin");
    assertTrue(session2.getMetadata().getSchema().existsClass("testa"));
    session2.close();
  }

  @After
  public void after() throws InterruptedException {
    server0.dropDatabase("import-test");
    server0.dropDatabase("imported-test");
    server0.shutdown();
    server1.shutdown();
    server2.shutdown();
    File file = new File("target/export.tar.gz");
    if (file.exists()) file.delete();
    ODatabaseDocumentTx.closeAll();
  }
}

@@ -55,52 +55,8 @@
 d;%0A%0A
-import static org.junit.Assert.assertTrue;%0A%0A
 impo
@@ -545,259 +545,547 @@
 t.se
-rver.OServer;%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Apublic class DistributedDatabaseImportIT %7B%0A%0A  private OServer server0;%0A  private OServer server1;%0A  private OServer
+tup.LocalTestSetup;%0Aimport com.orientechnologies.orient.setup.SetupConfig;%0Aimport com.orientechnologies.orient.setup.configs.SimpleDServerConfig;%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0A%0Aimport static org.junit.Assert.assertTrue;%0A%0Apublic class DistributedDatabaseImportIT %7B%0A%0A  // Relies on direct access to OServer to import DB and can run only on local setup.%0A  private LocalTestSetup setup;%0A  private SetupConfig config;%0A  private String server0, server1,
  ser
@@ -1151,90 +1151,85 @@
     
-server0 = OServer.startFromClasspathConfig(%22orientdb-s
+config = new SimpleDServerConfig();%0A    server0 = S
 imple
--ds
+DS
 erver
--c
+C
 onfig
--0.xml%22)
+.SERVER0
 ;%0A  
@@ -1244,80 +1244,35 @@
 1 = 
-OServer.startFromClasspathConfig(%22orientdb-s
+S
 imple
--ds
+DS
 erver
--c
+C
 onfig
--1.xml%22)
+.SERVER1
 ;%0A  
@@ -1287,79 +1287,93 @@
 2 = 
-OServer.startFromClasspathConfig(%22orientdb-simple-dserver-config-2.xml%22
+SimpleDServerConfig.SERVER2;%0A    setup = new LocalTestSetup(config);%0A    setup.setup(
 );%0A 
@@ -1454,21 +1454,58 @@
 tx1 = se
-rver0
+tup.getServer(server0).getServerInstance()
 .getCont
@@ -2285,21 +2285,58 @@
 tx2 = se
-rver1
+tup.getServer(server1).getServerInstance()
 .getCont
@@ -2597,150 +2597,174 @@
   se
-rver0.dropDatabase(%22import-test%22);%0A    server0.dropDatabase(%22imported-test%22);%0A    server0.shutdown();%0A    server1.shutdown();%0A    server2.shut
+tup.getServer(server0).getServerInstance().dropDatabase(%22import-test%22);%0A    setup.getServer(server0).getServerInstance().dropDatabase(%22imported-test%22);%0A    setup.tear
 down

move DistributedDatabaseImportIT to new setup