|
| 1 | +package io.jenkins.plugins.gitlabbranchsource; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.ArgumentMatchers.anyString; |
| 6 | + |
| 7 | +import hudson.model.TaskListener; |
| 8 | +import hudson.security.AccessControlled; |
| 9 | +import hudson.util.StreamTaskListener; |
| 10 | +import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper; |
| 11 | +import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer; |
| 12 | +import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers; |
| 13 | +import java.io.ByteArrayOutputStream; |
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Set; |
| 18 | +import jenkins.branch.BranchSource; |
| 19 | +import jenkins.scm.api.SCMHead; |
| 20 | +import org.gitlab4j.api.GitLabApi; |
| 21 | +import org.gitlab4j.api.GitLabApiException; |
| 22 | +import org.gitlab4j.api.MergeRequestApi; |
| 23 | +import org.gitlab4j.api.ProjectApi; |
| 24 | +import org.gitlab4j.api.RepositoryApi; |
| 25 | +import org.gitlab4j.api.models.Project; |
| 26 | +import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject; |
| 27 | +import org.junit.ClassRule; |
| 28 | +import org.junit.Test; |
| 29 | +import org.jvnet.hudson.test.JenkinsRule; |
| 30 | +import org.mockito.MockedStatic; |
| 31 | +import org.mockito.Mockito; |
| 32 | + |
| 33 | +public class GitLabSCMSourceTest { |
| 34 | + |
| 35 | + private static final String SERVER = "server"; |
| 36 | + private static final String PROJECT_NAME = "project"; |
| 37 | + private static final String SOURCE_ID = "id"; |
| 38 | + |
| 39 | + @ClassRule |
| 40 | + public static JenkinsRule j = new JenkinsRule(); |
| 41 | + |
| 42 | + @Test |
| 43 | + public void retrieveMRWithEmptyProjectSettings() throws GitLabApiException, IOException, InterruptedException { |
| 44 | + GitLabApi gitLabApi = Mockito.mock(GitLabApi.class); |
| 45 | + ProjectApi projectApi = Mockito.mock(ProjectApi.class); |
| 46 | + RepositoryApi repoApi = Mockito.mock(RepositoryApi.class); |
| 47 | + MergeRequestApi mrApi = Mockito.mock(MergeRequestApi.class); |
| 48 | + Mockito.when(gitLabApi.getProjectApi()).thenReturn(projectApi); |
| 49 | + Mockito.when(gitLabApi.getMergeRequestApi()).thenReturn(mrApi); |
| 50 | + Mockito.when(gitLabApi.getRepositoryApi()).thenReturn(repoApi); |
| 51 | + Mockito.when(projectApi.getProject(any())).thenReturn(new Project()); |
| 52 | + try (MockedStatic<GitLabHelper> utilities = Mockito.mockStatic(GitLabHelper.class)) { |
| 53 | + utilities |
| 54 | + .when(() -> GitLabHelper.apiBuilder(any(AccessControlled.class), anyString())) |
| 55 | + .thenReturn(gitLabApi); |
| 56 | + GitLabServers.get().addServer(new GitLabServer("", SERVER, "")); |
| 57 | + GitLabSCMSourceBuilder sb = |
| 58 | + new GitLabSCMSourceBuilder(SOURCE_ID, SERVER, "creds", "po", "group/project", "project"); |
| 59 | + WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME); |
| 60 | + BranchSource source = new BranchSource(sb.build()); |
| 61 | + source.getSource() |
| 62 | + .setTraits(Arrays.asList(new BranchDiscoveryTrait(0), new OriginMergeRequestDiscoveryTrait(1))); |
| 63 | + project.getSourcesList().add(source); |
| 64 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 65 | + final TaskListener listener = new StreamTaskListener(out, StandardCharsets.UTF_8); |
| 66 | + Set<SCMHead> scmHead = source.getSource().fetch(listener); |
| 67 | + assertEquals(0, scmHead.size()); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments