Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,41 @@
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
* the checks we just ensure that we can create the context with the successful build (otherwise we would need
* 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(
Expand All @@ -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);

Expand All @@ -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"
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.api.Test;

class GiteaChecksDetailsTest {

@Test
void shouldReturnAllGiteaObjectsCorrectly() {
ChecksDetails details = new ChecksDetailsBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,113 +24,83 @@
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;
import org.jenkinsci.plugin.gitea.PullRequestSCMRevision;
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<Object[]> contextBuilders() {
return Arrays.asList(new Object[][] {
{
"Freestyle (run)",
(Function<GiteaChecksPublisherITest, GiteaChecksContext>)
GiteaChecksPublisherITest::createGiteaChecksContextWithGiteaSCMFreestyle,
false
},
{
"Freestyle (job)",
(Function<GiteaChecksPublisherITest, GiteaChecksContext>)
GiteaChecksPublisherITest::createGiteaChecksContextWithGiteaSCMFreestyle,
true
},
{
"Pipeline (run)",
(Function<GiteaChecksPublisherITest, GiteaChecksContext>)
GiteaChecksPublisherITest::createGiteaChecksContextWithGiteaSCMFromPipeline,
false
},
{
"Pipeline (job)",
(Function<GiteaChecksPublisherITest, GiteaChecksContext>)
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<GiteaChecksPublisherITest, GiteaChecksContext> 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<Arguments> contextBuilders() {
return Stream.of(
Arguments.of("Freestyle (run)", (Supplier<GiteaChecksContext>)
() -> createGiteaChecksContextWithGiteaSCMFreestyle(false)),
Arguments.of("Freestyle (job)", (Supplier<GiteaChecksContext>)
() -> createGiteaChecksContextWithGiteaSCMFreestyle(true)),
Arguments.of("Pipeline (run)", (Supplier<GiteaChecksContext>)
() -> createGiteaChecksContextWithGiteaSCMFromPipeline(false)),
Arguments.of("Pipeline (job)", (Supplier<GiteaChecksContext>)
() -> 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<GiteaChecksContext> contextBuilder) {
ChecksDetails details = new ChecksDetailsBuilder()
.withName("Jenkins")
.withStatus(ChecksStatus.COMPLETED)
Expand Down Expand Up @@ -174,19 +142,24 @@
.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<GiteaChecksContext> contextBuilder) {
logging.record(GiteaChecksPublisher.class.getName(), Level.WARNING).capture(1);

ChecksDetails details = new ChecksDetailsBuilder()
.withName("Jenkins")
Expand All @@ -208,13 +181,13 @@
.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")
Expand All @@ -230,20 +203,25 @@
.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 <R extends Run<J, R> & Queue.Executable, J extends Job<J, R> & ParameterizedJobMixIn.ParameterizedJob<J, R>>

Check warning on line 221 in src/test/java/io/jenkins/plugins/checks/gitea/GiteaChecksPublisherITest.java

View check run for this annotation

ci.jenkins.io / Java Compiler

compiler:testCompile

NORMAL: getParent() in hudson.model.AbstractItem implements getParent() in hudson.model.Item
GiteaChecksContext createGiteaChecksContextWithGiteaSCM(final J job) {
Run run = buildSuccessfully(job);
GiteaChecksContext createGiteaChecksContextWithGiteaSCM(final J job, final boolean fromJob)
throws Exception {
Run<J, R> run = r.buildAndAssertSuccess(job);

SCMFacade scmFacade = mock(SCMFacade.class);
GiteaSCMSource source = mock(GiteaSCMSource.class);
Expand Down
Loading