package com.atlassian.bitbucket.mesh.git.builder.branch;

import com.atlassian.bitbucket.mesh.git.GitAgent;
import com.atlassian.bitbucket.mesh.git.ref.Branch;
import com.atlassian.bitbucket.mesh.repository.Repository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class CreateBranchStdioHandlerTest {

    @Mock
    private GitAgent agent;
    @Mock
    private Repository repository;

    @Test
    public void testUsesAgentToResolveBranchName() {
        String branchName = "On your left.";
        Branch branch = new Branch.Builder()
                .id(branchName)
                .latestCommit("It’s probably too late to go to the bathroom, right?")
                .isDefault(true)
                .build();

        when(agent.resolveBranch(repository, branchName)).thenReturn(branch);

        CreateBranchStdioHandler handler = new CreateBranchStdioHandler(agent, branchName, repository);
        assertEquals(branch, handler.getResult());
        verify(agent).resolveBranch(repository, branchName);
    }

    @Test(expected = NullPointerException.class)
    public void testThrowsIfAgentIsNull() {
        new CreateBranchStdioHandler(null, "I do what he does, just slower.", repository);
    }

    @Test(expected = NullPointerException.class)
    public void testThrowsIfBranchNameIsNull() {
        new CreateBranchStdioHandler(agent, null, repository);
    }

    @Test(expected = NullPointerException.class)
    public void testThrowsIfRepositoryIsNull() {
        new CreateBranchStdioHandler(agent, "Honestly, until this exact second, " +
                "I thought you were a Build-a-Bear.", null);
    }
}