package uk.ac.ic.wlgitbridge.bridge.repo;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import uk.ac.ic.wlgitbridge.util.Files;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

import static org.junit.Assert.*;

/**
 * Created by winston on 23/08/2016.
 */
public class FSGitRepoStoreTest {

    public static File makeTempRepoDir(
            TemporaryFolder tmpFolder,
            String name
    ) throws IOException {
        File tmp = tmpFolder.newFolder(name);
        Path rootdir = Paths.get(
                "src/test/resources/uk/ac/ic/wlgitbridge/"
                        + "bridge/repo/FSGitRepoStoreTest/rootdir"
        );
        FileUtils.copyDirectory(rootdir.toFile(), tmp);
        Files.renameAll(tmp, "DOTgit", ".git");
        return tmp;
    }

    private FSGitRepoStore repoStore;
    private File original;

    @Before
    public void setup() throws IOException {
        TemporaryFolder tmpFolder = new TemporaryFolder();
        tmpFolder.create();
        File tmp = makeTempRepoDir(tmpFolder, "rootdir");
        original = tmpFolder.newFolder("original");
        FileUtils.copyDirectory(tmp, original);
        repoStore = new FSGitRepoStore(tmp.getAbsolutePath());
    }

    @Test
    public void testPurgeNonexistentProjects() {
        File toDelete = new File(repoStore.getRootDirectory(), "idontexist");
        File wlgb = new File(repoStore.getRootDirectory(), ".wlgb");
        assertTrue(toDelete.exists());
        assertTrue(wlgb.exists());
        repoStore.purgeNonexistentProjects(Arrays.asList("proj1", "proj2"));
        assertFalse(toDelete.exists());
        assertTrue(wlgb.exists());
    }

    @Test
    public void totalSizeShouldChangeWhenFilesAreCreatedAndDeleted()
            throws IOException {
        long old = repoStore.totalSize();
        File temp = new File(repoStore.getRootDirectory(), "__temp.txt");
        try (
                OutputStream out = new FileOutputStream(
                        temp
                )
        ) {
            out.write(new byte[16 * 1024 * 1024]);
        }
        long new_ = repoStore.totalSize();
        assertTrue(new_ > old);
        assertTrue(temp.delete());
        long new__ = repoStore.totalSize();
        assertTrue(new__ < new_);
    }

    @Test
    public void zipAndUnzipShouldBeTheSame() throws IOException {
        long beforeSize = repoStore.totalSize();
        InputStream zipped = repoStore.bzip2Project("proj1");
        repoStore.remove("proj1");
        assertTrue(beforeSize > repoStore.totalSize());
        repoStore.unbzip2Project("proj1", zipped);
        assertEquals(beforeSize, repoStore.totalSize());
        assertTrue(
                Files.contentsAreEqual(
                        original,
                        repoStore.getRootDirectory()
                )
        );
    }

}
@@ -2476,46 +2476,183 @@
     
-long beforeSize = repoStore.totalSize(
+File expected = new File(original, %22proj1%22);%0A        File actual = new File(repoStore.getRootDirectory(), %22proj1%22);%0A        assertTrue(Files.contentsAreEqual(expected, actual)
 );%0A 
@@ -2765,45 +2765,27 @@
 sert
-True(beforeSize %3E repoStore.totalSize
+False(actual.exists
 ());
@@ -2854,224 +2854,53 @@
 sert
-Equals(beforeSize, repoStore.totalSize());%0A        assertTrue(%0A                Files.contentsAreEqual(%0A                        original,%0A                        repoStore.getRootDirectory()%0A                )%0A        
+True(Files.contentsAreEqual(expected, actual)
 );%0A 

Fix for zip unzip test