diff --git a/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabHookCreatorParameterizedTest.java b/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabHookCreatorParameterizedTest.java index f4f02168..5068ad46 100644 --- a/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabHookCreatorParameterizedTest.java +++ b/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabHookCreatorParameterizedTest.java @@ -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 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); @@ -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"); diff --git a/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java b/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java index 4326b01e..248addb5 100644 --- a/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java +++ b/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java @@ -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 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 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()); } } diff --git a/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceTest.java b/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceTest.java index 9c261aaf..73a35c27 100644 --- a/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceTest.java +++ b/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceTest.java @@ -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); diff --git a/src/test/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelperTest.java b/src/test/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelperTest.java index e8330b03..6ceedad0 100644 --- a/src/test/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelperTest.java +++ b/src/test/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelperTest.java @@ -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"); diff --git a/src/test/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabPipelineStatusNotifierTest.java b/src/test/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabPipelineStatusNotifierTest.java index 11dc9760..e739a99e 100644 --- a/src/test/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabPipelineStatusNotifierTest.java +++ b/src/test/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabPipelineStatusNotifierTest.java @@ -20,13 +20,15 @@ import org.gitlab4j.api.GitLabApi; import org.gitlab4j.api.MergeRequestApi; import org.gitlab4j.api.models.MergeRequest; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mockito; -public class GitLabPipelineStatusNotifierTest { +class GitLabPipelineStatusNotifierTest { @Test - public void should_set_branch_status_name() { + void should_set_branch_status_name() { GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(null, null); BranchSCMHead head = new BranchSCMHead("head"); @@ -43,7 +45,7 @@ public void should_set_branch_status_name() { } @Test - public void should_set_branch_status_name_withBuildStatusNameCustomPart() { + void should_set_branch_status_name_withBuildStatusNameCustomPart() { GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(null, null); sourceContext.withBuildStatusNameCustomPart("CUSTOM"); sourceContext.withBuildStatusNameOverwrite(false); @@ -64,7 +66,7 @@ public void should_set_branch_status_name_withBuildStatusNameCustomPart() { } @Test - public void should_set_branch_status_name_withIgnoreTypeInStatusName() { + void should_set_branch_status_name_withIgnoreTypeInStatusName() { GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(null, null); sourceContext.withIgnoreTypeInStatusName(true); @@ -78,7 +80,7 @@ public void should_set_branch_status_name_withIgnoreTypeInStatusName() { } @Test - public void should_set_merge_request_head_status_name() { + void should_set_merge_request_head_status_name() { GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(null, null); BranchSCMHead targetHead = new BranchSCMHead("target"); @@ -100,7 +102,7 @@ public void should_set_merge_request_head_status_name() { } @Test - public void should_set_merge_request_merge_status_name() { + void should_set_merge_request_merge_status_name() { GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(null, null); BranchSCMHead targetHead = new BranchSCMHead("target"); @@ -122,7 +124,7 @@ public void should_set_merge_request_merge_status_name() { } @Test - public void should_set_tag_status_name() { + void should_set_tag_status_name() { GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(null, null); GitTagSCMHead head = new GitTagSCMHead("tagName", 0); @@ -139,7 +141,7 @@ public void should_set_tag_status_name() { } @Test - public void should_set_branch_ref_name() { + void should_set_branch_ref_name() { String branchName = "branch_name"; BranchSCMHead head = new BranchSCMHead(branchName); SCMRevision revision = new BranchSCMRevision(head, "hash"); @@ -150,7 +152,7 @@ public void should_set_branch_ref_name() { } @Test - public void should_set_merge_request_ref_name() { + void should_set_merge_request_ref_name() { String sourceBranchName = "sourceBranchName"; String targetBranchName = "targetBranchName"; @@ -167,7 +169,7 @@ public void should_set_merge_request_ref_name() { } @Test - public void should_set_tag_ref_name() { + void should_set_tag_ref_name() { String tagName = "tagName"; GitTagSCMHead head = new GitTagSCMHead(tagName, 0); SCMRevision revision = new GitTagSCMRevision(head, "tag-hash"); @@ -177,61 +179,16 @@ public void should_set_tag_ref_name() { assertThat(refName, is(tagName)); } - @Test - public void should_get_mr_project_id() throws Exception { - String projectPath = "project_path"; - Long projectId = Long.valueOf(100); - - ItemGroup parent = Mockito.mock(ItemGroup.class); - Mockito.when(parent.getFullName()).thenReturn("folder/project"); - - Job job = new FreeStyleProject(parent, "MR-123"); - - GitLabApi gitLabApi = Mockito.mock(GitLabApi.class); - MergeRequestApi mrApi = Mockito.mock(MergeRequestApi.class); - MergeRequest mr = Mockito.mock(MergeRequest.class); - - Mockito.when(gitLabApi.getMergeRequestApi()).thenReturn(mrApi); - Mockito.when(mrApi.getMergeRequest(any(), eq(Long.valueOf(123)))).thenReturn(mr); - Mockito.when(mr.getSourceProjectId()).thenReturn(projectId); - - Long sourceProjectId = GitLabPipelineStatusNotifier.getSourceProjectId(job, gitLabApi, projectPath); - - assertThat(sourceProjectId, is(projectId)); - } - - @Test - public void should_get_mr_project_id_projects_using_both_strategy_head() throws Exception { - String projectPath = "project_path"; - Long projectId = Long.valueOf(100); - - ItemGroup parent = Mockito.mock(ItemGroup.class); - Mockito.when(parent.getFullName()).thenReturn("folder/project"); - - Job job = new FreeStyleProject(parent, "MR-123-head"); - - GitLabApi gitLabApi = Mockito.mock(GitLabApi.class); - MergeRequestApi mrApi = Mockito.mock(MergeRequestApi.class); - MergeRequest mr = Mockito.mock(MergeRequest.class); - - Mockito.when(gitLabApi.getMergeRequestApi()).thenReturn(mrApi); - Mockito.when(mrApi.getMergeRequest(any(), eq(Long.valueOf(123)))).thenReturn(mr); - Mockito.when(mr.getSourceProjectId()).thenReturn(projectId); - - Long sourceProjectId = GitLabPipelineStatusNotifier.getSourceProjectId(job, gitLabApi, projectPath); - - assertThat(sourceProjectId, is(projectId)); - } - - @Test - public void should_get_mr_project_id_projects_using_both_strategy_merge() throws Exception { + @ParameterizedTest + @ValueSource(strings = {"MR-123", "MR-123-head", "MR-123-merge"}) + void should_get_mr_project_id(String name) throws Exception { String projectPath = "project_path"; - Long projectId = Long.valueOf(100); + Long projectId = 100L; ItemGroup parent = Mockito.mock(ItemGroup.class); Mockito.when(parent.getFullName()).thenReturn("folder/project"); - Job job = new FreeStyleProject(parent, "MR-123-merge"); + Job job = new FreeStyleProject(parent, name); GitLabApi gitLabApi = Mockito.mock(GitLabApi.class); MergeRequestApi mrApi = Mockito.mock(MergeRequestApi.class); diff --git a/src/test/java/io/jenkins/plugins/gitlabserverconfig/casc/ConfigurationAsCodeTest.java b/src/test/java/io/jenkins/plugins/gitlabserverconfig/casc/ConfigurationAsCodeTest.java index b82ad456..fb61e89c 100644 --- a/src/test/java/io/jenkins/plugins/gitlabserverconfig/casc/ConfigurationAsCodeTest.java +++ b/src/test/java/io/jenkins/plugins/gitlabserverconfig/casc/ConfigurationAsCodeTest.java @@ -15,23 +15,21 @@ import io.jenkins.plugins.casc.ConfiguratorRegistry; import io.jenkins.plugins.casc.misc.ConfiguredWithCode; import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule; +import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode; import io.jenkins.plugins.casc.model.CNode; import io.jenkins.plugins.gitlabserverconfig.credentials.PersonalAccessTokenImpl; import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer; import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers; import java.util.Collections; import java.util.List; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.Test; -public class ConfigurationAsCodeTest { - - @ClassRule - @ConfiguredWithCode("configuration-as-code.yml") - public static JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule(); +@WithJenkinsConfiguredWithCode +class ConfigurationAsCodeTest { @Test - public void should_support_configuration_as_code() { + @ConfiguredWithCode("configuration-as-code.yml") + void should_support_configuration_as_code(JenkinsConfiguredWithCodeRule j) { List servers = GitLabServers.get().getServers(); assertThat(servers.size(), is(1)); GitLabServer server = servers.get(0); @@ -41,8 +39,8 @@ public void should_support_configuration_as_code() { assertThat(server.isManageSystemHooks(), is(true)); assertThat(server.getHooksRootUrl(), is("https://jenkins.intranet/")); - List credentials = CredentialsProvider.lookupCredentials( - PersonalAccessTokenImpl.class, j.jenkins, ACL.SYSTEM, Collections.emptyList()); + List credentials = CredentialsProvider.lookupCredentialsInItemGroup( + PersonalAccessTokenImpl.class, j.jenkins, ACL.SYSTEM2, Collections.emptyList()); assertThat(credentials, hasSize(1)); final PersonalAccessTokenImpl credential = credentials.get(0); assertThat(credential.getToken().getPlainText(), is("glpat-XfsqZvVtAx5YCph5bq3r")); @@ -50,7 +48,8 @@ public void should_support_configuration_as_code() { } @Test - public void should_support_configuration_export() throws Exception { + @ConfiguredWithCode("configuration-as-code.yml") + void should_support_configuration_export(JenkinsConfiguredWithCodeRule j) throws Exception { ConfiguratorRegistry registry = ConfiguratorRegistry.get(); ConfigurationContext context = new ConfigurationContext(registry); CNode gitLabServers = getUnclassifiedRoot(context).get("gitLabServers"); diff --git a/src/test/java/io/jenkins/plugins/gitlabserverconfig/credentials/PersonalAccessTokenImplTest.java b/src/test/java/io/jenkins/plugins/gitlabserverconfig/credentials/PersonalAccessTokenImplTest.java index ee8871c2..e6609afe 100644 --- a/src/test/java/io/jenkins/plugins/gitlabserverconfig/credentials/PersonalAccessTokenImplTest.java +++ b/src/test/java/io/jenkins/plugins/gitlabserverconfig/credentials/PersonalAccessTokenImplTest.java @@ -5,19 +5,26 @@ import hudson.model.AbstractProject; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.Builder; -import org.junit.ClassRule; -import org.junit.Test; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.TestExtension; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.kohsuke.stapler.DataBoundConstructor; -public class PersonalAccessTokenImplTest { +@WithJenkins +class PersonalAccessTokenImplTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; + + @BeforeAll + static void setUp(JenkinsRule rule) { + j = rule; + } @Test - public void configRoundtrip() throws Exception { + void configRoundtrip() throws Exception { PersonalAccessTokenImpl expected = new PersonalAccessTokenImpl( CredentialsScope.GLOBAL, "magic-id", "configRoundtrip", "sAf_Xasnou47yxoAsC"); CredentialsBuilder builder = new CredentialsBuilder(expected); @@ -41,11 +48,10 @@ public CredentialsBuilder(Credentials credentials) { public static class DescriptorImpl extends BuildStepDescriptor { @Override - public String getDisplayName() { + public @NotNull String getDisplayName() { return "CredentialsBuilder"; } - @SuppressWarnings("rawtypes") @Override public boolean isApplicable(Class jobType) { return true; diff --git a/src/test/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServerTest.java b/src/test/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServerTest.java index 1348860e..17ac909c 100644 --- a/src/test/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServerTest.java +++ b/src/test/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServerTest.java @@ -4,25 +4,31 @@ import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; import org.apache.http.HttpStatus; import org.htmlunit.html.HtmlPage; -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.Issue; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.JenkinsRule.WebClient; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.xml.sax.SAXException; -public class GitLabServerTest { +@WithJenkins +class GitLabServerTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; + + @BeforeAll + static void setUp(JenkinsRule rule) { + j = rule; + } @Test - public void testFixEmptyAndTrimOne() throws Exception { + void testFixEmptyAndTrimOne() throws Exception { GitLabServer server = new GitLabServer("https://gitlab.com", "default", null); server.setHooksRootUrl("https://myhooks/"); assertThat(server.getName(), is("default")); @@ -32,7 +38,7 @@ public void testFixEmptyAndTrimOne() throws Exception { } @Test - public void testFixEmptyAndTrimTwo() throws Exception { + void testFixEmptyAndTrimTwo() throws Exception { GitLabServer server = new GitLabServer(" https://gitlab.com ", " default ", null); server.setHooksRootUrl(" https://myhooks/ "); assertThat(server.getName(), is("default")); @@ -42,7 +48,7 @@ public void testFixEmptyAndTrimTwo() throws Exception { } @Test - public void testFixEmptyAndTrimThree() throws Exception { + void testFixEmptyAndTrimThree() throws Exception { GitLabServer server = new GitLabServer(null, null, null); server.setHooksRootUrl(null); assertThat(server.getName(), startsWith("gitlab-")); @@ -52,7 +58,7 @@ public void testFixEmptyAndTrimThree() throws Exception { } @Test - public void testFixEmptyAndTrimFour() throws Exception { + void testFixEmptyAndTrimFour() throws Exception { GitLabServer server = new GitLabServer("https://whatever.com", "whatever", null); server.setHooksRootUrl("https://myhooks/"); assertThat(server.getName(), is("whatever")); @@ -62,7 +68,7 @@ public void testFixEmptyAndTrimFour() throws Exception { } @Test - public void testFixEmptyAndTrimFive() throws Exception { + void testFixEmptyAndTrimFive() throws Exception { GitLabServer server = new GitLabServer("", "", ""); server.setHooksRootUrl(""); assertThat(server.getName(), startsWith("gitlab-")); @@ -73,7 +79,7 @@ public void testFixEmptyAndTrimFive() throws Exception { @Test @Issue("SECURITY-3251") - public void testGetDoCheckServerUrl() throws IOException, SAXException { + void testGetDoCheckServerUrl() throws IOException, SAXException { try (WebClient wc = j.createWebClient()) { wc.setThrowExceptionOnFailingStatusCode(false); HtmlPage page = wc.goTo( diff --git a/src/test/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServersTest.java b/src/test/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServersTest.java index 61a7e171..a64c2c05 100644 --- a/src/test/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServersTest.java +++ b/src/test/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServersTest.java @@ -21,24 +21,23 @@ import jenkins.model.Jenkins; import org.acegisecurity.Authentication; import org.jenkinsci.plugins.plaincredentials.StringCredentials; -import org.junit.Rule; -import org.junit.Test; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; -import org.jvnet.hudson.test.LoggerRule; +import org.jvnet.hudson.test.LogRecorder; import org.jvnet.hudson.test.TestExtension; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class GitLabServersTest { - @Rule - public LoggerRule logger = - new LoggerRule().record(OldDataMonitor.class, Level.FINE).capture(50); +@WithJenkins +class GitLabServersTest { - @Rule - public JenkinsRule j = new JenkinsRule(); + private final LogRecorder logger = + new LogRecorder().record(OldDataMonitor.class, Level.FINE).capture(50); @LocalData @Test - public void migrationToCredentials() throws Throwable { + void migrationToCredentials(JenkinsRule j) throws Throwable { // LocalData creating using the following: /* GitLabServer server = new GitLabServer("http://localhost", "my-server", null); @@ -62,7 +61,7 @@ public void migrationToCredentials() throws Throwable { @TestExtension("migrationToCredentials") public static class CredentialsProviderThatRequiresDescriptorLookup extends CredentialsProvider { @Override - public List getCredentials( + public @NotNull List getCredentials( @NonNull Class type, @Nullable ItemGroup itemGroup, @Nullable Authentication authentication,