package com.atlassian.stash.internal.plugin.hooks.task;

import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.bitbucket.event.repository.RepositoryDeletedEvent;
import com.atlassian.bitbucket.repository.Repository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static com.atlassian.stash.internal.plugin.hooks.task.IncompleteTasksMergeCheckConfig.PLUGIN_KEY;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class IncompleteTasksMergeCheckConfigTest {

    @Mock
    private PluginSettingsFactory factory;
    @Mock
    private PluginSettings pluginSettings;
    @Mock
    private RepositoryDeletedEvent event;
    @Mock
    private Repository repository;

    private IncompleteTasksMergeCheckConfig pullRequestTaskConfig;

    @Before
    public void setUp() throws Exception {
        when(repository.getId()).thenReturn(99);
        when(factory.createSettingsForKey(PLUGIN_KEY)).thenReturn(pluginSettings);
        pullRequestTaskConfig = new IncompleteTasksMergeCheckConfig(factory);
    }

    @Test
    public void testOnRepositoryDeletedShouldRemoveKeyFromPluginSettings() throws Exception {
        when(event.getRepository()).thenReturn(repository);
        pullRequestTaskConfig.onRepositoryDeleted(event);
        verify(pluginSettings).remove(createKey());
    }

    @Test
    public void testIsRequirePullTasksResolvedShouldReturnTrueIfPluginSettingsReturnsTrue() throws Exception {
        when(pluginSettings.get(createKey())).thenReturn("true");
        assertTrue(pullRequestTaskConfig.isResolutionRequired(repository));
    }

    @Test
    public void testIsRequirePullTasksResolvedShouldReturnFalseIfPluginSettingsReturnsFalse() throws Exception {
        when(pluginSettings.get(createKey())).thenReturn("false");
        assertFalse(pullRequestTaskConfig.isResolutionRequired(repository));
    }

    @Test
    public void testIsRequirePullTasksResolvedShouldReturnFalseIfPluginSettingsReturnsNull() throws Exception {
        when(pluginSettings.get(createKey())).thenReturn(null);
        assertFalse(pullRequestTaskConfig.isResolutionRequired(repository));
    }

    @Test
    public void testSetRequirePullTasksResolvedTrueShouldSetTrueOnPluginSetting() throws Exception {
        pullRequestTaskConfig.setResolutionRequired(repository, true);
        verify(pluginSettings).put(createKey(), "true");
    }

    @Test
    public void testSetRequirePullTasksResolvedFalseShouldSetFalseOnPluginSetting() throws Exception {
        pullRequestTaskConfig.setResolutionRequired(repository, false);
        verify(pluginSettings).put(createKey(), "false");
    }

    private String createKey() {
        return "repo." + repository.getId() + ".requireTasksResolved";
    }
}