Skip to content

Commit fc1b756

Browse files
Migrate tests to JUnit5
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 026eb86 commit fc1b756

6 files changed

Lines changed: 80 additions & 116 deletions

File tree

src/test/java/jenkins/plugins/github/api/SmokeTest.java

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
package jenkins.plugins.github.api;
22

33
import java.io.IOException;
4-
import java.util.ArrayList;
54
import java.util.Set;
65
import java.util.TreeSet;
6+
import java.util.stream.Stream;
77

88
import jenkins.plugins.github.api.mock.MockGitHub;
99
import jenkins.plugins.github.api.mock.MockOrganization;
1010
import jenkins.plugins.github.api.mock.MockUser;
1111
import okhttp3.OkHttpClient;
12-
import org.junit.Test;
13-
import org.junit.runner.RunWith;
14-
import org.junit.runners.Parameterized;
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.MethodSource;
1514
import org.kohsuke.github.GHOrganization;
1615
import org.kohsuke.github.GHRepository;
1716
import org.kohsuke.github.GHUser;
1817
import org.kohsuke.github.GitHub;
1918
import org.kohsuke.github.GitHubBuilder;
20-
import org.kohsuke.github.HttpConnector;
2119
import org.kohsuke.github.extras.okhttp3.OkHttpConnector;
2220

23-
import edu.umd.cs.findbugs.annotations.NonNull;
21+
import static org.hamcrest.MatcherAssert.assertThat;
22+
import static org.hamcrest.Matchers.*;
2423

