/*
 * Copyright 2010-2016 OrientDB LTD (http://orientdb.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 com.orientechnologies.orient.test.database.auto;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.command.OCommandOutputListener;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.db.tool.ODatabaseCompare;
import com.orientechnologies.orient.core.db.tool.ODatabaseExport;
import com.orientechnologies.orient.core.db.tool.ODatabaseImport;
import com.orientechnologies.orient.core.hook.ORecordHook;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.testng.Assert;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

@Test(groups = {"db", "import-export"})
public class DbImportExportTest extends DocumentDBBaseTest implements OCommandOutputListener {
  public static final String EXPORT_FILE_PATH = "target/db.export.gz";
  public static final String NEW_DB_PATH = "target/test-import";
  public static final String NEW_DB_URL = "target/test-import";

  private String testPath;
  private String exportFilePath;
  private boolean dumpMode = false;

  @Parameters(value = {"url", "testPath"})
  public DbImportExportTest(@Optional String url, String testPath) {
    super(url);
    this.testPath = testPath;

    exportFilePath = System.getProperty("exportFilePath", EXPORT_FILE_PATH);
  }

  @Test
  public void testDbExport() throws IOException {
    ODatabaseDocumentTx database = new ODatabaseDocumentTx(url);
    database.open("admin", "admin");

    // ADD A CUSTOM TO THE CLASS
    database
        .command(new OCommandSQL("alter class V custom onBeforeCreate=onBeforeCreateItem"))
        .execute();

    ODatabaseExport export = new ODatabaseExport(database, testPath + "/" + exportFilePath, this);
    export.exportDatabase();
    export.close();

    database.close();
  }

  @Test(dependsOnMethods = "testDbExport")
  public void testDbImport() throws IOException {
    final File importDir = new File(testPath + "/" + NEW_DB_PATH);
    if (importDir.exists()) for (File f : importDir.listFiles()) f.delete();
    else importDir.mkdir();

    final ODatabaseDocumentTx database =
        new ODatabaseDocumentTx(getStorageType() + ":" + testPath + "/" + NEW_DB_URL);
    database.create();

    ODatabaseImport dbImport = new ODatabaseImport(database, testPath + "/" + exportFilePath, this);

    // UNREGISTER ALL THE HOOKS
    for (final ORecordHook hook : new ArrayList<>(database.getHooks().keySet())) {
      database.unregisterHook(hook);
    }
    dbImport.setPreserveRids(true);
    dbImport.setDeleteRIDMapping(false);
    dbImport.importDatabase();
    dbImport.close();

    database.close();
  }

  @Test(dependsOnMethods = "testDbImport")
  public void testCompareDatabases() throws IOException {
    if ("remote".equals(getStorageType()) || url.startsWith("remote:")) {
      String env = getTestEnv();
      if (env == null || env.equals("dev")) return;

      // EXECUTES ONLY IF NOT REMOTE ON CI/RELEASE TEST ENV
    }

    String urlPrefix = getStorageType() + ":";

    final ODatabaseCompare databaseCompare =
        new ODatabaseCompare(
            url,
            urlPrefix + testPath + "/" + DbImportExportTest.NEW_DB_URL,
            "admin",
            "admin",
            this);
    databaseCompare.setCompareEntriesForAutomaticIndexes(true);
    databaseCompare.setCompareIndexMetadata(true);

    Assert.assertTrue(databaseCompare.compare());
  }

  @Override
  @Test(enabled = false)
  public void onMessage(final String iText) {
    if (iText != null && iText.contains("ERR"))
      // ACTIVATE DUMP MODE
      dumpMode = true;

    if (dumpMode) OLogManager.instance().error(this, iText, null);
  }
}

@@ -2003,21 +2003,25 @@
 stPath;%0A
-%0A
     
+this.
 exportFi
@@ -2148,24 +2148,30 @@
 ption %7B%0A    
+final 
 ODatabaseDoc
@@ -2412,32 +2412,38 @@
 execute();%0A%0A    
+final 
 ODatabaseExport 
@@ -2446,24 +2446,32 @@
 ort export =
+%0A       
  new ODataba
@@ -2570,33 +2570,32 @@
 export.close();%0A
-%0A
     database.clo
@@ -2793,21 +2793,35 @@
 ists()) 
-for (
+%7B%0A      for (final 
 File f :
@@ -2843,16 +2843,26 @@
 Files())
+ %7B%0A       
  f.delet
@@ -2874,12 +2874,30 @@
     
-else
+  %7D%0A    %7D else %7B%0A     
  imp
@@ -2911,16 +2911,22 @@
 mkdir();
+%0A    %7D
 %0A%0A    fi
@@ -3075,16 +3075,22 @@
 );%0A%0A    
+final 
 ODatabas
@@ -3107,16 +3107,24 @@
 Import =
+%0A       
  new ODa
@@ -3680,24 +3680,30 @@
 %22)) %7B%0A      
+final 
 String env =
@@ -3760,16 +3760,26 @@
 (%22dev%22))
+ %7B%0A       
  return;
@@ -3779,16 +3779,23 @@
 return;%0A
+      %7D
 %0A      /
@@ -3853,21 +3853,26 @@
 V%0A    %7D%0A
-%0A
     
+final 
 String u
@@ -4433,16 +4433,18 @@
 (%22ERR%22))
+ %7B
 %0A      /
@@ -4487,16 +4487,21 @@
 = true;%0A
+    %7D
 %0A    if 
@@ -4510,16 +4510,24 @@
 umpMode)
+ %7B%0A     
  OLogMan
@@ -4568,14 +4568,20 @@
  null);%0A
+    %7D%0A
   %7D%0A%7D%0A

test changes