/src/test/java/com/atlassian/bitbucket/internal/unapprove/GitRescopeAnalyzerMockTest.java

https://bitbucket.org/atlassian/stash-auto-unapprove-plugin · Java · 161 lines · 133 code · 26 blank · 2 comment · 0 complexity · 7f88481de355998d4d3c7a4a464e6faf MD5 · raw file

  1. package com.atlassian.bitbucket.internal.unapprove;
  2. import com.atlassian.bitbucket.ResourceBusyException;
  3. import com.atlassian.bitbucket.i18n.KeyedMessage;
  4. import com.atlassian.bitbucket.pull.PullRequest;
  5. import com.atlassian.bitbucket.repository.Repository;
  6. import com.atlassian.bitbucket.scm.CommandCanceledException;
  7. import com.atlassian.bitbucket.scm.git.GitScm;
  8. import com.atlassian.bitbucket.scm.git.command.GitCommand;
  9. import com.atlassian.bitbucket.scm.git.command.GitCommandBuilderFactory;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import org.junit.runner.RunWith;
  13. import org.mockito.Answers;
  14. import org.mockito.ArgumentMatcher;
  15. import org.mockito.Mock;
  16. import org.mockito.junit.MockitoJUnitRunner;
  17. import java.io.File;
  18. import java.time.Duration;
  19. import java.util.List;
  20. import static com.atlassian.bitbucket.internal.unapprove.GitRescopeAnalyzer.RESOURCE;
  21. import static com.atlassian.bitbucket.internal.unapprove.PullRequestTestHelper.mockPullRequest;
  22. import static org.junit.Assert.assertFalse;
  23. import static org.junit.Assert.assertTrue;
  24. import static org.mockito.Mockito.*;
  25. @RunWith(MockitoJUnitRunner.Silent.class)
  26. public class GitRescopeAnalyzerMockTest {
  27. private static final Duration PATCH_ID_TIMEOUT = Duration.ofSeconds(10);
  28. private GitRescopeAnalyzer analyzer;
  29. @Mock(answer = Answers.RETURNS_DEEP_STUBS)
  30. private GitCommandBuilderFactory builderFactory;
  31. @Mock
  32. private Repository repository;
  33. @Mock
  34. private AutoUnapproveConfig unapproveConfig;
  35. @Before
  36. public void setup() {
  37. when(repository.getScmId()).thenReturn(GitScm.ID);
  38. when(unapproveConfig.isPatchIdEnabled()).thenReturn(true);
  39. when(unapproveConfig.getPatchIdTimeout()).thenReturn(PATCH_ID_TIMEOUT);
  40. analyzer = new GitRescopeAnalyzer(builderFactory, unapproveConfig);
  41. }
  42. @Test
  43. public void testIsUpdatedCommandCancelled() {
  44. when(builderFactory.builder(repository).command("diff"))
  45. .thenThrow(new CommandCanceledException(new KeyedMessage("", "", "")));
  46. boolean updated = analyzer.isUpdated(mockPullRequest(repository, "currentFrom", "currentTo"), "previousFrom");
  47. assertTrue(updated);
  48. verify(builderFactory.builder(repository)).command("diff");
  49. verify(builderFactory.builder(repository), never()).command("patch-id");
  50. }
  51. @Test
  52. public void testIsUpdatedForHgRepository() {
  53. when(repository.getScmId()).thenReturn("hg");
  54. assertTrue(analyzer.isUpdated(mockPullRequest(repository, "currentFrom", "currentTo"), "previousFrom"));
  55. // Git processes should not be run for repositories from other SCMs
  56. verifyNoInteractions(builderFactory);
  57. }
  58. @Test
  59. public void testIsUpdatedResourceBusy() {
  60. when(builderFactory.builder(repository))
  61. .thenThrow(new ResourceBusyException(new KeyedMessage("", "", ""), RESOURCE));
  62. assertTrue(analyzer.isUpdated(mockPullRequest(repository, "currentFrom", "currentTo"), "previousFrom"));
  63. verify(unapproveConfig, never()).getTempDir();
  64. }
  65. @Test
  66. public void testIsUpdatedWithPatchId() {
  67. String toHash = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
  68. String fromHash = "cccccccccccccccccccccccccccccccccccccccc";
  69. String initialFromHash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  70. PullRequest pullRequest = mockPullRequest(repository, initialFromHash, toHash);
  71. GitCommand diffCommand = mock(GitCommand.class);
  72. when(builderFactory.builder(same(repository))
  73. .command(eq("diff"))
  74. .argument(eq("--binary"))
  75. .argument(eq("--full-index"))
  76. .argument(eq(toHash + "..." + fromHash))
  77. .argument(eq("--"))
  78. .build(any()))
  79. .thenReturn(diffCommand);
  80. File file = mock(File.class);
  81. when(diffCommand.call()).thenReturn(file);
  82. GitCommand<List<String>> patchIdsCommand = mock(GitCommand.class);
  83. when(builderFactory.builder(repository)
  84. .command(eq("patch-id"))
  85. .argument("--stable")
  86. .inputHandler(argThat((ArgumentMatcher<GitRescopeAnalyzer.FileInputHandler>) argument ->
  87. file.equals(argument.getFile())))
  88. .build(any(GitRescopeAnalyzer.PatchIdOutputHandler.class))).thenReturn(patchIdsCommand);
  89. assertTrue(analyzer.isUpdated(pullRequest, fromHash));
  90. verify(builderFactory.builder(repository)).command("diff");
  91. verify(diffCommand).setTimeout(PATCH_ID_TIMEOUT);
  92. verify(diffCommand).call();
  93. verify(builderFactory.builder(repository)).command("patch-id");
  94. verify(patchIdsCommand).setTimeout(PATCH_ID_TIMEOUT);
  95. verify(patchIdsCommand).call();
  96. }
  97. @Test
  98. public void testIsUpdatedWithPatchIdNoDiff() {
  99. String toHash = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
  100. String fromHash = "cccccccccccccccccccccccccccccccccccccccc";
  101. String initialFromHash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  102. PullRequest pullRequest = mockPullRequest(repository, initialFromHash, toHash);
  103. GitCommand diffCommand = mock(GitCommand.class);
  104. when(builderFactory.builder(same(repository))
  105. .command(eq("diff"))
  106. .argument(eq("--binary"))
  107. .argument(eq("--full-index"))
  108. .argument(eq(toHash + "..." + fromHash))
  109. .argument(eq("--"))
  110. .build(any()))
  111. .thenReturn(diffCommand);
  112. when(diffCommand.call()).thenReturn(null); // no diff
  113. assertTrue(analyzer.isUpdated(pullRequest, fromHash));
  114. verify(diffCommand).setTimeout(PATCH_ID_TIMEOUT);
  115. verify(diffCommand).call();
  116. verify(builderFactory.builder(repository)).command("diff");
  117. verify(builderFactory.builder(repository), never()).command("patch-id");
  118. }
  119. @Test
  120. public void testIsUpdatedWithUnchangedFromCommit() {
  121. PullRequest pullRequest = mockPullRequest(repository, "currentFrom", "currentTo");
  122. assertFalse(analyzer.isUpdated(pullRequest, "currentFrom"));
  123. // Git processes should not be run when the "fromRef" commit is not updated
  124. verifyNoInteractions(builderFactory);
  125. }
  126. @Test
  127. public void testIsUpdatedWithoutPatchId() {
  128. when(unapproveConfig.isPatchIdEnabled()).thenReturn(false);
  129. PullRequest pullRequest = mockPullRequest(repository, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  130. "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
  131. assertTrue(analyzer.isUpdated(pullRequest, "cccccccccccccccccccccccccccccccccccccccc"));
  132. verify(repository, never()).getScmId();
  133. verifyNoInteractions(builderFactory);
  134. }
  135. }