/**
 * Copyright (c) 2002-2015 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.kernel.impl.storemigration;

import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.api.index.SchemaIndexProvider;
import org.neo4j.kernel.impl.api.index.inmemory.InMemoryIndexProvider;
import org.neo4j.kernel.impl.store.StoreFactory;
import org.neo4j.kernel.impl.storemigration.legacystore.v20.Legacy20Store;
import org.neo4j.kernel.impl.storemigration.monitoring.SilentMigrationProgressMonitor;
import org.neo4j.kernel.logging.DevNullLoggingService;
import org.neo4j.kernel.logging.Logging;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.test.EphemeralFileSystemRule;
import org.neo4j.test.PageCacheRule;

import static org.junit.Assert.assertTrue;

public class StoreMigratorTest
{
    private SchemaIndexProvider schemaIndexProvider = new InMemoryIndexProvider();

    private File createNeoStoreWithOlderVersion( String version ) throws IOException
    {
        File storeDir = new File( "dir" ).getAbsoluteFile();
        EphemeralFileSystemAbstraction fileSystem = fs.get();
        fileSystem.mkdirs( storeDir );
        MigrationTestUtils.prepareSampleLegacyDatabase( version, fileSystem, storeDir );
        return storeDir;
    }

    @Test
    public void shouldBeAbleToResumeMigration() throws Exception
    {
        // GIVEN a legacy database
        File storeDirectory = createNeoStoreWithOlderVersion( Legacy20Store.LEGACY_VERSION );
        // and a state of the migration saying that it has done the actual migration
        Logging logging = new DevNullLoggingService();
        PageCache pageCache = pageCacheRule.getPageCache( fs.get() );
        StoreMigrator migrator = new StoreMigrator( new SilentMigrationProgressMonitor(), fs.get(), logging );
        File migrationDir = new File( storeDirectory, StoreUpgrader.MIGRATION_DIRECTORY );
        fs.get().mkdirs( migrationDir );
        assertTrue( migrator.needsMigration( storeDirectory ) );
        migrator.migrate( storeDirectory, migrationDir, schemaIndexProvider, pageCache );
        migrator.close();

        // WHEN simulating resuming the migration
        migrator = new StoreMigrator( new SilentMigrationProgressMonitor(), fs.get(), logging );
        migrator.moveMigratedFiles( migrationDir, storeDirectory );

        // THEN starting the new store should be successful
        StoreFactory storeFactory = new StoreFactory( fs.get(), storeDirectory, pageCache,
                logging.getMessagesLog( getClass() ), new Monitors() );
        storeFactory.newNeoStore( false ).close();
    }

    @Rule
    public final EphemeralFileSystemRule fs = new EphemeralFileSystemRule();
    @Rule
    public final PageCacheRule pageCacheRule = new PageCacheRule();
}

@@ -955,32 +955,67 @@
 o4j.
-graphdb.mockfs.Ephemeral
+io.fs.DefaultFileSystemAbstraction;%0Aimport org.neo4j.io.fs.
 File
@@ -1578,68 +1578,113 @@
 est.
-EphemeralFileSystemRule;%0Aimport org.neo4j.test.PageCacheRule
+PageCacheRule;%0Aimport org.neo4j.test.TargetDirectory;%0Aimport org.neo4j.test.TargetDirectory.TestDirectory
 ;%0A%0Ai
@@ -1771,16 +1771,22 @@
 private 
+final 
 SchemaIn
@@ -1968,143 +1968,46 @@
 r = 
-new File( %22dir%22 ).getAbsoluteFile();%0A        EphemeralFileSystemAbstraction fileSystem = fs.get();%0A        fileSystem.mkdirs( storeDir 
+directory.directory().getAbsoluteFile(
 );%0A 
@@ -2071,25 +2071,17 @@
 rsion, f
-ileSystem
+s
 , storeD
@@ -2528,22 +2528,16 @@
 ache( fs
-.get()
  );%0A    
@@ -2616,38 +2616,32 @@
 essMonitor(), fs
-.get()
 , logging );%0A   
@@ -2743,14 +2743,8 @@
  fs.
-get().
 mkdi
@@ -3073,22 +3073,16 @@
 or(), fs
-.get()
 , loggin
@@ -3275,14 +3275,8 @@
 ( fs
-.get()
 , st
@@ -3457,39 +3457,126 @@
 c final 
-EphemeralFileSystemRule
+TestDirectory directory = TargetDirectory.testDirForTest( getClass() );%0A    public final FileSystemAbstraction
  fs = ne
@@ -3581,17 +3581,15 @@
 new 
-Ephemeral
+Default
 File
@@ -3594,20 +3594,27 @@
 leSystem
-Rule
+Abstraction
 ();%0A    

Test uses real FS due to extreme memory limitations on some build machines