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

import com.atlassian.bitbucket.internal.ssh.server.SshServer;
import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.ssh.SshAccessKeyService;
import com.atlassian.bitbucket.ssh.SshConfiguration;
import com.atlassian.bitbucket.ssh.SshConfigurationService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
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.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ShowAccessKeySshCloneUrlConditionTest {

    @InjectMocks
    private ShowAccessKeySshCloneUrlCondition condition;
    @Mock
    private SshConfiguration configuration;
    @Mock
    private SshConfigurationService configurationService;
    @Mock
    private SshAccessKeyService keyAccessService;
    @Mock
    private Project project;
    @Mock
    private Repository repository;
    @Mock
    private SshServer sshServer;

    @Before
    public void setUp() throws Exception {
        condition.init(Collections.<String, String>emptyMap());
        when(sshServer.isRunning()).thenReturn(true);
        when(repository.getProject()).thenReturn(project);
        when(configurationService.getConfiguration()).thenReturn(configuration);
        when(configuration.isAccessKeysEnabled()).thenReturn(true);
    }

    @Test
    public void testAccessKeysDisabled() {
        when(configuration.isAccessKeysEnabled()).thenReturn(false);
        assertFalse(condition.shouldDisplay(Collections.<String, Object>singletonMap("repository", repository)));
    }

    @Test
    public void testHasRepoKeys() {
        when(keyAccessService.existsForRepository(repository)).thenReturn(true);
        when(keyAccessService.existsForProject(project)).thenReturn(true);
        assertTrue(condition.shouldDisplay(Collections.<String, Object>singletonMap("repository", repository)));
    }

    @Test
    public void testHasProjectKeysButNoRepo() {
        when(keyAccessService.existsForRepository(repository)).thenReturn(false);
        when(keyAccessService.existsForProject(project)).thenReturn(true);
        assertTrue(condition.shouldDisplay(Collections.<String, Object>singletonMap("repository", repository)));
    }

    @Test
    public void testNoRepoOrProjectKeys() {
        when(keyAccessService.existsForRepository(repository)).thenReturn(false);
        when(keyAccessService.existsForProject(project)).thenReturn(false);
        assertFalse(condition.shouldDisplay(Collections.<String, Object>singletonMap("repository", repository)));
    }

    @Test
    public void testNoRepositoryInContext() {
        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }

    @Test
    public void testSshNotRunning() {
        when(sshServer.isRunning()).thenReturn(false);
        assertFalse(condition.shouldDisplay(Collections.<String, Object>emptyMap()));
    }
}
