diff --git a/pom.xml b/pom.xml index 4c208e6..db761b9 100644 --- a/pom.xml +++ b/pom.xml @@ -137,19 +137,6 @@ test - - org.testcontainers - testcontainers - ${testcontainers.version} - test - - - org.apache.commons - commons-compress - - - - org.wiremock wiremock-standalone diff --git a/src/test/java/io/jenkins/plugins/checks/IntegrationTestBase.java b/src/test/java/io/jenkins/plugins/checks/IntegrationTestBase.java deleted file mode 100644 index a79ddd5..0000000 --- a/src/test/java/io/jenkins/plugins/checks/IntegrationTestBase.java +++ /dev/null @@ -1,21 +0,0 @@ -package io.jenkins.plugins.checks; - -import io.jenkins.plugins.util.IntegrationTest; -import org.junit.Rule; -import org.jvnet.hudson.test.JenkinsRule; - -/** - * Base class for integration tests. - */ -public abstract class IntegrationTestBase extends IntegrationTest { - /** - * Provide a jenkins rule. - */ - @Rule - public JenkinsRule r = new JenkinsRule(); - - @Override - protected JenkinsRule getJenkins() { - return r; - } -} diff --git a/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextITest.java b/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextITest.java index 3ca59ed..997ed15 100644 --- a/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextITest.java +++ b/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextITest.java @@ -7,22 +7,32 @@ import hudson.model.Run; import hudson.plugins.git.BranchSpec; import hudson.plugins.git.GitSCM; -import io.jenkins.plugins.checks.IntegrationTestBase; -import java.io.IOException; import java.util.Collections; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; -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; /** * Integration tests for {@link GitSCMChecksContext}. */ -public class GitSCMChecksContextITest extends IntegrationTestBase { +@WithJenkins +class GitSCMChecksContextITest { + private static final String EXISTING_HASH = "4ecc8623b06d99d5f029b66927438554fdd6a467"; private static final String HTTP_URL = "https://github.com/jenkinsci/gitea-checks-plugin.git"; private static final String CREDENTIALS_ID = "credentials"; private static final String URL_NAME = "url"; + private JenkinsRule r; + + @BeforeEach + void setUp(final JenkinsRule rule) { + r = rule; + } + /** * 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 @@ -30,8 +40,8 @@ public class GitSCMChecksContextITest extends IntegrationTestBase { * Wiremock to handle the requests to Gitea). */ @Test - public void shouldRetrieveContextFromFreeStyleBuild() throws IOException { - FreeStyleProject job = createFreeStyleProject(); + void shouldRetrieveContextFromFreeStyleBuild() throws Exception { + FreeStyleProject job = r.createFreeStyleProject(); BranchSpec branchSpec = new BranchSpec(EXISTING_HASH); GitSCM scm = new GitSCM( @@ -42,7 +52,7 @@ public void shouldRetrieveContextFromFreeStyleBuild() throws IOException { Collections.emptyList()); job.setScm(scm); - Run run = buildSuccessfully(job); + Run run = r.buildAndAssertSuccess(job); GitSCMChecksContext gitSCMChecksContext = new GitSCMChecksContext(run, URL_NAME); @@ -56,8 +66,8 @@ public void shouldRetrieveContextFromFreeStyleBuild() throws IOException { * Then this build is used to create a new {@link GitSCMChecksContext}. */ @Test - public void shouldRetrieveContextFromPipeline() { - WorkflowJob job = createPipeline(); + void shouldRetrieveContextFromPipeline() throws Exception { + WorkflowJob job = r.createProject(WorkflowJob.class); assertDoesNotThrow(() -> job.setDefinition(new CpsFlowDefinition( "node {\n" @@ -72,7 +82,7 @@ public void shouldRetrieveContextFromPipeline() { + "}\n", true))); - Run run = buildSuccessfully(job); + Run run = r.buildAndAssertSuccess(job); GitSCMChecksContext gitSCMChecksContext = new GitSCMChecksContext(run, URL_NAME); diff --git a/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextTest.java b/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextTest.java index 8118212..6c9ebcb 100644 --- a/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextTest.java +++ b/src/test/java/io/jenkins/plugins/checks/gitea/GitSCMChecksContextTest.java @@ -7,8 +7,9 @@ import org.junit.jupiter.api.Test; class GitSCMChecksContextTest { + @Test - public void shouldGetRepository() { + void shouldGetRepository() { for (String url : new String[] { "git@197.168.2.0:jenkinsci/gitea-checks-plugin", "git@localhost:jenkinsci/gitea-checks-plugin", diff --git a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaChecksDetailsTest.java b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaChecksDetailsTest.java index 3be871b..11270f6 100644 --- a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaChecksDetailsTest.java +++ b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaChecksDetailsTest.java @@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test; class GiteaChecksDetailsTest { + @Test void shouldReturnAllGiteaObjectsCorrectly() { ChecksDetails details = new ChecksDetailsBuilder() diff --git a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaChecksPublisherITest.java b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaChecksPublisherITest.java index 66b4a3d..567af8d 100644 --- a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaChecksPublisherITest.java +++ b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaChecksPublisherITest.java @@ -6,13 +6,11 @@ import com.cloudbees.plugins.credentials.common.StandardCredentials; import com.github.tomakehurst.wiremock.core.WireMockConfiguration; -import com.github.tomakehurst.wiremock.junit.WireMockRule; -import edu.umd.cs.findbugs.annotations.CheckForNull; +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; import hudson.model.FreeStyleProject; import hudson.model.Job; import hudson.model.Queue; import hudson.model.Run; -import io.jenkins.plugins.checks.IntegrationTestBase; import io.jenkins.plugins.checks.api.ChecksAction; import io.jenkins.plugins.checks.api.ChecksAnnotation.ChecksAnnotationBuilder; import io.jenkins.plugins.checks.api.ChecksAnnotation.ChecksAnnotationLevel; @@ -26,11 +24,11 @@ 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.function.Supplier; import java.util.logging.Level; +import java.util.stream.Stream; import jenkins.model.ParameterizedJobMixIn; import jenkins.scm.api.SCMHead; import org.jenkinsci.plugin.gitea.GiteaSCMSource; @@ -38,101 +36,71 @@ import org.jenkinsci.plugins.displayurlapi.ClassicDisplayURLProvider; 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.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.jvnet.hudson.test.Issue; -import org.jvnet.hudson.test.LoggerRule; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.LogRecorder; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; /** * Tests if the {@link GiteaChecksPublisher} actually sends out the requests to * Gitea in order to publish the check runs. */ -@RunWith(Parameterized.class) -@SuppressWarnings({ - "PMD.ExcessiveImports", - "checkstyle:ClassDataAbstractionCoupling", - "rawtypes", - "checkstyle:ClassFanOutComplexity", - "checkstyle:JavaNCSS" -}) -public class GiteaChecksPublisherITest extends IntegrationTestBase { - /** - * Provides parameters for tests. - * @return A list of methods used to create GiteaChecksContexts, with which each test should be run. - */ - @Parameterized.Parameters(name = "{0}") - @SuppressWarnings("PMD.UnnecessaryVarargsArrayCreation") // TODO: fix me? - public static Collection contextBuilders() { - return Arrays.asList(new Object[][] { - { - "Freestyle (run)", - (Function) - GiteaChecksPublisherITest::createGiteaChecksContextWithGiteaSCMFreestyle, - false - }, - { - "Freestyle (job)", - (Function) - GiteaChecksPublisherITest::createGiteaChecksContextWithGiteaSCMFreestyle, - true - }, - { - "Pipeline (run)", - (Function) - GiteaChecksPublisherITest::createGiteaChecksContextWithGiteaSCMFromPipeline, - false - }, - { - "Pipeline (job)", - (Function) - GiteaChecksPublisherITest::createGiteaChecksContextWithGiteaSCMFromPipeline, - true - } - }); - } +@WithJenkins +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class GiteaChecksPublisherITest { /** - * Human readable name of the context builder - used only for test name formatting. + * Recorder for the log system. */ - @SuppressWarnings("checkstyle:VisibilityModifier") - @Parameterized.Parameter(0) - @CheckForNull - public String contextBuilderName; + private final LogRecorder logging = new LogRecorder(); /** - * Reference to method used to create GiteaChecksContext with either a pipeline or freestyle job. + * Provides a mock server. */ - @SuppressWarnings("checkstyle:VisibilityModifier") - @Parameterized.Parameter(1) - public Function contextBuilder; + @RegisterExtension + private static WireMockExtension wireMock = WireMockExtension.newInstance() + .options(WireMockConfiguration.options().dynamicPort()) + .build(); - /** - * Create GiteaChecksContext from the job instead of the run. - */ - @SuppressWarnings("checkstyle:VisibilityModifier") - @Parameterized.Parameter(2) - public boolean fromJob; + private JenkinsRule r; - /** - * Rule for the log system. - */ - @Rule - public LoggerRule loggerRule = new LoggerRule(); + @BeforeEach + void setUp(final JenkinsRule rule) { + r = rule; + } /** - * A rule which provides a mock server. + * Provides parameters for tests. + * @return A list of methods used to create GiteaChecksContexts, with which each test should be run. */ - @Rule - public WireMockRule wireMockRule = - new WireMockRule(WireMockConfiguration.options().dynamicPort()); + Stream contextBuilders() { + return Stream.of( + Arguments.of("Freestyle (run)", (Supplier) + () -> createGiteaChecksContextWithGiteaSCMFreestyle(false)), + Arguments.of("Freestyle (job)", (Supplier) + () -> createGiteaChecksContextWithGiteaSCMFreestyle(true)), + Arguments.of("Pipeline (run)", (Supplier) + () -> createGiteaChecksContextWithGiteaSCMFromPipeline(false)), + Arguments.of("Pipeline (job)", (Supplier) + () -> createGiteaChecksContextWithGiteaSCMFromPipeline(true))); + } /** * Checks should be published to Gitea correctly when Gitea SCM is found and parameters are correctly set. + * + * @param contextBuilderName Human-readable name of the context builder - used only for test name formatting. + * @param contextBuilder Reference to the method used to create GiteaChecksContext with either a pipeline or freestyle job. */ - @Test - public void shouldPublishGiteaCheckRunCorrectly() { + @ParameterizedTest(name = "{0}") + @MethodSource("contextBuilders") + void shouldPublishGiteaCheckRunCorrectly( + final String contextBuilderName, final Supplier contextBuilder) { ChecksDetails details = new ChecksDetailsBuilder() .withName("Jenkins") .withStatus(ChecksStatus.COMPLETED) @@ -174,19 +142,24 @@ public void shouldPublishGiteaCheckRunCorrectly() { .build(); new GiteaChecksPublisher( - contextBuilder.apply(this), - new PluginLogger(getJenkins().createTaskListener().getLogger(), "Gitea Checks"), - wireMockRule.baseUrl()) + contextBuilder.get(), + new PluginLogger(r.createTaskListener().getLogger(), "Gitea Checks"), + wireMock.baseUrl()) .publish(details); } /** - * If exception happens when publishing checks, it should output all parameters of the check to the system log. + * If an exception happens when publishing checks, it should output all parameters of the check to the system log. + * + * @param contextBuilderName Human-readable name of the context builder - used only for test name formatting. + * @param contextBuilder Reference to the method used to create GiteaChecksContext with either a pipeline or freestyle job. */ @Issue("issue-20") - @Test - public void shouldLogChecksParametersIfExceptionHappensWhenPublishChecks() { - loggerRule.record(GiteaChecksPublisher.class.getName(), Level.WARNING).capture(1); + @ParameterizedTest(name = "{0}") + @MethodSource("contextBuilders") + void shouldLogChecksParametersIfExceptionHappensWhenPublishChecks( + final String contextBuilderName, final Supplier contextBuilder) { + logging.record(GiteaChecksPublisher.class.getName(), Level.WARNING).capture(1); ChecksDetails details = new ChecksDetailsBuilder() .withName("Jenkins") @@ -208,13 +181,13 @@ public void shouldLogChecksParametersIfExceptionHappensWhenPublishChecks() { .build(); new GiteaChecksPublisher( - contextBuilder.apply(this), - new PluginLogger(getJenkins().createTaskListener().getLogger(), "Gitea Checks"), - wireMockRule.baseUrl()) + contextBuilder.get(), + new PluginLogger(r.createTaskListener().getLogger(), "Gitea Checks"), + wireMock.baseUrl()) .publish(details); - assertThat(loggerRule.getRecords().size()).isEqualTo(1); - assertThat(loggerRule.getMessages().get(0)) + assertThat(logging.getRecords()).hasSize(1); + assertThat(logging.getMessages().get(0)) .contains("Failed Publishing Gitea checks: ") .contains("name='Jenkins'") .contains("status=COMPLETED") @@ -230,20 +203,25 @@ public void shouldLogChecksParametersIfExceptionHappensWhenPublishChecks() { .contains("message='say hello to Jenkins'"); } - private GiteaChecksContext createGiteaChecksContextWithGiteaSCMFreestyle() { - FreeStyleProject job = createFreeStyleProject(); - return createGiteaChecksContextWithGiteaSCM(job); + private GiteaChecksContext createGiteaChecksContextWithGiteaSCMFreestyle(final boolean fromJob) { + return assertDoesNotThrow(() -> { + FreeStyleProject job = r.createFreeStyleProject(); + return createGiteaChecksContextWithGiteaSCM(job, fromJob); + }); } - private GiteaChecksContext createGiteaChecksContextWithGiteaSCMFromPipeline() { - WorkflowJob job = createPipeline(); - assertDoesNotThrow(() -> job.setDefinition(new CpsFlowDefinition("node {}", true))); - return createGiteaChecksContextWithGiteaSCM(job); + private GiteaChecksContext createGiteaChecksContextWithGiteaSCMFromPipeline(final boolean fromJob) { + return assertDoesNotThrow(() -> { + WorkflowJob job = r.createProject(WorkflowJob.class); + job.setDefinition(new CpsFlowDefinition("node {}", true)); + return createGiteaChecksContextWithGiteaSCM(job, fromJob); + }); } private & Queue.Executable, J extends Job & ParameterizedJobMixIn.ParameterizedJob> - GiteaChecksContext createGiteaChecksContextWithGiteaSCM(final J job) { - Run run = buildSuccessfully(job); + GiteaChecksContext createGiteaChecksContextWithGiteaSCM(final J job, final boolean fromJob) + throws Exception { + Run run = r.buildAndAssertSuccess(job); SCMFacade scmFacade = mock(SCMFacade.class); GiteaSCMSource source = mock(GiteaSCMSource.class); diff --git a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaPublisherFactoryTest.java b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaPublisherFactoryTest.java index 4f98bfc..cbf2d29 100644 --- a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaPublisherFactoryTest.java +++ b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaPublisherFactoryTest.java @@ -19,6 +19,7 @@ import org.junit.jupiter.api.Test; class GiteaPublisherFactoryTest { + @Test void shouldCreateGiteaChecksPublisherFromRunForProjectWithValidGiteaSCMSource() { var run = mock(Run.class); diff --git a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaSCMSourceChecksContextTest.java b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaSCMSourceChecksContextTest.java index 19d47f7..eb2aa4e 100644 --- a/src/test/java/io/jenkins/plugins/checks/gitea/GiteaSCMSourceChecksContextTest.java +++ b/src/test/java/io/jenkins/plugins/checks/gitea/GiteaSCMSourceChecksContextTest.java @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Test; class GiteaSCMSourceChecksContextTest { + private static final String URL = "URL"; @Test