package com.atlassian.bitbucket.internal.key.ssh.condition;

import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.ssh.SshAccessKeyService;
import com.atlassian.bitbucket.ssh.SshKey;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Collections;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class RepositoryHasSshAccessKeyConditionTest {
    private RepositoryHasSshAccessKeyCondition condition;

    @Mock
    private Repository repository;
    @Mock
    private SshKey sshKey;
    @Mock
    private SshAccessKeyService sshKeyAccessService = null;

    @Before
    public void setUp() throws Exception {
        condition = new RepositoryHasSshAccessKeyCondition(sshKeyAccessService);
        condition.init(Collections.<String, String>emptyMap());
    }

    @Test
    public void testNoRepository() throws Exception {
        when(sshKeyAccessService.existsForRepository(eq(repository))).thenReturn(false);
        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }

    @Test
    public void testNoSshKeyAccessEntries() throws Exception {
        when(sshKeyAccessService.existsForRepository(eq(repository))).thenReturn(false);
        assertFalse(condition.shouldDisplay(Collections.<String, Object>singletonMap("repository", repository)));
    }

    @Test
    public void testHasSshKeyAccessEntries() throws Exception {
        when(sshKeyAccessService.existsForRepository(eq(repository))).thenReturn(true);
        assertTrue(condition.shouldDisplay(Collections.<String, Object>singletonMap("repository", repository)));
    }
}
