-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Migrate annotations and imports * Migrate assertions * Migrate GHMock Rule to Extension * Migrate to DataProviderRunner to ParameterizedTest * Remove public visibility for test classes and methods * Minor clean up
- Loading branch information
1 parent
d2e9e60
commit b447f83
Showing
53 changed files
with
1,037 additions
and
1,080 deletions.
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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package com.cloudbees.jenkins; | ||
|
||
import com.github.tomakehurst.wiremock.common.Slf4jNotifier; | ||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import com.github.tomakehurst.wiremock.junit5.WireMockExtension; | ||
import hudson.Launcher; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.Build; | ||
|
@@ -13,24 +13,22 @@ | |
import hudson.plugins.git.Revision; | ||
import hudson.plugins.git.util.BuildData; | ||
import hudson.util.VersionNumber; | ||
import jakarta.inject.Inject; | ||
import org.eclipse.jgit.lib.ObjectId; | ||
import org.jenkinsci.plugins.github.config.GitHubPluginConfig; | ||
import org.jenkinsci.plugins.github.test.GHMockRule; | ||
import org.jenkinsci.plugins.github.test.GHMockRule.FixedGHRepoNameTestContributor; | ||
import org.jenkinsci.plugins.github.test.InjectJenkinsMembersRule; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.RuleChain; | ||
import org.junit.runner.RunWith; | ||
import org.jenkinsci.plugins.github.test.GitHubMockExtension; | ||
import org.jenkinsci.plugins.github.test.GitHubMockExtension.FixedGHRepoNameTestContributor; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.TestBuilder; | ||
import org.jvnet.hudson.test.TestExtension; | ||
import org.jvnet.hudson.test.junit.jupiter.WithJenkins; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import jakarta.inject.Inject; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import static com.cloudbees.jenkins.GitHubSetCommitStatusBuilderTest.SOME_SHA; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; | ||
|
@@ -45,43 +43,42 @@ | |
* | ||
* @author <a href="mailto:[email protected]">Oleg Nenashev</a> | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
@WithJenkins | ||
@ExtendWith(MockitoExtension.class) | ||
public class GitHubCommitNotifierTest { | ||
|
||
@Mock | ||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
public BuildData data; | ||
|
||
@Mock | ||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
public Revision rev; | ||
|
||
@Inject | ||
public GitHubPluginConfig config; | ||
|
||
public JenkinsRule jRule = new JenkinsRule(); | ||
private JenkinsRule jRule; | ||
|
||
@Rule | ||
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this)); | ||
|
||
@Rule | ||
public GHMockRule github = new GHMockRule( | ||
new WireMockRule( | ||
wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true)) | ||
)) | ||
@RegisterExtension | ||
static GitHubMockExtension github = new GitHubMockExtension(WireMockExtension.newInstance() | ||
.options(wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true)))) | ||
.stubUser() | ||
.stubRepo() | ||
.stubStatuses(); | ||
|
||
|
||
@Before | ||
public void before() throws Throwable { | ||
when(data.getLastBuiltRevision()).thenReturn(rev); | ||
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS); | ||
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA)); | ||
@BeforeEach | ||
void before(JenkinsRule rule) throws Throwable { | ||
jRule = rule; | ||
jRule.getInstance().getInjector().injectMembers(this); | ||
|
||
when(data.getLastBuiltRevision()).thenReturn(rev); | ||
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS); | ||
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA)); | ||
} | ||
|
||
@Test | ||
@Issue("JENKINS-23641") | ||
public void testNoBuildData() throws Exception { | ||
void testNoBuildData() throws Exception { | ||
FreeStyleProject prj = jRule.createFreeStyleProject("23641_noBuildData"); | ||
prj.getPublishersList().add(new GitHubCommitNotifier()); | ||
Build b = prj.scheduleBuild2(0).get(); | ||
|
@@ -91,7 +88,7 @@ public void testNoBuildData() throws Exception { | |
|
||
@Test | ||
@Issue("JENKINS-23641") | ||
public void testNoBuildRevision() throws Exception { | ||
void testNoBuildRevision() throws Exception { | ||
FreeStyleProject prj = jRule.createFreeStyleProject(); | ||
prj.setScm(new GitSCM("http://non.existent.git.repo.nowhere/repo.git")); | ||
prj.getPublishersList().add(new GitHubCommitNotifier()); | ||
|
@@ -103,7 +100,7 @@ public void testNoBuildRevision() throws Exception { | |
|
||
@Test | ||
@Issue("JENKINS-25312") | ||
public void testMarkUnstableOnCommitNotifierFailure() throws Exception { | ||
void testMarkUnstableOnCommitNotifierFailure() throws Exception { | ||
FreeStyleProject prj = jRule.createFreeStyleProject(); | ||
prj.getPublishersList().add(new GitHubCommitNotifier(Result.UNSTABLE.toString())); | ||
Build b = prj.scheduleBuild2(0).get(); | ||
|
@@ -112,15 +109,15 @@ public void testMarkUnstableOnCommitNotifierFailure() throws Exception { | |
|
||
@Test | ||
@Issue("JENKINS-25312") | ||
public void testMarkSuccessOnCommitNotifierFailure() throws Exception { | ||
void testMarkSuccessOnCommitNotifierFailure() throws Exception { | ||
FreeStyleProject prj = jRule.createFreeStyleProject(); | ||
prj.getPublishersList().add(new GitHubCommitNotifier(Result.SUCCESS.toString())); | ||
Build b = prj.scheduleBuild2(0).get(); | ||
jRule.assertBuildStatus(Result.SUCCESS, b); | ||
} | ||
|
||
@Test | ||
public void shouldWriteStatusOnGH() throws Exception { | ||
void shouldWriteStatusOnGH() throws Exception { | ||
config.getConfigs().add(github.serverConfig()); | ||
FreeStyleProject prj = jRule.createFreeStyleProject(); | ||
|
||
|
@@ -136,7 +133,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen | |
|
||
prj.scheduleBuild2(0).get(); | ||
|
||
github.service().verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA))); | ||
github.verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA))); | ||
} | ||
|
||
private Build safelyGenerateBuild(FreeStyleProject prj) throws InterruptedException, java.util.concurrent.ExecutionException { | ||
|
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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package com.cloudbees.jenkins; | ||
|
||
import com.github.tomakehurst.wiremock.common.Slf4jNotifier; | ||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import com.github.tomakehurst.wiremock.junit5.WireMockExtension; | ||
import hudson.Launcher; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.Build; | ||
|
@@ -11,26 +11,25 @@ | |
import hudson.plugins.git.Revision; | ||
import hudson.plugins.git.util.BuildData; | ||
import hudson.tasks.Builder; | ||
import jakarta.inject.Inject; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.eclipse.jgit.lib.ObjectId; | ||
import org.jenkinsci.plugins.github.config.GitHubPluginConfig; | ||
import org.jenkinsci.plugins.github.test.GHMockRule; | ||
import org.jenkinsci.plugins.github.test.GHMockRule.FixedGHRepoNameTestContributor; | ||
import org.jenkinsci.plugins.github.test.InjectJenkinsMembersRule; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.RuleChain; | ||
import org.junit.runner.RunWith; | ||
import org.jenkinsci.plugins.github.test.GitHubMockExtension; | ||
import org.jenkinsci.plugins.github.test.GitHubMockExtension.FixedGHRepoNameTestContributor; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.TestBuilder; | ||
import org.jvnet.hudson.test.TestExtension; | ||
import org.jvnet.hudson.test.junit.jupiter.WithJenkins; | ||
import org.jvnet.hudson.test.recipes.LocalData; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import jakarta.inject.Inject; | ||
import java.util.List; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; | ||
|
@@ -44,44 +43,43 @@ | |
* | ||
* @author <a href="mailto:[email protected]">Oleg Nenashev</a> | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
@WithJenkins | ||
@ExtendWith(MockitoExtension.class) | ||
public class GitHubSetCommitStatusBuilderTest { | ||
|
||
public static final String SOME_SHA = StringUtils.repeat("f", 40); | ||
|
||
@Mock | ||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
public BuildData data; | ||
|
||
@Mock | ||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
public Revision rev; | ||
|
||
@Inject | ||
public GitHubPluginConfig config; | ||
|
||
public JenkinsRule jRule = new JenkinsRule(); | ||
private JenkinsRule jRule; | ||
|
||
@Rule | ||
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this)); | ||
|
||
@Rule | ||
public GHMockRule github = new GHMockRule( | ||
new WireMockRule( | ||
wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true)) | ||
)) | ||
@RegisterExtension | ||
static GitHubMockExtension github = new GitHubMockExtension(WireMockExtension.newInstance() | ||
.options(wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true)))) | ||
.stubUser() | ||
.stubRepo() | ||
.stubStatuses(); | ||
|
||
@Before | ||
public void before() throws Throwable { | ||
when(data.getLastBuiltRevision()).thenReturn(rev); | ||
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS); | ||
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA)); | ||
@BeforeEach | ||
void before(JenkinsRule rule) throws Throwable { | ||
jRule = rule; | ||
jRule.getInstance().getInjector().injectMembers(this); | ||
|
||
when(data.getLastBuiltRevision()).thenReturn(rev); | ||
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS); | ||
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA)); | ||
} | ||
|
||
@Test | ||
@Issue("JENKINS-23641") | ||
public void shouldIgnoreIfNoBuildData() throws Exception { | ||
void shouldIgnoreIfNoBuildData() throws Exception { | ||
FreeStyleProject prj = jRule.createFreeStyleProject("23641_noBuildData"); | ||
prj.getBuildersList().add(new GitHubSetCommitStatusBuilder()); | ||
Build b = prj.scheduleBuild2(0).get(); | ||
|
@@ -91,7 +89,7 @@ public void shouldIgnoreIfNoBuildData() throws Exception { | |
@Test | ||
@LocalData | ||
@Issue("JENKINS-32132") | ||
public void shouldLoadNullStatusMessage() throws Exception { | ||
void shouldLoadNullStatusMessage() throws Exception { | ||
config.getConfigs().add(github.serverConfig()); | ||
FreeStyleProject prj = jRule.getInstance().getItemByFullName("step", FreeStyleProject.class); | ||
|
||
|
@@ -107,7 +105,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen | |
prj.getBuildersList().replaceBy(builders); | ||
prj.scheduleBuild2(0).get(); | ||
|
||
github.service().verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA))); | ||
github.verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA))); | ||
} | ||
|
||
@TestExtension | ||
|
Oops, something went wrong.