|
|
@ -2,7 +2,8 @@ package com.englishtown.bitbucket.hook; |
|
|
|
|
|
|
|
|
|
|
|
import com.atlassian.bitbucket.hook.repository.RepositoryHookContext; |
|
|
|
import com.atlassian.bitbucket.hook.repository.RepositoryHookContext; |
|
|
|
import com.atlassian.bitbucket.i18n.I18nService; |
|
|
|
import com.atlassian.bitbucket.i18n.I18nService; |
|
|
|
import com.atlassian.bitbucket.repository.*; |
|
|
|
import com.atlassian.bitbucket.repository.Repository; |
|
|
|
|
|
|
|
import com.atlassian.bitbucket.repository.RepositoryService; |
|
|
|
import com.atlassian.bitbucket.scm.CommandErrorHandler; |
|
|
|
import com.atlassian.bitbucket.scm.CommandErrorHandler; |
|
|
|
import com.atlassian.bitbucket.scm.CommandExitHandler; |
|
|
|
import com.atlassian.bitbucket.scm.CommandExitHandler; |
|
|
|
import com.atlassian.bitbucket.scm.CommandOutputHandler; |
|
|
|
import com.atlassian.bitbucket.scm.CommandOutputHandler; |
|
|
@ -14,15 +15,18 @@ import com.atlassian.bitbucket.setting.SettingsValidationErrors; |
|
|
|
import com.atlassian.sal.api.pluginsettings.PluginSettings; |
|
|
|
import com.atlassian.sal.api.pluginsettings.PluginSettings; |
|
|
|
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; |
|
|
|
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Before; |
|
|
|
|
|
|
|
import org.junit.Rule; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
|
|
|
|
import org.mockito.ArgumentCaptor; |
|
|
|
import org.mockito.ArgumentCaptor; |
|
|
|
|
|
|
|
import org.mockito.ArgumentMatchers; |
|
|
|
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.junit.MockitoJUnit; |
|
|
|
|
|
|
|
import org.mockito.junit.MockitoRule; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.*; |
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
@ -32,12 +36,14 @@ import static org.mockito.Mockito.*; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Unit tests for {@link MirrorRepositoryHook} |
|
|
|
* Unit tests for {@link MirrorRepositoryHook} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@RunWith(MockitoJUnitRunner.class) |
|
|
|
|
|
|
|
public class MirrorRepositoryHookTest { |
|
|
|
public class MirrorRepositoryHookTest { |
|
|
|
|
|
|
|
|
|
|
|
private MirrorRepositoryHook hook; |
|
|
|
private MirrorRepositoryHook hook; |
|
|
|
private GitScmCommandBuilder builder; |
|
|
|
private GitScmCommandBuilder builder; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Rule |
|
|
|
|
|
|
|
public MockitoRule mockitoRule = MockitoJUnit.rule(); |
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
@Mock |
|
|
|
private ScmService scmService; |
|
|
|
private ScmService scmService; |
|
|
|
@Mock |
|
|
|
@Mock |
|
|
@ -60,7 +66,7 @@ public class MirrorRepositoryHookTest { |
|
|
|
private final String username = "test-user"; |
|
|
|
private final String username = "test-user"; |
|
|
|
private final String password = "test-password"; |
|
|
|
private final String password = "test-password"; |
|
|
|
private final String repository = "https://test-user:test-password@bitbucket-mirror.englishtown.com/scm/test/test.git"; |
|
|
|
private final String repository = "https://test-user:test-password@bitbucket-mirror.englishtown.com/scm/test/test.git"; |
|
|
|
private final String branchesIncludePattern ="master"; |
|
|
|
private final String refspec = "+refs/heads/master:refs/heads/master +refs/heads/develop:refs/heads/develop"; |
|
|
|
|
|
|
|
|
|
|
|
@Captor |
|
|
|
@Captor |
|
|
|
ArgumentCaptor<Runnable> argumentCaptor; |
|
|
|
ArgumentCaptor<Runnable> argumentCaptor; |
|
|
@ -94,44 +100,6 @@ public class MirrorRepositoryHookTest { |
|
|
|
verifyExecutor(); |
|
|
|
verifyExecutor(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testPostReceiveWithBranchesPatternUpdateOperation() throws Exception { |
|
|
|
|
|
|
|
when(passwordEncryptor.decrypt(anyString())).thenReturn(password); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Repository repo = mock(Repository.class); |
|
|
|
|
|
|
|
RefChange refChange = mock(RefChange.class); |
|
|
|
|
|
|
|
MinimalRef ref = mock(MinimalRef.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<RefChange> refChanges = new ArrayList<>(); |
|
|
|
|
|
|
|
refChanges.add(refChange); |
|
|
|
|
|
|
|
when(refChange.getRef()).thenReturn(ref); |
|
|
|
|
|
|
|
when(refChange.getType()).thenReturn(RefChangeType.UPDATE); |
|
|
|
|
|
|
|
when(ref.getType()).thenReturn(StandardRefType.BRANCH); |
|
|
|
|
|
|
|
when(ref.getDisplayId()).thenReturn("master"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hook.postReceive(buildContext(repo), refChanges); |
|
|
|
|
|
|
|
verifyExecutorWithBranchesPatternUpdateOperation(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testPostReceiveWithBranchesPatternDeleteOperation() throws Exception { |
|
|
|
|
|
|
|
when(passwordEncryptor.decrypt(anyString())).thenReturn(password); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Repository repo = mock(Repository.class); |
|
|
|
|
|
|
|
RefChange refChange = mock(RefChange.class); |
|
|
|
|
|
|
|
MinimalRef ref = mock(MinimalRef.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<RefChange> refChanges = new ArrayList<>(); |
|
|
|
|
|
|
|
refChanges.add(refChange); |
|
|
|
|
|
|
|
when(refChange.getRef()).thenReturn(ref); |
|
|
|
|
|
|
|
when(refChange.getType()).thenReturn(RefChangeType.DELETE); |
|
|
|
|
|
|
|
when(ref.getType()).thenReturn(StandardRefType.BRANCH); |
|
|
|
|
|
|
|
when(ref.getDisplayId()).thenReturn("master"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hook.postReceive(buildContext(repo), refChanges); |
|
|
|
|
|
|
|
verifyExecutorWithBranchesPatternDeleteOperation(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testEmptyRepositoriesNotMirrored() { |
|
|
|
public void testEmptyRepositoriesNotMirrored() { |
|
|
|
Repository repo = mock(Repository.class); |
|
|
|
Repository repo = mock(Repository.class); |
|
|
@ -139,7 +107,7 @@ public class MirrorRepositoryHookTest { |
|
|
|
|
|
|
|
|
|
|
|
hook.postReceive(buildContext(repo), new ArrayList<>()); |
|
|
|
hook.postReceive(buildContext(repo), new ArrayList<>()); |
|
|
|
|
|
|
|
|
|
|
|
verify(executor, never()).submit(Matchers.<Runnable>any()); |
|
|
|
verify(executor, never()).submit(ArgumentMatchers.<Runnable>any()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
@ -152,7 +120,7 @@ public class MirrorRepositoryHookTest { |
|
|
|
ms.mirrorRepoUrl = mirrorRepoUrlHttp; |
|
|
|
ms.mirrorRepoUrl = mirrorRepoUrlHttp; |
|
|
|
ms.username = username; |
|
|
|
ms.username = username; |
|
|
|
ms.password = password; |
|
|
|
ms.password = password; |
|
|
|
hook.runMirrorCommand(ms, mock(Repository.class), Collections.emptyList()); |
|
|
|
hook.runMirrorCommand(ms, mock(Repository.class)); |
|
|
|
|
|
|
|
|
|
|
|
verify(executor).submit(argumentCaptor.capture()); |
|
|
|
verify(executor).submit(argumentCaptor.capture()); |
|
|
|
Runnable runnable = argumentCaptor.getValue(); |
|
|
|
Runnable runnable = argumentCaptor.getValue(); |
|
|
@ -192,40 +160,11 @@ public class MirrorRepositoryHookTest { |
|
|
|
verify(builder, times(1)).argument(eq("--prune")); |
|
|
|
verify(builder, times(1)).argument(eq("--prune")); |
|
|
|
verify(builder, times(1)).argument(eq("--atomic")); |
|
|
|
verify(builder, times(1)).argument(eq("--atomic")); |
|
|
|
verify(builder, times(1)).argument(eq(repository)); |
|
|
|
verify(builder, times(1)).argument(eq(repository)); |
|
|
|
verify(builder, times(1)).argument(eq("+refs/heads/*:refs/heads/*")); |
|
|
|
verify(builder, times(1)).argument(eq("--force")); |
|
|
|
verify(builder, times(1)).argument(eq("+refs/tags/*:refs/tags/*")); |
|
|
|
verify(builder, times(1)).argument(eq("+refs/tags/*:refs/tags/*")); |
|
|
|
verify(cmd, times(1)).call(); |
|
|
|
verify(builder, times(1)).argument(eq("+refs/notes/*:refs/notes/*")); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void verifyExecutorWithBranchesPatternUpdateOperation() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verify(executor).submit(argumentCaptor.capture()); |
|
|
|
|
|
|
|
Runnable runnable = argumentCaptor.getValue(); |
|
|
|
|
|
|
|
runnable.run(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verify(builder, times(1)).command(eq("push")); |
|
|
|
|
|
|
|
verify(builder, times(1)).argument(eq("--prune")); |
|
|
|
|
|
|
|
verify(builder, times(1)).argument(eq("--atomic")); |
|
|
|
|
|
|
|
verify(builder, times(1)).argument(eq(repository)); |
|
|
|
|
|
|
|
verify(builder, times(1)).argument(eq("+refs/heads/master:refs/heads/master")); |
|
|
|
verify(builder, times(1)).argument(eq("+refs/heads/master:refs/heads/master")); |
|
|
|
verify(builder, times(1)).argument(eq("+refs/tags/*:refs/tags/*")); |
|
|
|
verify(builder, times(1)).argument(eq("+refs/heads/develop:refs/heads/develop")); |
|
|
|
verify(cmd, times(1)).call(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void verifyExecutorWithBranchesPatternDeleteOperation() throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verify(executor).submit(argumentCaptor.capture()); |
|
|
|
|
|
|
|
Runnable runnable = argumentCaptor.getValue(); |
|
|
|
|
|
|
|
runnable.run(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verify(builder, times(1)).command(eq("push")); |
|
|
|
|
|
|
|
verify(builder, times(1)).argument(eq("--prune")); |
|
|
|
|
|
|
|
verify(builder, times(1)).argument(eq("--atomic")); |
|
|
|
|
|
|
|
verify(builder, times(1)).argument(eq(repository)); |
|
|
|
|
|
|
|
verify(builder, times(1)).argument(eq("+:refs/heads/master")); |
|
|
|
|
|
|
|
verify(builder, times(1)).argument(eq("+refs/tags/*:refs/tags/*")); |
|
|
|
|
|
|
|
verify(cmd, times(1)).call(); |
|
|
|
verify(cmd, times(1)).call(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
@ -270,9 +209,9 @@ public class MirrorRepositoryHookTest { |
|
|
|
.thenReturn("") |
|
|
|
.thenReturn("") |
|
|
|
.thenReturn(password); |
|
|
|
.thenReturn(password); |
|
|
|
|
|
|
|
|
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_BRANCHES_INCLUDE_PATTERN + "0"), eq(""))) |
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_REFSPEC + "0"), eq(""))) |
|
|
|
.thenReturn("??") |
|
|
|
.thenReturn("??") |
|
|
|
.thenReturn("master") |
|
|
|
.thenReturn("+refs/heads/master:refs/heads/master") |
|
|
|
.thenReturn(""); |
|
|
|
.thenReturn(""); |
|
|
|
|
|
|
|
|
|
|
|
Repository repo = mock(Repository.class); |
|
|
|
Repository repo = mock(Repository.class); |
|
|
@ -289,7 +228,7 @@ public class MirrorRepositoryHookTest { |
|
|
|
verify(errors).addFieldError(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL + "0"), anyString()); |
|
|
|
verify(errors).addFieldError(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL + "0"), anyString()); |
|
|
|
verify(errors, never()).addFieldError(eq(MirrorRepositoryHook.SETTING_USERNAME + "0"), anyString()); |
|
|
|
verify(errors, never()).addFieldError(eq(MirrorRepositoryHook.SETTING_USERNAME + "0"), anyString()); |
|
|
|
verify(errors, never()).addFieldError(eq(MirrorRepositoryHook.SETTING_PASSWORD + "0"), anyString()); |
|
|
|
verify(errors, never()).addFieldError(eq(MirrorRepositoryHook.SETTING_PASSWORD + "0"), anyString()); |
|
|
|
verify(errors).addFieldError(eq(MirrorRepositoryHook.SETTING_BRANCHES_INCLUDE_PATTERN + "0"), anyString()); |
|
|
|
verify(errors).addFieldError(eq(MirrorRepositoryHook.SETTING_REFSPEC + "0"), anyString()); |
|
|
|
|
|
|
|
|
|
|
|
errors = mock(SettingsValidationErrors.class); |
|
|
|
errors = mock(SettingsValidationErrors.class); |
|
|
|
hook.validate(settings, errors, repo); |
|
|
|
hook.validate(settings, errors, repo); |
|
|
@ -347,7 +286,7 @@ public class MirrorRepositoryHookTest { |
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_MIRROR_REPO_URL), eq(""))).thenReturn(mirrorRepoUrlHttp); |
|
|
|
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_USERNAME), eq(""))).thenReturn(username); |
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_PASSWORD), eq(""))).thenReturn(password); |
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_PASSWORD), eq(""))).thenReturn(password); |
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_BRANCHES_INCLUDE_PATTERN), eq(""))).thenReturn(branchesIncludePattern); |
|
|
|
when(settings.getString(eq(MirrorRepositoryHook.SETTING_REFSPEC), eq(""))).thenReturn(refspec); |
|
|
|
return settings; |
|
|
|
return settings; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|