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

import com.atlassian.event.api.EventPublisher;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
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 org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class RequiredApproversHelperTest {

    @Mock
    Repository repository;
    @Mock
    PluginSettingsFactory factory;
    @Mock
    PluginSettings pluginSettings;
    @Mock
    EventPublisher eventPublisher;

    RequiredApproversHelper helper;


    @Before
    public void setup() {
        when(factory.createSettingsForKey(any(String.class))).thenReturn(pluginSettings);
        helper = new RequiredApproversHelper(factory, eventPublisher);
    }

    @Test
    public void testEventFiredWhenCountChanged() throws Exception {
        when(pluginSettings.put(any(String.class), any())).thenReturn("1");
        helper.saveRequiredApproversCount(repository, 0);
        verify(eventPublisher).publish(any());
    }

    @Test
    public void testEventNotFiredWhenCountNotChanged() throws Exception {
        when(pluginSettings.put(any(String.class), any())).thenReturn("1");
        helper.saveRequiredApproversCount(repository, 1);
        verifyZeroInteractions(eventPublisher);
    }

    @Test
    public void testNulls() throws Exception {
        when(pluginSettings.put(any(String.class), any())).thenReturn(null);
        helper.saveRequiredApproversCount(repository, null);
        verifyZeroInteractions(eventPublisher);
    }
}
