From 40b5d750a0a4b1e528978479645095d06a92e34a Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Mon, 10 Mar 2025 09:40:01 +0100 Subject: [PATCH] Migrate tests to JUnit5 * Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup --- .../github/CheckRunGHEventSubscriberTest.java | 106 ++++---- .../github/GitHubChecksDetailsTest.java | 14 +- .../GitHubChecksPublisherFactoryTest.java | 19 +- .../github/GitHubChecksPublisherITest.java | 245 ++++++++---------- .../GitHubSCMSourceChecksContextTest.java | 25 +- .../github/GitSCMChecksContextITest.java | 35 ++- .../github/GitSCMChecksContextTest.java | 2 +- .../config/GitHubChecksConfigITest.java | 17 +- .../GitHubSCMSourceStatusChecksTraitTest.java | 1 + .../GitHubStatusChecksPropertiesTest.java | 9 +- 10 files changed, 227 insertions(+), 246 deletions(-) diff --git a/src/test/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriberTest.java b/src/test/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriberTest.java index 72414b9c..843b0705 100644 --- a/src/test/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriberTest.java +++ b/src/test/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriberTest.java @@ -1,13 +1,16 @@ package io.jenkins.plugins.checks.github; -import hudson.model.*; +import hudson.model.Item; +import hudson.model.Job; +import hudson.model.ParametersAction; +import hudson.model.Run; +import hudson.model.StringParameterValue; import io.jenkins.plugins.util.JenkinsFacade; import org.apache.commons.io.FileUtils; import org.jenkinsci.plugins.github.extension.GHSubscriberEvent; import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; -import org.junit.Rule; import org.junit.jupiter.api.Test; -import org.jvnet.hudson.test.LoggerRule; +import org.jvnet.hudson.test.LogRecorder; import org.kohsuke.github.GHEvent; import java.io.File; @@ -16,20 +19,17 @@ import java.util.Optional; import java.util.logging.Level; -import static org.assertj.core.api.Assertions.*; -import static org.mockito.Mockito.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class CheckRunGHEventSubscriberTest { - static final String RERUN_REQUEST_JSON_FOR_PR = "check-run-event-with-rerun-action-for-pr.json"; - static final String RERUN_REQUEST_JSON_FOR_MASTER = "check-run-event-with-rerun-action-for-master.json"; - static final String RERUN_REQUEST_JSON_FOR_PR_MISSING_CHECKSUITE = "check-run-event-with-rerun-action-for-pr-missing-check-suite.json"; - static final String RERUN_REQUEST_JSON_FOR_PR_MISSING_CHECKSUITE_HEAD_BRANCH = "check-run-event-with-rerun-action-for-pr-missing-check-suite-head-branch.json"; - /** - * Rule for the log system. - */ - @Rule - public LoggerRule loggerRule = new LoggerRule(); + private static final String RERUN_REQUEST_JSON_FOR_PR = "check-run-event-with-rerun-action-for-pr.json"; + private static final String RERUN_REQUEST_JSON_FOR_MASTER = "check-run-event-with-rerun-action-for-master.json"; + private static final String RERUN_REQUEST_JSON_FOR_PR_MISSING_CHECKSUITE = "check-run-event-with-rerun-action-for-pr-missing-check-suite.json"; + private static final String RERUN_REQUEST_JSON_FOR_PR_MISSING_CHECKSUITE_HEAD_BRANCH = "check-run-event-with-rerun-action-for-pr-missing-check-suite-head-branch.json"; @Test void shouldBeApplicableForJobWithGitHubSCMSource() { @@ -65,35 +65,38 @@ void shouldSubscribeToCheckRunEvent() { @Test void shouldProcessCheckRunEventWithRerequestedAction() throws IOException { - loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1); - new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(SCMFacade.class)) - .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR)); - assertThat(loggerRule.getMessages().get(0)).contains("Received rerun request through GitHub checks API."); + try (LogRecorder logRecorder = new LogRecorder().record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1)) { + new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(SCMFacade.class)) + .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR)); + assertThat(logRecorder.getMessages().get(0)).contains("Received rerun request through GitHub checks API."); + } } @Test - void shouldThrowExceptionWhenCheckSuitesMissingFromPayload() throws IOException { + void shouldThrowExceptionWhenCheckSuitesMissingFromPayload() { assertThatThrownBy( - () -> new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(SCMFacade.class)) - .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR_MISSING_CHECKSUITE))) + () -> new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(SCMFacade.class)) + .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR_MISSING_CHECKSUITE))) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("Could not parse check run event:"); } @Test void shouldIgnoreHeadBranchMissingFromPayload() throws IOException { - loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1); - new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(SCMFacade.class)) - .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR_MISSING_CHECKSUITE_HEAD_BRANCH)); - assertThat(loggerRule.getMessages().get(0)).contains("Received rerun request through GitHub checks API."); + try (LogRecorder logRecorder = new LogRecorder().record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1)) { + new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(SCMFacade.class)) + .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR_MISSING_CHECKSUITE_HEAD_BRANCH)); + assertThat(logRecorder.getMessages().get(0)).contains("Received rerun request through GitHub checks API."); + } } @Test void shouldIgnoreCheckRunEventWithoutRerequestedAction() throws IOException { - loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.FINE).capture(1); - new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(SCMFacade.class)) - .onEvent(createEventWithRerunRequest("check-run-event-with-created-action.json")); - assertThat(loggerRule.getMessages()).contains("Unsupported check run action: created"); + try (LogRecorder logRecorder = new LogRecorder().record(CheckRunGHEventSubscriber.class.getName(), Level.FINE).capture(1)) { + new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(SCMFacade.class)) + .onEvent(createEventWithRerunRequest("check-run-event-with-created-action.json")); + assertThat(logRecorder.getMessages()).contains("Unsupported check run action: created"); + } } @Test @@ -107,16 +110,17 @@ void shouldScheduleRerunForPR() throws IOException { when(jenkinsFacade.getFullNameOf(job)).thenReturn("codingstyle/PR-1"); when(run.getParent()).thenReturn(job); when(run.getAction(ParametersAction.class)).thenReturn( - new ParametersAction(new StringParameterValue("test_key", "test_value")) + new ParametersAction(new StringParameterValue("test_key", "test_value")) ); when(job.getNextBuildNumber()).thenReturn(1); when(job.getName()).thenReturn("PR-1"); - loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1); - new CheckRunGHEventSubscriber(jenkinsFacade, scmFacade) - .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR)); - assertThat(loggerRule.getMessages()) - .contains("Scheduled rerun (build #1) for job codingstyle/PR-1, requested by XiongKezhi"); + try (LogRecorder logRecorder = new LogRecorder().record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1)) { + new CheckRunGHEventSubscriber(jenkinsFacade, scmFacade) + .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR)); + assertThat(logRecorder.getMessages()) + .contains("Scheduled rerun (build #1) for job codingstyle/PR-1, requested by XiongKezhi"); + } } @Test @@ -133,11 +137,12 @@ void shouldScheduleRerunForMaster() throws IOException { when(job.getNextBuildNumber()).thenReturn(1); when(job.getName()).thenReturn("master"); - loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1); - new CheckRunGHEventSubscriber(jenkinsFacade, scmFacade) - .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_MASTER)); - assertThat(loggerRule.getMessages()) - .contains("Scheduled rerun (build #1) for job codingstyle/master, requested by XiongKezhi"); + try (LogRecorder logRecorder = new LogRecorder().record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1)) { + new CheckRunGHEventSubscriber(jenkinsFacade, scmFacade) + .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_MASTER)); + assertThat(logRecorder.getMessages()) + .contains("Scheduled rerun (build #1) for job codingstyle/master, requested by XiongKezhi"); + } } @Test @@ -158,24 +163,25 @@ void shouldContainsUserAndBranchInShortDescriptionOfGitHubChecksRerunActionCause } @Test - void shouldHaveAccessableBranchNameInGitHubChecksRerunActionCause() { + void shouldHaveAccessibleBranchNameInGitHubChecksRerunActionCause() { CheckRunGHEventSubscriber.GitHubChecksRerunActionCause cause = new CheckRunGHEventSubscriber.GitHubChecksRerunActionCause("jenkins", "some_branch"); assertThat(cause.getBranchName()).isEqualTo("some_branch"); } - private void assertNoBuildIsScheduled(final JenkinsFacade jenkinsFacade, final SCMFacade scmFacade, - final GHSubscriberEvent event) { - loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.WARNING).capture(1); - new CheckRunGHEventSubscriber(jenkinsFacade, scmFacade).onEvent(event); - assertThat(loggerRule.getMessages()) - .contains("No build found for rerun request from repository: XiongKezhi/codingstyle and id: codingstyle/PR-1#2"); + private static void assertNoBuildIsScheduled(final JenkinsFacade jenkinsFacade, final SCMFacade scmFacade, + final GHSubscriberEvent event) { + try (LogRecorder logRecorder = new LogRecorder().record(CheckRunGHEventSubscriber.class.getName(), Level.WARNING).capture(1)) { + new CheckRunGHEventSubscriber(jenkinsFacade, scmFacade).onEvent(event); + assertThat(logRecorder.getMessages()) + .contains("No build found for rerun request from repository: XiongKezhi/codingstyle and id: codingstyle/PR-1#2"); + } } - private GHSubscriberEvent createEventWithRerunRequest(final String jsonFile) throws IOException { + private static GHSubscriberEvent createEventWithRerunRequest(final String jsonFile) throws IOException { return new GHSubscriberEvent("CheckRunGHEventSubscriberTest", GHEvent.CHECK_RUN, - FileUtils.readFileToString(new File(getClass().getResource(getClass().getSimpleName() + "/" - + jsonFile).getFile()), StandardCharsets.UTF_8)); + FileUtils.readFileToString(new File(CheckRunGHEventSubscriberTest.class.getResource( + CheckRunGHEventSubscriberTest.class.getSimpleName() + "/" + jsonFile).getFile()), StandardCharsets.UTF_8)); } } diff --git a/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksDetailsTest.java b/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksDetailsTest.java index c495aa46..3ac9bafe 100644 --- a/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksDetailsTest.java +++ b/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksDetailsTest.java @@ -1,19 +1,19 @@ package io.jenkins.plugins.checks.github; -import org.apache.commons.lang3.StringUtils; -import org.junit.jupiter.api.Test; - -import org.kohsuke.github.GHCheckRun.Conclusion; -import org.kohsuke.github.GHCheckRun.Status; - import io.jenkins.plugins.checks.api.ChecksConclusion; import io.jenkins.plugins.checks.api.ChecksDetails; import io.jenkins.plugins.checks.api.ChecksDetails.ChecksDetailsBuilder; import io.jenkins.plugins.checks.api.ChecksStatus; +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; +import org.kohsuke.github.GHCheckRun.Conclusion; +import org.kohsuke.github.GHCheckRun.Status; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; class GitHubChecksDetailsTest { + @Test void shouldReturnAllGitHubObjectsCorrectly() { ChecksDetails details = new ChecksDetailsBuilder() diff --git a/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherFactoryTest.java b/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherFactoryTest.java index 2363eb14..8e111835 100644 --- a/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherFactoryTest.java +++ b/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherFactoryTest.java @@ -1,9 +1,9 @@ package io.jenkins.plugins.checks.github; -import java.io.IOException; -import java.util.Optional; - import hudson.EnvVars; +import hudson.model.Job; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.plugins.git.GitSCM; import hudson.plugins.git.UserRemoteConfig; import jenkins.scm.api.SCMHead; @@ -13,14 +13,15 @@ import org.jenkinsci.plugins.github_branch_source.PullRequestSCMRevision; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.*; -import static org.mockito.Mockito.*; +import java.io.IOException; +import java.util.Optional; -import hudson.model.Job; -import hudson.model.Run; -import hudson.model.TaskListener; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class GitHubChecksPublisherFactoryTest { + @Test void shouldCreateGitHubChecksPublisherFromRunForProjectWithValidGitHubSCMSource() { Run run = mock(Run.class); @@ -113,7 +114,7 @@ void shouldCreateNullPublisherFromJobForInvalidProject() { .isNotPresent(); } - private DisplayURLProvider createDisplayURLProvider(final Run run, final Job job) { + private static DisplayURLProvider createDisplayURLProvider(final Run run, final Job job) { DisplayURLProvider urlProvider = mock(DisplayURLProvider.class); when(urlProvider.getRunURL(run)).thenReturn(null); diff --git a/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherITest.java b/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherITest.java index 3ac7e395..b63982ff 100644 --- a/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherITest.java +++ b/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherITest.java @@ -2,57 +2,24 @@ import com.cloudbees.plugins.credentials.CredentialsMatchers; import com.cloudbees.plugins.credentials.CredentialsScope; -import com.fasterxml.jackson.databind.PropertyNamingStrategies; -import com.github.tomakehurst.wiremock.core.WireMockConfiguration; -import com.github.tomakehurst.wiremock.junit.WireMockRule; -import hudson.model.Action; -import hudson.model.Result; -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Optional; -import java.util.function.Function; -import java.util.logging.Level; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.jvnet.hudson.test.Issue; -import org.jvnet.hudson.test.JenkinsRule; -import org.jvnet.hudson.test.LoggerRule; -import org.mockito.MockedStatic; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.InjectableValues; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.introspect.VisibilityChecker; - +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - -import org.kohsuke.github.GHCheckRun; -import org.kohsuke.github.GHCheckRunBuilder; -import org.kohsuke.github.GHRepository; -import org.kohsuke.github.GitHub; -import org.jenkinsci.plugins.displayurlapi.ClassicDisplayURLProvider; -import org.jenkinsci.plugins.github_branch_source.Connector; -import org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials; -import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; -import org.jenkinsci.plugins.github_branch_source.PullRequestSCMRevision; -import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; -import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import hudson.model.Action; import hudson.model.FreeStyleProject; import hudson.model.Job; import hudson.model.Queue; +import hudson.model.Result; import hudson.model.Run; import hudson.util.Secret; -import jenkins.model.ParameterizedJobMixIn; -import jenkins.scm.api.SCMHead; import io.jenkins.plugins.checks.api.ChecksAction; import io.jenkins.plugins.checks.api.ChecksAnnotation.ChecksAnnotationBuilder; import io.jenkins.plugins.checks.api.ChecksAnnotation.ChecksAnnotationLevel; @@ -63,8 +30,40 @@ import io.jenkins.plugins.checks.api.ChecksOutput.ChecksOutputBuilder; import io.jenkins.plugins.checks.api.ChecksStatus; import io.jenkins.plugins.util.PluginLogger; +import jenkins.model.ParameterizedJobMixIn; +import jenkins.scm.api.SCMHead; +import org.jenkinsci.plugins.displayurlapi.ClassicDisplayURLProvider; +import org.jenkinsci.plugins.github_branch_source.Connector; +import org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials; +import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; +import org.jenkinsci.plugins.github_branch_source.PullRequestSCMRevision; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.jvnet.hudson.test.Issue; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.LogRecorder; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; +import org.kohsuke.github.GHCheckRun; +import org.kohsuke.github.GHCheckRunBuilder; +import org.kohsuke.github.GHRepository; +import org.kohsuke.github.GitHub; +import org.mockito.MockedStatic; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.Arrays; +import java.util.Collections; +import java.util.Optional; +import java.util.function.Function; +import java.util.logging.Level; +import java.util.stream.Stream; -import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.*; +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY; +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.*; @@ -72,89 +71,61 @@ * Tests if the {@link GitHubChecksPublisher} actually sends out the requests to GitHub in order to publish the check * runs. */ -@RunWith(Parameterized.class) +@WithJenkins @SuppressWarnings({"PMD.ExcessiveImports", "checkstyle:ClassDataAbstractionCoupling", "rawtypes", "checkstyle:ClassFanOutComplexity", "checkstyle:JavaNCSS"}) -public class GitHubChecksPublisherITest { - - private static final String TEST_PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\n" + - "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDWV2v0jCfzbyTi\n" + - "r3mIufQSvXQj02e0Hbia0BOjYluZ2ife/RMs8mrzxAfWUtyrWsi+50OvbxXx+mk1\n" + - "drn+aR0z0YJ7gqymvn2zWUDv+99eWSb9yeKT3cZU7EpcwtL8APPLzSycoPeylkf8\n" + - "jtWopdglWO7AXnA+OIiW/luxgxzjUL6lrzye/9l67qQksy6F42+X5jKTZYx2e3vd\n" + - "I/NZgCGd/2h61RAHJH/2QwujYva2kc5pvm0JmwHKWqEWu+i6lcGXeL/C3zkyh8To\n" + - "ROFNMz/12+mUbqye1dAg19JcJtmM8ymHsmfFc9CGmXQyuAuhU4zPssA/2i0rPWl+\n" + - "xthlEA6TAgMBAAECggEASVrf8nCpF5H5IK+HO3jQhD1cawpl2mm1jR4bKnZ1/QCB\n" + - "Vrpr/pz0Z3q2Z+4x4V8Phu4k5vxwmUDnEsoQO3aD7QEN0/FT3zkgUeoA5GDiACso\n" + - "wgB+z7Y9s0Cu7nIqvN4ikaQlWXFpdDAkcNX9X1tqztVR2Ho5lcHJVUu129mQYGbY\n" + - "ivdmSIjLn9oqFhqOpdYtLSoiNtoJmhyFTQj0G+DTumS9G556sBRuZI7qwAKrd6+D\n" + - "GPvbgVC7mcGogDgUyIAMLj9Q+EfjlX+gfWtqabF9v5Wxp0u1vdC+mdmL/IgbqGTW\n" + - "DYEQAS1gkkLYXQZXBp7vREU5Oq/W2/okX4FaRNzW2QKBgQDurWDX3Jh+3Q+raQy4\n" + - "qyN3WZ4XUPzmVoQ11+GY/SkcFXK3r6xD+FZtjUv8yugarnjdYPeG3SUJKhEhVnl8\n" + - "Ja2CLruZB6sbfsQ2lA9vKR87upWV4DJftuAdFnWVVMD6ti1KCTHIDiUl0YAxWF6A\n" + - "EGKDrzQIVdTtBn6+Hrhn59AYtQKBgQDl5eKQiyA1wQ/nO4u+VskcrBcaTQJjNysz\n" + - "mo9k+jpJQqVrJ2kNopbkaZyz74IXI73rQ8CmctAPrSiucj1SeMBWWPWXDC+hxzjV\n" + - "NURdmEh7D0fpKAknn2WPrIDrqLgsVTiCEX/XicX3eCTuKf+mSUwv//6MFhDIntC4\n" + - "2PdCtMD/JwKBgBFEH55eCfYbfdezmMT/NGic5g/fvvvWxGe0v1A2+DNc5did78NX\n" + - "AsGYGCgocZQEjR/OtPlfpB8+mNClldJCU4P4Z3/RizJJAF7GZTtwaR8EB3A5MMu1\n" + - "yg6woj70S6WXaj1R3vUO+Ob8ed6X+vYeuVG3afc0ZlvjPWX5iPOTVH2FAoGAYqnc\n" + - "KChtNGSczKITgSaBvRpl99Wg9q+QjN8CN1XkedhuYaRSQ5XJqFFi/R4G+KNQOI2l\n" + - "Okn/3Rp1YRiKFMDZ2rTnAWIrdwSm8Wmg44IdaSLPu9KAy05vKc/grEKGeBBC5h9Y\n" + - "fEoWefRH9SZ1HwpJ9jepKLm3jkIKVapXw3sLcPUCgYBdbDosTe2LtF2buaNsBMQw\n" + - "H3c9fHllrDSy2Twr12ShSc5xMIqTWtiTAvEcMZYP4BX9uSPUWwaB7wMBx2CCMsXR\n" + - "sZLRujRKV9s5qSmUOXHQWIcmsEvjyxiNtVNhi3rXeMMgYISleUH4ife4evulPHzD\n" + - "gppAplykAFg49TGEqr7ihQ==\n" + - "-----END PRIVATE KEY-----"; +class GitHubChecksPublisherITest { + + private static final String TEST_PRIVATE_KEY = """ + -----BEGIN PRIVATE KEY----- + MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDWV2v0jCfzbyTi + r3mIufQSvXQj02e0Hbia0BOjYluZ2ife/RMs8mrzxAfWUtyrWsi+50OvbxXx+mk1 + drn+aR0z0YJ7gqymvn2zWUDv+99eWSb9yeKT3cZU7EpcwtL8APPLzSycoPeylkf8 + jtWopdglWO7AXnA+OIiW/luxgxzjUL6lrzye/9l67qQksy6F42+X5jKTZYx2e3vd + I/NZgCGd/2h61RAHJH/2QwujYva2kc5pvm0JmwHKWqEWu+i6lcGXeL/C3zkyh8To + ROFNMz/12+mUbqye1dAg19JcJtmM8ymHsmfFc9CGmXQyuAuhU4zPssA/2i0rPWl+ + xthlEA6TAgMBAAECggEASVrf8nCpF5H5IK+HO3jQhD1cawpl2mm1jR4bKnZ1/QCB + Vrpr/pz0Z3q2Z+4x4V8Phu4k5vxwmUDnEsoQO3aD7QEN0/FT3zkgUeoA5GDiACso + wgB+z7Y9s0Cu7nIqvN4ikaQlWXFpdDAkcNX9X1tqztVR2Ho5lcHJVUu129mQYGbY + ivdmSIjLn9oqFhqOpdYtLSoiNtoJmhyFTQj0G+DTumS9G556sBRuZI7qwAKrd6+D + GPvbgVC7mcGogDgUyIAMLj9Q+EfjlX+gfWtqabF9v5Wxp0u1vdC+mdmL/IgbqGTW + DYEQAS1gkkLYXQZXBp7vREU5Oq/W2/okX4FaRNzW2QKBgQDurWDX3Jh+3Q+raQy4 + qyN3WZ4XUPzmVoQ11+GY/SkcFXK3r6xD+FZtjUv8yugarnjdYPeG3SUJKhEhVnl8 + Ja2CLruZB6sbfsQ2lA9vKR87upWV4DJftuAdFnWVVMD6ti1KCTHIDiUl0YAxWF6A + EGKDrzQIVdTtBn6+Hrhn59AYtQKBgQDl5eKQiyA1wQ/nO4u+VskcrBcaTQJjNysz + mo9k+jpJQqVrJ2kNopbkaZyz74IXI73rQ8CmctAPrSiucj1SeMBWWPWXDC+hxzjV + NURdmEh7D0fpKAknn2WPrIDrqLgsVTiCEX/XicX3eCTuKf+mSUwv//6MFhDIntC4 + 2PdCtMD/JwKBgBFEH55eCfYbfdezmMT/NGic5g/fvvvWxGe0v1A2+DNc5did78NX + AsGYGCgocZQEjR/OtPlfpB8+mNClldJCU4P4Z3/RizJJAF7GZTtwaR8EB3A5MMu1 + yg6woj70S6WXaj1R3vUO+Ob8ed6X+vYeuVG3afc0ZlvjPWX5iPOTVH2FAoGAYqnc + KChtNGSczKITgSaBvRpl99Wg9q+QjN8CN1XkedhuYaRSQ5XJqFFi/R4G+KNQOI2l + Okn/3Rp1YRiKFMDZ2rTnAWIrdwSm8Wmg44IdaSLPu9KAy05vKc/grEKGeBBC5h9Y + fEoWefRH9SZ1HwpJ9jepKLm3jkIKVapXw3sLcPUCgYBdbDosTe2LtF2buaNsBMQw + H3c9fHllrDSy2Twr12ShSc5xMIqTWtiTAvEcMZYP4BX9uSPUWwaB7wMBx2CCMsXR + sZLRujRKV9s5qSmUOXHQWIcmsEvjyxiNtVNhi3rXeMMgYISleUH4ife4evulPHzD + gppAplykAFg49TGEqr7ihQ== + -----END PRIVATE KEY-----"""; /** * Provides parameters for tests. + * * @return A list of methods used to create GitHubChecksContexts, with which each test should be run. */ - @Parameterized.Parameters(name = "{0}") - public static Collection contextBuilders() { - return Arrays.asList(new Object[][]{ - {"Freestyle (run)", (Function) GitHubChecksPublisherITest::createGitHubChecksContextWithGitHubSCMFreestyle, false}, - {"Freestyle (job)", (Function) GitHubChecksPublisherITest::createGitHubChecksContextWithGitHubSCMFreestyle, true}, - {"Pipeline (run)", (Function) GitHubChecksPublisherITest::createGitHubChecksContextWithGitHubSCMFromPipeline, false}, - {"Pipeline (job)", (Function) GitHubChecksPublisherITest::createGitHubChecksContextWithGitHubSCMFromPipeline, true} - }); + static Stream contextBuilders() { + return Stream.of( + new Object[]{"Freestyle (run)", (Function) test -> test.createGitHubChecksContextWithGitHubSCMFreestyle(false), false}, + new Object[]{"Freestyle (job)", (Function) test -> test.createGitHubChecksContextWithGitHubSCMFreestyle(true), true}, + new Object[]{"Pipeline (run)", (Function) test -> test.createGitHubChecksContextWithGitHubSCMFromPipeline(false), false}, + new Object[]{"Pipeline (job)", (Function) test -> test.createGitHubChecksContextWithGitHubSCMFromPipeline(true), true} + ); } - /** - * Human readable name of the context builder - used only for test name formatting. - */ - @SuppressWarnings("checkstyle:VisibilityModifier") - @Parameterized.Parameter(0) - public String contextBuilderName; - - /** - * Reference to method used to create GitHubChecksContext with either a pipeline or freestyle job. - */ - @SuppressWarnings("checkstyle:VisibilityModifier") - @Parameterized.Parameter(1) - public Function contextBuilder; - - /** - * Create GitHubChecksContext from the job instead of the run. - */ - @SuppressWarnings("checkstyle:VisibilityModifier") - @Parameterized.Parameter(2) - public boolean fromJob; - - /** - * Rule for the log system. - */ - @Rule - public LoggerRule loggerRule = new LoggerRule(); - - @Rule - public JenkinsRule j = new JenkinsRule(); + private JenkinsRule j; /** * A rule which provides a mock server. */ - @Rule - public WireMockRule wireMockRule = new WireMockRule( - WireMockConfiguration.options().dynamicPort()); + @RegisterExtension + private static WireMockExtension wireMock = WireMockExtension.newInstance().options(WireMockConfiguration.options().dynamicPort()).build(); private MockedStatic mockCredentialsMatchers() { final var gitHubAppCredentials = new GitHubAppCredentials(CredentialsScope.GLOBAL, "cred-id", null, "app-id", Secret.fromString(TEST_PRIVATE_KEY)); @@ -164,11 +135,17 @@ private MockedStatic mockCredentialsMatchers() { return credentialsMatchers; } + @BeforeEach + void setUp(JenkinsRule j) { + this.j = j; + } + /** * Checks should be published to GitHub correctly when GitHub SCM is found and parameters are correctly set. */ - @Test - public void shouldPublishGitHubCheckRunCorrectly() { + @ParameterizedTest(name = "{0}") + @MethodSource("contextBuilders") + void shouldPublishGitHubCheckRunCorrectly(String contextBuilderName, Function contextBuilder, boolean fromJob) { try (var credentialsMatchers = mockCredentialsMatchers()) { ChecksDetails details = new ChecksDetailsBuilder() .withName("Jenkins") @@ -213,7 +190,7 @@ public void shouldPublishGitHubCheckRunCorrectly() { new GitHubChecksPublisher(contextBuilder.apply(this), new PluginLogger(j.createTaskListener().getLogger(), "GitHub Checks"), - wireMockRule.baseUrl()) + wireMock.baseUrl()) .publish(details); } } @@ -222,10 +199,11 @@ public void shouldPublishGitHubCheckRunCorrectly() { * If exception happens when publishing checks, it should output all parameters of the check to the system log. */ @Issue("issue-20") - @Test - public void shouldLogChecksParametersIfExceptionHappensWhenPublishChecks() { - try (var credentialsMatchers = mockCredentialsMatchers()) { - loggerRule.record(GitHubChecksPublisher.class.getName(), Level.WARNING).capture(1); + @ParameterizedTest(name = "{0}") + @MethodSource("contextBuilders") + void shouldLogChecksParametersIfExceptionHappensWhenPublishChecks(String contextBuilderName, Function contextBuilder, boolean fromJob) { + try (var credentialsMatchers = mockCredentialsMatchers(); + LogRecorder logRecorder = new LogRecorder().record(GitHubChecksPublisher.class.getName(), Level.WARNING).capture(1)) { ChecksDetails details = new ChecksDetailsBuilder() .withName("Jenkins") @@ -249,11 +227,11 @@ public void shouldLogChecksParametersIfExceptionHappensWhenPublishChecks() { new GitHubChecksPublisher(contextBuilder.apply(this), new PluginLogger(j.createTaskListener().getLogger(), "GitHub Checks"), - wireMockRule.baseUrl()) + wireMock.baseUrl()) .publish(details); - assertThat(loggerRule.getRecords().size()).isEqualTo(1); - assertThat(loggerRule.getMessages().get(0)) + assertThat(logRecorder.getRecords().size()).isEqualTo(1); + assertThat(logRecorder.getMessages().get(0)) .contains("Failed Publishing GitHub checks: ") .contains("name='Jenkins'") .contains("status=COMPLETED") @@ -273,10 +251,11 @@ public void shouldLogChecksParametersIfExceptionHappensWhenPublishChecks() { /** * We can't mock the id field on {@link org.kohsuke.github.GHObject}s thanks to {@link com.infradna.tool.bridge_method_injector.WithBridgeMethods}. * So, create a stub GHCheckRun with the id we want. + * * @param id id of check run to spoof * @return Stubbed {@link GHCheckRun} with only the id of {@link GHCheckRun} set */ - private GHCheckRun createStubCheckRun(final long id) throws JsonProcessingException { + private static GHCheckRun createStubCheckRun(final long id) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(new VisibilityChecker.Std(NONE, NONE, NONE, NONE, ANY)); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); @@ -294,9 +273,10 @@ private GHCheckRun createStubCheckRun(final long id) throws JsonProcessingExcept /** * Test that publishing a second check with the same name will update rather than overwrite the existing check. */ - @Test + @ParameterizedTest(name = "{0}") + @MethodSource("contextBuilders") @SuppressFBWarnings(value = "RCN", justification = "False positive of SpotBugs") - public void testChecksPublisherUpdatesCorrectly() throws Exception { + void testChecksPublisherUpdatesCorrectly(String contextBuilderName, Function contextBuilder, boolean fromJob) throws Exception { GitHub gitHub = mock(GitHub.class); GHRepository repository = mock(GHRepository.class); when(gitHub.getRepository(anyString())).thenReturn(repository); @@ -357,8 +337,7 @@ public void testChecksPublisherUpdatesCorrectly() throws Exception { if (fromJob) { assertThat(context.getId(checksName1)).isNotPresent(); - } - else { + } else { assertThat(context.getId(checksName1)).isPresent().get().isEqualTo(checksId1); } assertThat(context.getId(checksName2)).isNotPresent(); @@ -378,8 +357,7 @@ public void testChecksPublisherUpdatesCorrectly() throws Exception { if (fromJob) { assertThat(context.getId(checksName1)).isNotPresent(); assertThat(context.getId(checksName1)).isNotPresent(); - } - else { + } else { assertThat(context.getId(checksName1)).isPresent().get().isEqualTo(checksId1); assertThat(context.getId(checksName2)).isPresent().get().isEqualTo(checksId2); } @@ -399,28 +377,27 @@ public void testChecksPublisherUpdatesCorrectly() throws Exception { if (fromJob) { assertThat(context.getId(checksName1)).isNotPresent(); assertThat(context.getId(checksName1)).isNotPresent(); - } - else { + } else { assertThat(context.getId(checksName1)).isPresent().get().isEqualTo(checksId1); assertThat(context.getId(checksName2)).isPresent().get().isEqualTo(checksId2); } } } - private GitHubChecksContext createGitHubChecksContextWithGitHubSCMFreestyle() { + private GitHubChecksContext createGitHubChecksContextWithGitHubSCMFreestyle(final boolean fromJob) { try { FreeStyleProject job = j.createFreeStyleProject(); - return createGitHubChecksContextWithGitHubSCM(job); + return createGitHubChecksContextWithGitHubSCM(job, fromJob); } catch (Exception e) { throw new AssertionError(e); } } - private GitHubChecksContext createGitHubChecksContextWithGitHubSCMFromPipeline() { + private GitHubChecksContext createGitHubChecksContextWithGitHubSCMFromPipeline(final boolean fromJob) { try { WorkflowJob job = j.createProject(WorkflowJob.class); job.setDefinition(new CpsFlowDefinition("node {}", true)); - return createGitHubChecksContextWithGitHubSCM(job); + return createGitHubChecksContextWithGitHubSCM(job, fromJob); } catch (Exception e) { throw new AssertionError(e); } @@ -431,7 +408,7 @@ private GitHubChecksContext createGitHubChecksContextWithGitHubSCMFromPipeline() } private & Queue.Executable, J extends Job & ParameterizedJobMixIn.ParameterizedJob> - GitHubChecksContext createGitHubChecksContextWithGitHubSCM(final J job) throws Exception { + GitHubChecksContext createGitHubChecksContextWithGitHubSCM(final J job, final boolean fromJob) throws Exception { Run run = buildSuccessfully(job); SCMFacade scmFacade = mock(SCMFacade.class); diff --git a/src/test/java/io/jenkins/plugins/checks/github/GitHubSCMSourceChecksContextTest.java b/src/test/java/io/jenkins/plugins/checks/github/GitHubSCMSourceChecksContextTest.java index a11ad3f6..4f0547b8 100644 --- a/src/test/java/io/jenkins/plugins/checks/github/GitHubSCMSourceChecksContextTest.java +++ b/src/test/java/io/jenkins/plugins/checks/github/GitHubSCMSourceChecksContextTest.java @@ -1,22 +1,23 @@ package io.jenkins.plugins.checks.github; -import java.util.Optional; - import edu.hm.hafner.util.FilteredLog; +import hudson.model.Job; +import hudson.model.Run; +import jenkins.plugins.git.AbstractGitSCMSource; +import jenkins.scm.api.SCMHead; +import jenkins.scm.api.SCMRevision; import org.jenkinsci.plugins.displayurlapi.ClassicDisplayURLProvider; import org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials; import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; import org.jenkinsci.plugins.github_branch_source.PullRequestSCMRevision; import org.junit.jupiter.api.Test; -import jenkins.plugins.git.AbstractGitSCMSource; -import jenkins.scm.api.SCMHead; -import jenkins.scm.api.SCMRevision; -import static org.assertj.core.api.Assertions.*; -import static org.mockito.Mockito.*; +import java.util.Optional; -import hudson.model.Job; -import hudson.model.Run; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class GitHubSCMSourceChecksContextTest { private static final String URL = "URL"; @@ -29,7 +30,7 @@ void shouldGetHeadShaFromMasterBranch() { GitHubSCMSource source = mock(GitHubSCMSource.class); assertThat(GitHubSCMSourceChecksContext.fromJob(job, URL, - createGitHubSCMFacadeWithRevision(job, source, head, revision, "a1b2c3")) + createGitHubSCMFacadeWithRevision(job, source, head, revision, "a1b2c3")) .getHeadSha()) .isEqualTo("a1b2c3"); } @@ -42,7 +43,7 @@ void shouldGetHeadShaFromPullRequest() { GitHubSCMSource source = mock(GitHubSCMSource.class); assertThat(GitHubSCMSourceChecksContext.fromJob(job, URL, - createGitHubSCMFacadeWithRevision(job, source, head, revision, "a1b2c3")) + createGitHubSCMFacadeWithRevision(job, source, head, revision, "a1b2c3")) .getHeadSha()) .isEqualTo("a1b2c3"); } @@ -58,7 +59,7 @@ void shouldGetHeadShaFromRun() { when(job.getLastBuild()).thenReturn(run); assertThat(GitHubSCMSourceChecksContext.fromRun(run, URL, - createGitHubSCMFacadeWithRevision(run, source, revision, "a1b2c3")) + createGitHubSCMFacadeWithRevision(run, source, revision, "a1b2c3")) .getHeadSha()) .isEqualTo("a1b2c3"); } diff --git a/src/test/java/io/jenkins/plugins/checks/github/GitSCMChecksContextITest.java b/src/test/java/io/jenkins/plugins/checks/github/GitSCMChecksContextITest.java index baa0d509..c5450551 100644 --- a/src/test/java/io/jenkins/plugins/checks/github/GitSCMChecksContextITest.java +++ b/src/test/java/io/jenkins/plugins/checks/github/GitSCMChecksContextITest.java @@ -1,35 +1,32 @@ package io.jenkins.plugins.checks.github; import hudson.model.Action; +import hudson.model.FreeStyleProject; import hudson.model.Result; -import java.util.Collections; - +import hudson.model.Run; +import hudson.plugins.git.BranchSpec; +import hudson.plugins.git.GitSCM; import jenkins.model.ParameterizedJobMixIn; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -import static org.assertj.core.api.Assertions.*; +import java.util.Collections; -import hudson.model.FreeStyleProject; -import hudson.model.Run; -import hudson.plugins.git.BranchSpec; -import hudson.plugins.git.GitSCM; -import org.jvnet.hudson.test.JenkinsRule; +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests for {@link GitSCMChecksContext}. */ -public class GitSCMChecksContextITest { +@WithJenkins +class GitSCMChecksContextITest { private static final String EXISTING_HASH = "4ecc8623b06d99d5f029b66927438554fdd6a467"; private static final String HTTP_URL = "https://github.com/jenkinsci/github-checks-plugin.git"; private static final String CREDENTIALS_ID = "credentials"; private static final String URL_NAME = "url"; - @Rule - public JenkinsRule j = new JenkinsRule(); - /** * Creates a FreeStyle job that uses {@link hudson.plugins.git.GitSCM} and runs a successful build. * Then this build is used to create a new {@link GitSCMChecksContext}. So the build actually is not publishing @@ -37,7 +34,7 @@ public class GitSCMChecksContextITest { * Wiremock to handle the requests to GitHub). */ @Test - public void shouldRetrieveContextFromFreeStyleBuild() throws Exception { + void shouldRetrieveContextFromFreeStyleBuild(JenkinsRule j) throws Exception { FreeStyleProject job = j.createFreeStyleProject(); BranchSpec branchSpec = new BranchSpec(EXISTING_HASH); @@ -46,7 +43,7 @@ public void shouldRetrieveContextFromFreeStyleBuild() throws Exception { null, null, Collections.emptyList()); job.setScm(scm); - Run run = buildSuccessfully(job); + Run run = buildSuccessfully(j, job); GitSCMChecksContext gitSCMChecksContext = new GitSCMChecksContext(run, URL_NAME); @@ -55,7 +52,7 @@ public void shouldRetrieveContextFromFreeStyleBuild() throws Exception { assertThat(gitSCMChecksContext.getCredentialsId()).isEqualTo(CREDENTIALS_ID); } - private Run buildSuccessfully(ParameterizedJobMixIn.ParameterizedJob job) throws Exception { + private Run buildSuccessfully(JenkinsRule j, ParameterizedJobMixIn.ParameterizedJob job) throws Exception { return j.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0, new Action[0])); } @@ -64,7 +61,7 @@ public void shouldRetrieveContextFromFreeStyleBuild() throws Exception { * Then this build is used to create a new {@link GitSCMChecksContext}. */ @Test - public void shouldRetrieveContextFromPipeline() throws Exception { + void shouldRetrieveContextFromPipeline(JenkinsRule j) throws Exception { WorkflowJob job = j.createProject(WorkflowJob.class); job.setDefinition(new CpsFlowDefinition("node {\n" @@ -77,7 +74,7 @@ public void shouldRetrieveContextFromPipeline() throws Exception { + " }\n" + "}\n", true)); - Run run = buildSuccessfully(job); + Run run = buildSuccessfully(j, job); GitSCMChecksContext gitSCMChecksContext = new GitSCMChecksContext(run, URL_NAME); diff --git a/src/test/java/io/jenkins/plugins/checks/github/GitSCMChecksContextTest.java b/src/test/java/io/jenkins/plugins/checks/github/GitSCMChecksContextTest.java index 9da81e51..ead3414b 100644 --- a/src/test/java/io/jenkins/plugins/checks/github/GitSCMChecksContextTest.java +++ b/src/test/java/io/jenkins/plugins/checks/github/GitSCMChecksContextTest.java @@ -8,7 +8,7 @@ class GitSCMChecksContextTest { @Test - public void shouldGetRepository() { + void shouldGetRepository() { for (String url : new String[]{ "git@197.168.2.0:jenkinsci/github-checks-plugin", "git@localhost:jenkinsci/github-checks-plugin", diff --git a/src/test/java/io/jenkins/plugins/checks/github/config/GitHubChecksConfigITest.java b/src/test/java/io/jenkins/plugins/checks/github/config/GitHubChecksConfigITest.java index 0a0c6a6d..dee82fac 100644 --- a/src/test/java/io/jenkins/plugins/checks/github/config/GitHubChecksConfigITest.java +++ b/src/test/java/io/jenkins/plugins/checks/github/config/GitHubChecksConfigITest.java @@ -2,31 +2,28 @@ import hudson.util.StreamTaskListener; import io.jenkins.plugins.checks.github.GitHubChecksPublisherFactory; -import java.io.IOException; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.nio.charset.StandardCharsets; -import org.jvnet.hudson.test.JenkinsRule; - import static org.assertj.core.api.Assertions.assertThat; /** * Integration test for {@link GitHubChecksConfig}. */ -public class GitHubChecksConfigITest { - - @Rule - public JenkinsRule j = new JenkinsRule(); +@WithJenkins +class GitHubChecksConfigITest { /** * When a job has not {@link org.jenkinsci.plugins.github_branch_source.GitHubSCMSource} or * {@link hudson.plugins.git.GitSCM}, the default config should be used and no verbose log should be output. */ @Test - public void shouldUseDefaultConfigWhenNoSCM() throws IOException { + void shouldUseDefaultConfigWhenNoSCM(JenkinsRule j) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); GitHubChecksPublisherFactory.fromJob(j.createFreeStyleProject(), new StreamTaskListener(os, StandardCharsets.UTF_8)); diff --git a/src/test/java/io/jenkins/plugins/checks/github/status/GitHubSCMSourceStatusChecksTraitTest.java b/src/test/java/io/jenkins/plugins/checks/github/status/GitHubSCMSourceStatusChecksTraitTest.java index 4cd2721a..b0d58239 100644 --- a/src/test/java/io/jenkins/plugins/checks/github/status/GitHubSCMSourceStatusChecksTraitTest.java +++ b/src/test/java/io/jenkins/plugins/checks/github/status/GitHubSCMSourceStatusChecksTraitTest.java @@ -9,6 +9,7 @@ import static org.mockito.Mockito.mock; class GitHubSCMSourceStatusChecksTraitTest { + @Test void shouldOnlyApplyTraitConfigurationsToGitHubBranchSourceNotificationsWhenItsNotDisabled() { GitHubSCMSourceContext context = new GitHubSCMSourceContext(mock(SCMSourceCriteria.class), diff --git a/src/test/java/io/jenkins/plugins/checks/github/status/GitHubStatusChecksPropertiesTest.java b/src/test/java/io/jenkins/plugins/checks/github/status/GitHubStatusChecksPropertiesTest.java index 869d9be1..30a7e79a 100644 --- a/src/test/java/io/jenkins/plugins/checks/github/status/GitHubStatusChecksPropertiesTest.java +++ b/src/test/java/io/jenkins/plugins/checks/github/status/GitHubStatusChecksPropertiesTest.java @@ -15,6 +15,7 @@ import static org.mockito.Mockito.when; class GitHubStatusChecksPropertiesTest { + @Test void shouldUsePropertiesFromGitHubSCMSourceTrait() { Job job = mock(Job.class); @@ -91,10 +92,10 @@ void shouldNotApplicableToJobWithoutSupportedSCM() { false, "Jenkins", false, false, false); } - private void assertJobWithStatusChecksProperties(final Job job, final GitHubStatusChecksProperties properties, - final boolean isApplicable, final String name, - final boolean isSkip, final boolean isUnstableBuildNeutral, - final boolean isSuppressLogs) { + private static void assertJobWithStatusChecksProperties(final Job job, final GitHubStatusChecksProperties properties, + final boolean isApplicable, final String name, + final boolean isSkip, final boolean isUnstableBuildNeutral, + final boolean isSuppressLogs) { assertThat(properties.isApplicable(job)).isEqualTo(isApplicable); assertThat(properties.getName(job)).isEqualTo(name); assertThat(properties.isSkipped(job)).isEqualTo(isSkip);