-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix JENKINS-71955 NullPointerException because getMergeRequestsEnabled is null #456
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.jenkins.plugins.gitlabbranchsource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
|
||
import hudson.model.TaskListener; | ||
import hudson.security.AccessControlled; | ||
import hudson.util.StreamTaskListener; | ||
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper; | ||
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer; | ||
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers; | ||
import java.io.ByteArrayOutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.Set; | ||
import jenkins.branch.BranchSource; | ||
import jenkins.scm.api.SCMHead; | ||
import org.gitlab4j.api.GitLabApi; | ||
import org.gitlab4j.api.MergeRequestApi; | ||
import org.gitlab4j.api.ProjectApi; | ||
import org.gitlab4j.api.RepositoryApi; | ||
import org.gitlab4j.api.models.Project; | ||
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.RestartableJenkinsRule; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
|
||
public class GitLabSCMSourceTest { | ||
|
||
private static final String SERVER = "server"; | ||
private static final String PROJECT_NAME = "project"; | ||
private static final String SOURCE_ID = "id"; | ||
|
||
@Rule | ||
public final RestartableJenkinsRule plan = new RestartableJenkinsRule(); | ||
|
||
@Test | ||
public void retrieveMRWithEmptyProjectSettings() { | ||
plan.then(j -> { | ||
GitLabApi gitLabApi = Mockito.mock(GitLabApi.class); | ||
ProjectApi projectApi = Mockito.mock(ProjectApi.class); | ||
RepositoryApi repoApi = Mockito.mock(RepositoryApi.class); | ||
MergeRequestApi mrApi = Mockito.mock(MergeRequestApi.class); | ||
Mockito.when(gitLabApi.getProjectApi()).thenReturn(projectApi); | ||
Mockito.when(gitLabApi.getMergeRequestApi()).thenReturn(mrApi); | ||
Mockito.when(gitLabApi.getRepositoryApi()).thenReturn(repoApi); | ||
Mockito.when(projectApi.getProject(any())).thenReturn(new Project()); | ||
try (MockedStatic<GitLabHelper> utilities = Mockito.mockStatic(GitLabHelper.class)) { | ||
utilities | ||
.when(() -> GitLabHelper.apiBuilder(any(AccessControlled.class), anyString())) | ||
.thenReturn(gitLabApi); | ||
GitLabServers.get().addServer(new GitLabServer("", SERVER, "")); | ||
GitLabSCMSourceBuilder sb = | ||
new GitLabSCMSourceBuilder(SOURCE_ID, SERVER, "creds", "po", "group/project", "project"); | ||
WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME); | ||
BranchSource source = new BranchSource(sb.build()); | ||
source.getSource() | ||
.setTraits(Arrays.asList(new BranchDiscoveryTrait(0), new OriginMergeRequestDiscoveryTrait(1))); | ||
project.getSourcesList().add(source); | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
final TaskListener listener = new StreamTaskListener(out, StandardCharsets.UTF_8); | ||
Set<SCMHead> scmHead = source.getSource().fetch(listener); | ||
assertEquals(0, scmHead.size()); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason to use
RestartableJenkinsRule
here over plainJenkinsRule
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No particular reason. I changed the test to use JenkinsRule.