setups = new ArrayList<>();
+
+ public GitHubMockExtension(Builder builder) {
+ super(builder);
+ }
+
+ @Override
+ protected void onBeforeEach(WireMockRuntimeInfo wireMockRuntimeInfo) {
+ super.onBeforeAll(wireMockRuntimeInfo);
+
+ for (Runnable setup : setups) {
+ setup.run();
+ }
+ }
+
+ /**
+ * Ready-to-use global config with wiremock service. Just add it to plugin config
+ * {@code GitHubPlugin.configuration().getConfigs().add(github.serverConfig());}
+ *
+ * @return part of global plugin config
+ */
+ public GitHubServerConfig serverConfig() {
+ GitHubServerConfig conf = new GitHubServerConfig("creds");
+ conf.setApiUrl("http://localhost:" + getPort());
+ return conf;
+ }
+
+ /**
+ * Stubs /user response with predefined content
+ *
+ * More info: https://developer.github.com/v3/users/#get-the-authenticated-user
+ */
+ public GitHubMockExtension stubUser() {
+ setups.add(() ->
+ stubFor(get(urlPathEqualTo("/user"))
+ .willReturn(aResponse()
+ .withStatus(HTTP_OK)
+ .withHeader("Content-Type", "application/json; charset=utf-8")
+ .withBody(classpath(GitHubMockExtension.class, "user.json")))));
+ return this;
+ }
+
+ /**
+ * Stubs /repos/org/repo response with predefined content
+ *
+ * More info: https://developer.github.com/v3/repos/#get
+ */
+ public GitHubMockExtension stubRepo() {
+ setups.add(() ->
+ stubFor(get(urlPathMatching(format("/repos/%s/%s", REPO.getUserName(), REPO.getRepositoryName())))
+ .willReturn(aResponse()
+ .withStatus(HTTP_OK)
+ .withHeader("Content-Type", "application/json; charset=utf-8")
+ .withBody(classpath(GitHubMockExtension.class, "repos-repo.json")))));
+ return this;
+ }
+
+ /**
+ * Returns 201 CREATED on POST to statuses endpoint (but without content)
+ *
+ * More info: https://developer.github.com/v3/repos/statuses/
+ */
+ public GitHubMockExtension stubStatuses() {
+ setups.add(() ->
+ stubFor(post(urlPathMatching(format("/repos/%s/%s/statuses/.*", REPO.getUserName(), REPO.getRepositoryName())))
+ .willReturn(aResponse()
+ .withStatus(HTTP_CREATED))));
+ return this;
+ }
+
+ /**
+ * Adds predefined repo to list which job can return. This is useful to avoid SCM usage.
+ *
+ * {@code @TestExtension
+ * public static final FixedGHRepoNameTestContributor CONTRIBUTOR = new FixedGHRepoNameTestContributor();
+ * }
+ */
+ public static class FixedGHRepoNameTestContributor extends GitHubRepositoryNameContributor {
+ @Override
+ public void parseAssociatedNames(Item job, Collection result) {
+ result.add(GitHubMockExtension.REPO);
+ }
+ }
+
+}
diff --git a/src/test/java/org/jenkinsci/plugins/github/test/GitHubServerConfigMatcher.java b/src/test/java/org/jenkinsci/plugins/github/test/GitHubServerConfigMatcher.java
index 6763e8dd0..2a391af6e 100644
--- a/src/test/java/org/jenkinsci/plugins/github/test/GitHubServerConfigMatcher.java
+++ b/src/test/java/org/jenkinsci/plugins/github/test/GitHubServerConfigMatcher.java
@@ -5,10 +5,6 @@
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.Map;
import static org.hamcrest.Matchers.is;
import static org.jenkinsci.plugins.github.config.GitHubServerConfig.tokenFor;
@@ -17,7 +13,6 @@
* @author lanwen (Merkushev Kirill)
*/
public final class GitHubServerConfigMatcher {
- private static final Logger LOG = LoggerFactory.getLogger(GitHubServerConfigMatcher.class);
private GitHubServerConfigMatcher() {
}
diff --git a/src/test/java/org/jenkinsci/plugins/github/test/HookSecretHelper.java b/src/test/java/org/jenkinsci/plugins/github/test/HookSecretHelper.java
index 0d6d7e3db..b2d7d8960 100644
--- a/src/test/java/org/jenkinsci/plugins/github/test/HookSecretHelper.java
+++ b/src/test/java/org/jenkinsci/plugins/github/test/HookSecretHelper.java
@@ -27,7 +27,7 @@ private HookSecretHelper() {
/**
* Stores the secret and sets it as the current hook secret.
- *
+ *
* @param config where to save
* @param secretText The secret/key.
*/
@@ -56,13 +56,13 @@ public void run() {
config.setHookSecretConfigs(Collections.singletonList(new HookSecretConfig(credentials.getId())));
}
-
+
/**
* Stores the secret and sets it as the current hook secret.
* @param secretText The secret/key.
*/
public static void storeSecret(final String secretText) {
- storeSecretIn(Jenkins.getInstance().getDescriptorByType(GitHubPluginConfig.class), secretText);
+ storeSecretIn(Jenkins.get().getDescriptorByType(GitHubPluginConfig.class), secretText);
}
/**
@@ -78,6 +78,6 @@ public static void removeSecretIn(GitHubPluginConfig config) {
* Unsets the current hook secret.
*/
public static void removeSecret() {
- removeSecretIn(Jenkins.getInstance().getDescriptorByType(GitHubPluginConfig.class));
+ removeSecretIn(Jenkins.get().getDescriptorByType(GitHubPluginConfig.class));
}
}
diff --git a/src/test/java/org/jenkinsci/plugins/github/test/InjectJenkinsMembersRule.java b/src/test/java/org/jenkinsci/plugins/github/test/InjectJenkinsMembersRule.java
deleted file mode 100644
index ae0127783..000000000
--- a/src/test/java/org/jenkinsci/plugins/github/test/InjectJenkinsMembersRule.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package org.jenkinsci.plugins.github.test;
-
-import org.junit.rules.ExternalResource;
-import org.jvnet.hudson.test.JenkinsRule;
-
-/**
- * Helpful class to make possible usage of
- * {@code @Inject
- * public GitHubPluginConfig config;
- * }
- *
- * in test fields instead of static calls {@link org.jenkinsci.plugins.github.GitHubPlugin#configuration()}
- *
- * See {@link com.cloudbees.jenkins.GitHubSetCommitStatusBuilderTest} for example
- * Should be used after JenkinsRule initialized
- *
- * {@code public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this)); }
- *
- * @author lanwen (Merkushev Kirill)
- */
-public class InjectJenkinsMembersRule extends ExternalResource {
-
- private JenkinsRule jRule;
- private Object instance;
-
- /**
- * @param jRule Jenkins rule
- * @param instance test class instance
- */
- public InjectJenkinsMembersRule(JenkinsRule jRule, Object instance) {
- this.jRule = jRule;
- this.instance = instance;
- }
-
- @Override
- protected void before() throws Throwable {
- jRule.getInstance().getInjector().injectMembers(instance);
- }
-}
diff --git a/src/test/java/org/jenkinsci/plugins/github/util/BuildDataHelperTest.java b/src/test/java/org/jenkinsci/plugins/github/util/BuildDataHelperTest.java
index 0f58cc9e0..0cf91e16b 100644
--- a/src/test/java/org/jenkinsci/plugins/github/util/BuildDataHelperTest.java
+++ b/src/test/java/org/jenkinsci/plugins/github/util/BuildDataHelperTest.java
@@ -1,10 +1,8 @@
package org.jenkinsci.plugins.github.util;
import hudson.plugins.git.util.BuildData;
-
-import org.junit.Test;
-import org.junit.experimental.runners.Enclosed;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import java.util.ArrayList;
@@ -20,16 +18,16 @@
/**
* @author Manuel de la Peña
*/
-@RunWith(Enclosed.class)
-public class BuildDataHelperTest {
+class BuildDataHelperTest {
- public static class WhenBuildingRegularJobs {
+ @Nested
+ class WhenBuildingRegularJobs {
private static final String GITHUB_USERNAME = "user1";
@Test
@Issue("JENKINS-53149")
- public void shouldCalculateDataBuildFromProject() throws Exception {
+ void shouldCalculateDataBuildFromProject() throws Exception {
BuildData projectBuildData = new BuildData();
projectBuildData.remoteUrls = new HashSet<>();
@@ -48,7 +46,7 @@ public void shouldCalculateDataBuildFromProject() throws Exception {
@Test
@Issue("JENKINS-53149")
- public void shouldCalculateDataBuildFromProjectWithTwoBuildDatas() throws Exception {
+ void shouldCalculateDataBuildFromProjectWithTwoBuildDatas() throws Exception {
BuildData sharedLibBuildData = new BuildData();
sharedLibBuildData.remoteUrls = new HashSet<>();
@@ -74,7 +72,7 @@ public void shouldCalculateDataBuildFromProjectWithTwoBuildDatas() throws Except
@Test
@Issue("JENKINS-53149")
- public void shouldCalculateDataBuildFromProjectWithEmptyBuildDatas() throws Exception {
+ void shouldCalculateDataBuildFromProjectWithEmptyBuildDatas() throws Exception {
BuildData buildData = BuildDataHelper.calculateBuildData(
"master", "project/master", Collections.EMPTY_LIST);
@@ -83,7 +81,7 @@ public void shouldCalculateDataBuildFromProjectWithEmptyBuildDatas() throws Exce
@Test
@Issue("JENKINS-53149")
- public void shouldCalculateDataBuildFromProjectWithNullBuildDatas() throws Exception {
+ void shouldCalculateDataBuildFromProjectWithNullBuildDatas() throws Exception {
BuildData buildData = BuildDataHelper.calculateBuildData(
"master", "project/master", null);
@@ -92,13 +90,14 @@ public void shouldCalculateDataBuildFromProjectWithNullBuildDatas() throws Excep
}
- public static class WhenBuildingOrganizationJobs {
+ @Nested
+ class WhenBuildingOrganizationJobs {
private static final String ORGANIZATION_NAME = "Organization";
@Test
@Issue("JENKINS-53149")
- public void shouldCalculateDataBuildFromProject() throws Exception {
+ void shouldCalculateDataBuildFromProject() throws Exception {
BuildData projectBuildData = new BuildData();
projectBuildData.remoteUrls = new HashSet<>();
@@ -117,7 +116,7 @@ public void shouldCalculateDataBuildFromProject() throws Exception {
@Test
@Issue("JENKINS-53149")
- public void shouldCalculateDataBuildFromProjectWithTwoBuildDatas() throws Exception {
+ void shouldCalculateDataBuildFromProjectWithTwoBuildDatas() throws Exception {
BuildData sharedLibBuildData = new BuildData();
sharedLibBuildData.remoteUrls = new HashSet<>();
@@ -143,7 +142,7 @@ public void shouldCalculateDataBuildFromProjectWithTwoBuildDatas() throws Except
@Test
@Issue("JENKINS-53149")
- public void shouldCalculateDataBuildFromProjectWithEmptyBuildDatas() throws Exception {
+ void shouldCalculateDataBuildFromProjectWithEmptyBuildDatas() throws Exception {
BuildData buildData = BuildDataHelper.calculateBuildData(
"master", ORGANIZATION_NAME + "/project/master", Collections.EMPTY_LIST);
@@ -152,7 +151,7 @@ public void shouldCalculateDataBuildFromProjectWithEmptyBuildDatas() throws Exce
@Test
@Issue("JENKINS-53149")
- public void shouldCalculateDataBuildFromProjectWithNullBuildDatas() throws Exception {
+ void shouldCalculateDataBuildFromProjectWithNullBuildDatas() throws Exception {
BuildData buildData = BuildDataHelper.calculateBuildData(
"master", ORGANIZATION_NAME + "/project/master", null);
diff --git a/src/test/java/org/jenkinsci/plugins/github/util/JobInfoHelpersTest.java b/src/test/java/org/jenkinsci/plugins/github/util/JobInfoHelpersTest.java
index f7881acc7..93e8a2b65 100644
--- a/src/test/java/org/jenkinsci/plugins/github/util/JobInfoHelpersTest.java
+++ b/src/test/java/org/jenkinsci/plugins/github/util/JobInfoHelpersTest.java
@@ -4,9 +4,10 @@
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
-import org.junit.ClassRule;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
+import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
@@ -19,13 +20,18 @@
/**
* @author lanwen (Merkushev Kirill)
*/
-public class JobInfoHelpersTest {
+@WithJenkins
+class JobInfoHelpersTest {
- @ClassRule
- public static JenkinsRule jenkins = new JenkinsRule();
+ private JenkinsRule jenkins;
+
+ @BeforeEach
+ void setUp(JenkinsRule rule) throws Exception {
+ jenkins = rule;
+ }
@Test
- public void shouldMatchForProjectWithTrigger() throws Exception {
+ void shouldMatchForProjectWithTrigger() throws Exception {
FreeStyleProject prj = jenkins.createFreeStyleProject();
prj.addTrigger(new GitHubPushTrigger());
@@ -33,7 +39,7 @@ public void shouldMatchForProjectWithTrigger() throws Exception {
}
@Test
- public void shouldSeeProjectWithTriggerIsAliveForCleaner() throws Exception {
+ void shouldSeeProjectWithTriggerIsAliveForCleaner() throws Exception {
FreeStyleProject prj = jenkins.createFreeStyleProject();
prj.addTrigger(new GitHubPushTrigger());
@@ -41,31 +47,31 @@ public void shouldSeeProjectWithTriggerIsAliveForCleaner() throws Exception {
}
@Test
- public void shouldNotMatchProjectWithoutTrigger() throws Exception {
+ void shouldNotMatchProjectWithoutTrigger() throws Exception {
FreeStyleProject prj = jenkins.createFreeStyleProject();
assertThat("without trigger", withTrigger(GitHubPushTrigger.class).apply(prj), is(false));
}
@Test
- public void shouldNotMatchNullProject() throws Exception {
+ void shouldNotMatchNullProject() throws Exception {
assertThat("null project", withTrigger(GitHubPushTrigger.class).apply(null), is(false));
}
@Test
- public void shouldReturnNotBuildableOnNullProject() throws Exception {
+ void shouldReturnNotBuildableOnNullProject() throws Exception {
assertThat("null project", isBuildable().apply(null), is(false));
}
@Test
- public void shouldSeeProjectWithoutTriggerIsNotAliveForCleaner() throws Exception {
+ void shouldSeeProjectWithoutTriggerIsNotAliveForCleaner() throws Exception {
FreeStyleProject prj = jenkins.createFreeStyleProject();
assertThat("without trigger", isAlive().apply(prj), is(false));
}
@Test
- public void shouldGetTriggerFromAbstractProject() throws Exception {
+ void shouldGetTriggerFromAbstractProject() throws Exception {
GitHubPushTrigger trigger = new GitHubPushTrigger();
FreeStyleProject prj = jenkins.createFreeStyleProject();
@@ -75,7 +81,7 @@ public void shouldGetTriggerFromAbstractProject() throws Exception {
}
@Test
- public void shouldGetTriggerFromWorkflow() throws Exception {
+ void shouldGetTriggerFromWorkflow() throws Exception {
GitHubPushTrigger trigger = new GitHubPushTrigger();
WorkflowJob job = jenkins.getInstance().createProject(WorkflowJob.class, "Test Workflow");
job.addTrigger(trigger);
@@ -84,7 +90,7 @@ public void shouldGetTriggerFromWorkflow() throws Exception {
}
@Test
- public void shouldNotGetTriggerWhenNoOne() throws Exception {
+ void shouldNotGetTriggerWhenNoOne() throws Exception {
FreeStyleProject prj = jenkins.createFreeStyleProject();
assertThat("without trigger in project", triggerFrom((Item) prj, GitHubPushTrigger.class), nullValue());
diff --git a/src/test/java/org/jenkinsci/plugins/github/util/XSSApiTest.java b/src/test/java/org/jenkinsci/plugins/github/util/XSSApiTest.java
index 4ce33af75..e1bc391e7 100644
--- a/src/test/java/org/jenkinsci/plugins/github/util/XSSApiTest.java
+++ b/src/test/java/org/jenkinsci/plugins/github/util/XSSApiTest.java
@@ -1,10 +1,7 @@
package org.jenkinsci.plugins.github.util;
-import com.tngtech.java.junit.dataprovider.DataProvider;
-import com.tngtech.java.junit.dataprovider.DataProviderRunner;
-import com.tngtech.java.junit.dataprovider.UseDataProvider;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
import static java.lang.String.format;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -13,11 +10,9 @@
/**
* @author lanwen (Merkushev Kirill)
*/
-@RunWith(DataProviderRunner.class)
-public class XSSApiTest {
+class XSSApiTest {
- @DataProvider
- public static Object[][] links() {
+ static Object[][] links() {
return new Object[][]{
new Object[]{"javascript:alert(1);//", ""},
new Object[]{"javascript:alert(1)://", ""},
@@ -37,9 +32,9 @@ public static Object[][] links() {
};
}
- @Test
- @UseDataProvider("links")
- public void shouldSanitizeUrl(String url, String expected) throws Exception {
+ @ParameterizedTest
+ @MethodSource("links")
+ void shouldSanitizeUrl(String url, String expected) throws Exception {
assertThat(format("For %s", url), XSSApi.asValidHref(url), is(expected));
}
}
diff --git a/src/test/java/org/jenkinsci/plugins/github/webhook/GHEventHeaderTest.java b/src/test/java/org/jenkinsci/plugins/github/webhook/GHEventHeaderTest.java
index 6d29dbb3b..ee350a301 100644
--- a/src/test/java/org/jenkinsci/plugins/github/webhook/GHEventHeaderTest.java
+++ b/src/test/java/org/jenkinsci/plugins/github/webhook/GHEventHeaderTest.java
@@ -1,28 +1,29 @@
package org.jenkinsci.plugins.github.webhook;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.kohsuke.github.GHEvent;
import org.kohsuke.stapler.StaplerRequest2;
import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.nullValue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
/**
* @author lanwen (Merkushev Kirill)
*/
-@RunWith(MockitoJUnitRunner.class)
+@ExtendWith(MockitoExtension.class)
public class GHEventHeaderTest {
public static final String STRING_PUSH_HEADER = "push";
public static final String PARAM_NAME = "event";
public static final String UNKNOWN_EVENT = "unkn";
-
+
@Mock
private StaplerRequest2 req;
@@ -30,7 +31,7 @@ public class GHEventHeaderTest {
private GHEventHeader ann;
@Test
- public void shouldReturnParsedPushHeader() throws Exception {
+ void shouldReturnParsedPushHeader() throws Exception {
when(req.getHeader(GHEventHeader.PayloadHandler.EVENT_HEADER)).thenReturn(STRING_PUSH_HEADER);
Object event = new GHEventHeader.PayloadHandler().parse(req, ann, GHEvent.class, PARAM_NAME);
@@ -39,22 +40,23 @@ public void shouldReturnParsedPushHeader() throws Exception {
}
@Test
- public void shouldReturnNullOnEmptyHeader() throws Exception {
+ void shouldReturnNullOnEmptyHeader() throws Exception {
Object event = new GHEventHeader.PayloadHandler().parse(req, ann, GHEvent.class, PARAM_NAME);
assertThat("event with empty header", event, nullValue());
}
@Test
- public void shouldReturnNullOnUnknownEventHeader() throws Exception {
+ void shouldReturnNullOnUnknownEventHeader() throws Exception {
when(req.getHeader(GHEventHeader.PayloadHandler.EVENT_HEADER)).thenReturn(UNKNOWN_EVENT);
Object event = new GHEventHeader.PayloadHandler().parse(req, ann, GHEvent.class, PARAM_NAME);
assertThat("event with unknown event header", event, nullValue());
}
-
- @Test(expected = IllegalArgumentException.class)
- public void shouldThrowExcOnWrongTypeOfHeader() throws Exception {
- new GHEventHeader.PayloadHandler().parse(req, ann, String.class, PARAM_NAME);
+
+ @Test
+ void shouldThrowExcOnWrongTypeOfHeader() {
+ assertThrows(IllegalArgumentException.class, () ->
+ new GHEventHeader.PayloadHandler().parse(req, ann, String.class, PARAM_NAME));
}
}
diff --git a/src/test/java/org/jenkinsci/plugins/github/webhook/GHEventPayloadTest.java b/src/test/java/org/jenkinsci/plugins/github/webhook/GHEventPayloadTest.java
index f83af5f06..3c0b1a17e 100644
--- a/src/test/java/org/jenkinsci/plugins/github/webhook/GHEventPayloadTest.java
+++ b/src/test/java/org/jenkinsci/plugins/github/webhook/GHEventPayloadTest.java
@@ -1,11 +1,11 @@
package org.jenkinsci.plugins.github.webhook;
import com.cloudbees.jenkins.GitHubWebHookFullTest;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.kohsuke.stapler.StaplerRequest2;
import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
@@ -16,7 +16,7 @@
/**
* @author lanwen (Merkushev Kirill)
*/
-@RunWith(MockitoJUnitRunner.class)
+@ExtendWith(MockitoExtension.class)
public class GHEventPayloadTest {
public static final String NOT_EMPTY_PAYLOAD_CONTENT = "{}";
@@ -30,7 +30,7 @@ public class GHEventPayloadTest {
private GHEventPayload ann;
@Test
- public void shouldReturnPayloadFromForm() throws Exception {
+ void shouldReturnPayloadFromForm() throws Exception {
when(req.getContentType()).thenReturn(GitHubWebHookFullTest.FORM);
when(req.getParameter(PARAM_NAME)).thenReturn(NOT_EMPTY_PAYLOAD_CONTENT);
Object payload = new GHEventPayload.PayloadHandler().parse(req, ann, String.class, PARAM_NAME);
@@ -40,7 +40,7 @@ public void shouldReturnPayloadFromForm() throws Exception {
}
@Test
- public void shouldReturnNullOnUnknownContentType() throws Exception {
+ void shouldReturnNullOnUnknownContentType() throws Exception {
when(req.getContentType()).thenReturn(UNKNOWN_CONTENT_TYPE);
Object payload = new GHEventPayload.PayloadHandler().parse(req, ann, String.class, PARAM_NAME);
diff --git a/src/test/java/org/jenkinsci/plugins/github/webhook/GHWebhookSignatureSHA256Test.java b/src/test/java/org/jenkinsci/plugins/github/webhook/GHWebhookSignatureSHA256Test.java
index df2280160..e818d5a5d 100644
--- a/src/test/java/org/jenkinsci/plugins/github/webhook/GHWebhookSignatureSHA256Test.java
+++ b/src/test/java/org/jenkinsci/plugins/github/webhook/GHWebhookSignatureSHA256Test.java
@@ -1,7 +1,7 @@
package org.jenkinsci.plugins.github.webhook;
import hudson.util.Secret;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
@@ -11,7 +11,7 @@
*
* @since 1.45.0
*/
-public class GHWebhookSignatureSHA256Test {
+class GHWebhookSignatureSHA256Test {
private static final String SECRET_CONTENT = "It's a Secret to Everybody";
private static final String PAYLOAD = "Hello, World!";
@@ -19,7 +19,7 @@ public class GHWebhookSignatureSHA256Test {
private static final String EXPECTED_SHA256_DIGEST = "757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17";
@Test
- public void shouldComputeCorrectSHA256Signature() {
+ void shouldComputeCorrectSHA256Signature() {
Secret secret = Secret.fromString(SECRET_CONTENT);
GHWebhookSignature signature = GHWebhookSignature.webhookSignature(PAYLOAD, secret);
@@ -30,7 +30,7 @@ public void shouldComputeCorrectSHA256Signature() {
}
@Test
- public void shouldValidateSHA256SignatureCorrectly() {
+ void shouldValidateSHA256SignatureCorrectly() {
Secret secret = Secret.fromString(SECRET_CONTENT);
GHWebhookSignature signature = GHWebhookSignature.webhookSignature(PAYLOAD, secret);
@@ -40,7 +40,7 @@ public void shouldValidateSHA256SignatureCorrectly() {
}
@Test
- public void shouldRejectInvalidSHA256Signature() {
+ void shouldRejectInvalidSHA256Signature() {
Secret secret = Secret.fromString(SECRET_CONTENT);
GHWebhookSignature signature = GHWebhookSignature.webhookSignature(PAYLOAD, secret);
@@ -51,7 +51,7 @@ public void shouldRejectInvalidSHA256Signature() {
}
@Test
- public void shouldRejectSHA1SignatureWhenExpectingSHA256() {
+ void shouldRejectSHA1SignatureWhenExpectingSHA256() {
String secretContent = "test-secret";
Secret secret = Secret.fromString(secretContent);
GHWebhookSignature signature = GHWebhookSignature.webhookSignature(PAYLOAD, secret);
@@ -65,7 +65,7 @@ public void shouldRejectSHA1SignatureWhenExpectingSHA256() {
}
@Test
- public void shouldHandleDifferentPayloads() {
+ void shouldHandleDifferentPayloads() {
Secret secret = Secret.fromString(SECRET_CONTENT);
String payload1 = "payload1";
String payload2 = "payload2";
diff --git a/src/test/java/org/jenkinsci/plugins/github/webhook/RequirePostWithGHHookPayloadTest.java b/src/test/java/org/jenkinsci/plugins/github/webhook/RequirePostWithGHHookPayloadTest.java
index 878e9f1a6..b51d2f0fd 100644
--- a/src/test/java/org/jenkinsci/plugins/github/webhook/RequirePostWithGHHookPayloadTest.java
+++ b/src/test/java/org/jenkinsci/plugins/github/webhook/RequirePostWithGHHookPayloadTest.java
@@ -1,31 +1,36 @@
package org.jenkinsci.plugins.github.webhook;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
+import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.kohsuke.github.GHEvent;
import org.kohsuke.stapler.StaplerRequest2;
import org.mockito.Mock;
import org.mockito.Spy;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
import java.lang.reflect.InvocationTargetException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
-import static org.jenkinsci.plugins.github.test.HookSecretHelper.storeSecret;
import static org.jenkinsci.plugins.github.test.HookSecretHelper.removeSecret;
+import static org.jenkinsci.plugins.github.test.HookSecretHelper.storeSecret;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
/**
* @author lanwen (Merkushev Kirill)
*/
-@RunWith(MockitoJUnitRunner.class)
-public class RequirePostWithGHHookPayloadTest {
+@WithJenkins
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
+class RequirePostWithGHHookPayloadTest {
private static final String SECRET_CONTENT = "secret";
private static final String PAYLOAD = "sample payload";
@@ -33,106 +38,108 @@ public class RequirePostWithGHHookPayloadTest {
@Mock
private StaplerRequest2 req;
- @Rule
- public JenkinsRule jenkinsRule = new JenkinsRule();
+ private JenkinsRule jenkinsRule;
@Spy
private RequirePostWithGHHookPayload.Processor processor;
- @Before
- public void setSecret() {
+ @BeforeEach
+ void setUp(JenkinsRule rule) {
+ jenkinsRule = rule;
storeSecret(SECRET_CONTENT);
}
@Test
- public void shouldPassOnlyPost() throws Exception {
+ void shouldPassOnlyPost() throws Exception {
when(req.getMethod()).thenReturn("POST");
new RequirePostWithGHHookPayload.Processor().shouldBePostMethod(req);
}
- @Test(expected = InvocationTargetException.class)
- public void shouldNotPassOnNotPost() throws Exception {
+ @Test
+ void shouldNotPassOnNotPost() {
when(req.getMethod()).thenReturn("GET");
- new RequirePostWithGHHookPayload.Processor().shouldBePostMethod(req);
+ assertThrows(InvocationTargetException.class, () ->
+ new RequirePostWithGHHookPayload.Processor().shouldBePostMethod(req));
}
@Test
- public void shouldPassOnGHEventAndNotBlankPayload() throws Exception {
+ void shouldPassOnGHEventAndNotBlankPayload() throws Exception {
new RequirePostWithGHHookPayload.Processor().shouldContainParseablePayload(
new Object[]{GHEvent.PUSH, "{}"});
}
- @Test(expected = InvocationTargetException.class)
- public void shouldNotPassOnNullGHEventAndNotBlankPayload() throws Exception {
- new RequirePostWithGHHookPayload.Processor().shouldContainParseablePayload(
- new Object[]{null, "{}"});
+ @Test
+ void shouldNotPassOnNullGHEventAndNotBlankPayload() {
+ assertThrows(InvocationTargetException.class, () ->
+ new RequirePostWithGHHookPayload.Processor().shouldContainParseablePayload(
+ new Object[]{null, "{}"}));
}
- @Test(expected = InvocationTargetException.class)
- public void shouldNotPassOnGHEventAndBlankPayload() throws Exception {
- new RequirePostWithGHHookPayload.Processor().shouldContainParseablePayload(
- new Object[]{GHEvent.PUSH, " "});
+ @Test
+ void shouldNotPassOnGHEventAndBlankPayload() {
+ assertThrows(InvocationTargetException.class, () ->
+ new RequirePostWithGHHookPayload.Processor().shouldContainParseablePayload(
+ new Object[]{GHEvent.PUSH, " "}));
}
- @Test(expected = InvocationTargetException.class)
- public void shouldNotPassOnNulls() throws Exception {
- new RequirePostWithGHHookPayload.Processor().shouldContainParseablePayload(
- new Object[]{null, null});
+ @Test
+ void shouldNotPassOnNulls() {
+ assertThrows(InvocationTargetException.class, () ->
+ new RequirePostWithGHHookPayload.Processor().shouldContainParseablePayload(
+ new Object[]{null, null}));
}
- @Test(expected = InvocationTargetException.class)
- public void shouldNotPassOnGreaterCountOfArgs() throws Exception {
- new RequirePostWithGHHookPayload.Processor().shouldContainParseablePayload(
- new Object[]{GHEvent.PUSH, "{}", " "}
- );
+ @Test
+ void shouldNotPassOnGreaterCountOfArgs() {
+ assertThrows(InvocationTargetException.class, () ->
+ new RequirePostWithGHHookPayload.Processor().shouldContainParseablePayload(
+ new Object[]{GHEvent.PUSH, "{}", " "}
+ ));
}
- @Test(expected = InvocationTargetException.class)
- public void shouldNotPassOnLessCountOfArgs() throws Exception {
- new RequirePostWithGHHookPayload.Processor().shouldContainParseablePayload(
- new Object[]{GHEvent.PUSH}
- );
+ @Test
+ void shouldNotPassOnLessCountOfArgs() {
+ assertThrows(InvocationTargetException.class, () ->
+ new RequirePostWithGHHookPayload.Processor().shouldContainParseablePayload(
+ new Object[]{GHEvent.PUSH}
+ ));
}
@Test
@Issue("JENKINS-37481")
- public void shouldPassOnAbsentSignatureInRequestIfSecretIsNotConfigured() throws Exception {
- doReturn(PAYLOAD).when(processor).payloadFrom(req, null);
+ void shouldPassOnAbsentSignatureInRequestIfSecretIsNotConfigured() throws Exception {
removeSecret();
processor.shouldProvideValidSignature(req, null);
}
- @Test(expected = InvocationTargetException.class)
+ @Test
@Issue("JENKINS-48012")
- public void shouldNotPassOnAbsentSignatureInRequest() throws Exception {
- doReturn(PAYLOAD).when(processor).payloadFrom(req, null);
-
- processor.shouldProvideValidSignature(req, null);
+ void shouldNotPassOnAbsentSignatureInRequest() {
+ assertThrows(InvocationTargetException.class, () ->
+ processor.shouldProvideValidSignature(req, null));
}
- @Test(expected = InvocationTargetException.class)
- public void shouldNotPassOnInvalidSignature() throws Exception {
+ @Test
+ void shouldNotPassOnInvalidSignature() {
final String signature = "sha1=a94a8fe5ccb19ba61c4c0873d391e987982fbbd3";
-
when(req.getHeader(RequirePostWithGHHookPayload.Processor.SIGNATURE_HEADER)).thenReturn(signature);
doReturn(PAYLOAD).when(processor).payloadFrom(req, null);
-
- processor.shouldProvideValidSignature(req, null);
+ assertThrows(InvocationTargetException.class, () ->
+ processor.shouldProvideValidSignature(req, null));
}
- @Test(expected = InvocationTargetException.class)
- public void shouldNotPassOnMalformedSignature() throws Exception {
+ @Test
+ void shouldNotPassOnMalformedSignature() {
final String signature = "49d5f5cf800a81f257324912969a2d325d13d3fc";
-
when(req.getHeader(RequirePostWithGHHookPayload.Processor.SIGNATURE_HEADER)).thenReturn(signature);
doReturn(PAYLOAD).when(processor).payloadFrom(req, null);
-
- processor.shouldProvideValidSignature(req, null);
+ assertThrows(InvocationTargetException.class, () ->
+ processor.shouldProvideValidSignature(req, null));
}
@Test
- public void shouldPassWithValidSignature() throws Exception {
+ void shouldPassWithValidSignature() throws Exception {
final String signature = "sha1=49d5f5cf800a81f257324912969a2d325d13d3fc";
final String signature256 = "sha256=569beaec8ea1c9deccec283d0bb96aeec0a77310c70875343737ae72cffa7044";
@@ -145,7 +152,7 @@ public void shouldPassWithValidSignature() throws Exception {
@Test
@Issue("JENKINS-37481")
- public void shouldIgnoreSignHeaderOnNotDefinedSignInConfig() throws Exception {
+ void shouldIgnoreSignHeaderOnNotDefinedSignInConfig() throws Exception {
removeSecret();
final String signature = "sha1=49d5f5cf800a81f257324912969a2d325d13d3fc";
@@ -155,7 +162,7 @@ public void shouldIgnoreSignHeaderOnNotDefinedSignInConfig() throws Exception {
}
@Test
- public void shouldReturnValidPayloadOnApplicationJson() {
+ void shouldReturnValidPayloadOnApplicationJson() {
final String payload = "test";
doReturn(GHEventPayload.PayloadHandler.APPLICATION_JSON).when(req).getContentType();
@@ -166,7 +173,7 @@ public void shouldReturnValidPayloadOnApplicationJson() {
}
@Test
- public void shouldReturnValidPayloadOnFormUrlEncoded() {
+ void shouldReturnValidPayloadOnFormUrlEncoded() {
final String payload = "test";
doReturn(GHEventPayload.PayloadHandler.FORM_URLENCODED).when(req).getContentType();
diff --git a/src/test/java/org/jenkinsci/plugins/github/webhook/SignatureAlgorithmTest.java b/src/test/java/org/jenkinsci/plugins/github/webhook/SignatureAlgorithmTest.java
index 37b16eeeb..03b527923 100644
--- a/src/test/java/org/jenkinsci/plugins/github/webhook/SignatureAlgorithmTest.java
+++ b/src/test/java/org/jenkinsci/plugins/github/webhook/SignatureAlgorithmTest.java
@@ -1,6 +1,6 @@
package org.jenkinsci.plugins.github.webhook;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
@@ -10,10 +10,10 @@
*
* @since 1.45.0
*/
-public class SignatureAlgorithmTest {
+class SignatureAlgorithmTest {
@Test
- public void shouldHaveCorrectSHA256Properties() {
+ void shouldHaveCorrectSHA256Properties() {
SignatureAlgorithm algorithm = SignatureAlgorithm.SHA256;
assertThat("SHA-256 prefix", algorithm.getPrefix(), equalTo("sha256"));
@@ -23,7 +23,7 @@ public void shouldHaveCorrectSHA256Properties() {
}
@Test
- public void shouldHaveCorrectSHA1Properties() {
+ void shouldHaveCorrectSHA1Properties() {
SignatureAlgorithm algorithm = SignatureAlgorithm.SHA1;
assertThat("SHA-1 prefix", algorithm.getPrefix(), equalTo("sha1"));
@@ -33,7 +33,7 @@ public void shouldHaveCorrectSHA1Properties() {
}
@Test
- public void shouldDefaultToSHA256() {
+ void shouldDefaultToSHA256() {
assertThat("Default algorithm should be SHA-256",
SignatureAlgorithm.getDefault(), equalTo(SignatureAlgorithm.SHA256));
}
diff --git a/src/test/java/org/jenkinsci/plugins/github/webhook/WebhookManagerTest.java b/src/test/java/org/jenkinsci/plugins/github/webhook/WebhookManagerTest.java
index fcb3462f1..3f68c066f 100644
--- a/src/test/java/org/jenkinsci/plugins/github/webhook/WebhookManagerTest.java
+++ b/src/test/java/org/jenkinsci/plugins/github/webhook/WebhookManagerTest.java
@@ -11,12 +11,13 @@
import hudson.plugins.git.GitSCM;
import org.jenkinsci.plugins.github.GitHubPlugin;
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.WithoutJenkins;
+import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.kohsuke.github.GHEvent;
import org.kohsuke.github.GHHook;
import org.kohsuke.github.GHRepository;
@@ -24,7 +25,7 @@
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Spy;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
import java.net.MalformedURLException;
@@ -36,12 +37,12 @@
import static com.google.common.collect.ImmutableList.copyOf;
import static com.google.common.collect.Lists.asList;
import static com.google.common.collect.Lists.newArrayList;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.jenkinsci.plugins.github.test.HookSecretHelper.storeSecretIn;
import static org.jenkinsci.plugins.github.webhook.WebhookManager.forHookUrl;
-import static org.hamcrest.MatcherAssert.assertThat;
import static org.kohsuke.github.GHEvent.CREATE;
import static org.kohsuke.github.GHEvent.PULL_REQUEST;
import static org.kohsuke.github.GHEvent.PUSH;
@@ -51,8 +52,8 @@
import static org.mockito.ArgumentMatchers.anySet;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.lenient;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
@@ -62,15 +63,15 @@
/**
* @author lanwen (Merkushev Kirill)
*/
-@RunWith(MockitoJUnitRunner.class)
+@WithJenkins
+@ExtendWith(MockitoExtension.class)
public class WebhookManagerTest {
public static final GitSCM GIT_SCM = new GitSCM("ssh://git@github.com/dummy/dummy.git");
public static final URL HOOK_ENDPOINT = endpoint("http://hook.endpoint/");
public static final URL ANOTHER_HOOK_ENDPOINT = endpoint("http://another.url/");
- @Rule
- public JenkinsRule jenkins = new JenkinsRule();
+ private JenkinsRule jenkins;
@Spy
private WebhookManager manager = forHookUrl(HOOK_ENDPOINT);
@@ -87,15 +88,20 @@ public class WebhookManagerTest {
@Captor
ArgumentCaptor