25-
import static org.hamcrest.Matchers.contains;
26-
import static org.hamcrest.Matchers.is;
27-
import static org.junit.Assert.assertThat;
28-
29-
@RunWith(Parameterized.class)
30-
public class SmokeTest {
24+
class SmokeTest {
3125

3226
@FunctionalInterface
3327
public interface IOFunction {
@@ -41,50 +35,43 @@ public interface IOFunction {
4135
GitHub apply(MockGitHub t) throws IOException;
4236
}
4337

44-
@NonNull
45-
IOFunction connectFunction;
46-
47-
public SmokeTest(IOFunction connectFunction) {
48-
this.connectFunction = connectFunction;
49-
}
50-
51-
@Parameterized.Parameters(name = "connectFunction={index}")
52-
public static IOFunction[] connectFunctions() {
53-
HttpConnector okHttp3Connector = new OkHttpConnector(new OkHttpClient());
54-
ArrayList<IOFunction> list = new ArrayList<>();
55-
list.add ((mock) -> GitHub.connectToEnterpriseAnonymously(mock.open()));
56-
list.add ((mock) -> new GitHubBuilder().withConnector(okHttp3Connector).withEndpoint(mock.open()).build());
57-
58-
return list.toArray(new IOFunction[] {});
38+
static Stream<IOFunction> connectFunctions() {
39+
return Stream.of(
40+
mock -> GitHub.connectToEnterpriseAnonymously(mock.open()),
41+
mock -> new GitHubBuilder().withConnector(new OkHttpConnector(new OkHttpClient())).withEndpoint(mock.open()).build()
42+
);
5943
}
6044

61-
public GitHub openAndConnect(MockGitHub mock) throws IOException {
45+
private GitHub openAndConnect(IOFunction connectFunction, MockGitHub mock) throws IOException {
6246
return connectFunction.apply(mock);
6347
}
6448

65-
@Test
66-
public void given__veryBasicMockGitHub__when__connectingAnonymously__then__apiUrlValid() throws Exception {
49+
@ParameterizedTest(name = "connectFunction={index}")
50+
@MethodSource("connectFunctions")
51+
void given__veryBasicMockGitHub__when__connectingAnonymously__then__apiUrlValid(IOFunction connectFunction) throws Exception {
6752
try (MockGitHub mock = new MockGitHub()) {
68-
openAndConnect(mock).checkApiUrlValidity();
53+
openAndConnect(connectFunction, mock).checkApiUrlValidity();
6954
}
7055
}
7156

72-
@Test
73-
public void given__veryBasicMockGitHub__when__listingRepos__then__reposListed() throws Exception {
57+
@ParameterizedTest(name = "connectFunction={index}")
58+
@MethodSource("connectFunctions")
59+
void given__veryBasicMockGitHub__when__listingRepos__then__reposListed(IOFunction connectFunction) throws Exception {
7460
try (MockGitHub mock = new MockGitHub()) {
7561
mock.withOrg("org1").withPublicRepo("repo1").withPrivateRepo("repo2");
7662
mock.withOrg("org2").withPublicRepo("repo3");
7763
mock.withUser("user1").withPublicRepo("repo4").withPrivateRepo("repo5");
7864
Set<String> names = new TreeSet<>();
79-
for (GHRepository r: openAndConnect(mock).listAllPublicRepositories()) {
65+
for (GHRepository r: openAndConnect(connectFunction, mock).listAllPublicRepositories()) {
8066
names.add(r.getFullName());
8167
}
8268
assertThat(names, contains("org1/repo1", "org2/repo3", "user1/repo4"));
8369
}
8470
}
8571

86-
@Test
87-
public void given__veryBasicMockGitHub__when__listingManyRepos__then__reposListed() throws Exception {
72+
@ParameterizedTest(name = "connectFunction={index}")
73+
@MethodSource("connectFunctions")
74+
void given__veryBasicMockGitHub__when__listingManyRepos__then__reposListed(IOFunction connectFunction) throws Exception {
8875
try (MockGitHub mock = new MockGitHub()) {
8976
MockOrganization org1 = mock.withOrg("org1");
9077
Set<String> expected = new TreeSet<>();
@@ -94,15 +81,16 @@ public void given__veryBasicMockGitHub__when__listingManyRepos__then__reposListe
9481

9582
}
9683
Set<String> actual = new TreeSet<>();
97-
for (GHRepository r: openAndConnect(mock).listAllPublicRepositories()) {
84+
for (GHRepository r: openAndConnect(connectFunction, mock).listAllPublicRepositories()) {
9885
actual.add(r.getFullName());
9986
}
100-
assertThat(actual, is(actual));
87+
assertThat(actual, is(expected));
10188
}
10289
}
10390

104-
@Test
105-
public void given__veryBasicMockGitHub__when__gettingUser__then__userReturned() throws Exception {
91+
@ParameterizedTest(name = "connectFunction={index}")
92+
@MethodSource("connectFunctions")
93+
void given__veryBasicMockGitHub__when__gettingUser__then__userReturned(IOFunction connectFunction) throws Exception {
10694
try (MockGitHub mock = new MockGitHub()) {
10795
MockUser expected = mock.withUser("user1")
10896
.withAvatarUrl("http://avatar.test/user1")
@@ -114,18 +102,20 @@ public void given__veryBasicMockGitHub__when__gettingUser__then__userReturned()
114102
.withPrivateRepo("repo1")
115103
.withPublicRepo("repo2")
116104
.withPublicRepo("repo3");
117-
GHUser actual = openAndConnect(mock).getUser("user1");
105+
GHUser actual = openAndConnect(connectFunction, mock).getUser("user1");
118106
assertThat(actual.getLogin(), is(expected.getLogin()));
119107
assertThat(actual.getName(), is(expected.getName()));
120108
assertThat(actual.getAvatarUrl(), is(expected.getAvatarUrl()));
121109
assertThat(actual.getBlog(), is(expected.getBlog()));
122110
assertThat(actual.getCompany(), is(expected.getCompany()));
123-
assertThat(actual.getId(), is((long)expected.getId()));
111+
assertThat(actual.getId(), is(expected.getId()));
124112
assertThat(actual.getPublicRepoCount(), is(expected.getPublicRepos()));
125113
}
126114
}
127-
@Test
128-
public void given__veryBasicMockGitHub__when__gettingOrg__then__orgReturned() throws Exception {
115+
116+
@ParameterizedTest(name = "connectFunction={index}")
117+
@MethodSource("connectFunctions")
118+
void given__veryBasicMockGitHub__when__gettingOrg__then__orgReturned(IOFunction connectFunction) throws Exception {
129119
try (MockGitHub mock = new MockGitHub()) {
130120
MockOrganization expected = mock.withOrg("org1")
131121
.withAvatarUrl("http://avatar.test/org1")
@@ -136,12 +126,12 @@ public void given__veryBasicMockGitHub__when__gettingOrg__then__orgReturned() th
136126
.withPrivateRepo("repo1")
137127
.withPublicRepo("repo2")
138128
.withPublicRepo("repo3");
139-
GHOrganization actual = openAndConnect(mock).getOrganization("org1");
129+
GHOrganization actual = openAndConnect(connectFunction, mock).getOrganization("org1");
140130
assertThat(actual.getLogin(), is(expected.getLogin()));
141131
assertThat(actual.getName(), is(expected.getName()));
142132
assertThat(actual.getAvatarUrl(), is(expected.getAvatarUrl()));
143133
assertThat(actual.getBlog(), is(expected.getBlog()));
144-
assertThat(actual.getId(), is((long)expected.getId()));
134+
assertThat(actual.getId(), is(expected.getId()));
145135
assertThat(actual.getPublicRepoCount(), is(expected.getPublicRepos()));
146136
}
147137
}

src/test/java/jenkins/plugins/github/api/mock/MockGitHub.java

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,12 @@
99
import java.net.URI;
1010
import java.net.URISyntaxException;
1111
import java.text.SimpleDateFormat;
12-
import java.util.ArrayList;
13-
import java.util.Collections;
14-
import java.util.Comparator;
15-
import java.util.Date;
16-
import java.util.HashMap;
17-
import java.util.List;
18-
import java.util.Map;
19-
import java.util.TimeZone;
12+
import java.util.*;
2013
import java.util.concurrent.LinkedBlockingQueue;
21-
import java.util.concurrent.ThreadFactory;
2214
import java.util.concurrent.ThreadPoolExecutor;
2315
import java.util.concurrent.TimeUnit;
2416
import java.util.concurrent.atomic.AtomicLong;
25-
import jakarta.servlet.ServletException;
17+
2618
import org.eclipse.jetty.server.HttpConfiguration;
2719
import org.eclipse.jetty.server.HttpConnectionFactory;
2820
import org.eclipse.jetty.server.Server;
@@ -36,28 +28,26 @@
3628
import org.kohsuke.stapler.StaplerResponse2;
3729

3830
public class MockGitHub implements Closeable {
39-
private AtomicLong nextId = new AtomicLong();
40-
private Map<String, MockUser> users = new HashMap<>();
41-
private Map<String, MockOrganization> organizations = new HashMap<>();
31+
private final AtomicLong nextId = new AtomicLong();
32+
private final Map<String, MockUser> users = new HashMap<>();
33+
private final Map<String, MockOrganization> organizations = new HashMap<>();
4234

4335
private Server server;
4436

4537
private int localPort = -1;
46-
private JsonFactory factory = new JsonFactory();
38+
private final JsonFactory factory = new JsonFactory();
4739

4840
public String open() throws IOException {
4941
server = new Server(new ThreadPoolImpl(
50-
new ThreadPoolExecutor(10, 10, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),
51-
new ThreadFactory() {
52-
public Thread newThread(Runnable r) {
53-
Thread t = new Thread(r);
54-
t.setName("Jetty Thread Pool");
55-
return t;
56-
}
42+
new ThreadPoolExecutor(10, 10, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
43+
r -> {
44+
Thread t = new Thread(r);
45+
t.setName("Jetty Thread Pool");
46+
return t;
5747
})));
5848
ServletContextHandler context = new ServletContextHandler();
5949
server.setHandler(context);
60-
;
50+
6151
context.addServlet(Stapler.class, "/*");
6252

6353
ServerConnector connector = new ServerConnector(server);
@@ -179,7 +169,7 @@ public HttpResponse doRepositories(final @QueryParameter long since) {
179169
return new HttpResponse() {
180170
@Override
181171
public void generateResponse(StaplerRequest2 req, StaplerResponse2 rsp, Object node)
182-
throws IOException, ServletException {
172+
throws IOException {
183173
List<MockRepository> repositories = new ArrayList<>();
184174
for (MockOwner<?> o : owners()) {
185175
for (MockRepository r: o.repositories().values()) {
@@ -188,12 +178,7 @@ public void generateResponse(StaplerRequest2 req, StaplerResponse2 rsp, Object n
188178
}
189179
}
190180
}
191-
Collections.sort(repositories, new Comparator<MockRepository>() {
192-
@Override
193-
public int compare(MockRepository o1, MockRepository o2) {
194-
return Long.compare(o1.getId(), o2.getId());
195-
}
196-
});
181+
repositories.sort(Comparator.comparingLong(MockObject::getId));
197182
if (repositories.size() > 30) {
198183
rsp.addHeader("Link", String.format(
199184
"<%s/repositories?since=%d>; rel=\"next\", <%s/repositories{?since}>; rel=\"first\"",

src/test/java/jenkins/plugins/github/api/mock/MockObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class MockObject {
77
private final MockGitHub app;
88

99
private final long id;
10-
private long created;
10+
private final long created;
1111
private long updated;
1212

1313
public MockObject(MockGitHub app) {

src/test/java/jenkins/plugins/github/api/mock/MockOwner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public int getFollowers() {
111111
count++;
112112
}
113113
}
114-
return 0;
114+
return count;
115115
}
116116

117117
public int getFollowing() {

src/test/java/jenkins/plugins/github/api/mock/MockUser.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
package jenkins.plugins.github.api.mock;
22

3-
import java.util.HashMap;
4-
import java.util.HashSet;
5-
import java.util.Map;
6-
import java.util.Set;
7-
83
public class MockUser extends MockOwner<MockUser> {
9-
private Set<String> organizations = new HashSet<>();
104
private boolean siteAdmin;
115
private String company;
126
private boolean hireable;

0 commit comments

Comments
 (0)