Skip to content

Migrate tests to JUnit5 #278

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

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
70 changes: 32 additions & 38 deletions src/test/java/jenkins/plugins/github/api/SmokeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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<IOFunction> 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<String> 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<String> expected = new TreeSet<>();
Expand All @@ -95,15 +86,16 @@ public void given__veryBasicMockGitHub__when__listingManyRepos__then__reposListe

}
Set<String> 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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

@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")
Expand All @@ -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")
Expand All @@ -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()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import java.util.concurrent.atomic.AtomicLong;

public class MockGitHub implements Closeable {
private AtomicLong nextId = new AtomicLong();
private Map<String, MockUser> users = new HashMap<>();
private Map<String, MockOrganization> organizations = new HashMap<>();
private final AtomicLong nextId = new AtomicLong();
private final Map<String, MockUser> users = new HashMap<>();
private final Map<String, MockOrganization> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public int getFollowers() {
count++;
}
}
return 0;
return count;
}

public int getFollowing() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MockUser> {
private Set<String> organizations = new HashSet<>();
private final Set<String> organizations = new HashSet<>();
private boolean siteAdmin;
private String company;
private boolean hireable;
Expand Down