package uk.ac.ic.wlgitbridge.data.model;

import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockserver.client.server.MockServerClient;
import org.mockserver.junit.MockServerRule;
import uk.ac.ic.wlgitbridge.bridge.db.DBStore;
import uk.ac.ic.wlgitbridge.bridge.resource.ResourceCache;
import uk.ac.ic.wlgitbridge.bridge.resource.UrlResourceCache;
import uk.ac.ic.wlgitbridge.data.filestore.RawFile;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

/**
 * Created by m on 20/11/15.
 */
public class ResourceFetcherTest {
    @Rule
    public MockServerRule mockServerRule = new MockServerRule(this);

    private MockServerClient mockServerClient;

    @Test
    public void fetchesFilesThatAreMissingFromUrlStoreCache() throws IOException, GitUserException {
        final String testProjectName = "123abc";
        final String testUrl = "http://localhost:" + mockServerRule.getPort() + "/123abc";
        final String oldTestPath = "testPath";
        final String newTestPath = "missingPath";

        mockServerClient.when(
            request()
            .withMethod("GET")
            .withPath("/123abc")
        )
        .respond(
            response()
            .withStatusCode(200)
            .withBody("content")
        );

        final Mockery context = new Mockery();
        final DBStore dbStore = context.mock(DBStore.class);
        context.checking(new Expectations() {{
            // It should fetch the file once it finds it is missing.
            oneOf(dbStore).getPathForURLInProject(testProjectName, testUrl);
            will(returnValue(oldTestPath));

            // It should update the URL index store once it has fetched; at present, it does not actually change the stored path.
            oneOf(dbStore).addURLIndexForProject(testProjectName, testUrl, oldTestPath);
        }});

        ResourceCache resources = new UrlResourceCache(dbStore);
        TemporaryFolder repositoryFolder = new TemporaryFolder();
        repositoryFolder.create();
        Repository repository = new FileRepositoryBuilder().setWorkTree(repositoryFolder.getRoot()).build();
        Map<String, RawFile> fileTable = new RepositoryObjectTreeWalker(repository).getDirectoryContents().getFileTable();
        Map<String, byte[]> fetchedUrls = new HashMap<String, byte[]>();
        resources.get(testProjectName, testUrl, newTestPath, fileTable, fetchedUrls);

        // We don't bother caching in this case, at present.
        assertEquals(0, fetchedUrls.size());
    }
}
@@ -39,108 +39,8 @@
 l;%0A%0A
-import org.eclipse.jgit.lib.Repository;%0Aimport org.eclipse.jgit.storage.file.FileRepositoryBuilder;%0A
 impo
@@ -66,16 +66,16 @@
 ations;%0A
+
 import o
@@ -323,16 +323,232 @@
 BStore;%0A
+import uk.ac.ic.wlgitbridge.bridge.repo.FSGitRepoStore;%0Aimport uk.ac.ic.wlgitbridge.bridge.repo.GitProjectRepo;%0Aimport uk.ac.ic.wlgitbridge.bridge.repo.ProjectRepo;%0Aimport uk.ac.ic.wlgitbridge.bridge.repo.RepoStore;%0A
 import u
@@ -708,24 +708,24 @@
 re.RawFile;%0A
+
 import uk.ac
@@ -775,73 +775,8 @@
 ion;
-%0Aimport uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
 %0A%0Aim
@@ -2558,26 +2558,38 @@
         
-Repository
+String repoStorePath =
  reposit
@@ -2595,16 +2595,79 @@
 tory
+Folder.getRoot().getAbsolutePath();%0A        RepoStore repoStore
  = new F
 ileR
@@ -2666,194 +2666,180 @@
 ew F
-ile
+SGit
 Repo
-sitoryBuilder().setWorkTree(repositoryFolder.getRoot()).build();%0A        Map%3CString, RawFile%3E fileTable = new RepositoryObjectTreeWalker(repository).getDirectoryContents()
+Store(repoStorePath);%0A        ProjectRepo repo = new GitProjectRepo(%22repo%22);%0A        repo.initRepo(repoStore);%0A        Map%3CString, RawFile%3E fileTable = repo
 .getFile
 Tabl
@@ -2834,21 +2834,17 @@
 .getFile
-Table
+s
 ();%0A    
@@ -3111,9 +3111,10 @@
 ;%0A    %7D%0A
-
 %7D
+%0A

Refactor ResourceFetcherTest