package com.indeed.proctor.store;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.Ref;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class GitProctorCoreWithRepositoryTest extends RepositoryTestCase {

    private static final String GIT_USERNAME = "username";
    private static final String GIT_PASSWORD = "password";
    private static final String TEST_DEFINITION_DIRECTORY = "matrices/test-definitions";
    private static final String COMMIT_MESSAGE = "Initial commit";
    private static final String TEST_FILE_NAME = "GitProctorCoreTest.txt";

    private Git remoteGit;

    private String gitUrl;

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Override
    public void setUp() throws Exception {
        super.setUp();
        remoteGit = new Git(db);

        gitUrl = "file://" + remoteGit.getRepository().getWorkTree().getAbsolutePath();

        writeTrashFile(TEST_FILE_NAME, "GitProctorCoreTest");
        remoteGit.add().addFilepattern(TEST_FILE_NAME).call();
        remoteGit.commit().setMessage(COMMIT_MESSAGE).call();
    }

    @Test
    public void testCloneRepository() throws Exception {
        final File workingDir = temporaryFolder.newFolder("testCloneRepository");

        // Run cloneRepository
        final GitProctorCore gitProctorCore = new GitProctorCore(
                gitUrl,
                GIT_USERNAME,
                GIT_PASSWORD,
                TEST_DEFINITION_DIRECTORY,
                workingDir
        );

        final Git git = gitProctorCore.getGit();

        assertNotNull(git);
        assertEquals("master", git.getRepository().getBranch());

        final String localCommitMessage = git.log().call().iterator().next().getFullMessage();
        assertEquals(COMMIT_MESSAGE, localCommitMessage);
    }

    @Test
    public void testCloneRepositoryWithSingleBranch() throws Exception {
        final File workingDir = temporaryFolder.newFolder("testCloneRepositoryWithSingleBranch");
        final String branchName = "test";

        remoteGit.checkout().setCreateBranch(true).setName(branchName).call();
        writeTrashFile(TEST_FILE_NAME, "test");
        remoteGit.add().addFilepattern(TEST_FILE_NAME).call();

        final String commitMessage = "Create a new branch";
        remoteGit.commit().setMessage(commitMessage).call();

        // Run cloneRepository with single branch
        final GitProctorCore gitProctorCore = new GitProctorCore(
                gitUrl,
                GIT_USERNAME,
                GIT_PASSWORD,
                TEST_DEFINITION_DIRECTORY,
                workingDir,
                branchName
        );

        final Git git = gitProctorCore.getGit();

        assertNotNull(git);
        final List<Ref> branchList = git.branchList().call();
        assertEquals(1, branchList.size());
        assertEquals("refs/heads/" + branchName, branchList.get(0).getName());

        final String localCommitMessage = git.log().call().iterator().next().getFullMessage();
        assertEquals(commitMessage, localCommitMessage);

        // should not throw any exceptions
        gitProctorCore.checkoutBranch(branchName);
    }
}

@@ -61,16 +61,63 @@
 pi.Git;%0A
+import org.eclipse.jgit.api.ListBranchCommand;%0A
 import o
@@ -345,33 +345,46 @@
 org.
-junit
+assertj.core.api
 .Assert
+ions
 .assert
-Equals
+That
 ;%0Aim
@@ -1895,25 +1895,13 @@
 sert
-Equals(%22master%22, 
+That(
 git.
@@ -1928,16 +1928,36 @@
 ranch())
+.isEqualTo(%22master%22)
 ;%0A%0A     
@@ -2060,22 +2060,50 @@
   assert
+That(localCommitMessage).is
 Equal
-s
+To
 (COMMIT_
@@ -2113,37 +2113,120 @@
 SAGE
-, localCommitMessage);
+);%0A    %7D%0A%0A    /**%0A     * clone repo having master and test branch with only fetching refs of test branch
 %0A    
-%7D%0A
+ */
 %0A   
@@ -2442,24 +2442,86 @@
  = %22test%22;%0A%0A
+        /* create branch test with one commit off master ***/%0A
         remo
@@ -2579,32 +2579,33 @@
 chName).call();%0A
+%0A
         writeTra
@@ -3268,80 +3268,197 @@
 t().
-call();%0A        assertEquals(1, branchList.size());%0A        assertEquals
+setListMode(ListBranchCommand.ListMode.ALL).call();%0A        // should not contain master%0A        assertThat(branchList.stream().map(Ref::getName))%0A                .containsExactlyInAnyOrder
 (%22re
@@ -3486,35 +3486,34 @@
 me, 
-branchList.get(0).getName()
+%22refs/remotes/origin/test%22
 );%0A%0A
@@ -3625,16 +3625,19 @@
 sert
-Equals(c
+That(localC
 ommi
@@ -3648,16 +3648,21 @@
 sage
-, localC
+).isEqualTo(c
 ommi

Make test fail when checking out more refs