|
|
@ -6,6 +6,7 @@ import com.atlassian.stash.hook.repository.RepositoryHookContext; |
|
|
|
import com.atlassian.stash.i18n.I18nService; |
|
|
|
import com.atlassian.stash.i18n.I18nService; |
|
|
|
import com.atlassian.stash.repository.RefChange; |
|
|
|
import com.atlassian.stash.repository.RefChange; |
|
|
|
import com.atlassian.stash.repository.Repository; |
|
|
|
import com.atlassian.stash.repository.Repository; |
|
|
|
|
|
|
|
import com.atlassian.stash.repository.RepositoryMetadataService; |
|
|
|
import com.atlassian.stash.scm.CommandErrorHandler; |
|
|
|
import com.atlassian.stash.scm.CommandErrorHandler; |
|
|
|
import com.atlassian.stash.scm.CommandExitHandler; |
|
|
|
import com.atlassian.stash.scm.CommandExitHandler; |
|
|
|
import com.atlassian.stash.scm.CommandOutputHandler; |
|
|
|
import com.atlassian.stash.scm.CommandOutputHandler; |
|
|
@ -20,6 +21,7 @@ import org.junit.Test; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
import org.mockito.ArgumentCaptor; |
|
|
|
import org.mockito.ArgumentCaptor; |
|
|
|
import org.mockito.Captor; |
|
|
|
import org.mockito.Captor; |
|
|
|
|
|
|
|
import org.mockito.Matchers; |
|
|
|
import org.mockito.Mock; |
|
|
|
import org.mockito.Mock; |
|
|
|
import org.mockito.runners.MockitoJUnitRunner; |
|
|
|
import org.mockito.runners.MockitoJUnitRunner; |
|
|
|
|
|
|
|
|
|
|
@ -55,6 +57,8 @@ public class MirrorRepositoryHookTest { |
|
|
|
private PluginSettingsFactory pluginSettingsFactory; |
|
|
|
private PluginSettingsFactory pluginSettingsFactory; |
|
|
|
@Mock |
|
|
|
@Mock |
|
|
|
private PluginSettings pluginSettings; |
|
|
|
private PluginSettings pluginSettings; |
|
|
|
|
|
|
|
@Mock |
|
|
|
|
|
|
|
private RepositoryMetadataService repositoryMetadataService; |
|
|
|
|
|
|
|
|
|
|
|
private final String mirrorRepoUrlHttp = "https://stash-mirror.englishtown.com/scm/test/test.git"; |
|
|
|
private final String mirrorRepoUrlHttp = "https://stash-mirror.englishtown.com/scm/test/test.git"; |
|
|
|
private final String mirrorRepoUrlSsh = "ssh://git@stash-mirror.englishtown.com/scm/test/test.git"; |
|
|
|
private final String mirrorRepoUrlSsh = "ssh://git@stash-mirror.englishtown.com/scm/test/test.git"; |
|
|
@ -85,35 +89,29 @@ public class MirrorRepositoryHookTest { |
|
|
|
when(pluginSettingsFactory.createSettingsForKey(anyString())).thenReturn(pluginSettings); |
|
|
|
when(pluginSettingsFactory.createSettingsForKey(anyString())).thenReturn(pluginSettings); |
|
|
|
|
|
|
|
|
|
|
|
hook = new MirrorRepositoryHook(gitScm, mock(I18nService.class), executor, passwordEncryptor |
|
|
|
hook = new MirrorRepositoryHook(gitScm, mock(I18nService.class), executor, passwordEncryptor |
|
|
|
, settingsReflectionHelper, pluginSettingsFactory); |
|
|
|
, settingsReflectionHelper, pluginSettingsFactory, repositoryMetadataService); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testPostReceive() throws Exception { |
|
|
|
public void testPostReceive() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
when(passwordEncryptor.decrypt(anyString())).thenReturn(password); |
|
|
|
when(passwordEncryptor.decrypt(anyString())).thenReturn(password); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>(); |
|
|
|
|
|
|
|
map.put(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL, ""); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Settings settings = mock(Settings.class); |
|
|
|
|
|
|
|
when(settings.asMap()).thenReturn(map); |
|
|
|
|
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL), eq(""))).thenReturn(mirrorRepoUrlHttp); |
|
|
|
|
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_USERNAME), eq(""))).thenReturn(username); |
|
|
|
|
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_PASSWORD), eq(""))).thenReturn(password); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Repository repo = mock(Repository.class); |
|
|
|
Repository repo = mock(Repository.class); |
|
|
|
when(repo.getName()).thenReturn("test"); |
|
|
|
when(repo.getName()).thenReturn("test"); |
|
|
|
|
|
|
|
|
|
|
|
RepositoryHookContext context = mock(RepositoryHookContext.class); |
|
|
|
hook.postReceive(buildContext(repo), new ArrayList<RefChange>()); |
|
|
|
when(context.getSettings()).thenReturn(settings); |
|
|
|
verifyExecutor(); |
|
|
|
when(context.getRepository()).thenReturn(repo); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testEmptyRepositoriesNotMirrored() { |
|
|
|
|
|
|
|
Repository repo = mock(Repository.class); |
|
|
|
|
|
|
|
when(repositoryMetadataService.isEmpty(repo)).thenReturn(true); |
|
|
|
|
|
|
|
|
|
|
|
Collection<RefChange> refChanges = new ArrayList<RefChange>(); |
|
|
|
hook.postReceive(buildContext(repo), new ArrayList<RefChange>()); |
|
|
|
|
|
|
|
|
|
|
|
hook.postReceive(context, refChanges); |
|
|
|
verify(executor, never()).submit(Matchers.<Callable<Object>>any()); |
|
|
|
verifyExecutor(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
@ -122,7 +120,7 @@ public class MirrorRepositoryHookTest { |
|
|
|
GitScm gitScm = mock(GitScm.class); |
|
|
|
GitScm gitScm = mock(GitScm.class); |
|
|
|
when(gitScm.getCommandBuilderFactory()).thenThrow(new RuntimeException("Intentional unit test exception")); |
|
|
|
when(gitScm.getCommandBuilderFactory()).thenThrow(new RuntimeException("Intentional unit test exception")); |
|
|
|
MirrorRepositoryHook hook = new MirrorRepositoryHook(gitScm, mock(I18nService.class), executor, |
|
|
|
MirrorRepositoryHook hook = new MirrorRepositoryHook(gitScm, mock(I18nService.class), executor, |
|
|
|
passwordEncryptor, settingsReflectionHelper, pluginSettingsFactory); |
|
|
|
passwordEncryptor, settingsReflectionHelper, pluginSettingsFactory, repositoryMetadataService); |
|
|
|
MirrorRepositoryHook.MirrorSettings ms = new MirrorRepositoryHook.MirrorSettings(); |
|
|
|
MirrorRepositoryHook.MirrorSettings ms = new MirrorRepositoryHook.MirrorSettings(); |
|
|
|
ms.mirrorRepoUrl = mirrorRepoUrlHttp; |
|
|
|
ms.mirrorRepoUrl = mirrorRepoUrlHttp; |
|
|
|
ms.username = username; |
|
|
|
ms.username = username; |
|
|
@ -267,4 +265,23 @@ public class MirrorRepositoryHookTest { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private RepositoryHookContext buildContext(Repository repo) { |
|
|
|
|
|
|
|
RepositoryHookContext context = mock(RepositoryHookContext.class); |
|
|
|
|
|
|
|
Settings settings = defaultSettings(); |
|
|
|
|
|
|
|
when(context.getSettings()).thenReturn(settings); |
|
|
|
|
|
|
|
when(context.getRepository()).thenReturn(repo); |
|
|
|
|
|
|
|
return context; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Settings defaultSettings() { |
|
|
|
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>(); |
|
|
|
|
|
|
|
map.put(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL, ""); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Settings settings = mock(Settings.class); |
|
|
|
|
|
|
|
when(settings.asMap()).thenReturn(map); |
|
|
|
|
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL), eq(""))).thenReturn(mirrorRepoUrlHttp); |
|
|
|
|
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_USERNAME), eq(""))).thenReturn(username); |
|
|
|
|
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_PASSWORD), eq(""))).thenReturn(password); |
|
|
|
|
|
|
|
return settings; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|