Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate tests to JUnit5 #474

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,49 +1,42 @@
package io.jenkins.plugins.gitlabbranchsource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer;
import java.util.Arrays;
import jenkins.model.JenkinsLocationConfiguration;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

@RunWith(Parameterized.class)
public class GitLabHookCreatorParameterizedTest {
@WithJenkins
class GitLabHookCreatorParameterizedTest {

@ClassRule
public static JenkinsRule r = new JenkinsRule();
private static JenkinsRule r;

private final String jenkinsUrl;
private final boolean hookType;
private final String expectedPath;
@BeforeAll
static void setUp(JenkinsRule rule) {
r = rule;
}

@Parameters(name = "check {0}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] {
static Object[][] data() {
return new Object[][] {
{"intranet.local:8080", false, "/gitlab-systemhook/post"},
{"intranet.local", true, "/gitlab-webhook/post"},
{"www.mydomain.com:8000", true, "/gitlab-webhook/post"},
{"www.mydomain.com", false, "/gitlab-systemhook/post"},
{"www.mydomain.com/jenkins", true, "/gitlab-webhook/post"}
});
}

public GitLabHookCreatorParameterizedTest(String jenkinsUrl, boolean hookType, String expectedPath) {
this.jenkinsUrl = jenkinsUrl;
this.hookType = hookType;
this.expectedPath = expectedPath;
};
}

@Test
public void hookUrl() {
@ParameterizedTest
@MethodSource("data")
void hookUrl(String jenkinsUrl, boolean hookType, String expectedPath) {
Arrays.asList("http://", "https://").forEach(proto -> {
String expected = proto + jenkinsUrl + expectedPath;
JenkinsLocationConfiguration.get().setUrl(proto + jenkinsUrl);
Expand All @@ -54,8 +47,9 @@ public void hookUrl() {
});
}

@Test
public void hookUrlFromCustomRootUrl() {
@ParameterizedTest
@MethodSource("data")
void hookUrlFromCustomRootUrl(String jenkinsUrl, boolean hookType, String expectedPath) {
Arrays.asList("http://", "https://").forEach(proto -> {
String expected = proto + jenkinsUrl + expectedPath;
JenkinsLocationConfiguration.get().setUrl("http://whatever");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,61 @@
package io.jenkins.plugins.gitlabbranchsource;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.lang.reflect.Field;
import jenkins.branch.BranchSource;
import jenkins.scm.api.SCMSource;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.RestartableJenkinsRule;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

public class GitLabSCMSourceDeserializationTest {
@WithJenkins
class GitLabSCMSourceDeserializationTest {

private static final String PROJECT_NAME = "project";
private static final String SOURCE_ID = "id";

@Rule
public final RestartableJenkinsRule plan = new RestartableJenkinsRule();

@Test
public void afterRestartingJenkinsTransientFieldsAreNotNull() throws Exception {
plan.then(j -> {
GitLabSCMSourceBuilder sb =
new GitLabSCMSourceBuilder(SOURCE_ID, "server", "creds", "po", "group/project", "project");
WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME);
project.getSourcesList().add(new BranchSource(sb.build()));
});

plan.then(j -> {
SCMSource source = j.getInstance().getAllItems(WorkflowMultiBranchProject.class).stream()
.filter(p -> PROJECT_NAME.equals(p.getName()))
.map(p -> p.getSCMSource(SOURCE_ID))
.findFirst()
.get();

Class<? extends SCMSource> clazz = source.getClass();
Field mergeRequestContributorCache = clazz.getDeclaredField("mergeRequestContributorCache");
mergeRequestContributorCache.setAccessible(true);
Field mergeRequestMetadataCache = clazz.getDeclaredField("mergeRequestMetadataCache");
mergeRequestMetadataCache.setAccessible(true);
assertNotNull(mergeRequestMetadataCache.get(source));
assertNotNull(mergeRequestContributorCache.get(source));
});
void afterRestartingJenkinsTransientFieldsAreNotNull(JenkinsRule j) throws Throwable {
GitLabSCMSourceBuilder sb =
new GitLabSCMSourceBuilder(SOURCE_ID, "server", "creds", "po", "group/project", "project");
WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME);
project.getSourcesList().add(new BranchSource(sb.build()));

j.restart();

SCMSource source = j.getInstance().getAllItems(WorkflowMultiBranchProject.class).stream()
.filter(p -> PROJECT_NAME.equals(p.getName()))
.map(p -> p.getSCMSource(SOURCE_ID))
.findFirst()
.orElseThrow();

Class<? extends SCMSource> clazz = source.getClass();
Field mergeRequestContributorCache = clazz.getDeclaredField("mergeRequestContributorCache");
mergeRequestContributorCache.setAccessible(true);
Field mergeRequestMetadataCache = clazz.getDeclaredField("mergeRequestMetadataCache");
mergeRequestMetadataCache.setAccessible(true);
assertNotNull(mergeRequestMetadataCache.get(source));
assertNotNull(mergeRequestContributorCache.get(source));
}

@Test
public void projectIdSurvivesConfigRoundtrip() {
plan.then(j -> {
GitLabSCMSourceBuilder sb =
new GitLabSCMSourceBuilder(SOURCE_ID, "server", "creds", "po", "group/project", "project");
WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME);
GitLabSCMSource source = sb.build();
project.getSourcesList().add(new BranchSource(source));
long p = 42;
source.setProjectId(p);
j.configRoundtrip(project);

WorkflowMultiBranchProject item =
j.jenkins.getItemByFullName(PROJECT_NAME, WorkflowMultiBranchProject.class);
assertNotNull(item);
GitLabSCMSource scmSource = (GitLabSCMSource) item.getSCMSource(SOURCE_ID);
assertNotNull(scmSource);
assertEquals(Long.valueOf(p), scmSource.getProjectId());
});
void projectIdSurvivesConfigRoundtrip(JenkinsRule j) throws Exception {
GitLabSCMSourceBuilder sb =
new GitLabSCMSourceBuilder(SOURCE_ID, "server", "creds", "po", "group/project", "project");
WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME);
GitLabSCMSource source = sb.build();
project.getSourcesList().add(new BranchSource(source));
long p = 42;
source.setProjectId(p);
j.configRoundtrip(project);

WorkflowMultiBranchProject item = j.jenkins.getItemByFullName(PROJECT_NAME, WorkflowMultiBranchProject.class);
assertNotNull(item);
GitLabSCMSource scmSource = (GitLabSCMSource) item.getSCMSource(SOURCE_ID);
assertNotNull(scmSource);
assertEquals(Long.valueOf(p), scmSource.getProjectId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,29 @@
import org.gitlab4j.api.RepositoryApi;
import org.gitlab4j.api.models.Project;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

public class GitLabSCMSourceTest {
@WithJenkins
class GitLabSCMSourceTest {

private static final String SERVER = "server";
private static final String PROJECT_NAME = "project";
private static final String SOURCE_ID = "id";

@ClassRule
public static JenkinsRule j = new JenkinsRule();
private static JenkinsRule j;

@BeforeAll
static void setUp(JenkinsRule rule) {
j = rule;
}

@Test
public void retrieveMRWithEmptyProjectSettings() throws GitLabApiException, IOException, InterruptedException {
void retrieveMRWithEmptyProjectSettings() throws GitLabApiException, IOException, InterruptedException {
GitLabApi gitLabApi = Mockito.mock(GitLabApi.class);
ProjectApi projectApi = Mockito.mock(ProjectApi.class);
RepositoryApi repoApi = Mockito.mock(RepositoryApi.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import static org.hamcrest.Matchers.is;

import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class GitLabHelperTest {
class GitLabHelperTest {

@Test
public void server_url_does_not_have_trailing_slash() {
void server_url_does_not_have_trailing_slash() {
assertThat(GitLabHelper.getServerUrl(null), is("https://gitlab.com"));

GitLabServer server1 = new GitLabServer("https://company.com/gitlab/", "comp_server", "1245");
Expand Down
Loading
Loading