From 969c60929ec334cc8e2c73b3c8d670d1b6576410 Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Thu, 3 Apr 2025 14:19:00 +0200 Subject: [PATCH] Migrate tests to JUnit5 * Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup --- .../jenkins/plugins/github/api/SmokeTest.java | 70 +++++++++---------- .../plugins/github/api/mock/MockGitHub.java | 8 +-- .../plugins/github/api/mock/MockObject.java | 2 +- .../plugins/github/api/mock/MockOwner.java | 2 +- .../plugins/github/api/mock/MockUser.java | 4 +- 5 files changed, 39 insertions(+), 47 deletions(-) diff --git a/src/test/java/jenkins/plugins/github/api/SmokeTest.java b/src/test/java/jenkins/plugins/github/api/SmokeTest.java index 866fc3fa..9f6d282b 100644 --- a/src/test/java/jenkins/plugins/github/api/SmokeTest.java +++ b/src/test/java/jenkins/plugins/github/api/SmokeTest.java @@ -9,9 +9,8 @@ import jenkins.plugins.github.api.mock.MockOrganization; import jenkins.plugins.github.api.mock.MockUser; import okhttp3.OkHttpClient; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.kohsuke.github.GHOrganization; import org.kohsuke.github.GHRepository; import org.kohsuke.github.GHUser; @@ -20,14 +19,11 @@ import org.kohsuke.github.connector.GitHubConnector; import org.kohsuke.github.extras.okhttp3.OkHttpGitHubConnector; -import edu.umd.cs.findbugs.annotations.NonNull; - +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -@RunWith(Parameterized.class) -public class SmokeTest { +class SmokeTest { @FunctionalInterface public interface IOFunction { @@ -41,51 +37,46 @@ public interface IOFunction { GitHub apply(MockGitHub t) throws IOException; } - @NonNull - IOFunction connectFunction; - - public SmokeTest(IOFunction connectFunction) { - this.connectFunction = connectFunction; - } - - @Parameterized.Parameters(name = "connectFunction={index}") - public static IOFunction[] connectFunctions() { + static IOFunction[] connectFunctions() { OkHttpClient okHttpClient = new OkHttpClient(); GitHubConnector okHttpGitHubConnector = new OkHttpGitHubConnector(okHttpClient); ArrayList list = new ArrayList<>(); - list.add ((mock) -> GitHub.connectToEnterpriseAnonymously(mock.open())); - list.add ((mock) -> new GitHubBuilder().withConnector(okHttpGitHubConnector).withEndpoint(mock.open()).build()); + list.add (mock -> GitHub.connectToEnterpriseAnonymously(mock.open())); + list.add (mock -> new GitHubBuilder().withConnector(okHttpGitHubConnector).withEndpoint(mock.open()).build()); return list.toArray(new IOFunction[] {}); } - public GitHub openAndConnect(MockGitHub mock) throws IOException { + public GitHub openAndConnect(IOFunction connectFunction, MockGitHub mock) throws IOException { return connectFunction.apply(mock); } - @Test - public void given__veryBasicMockGitHub__when__connectingAnonymously__then__apiUrlValid() throws Exception { + @ParameterizedTest(name = "connectFunction={index}") + @MethodSource("connectFunctions") + void given__veryBasicMockGitHub__when__connectingAnonymously__then__apiUrlValid(IOFunction connectFunction) throws Exception { try (MockGitHub mock = new MockGitHub()) { - openAndConnect(mock).checkApiUrlValidity(); + openAndConnect(connectFunction, mock).checkApiUrlValidity(); } } - @Test - public void given__veryBasicMockGitHub__when__listingRepos__then__reposListed() throws Exception { + @ParameterizedTest(name = "connectFunction={index}") + @MethodSource("connectFunctions") + void given__veryBasicMockGitHub__when__listingRepos__then__reposListed(IOFunction connectFunction) throws Exception { try (MockGitHub mock = new MockGitHub()) { mock.withOrg("org1").withPublicRepo("repo1").withPrivateRepo("repo2"); mock.withOrg("org2").withPublicRepo("repo3"); mock.withUser("user1").withPublicRepo("repo4").withPrivateRepo("repo5"); Set names = new TreeSet<>(); - for (GHRepository r: openAndConnect(mock).listAllPublicRepositories()) { + for (GHRepository r: openAndConnect(connectFunction, mock).listAllPublicRepositories()) { names.add(r.getFullName()); } assertThat(names, contains("org1/repo1", "org2/repo3", "user1/repo4")); } } - @Test - public void given__veryBasicMockGitHub__when__listingManyRepos__then__reposListed() throws Exception { + @ParameterizedTest(name = "connectFunction={index}") + @MethodSource("connectFunctions") + void given__veryBasicMockGitHub__when__listingManyRepos__then__reposListed(IOFunction connectFunction) throws Exception { try (MockGitHub mock = new MockGitHub()) { MockOrganization org1 = mock.withOrg("org1"); Set expected = new TreeSet<>(); @@ -95,15 +86,16 @@ public void given__veryBasicMockGitHub__when__listingManyRepos__then__reposListe } Set actual = new TreeSet<>(); - for (GHRepository r: openAndConnect(mock).listAllPublicRepositories()) { + for (GHRepository r: openAndConnect(connectFunction, mock).listAllPublicRepositories()) { actual.add(r.getFullName()); } - assertThat(actual, is(actual)); + assertThat(actual, is(expected)); } } - @Test - public void given__veryBasicMockGitHub__when__gettingUser__then__userReturned() throws Exception { + @ParameterizedTest(name = "connectFunction={index}") + @MethodSource("connectFunctions") + void given__veryBasicMockGitHub__when__gettingUser__then__userReturned(IOFunction connectFunction) throws Exception { try (MockGitHub mock = new MockGitHub()) { MockUser expected = mock.withUser("user1") .withAvatarUrl("http://avatar.test/user1") @@ -115,18 +107,20 @@ public void given__veryBasicMockGitHub__when__gettingUser__then__userReturned() .withPrivateRepo("repo1") .withPublicRepo("repo2") .withPublicRepo("repo3"); - GHUser actual = openAndConnect(mock).getUser("user1"); + GHUser actual = openAndConnect(connectFunction, mock).getUser("user1"); assertThat(actual.getLogin(), is(expected.getLogin())); assertThat(actual.getName(), is(expected.getName())); assertThat(actual.getAvatarUrl(), is(expected.getAvatarUrl())); assertThat(actual.getBlog(), is(expected.getBlog())); assertThat(actual.getCompany(), is(expected.getCompany())); - assertThat(actual.getId(), is((long)expected.getId())); + assertThat(actual.getId(), is(expected.getId())); assertThat(actual.getPublicRepoCount(), is(expected.getPublicRepos())); } } - @Test - public void given__veryBasicMockGitHub__when__gettingOrg__then__orgReturned() throws Exception { + + @ParameterizedTest(name = "connectFunction={index}") + @MethodSource("connectFunctions") + void given__veryBasicMockGitHub__when__gettingOrg__then__orgReturned(IOFunction connectFunction) throws Exception { try (MockGitHub mock = new MockGitHub()) { MockOrganization expected = mock.withOrg("org1") .withAvatarUrl("http://avatar.test/org1") @@ -137,12 +131,12 @@ public void given__veryBasicMockGitHub__when__gettingOrg__then__orgReturned() th .withPrivateRepo("repo1") .withPublicRepo("repo2") .withPublicRepo("repo3"); - GHOrganization actual = openAndConnect(mock).getOrganization("org1"); + GHOrganization actual = openAndConnect(connectFunction, mock).getOrganization("org1"); assertThat(actual.getLogin(), is(expected.getLogin())); assertThat(actual.getName(), is(expected.getName())); assertThat(actual.getAvatarUrl(), is(expected.getAvatarUrl())); assertThat(actual.getBlog(), is(expected.getBlog())); - assertThat(actual.getId(), is((long)expected.getId())); + assertThat(actual.getId(), is(expected.getId())); assertThat(actual.getPublicRepoCount(), is(expected.getPublicRepos())); } } diff --git a/src/test/java/jenkins/plugins/github/api/mock/MockGitHub.java b/src/test/java/jenkins/plugins/github/api/mock/MockGitHub.java index 9dbc1aff..598a6689 100644 --- a/src/test/java/jenkins/plugins/github/api/mock/MockGitHub.java +++ b/src/test/java/jenkins/plugins/github/api/mock/MockGitHub.java @@ -22,14 +22,14 @@ import java.util.concurrent.atomic.AtomicLong; public class MockGitHub implements Closeable { - private AtomicLong nextId = new AtomicLong(); - private Map users = new HashMap<>(); - private Map organizations = new HashMap<>(); + private final AtomicLong nextId = new AtomicLong(); + private final Map users = new HashMap<>(); + private final Map organizations = new HashMap<>(); private HttpServer server; private String url; - private JsonFactory factory = new JsonFactory(); + private final JsonFactory factory = new JsonFactory(); public String open() throws IOException { server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); diff --git a/src/test/java/jenkins/plugins/github/api/mock/MockObject.java b/src/test/java/jenkins/plugins/github/api/mock/MockObject.java index 36227b02..e5181aac 100644 --- a/src/test/java/jenkins/plugins/github/api/mock/MockObject.java +++ b/src/test/java/jenkins/plugins/github/api/mock/MockObject.java @@ -7,7 +7,7 @@ public class MockObject { private final MockGitHub app; private final long id; - private long created; + private final long created; private long updated; public MockObject(MockGitHub app) { diff --git a/src/test/java/jenkins/plugins/github/api/mock/MockOwner.java b/src/test/java/jenkins/plugins/github/api/mock/MockOwner.java index 0aadb217..1555bce3 100644 --- a/src/test/java/jenkins/plugins/github/api/mock/MockOwner.java +++ b/src/test/java/jenkins/plugins/github/api/mock/MockOwner.java @@ -111,7 +111,7 @@ public int getFollowers() { count++; } } - return 0; + return count; } public int getFollowing() { diff --git a/src/test/java/jenkins/plugins/github/api/mock/MockUser.java b/src/test/java/jenkins/plugins/github/api/mock/MockUser.java index ee276cc0..887f088a 100644 --- a/src/test/java/jenkins/plugins/github/api/mock/MockUser.java +++ b/src/test/java/jenkins/plugins/github/api/mock/MockUser.java @@ -1,12 +1,10 @@ package jenkins.plugins.github.api.mock; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Set; public class MockUser extends MockOwner { - private Set organizations = new HashSet<>(); + private final Set organizations = new HashSet<>(); private boolean siteAdmin; private String company; private boolean hireable;