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

Support for projects #545

Merged
merged 8 commits into from
Oct 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Switch to WireMockMultiServerRule as base
  • Loading branch information
bitwiseman committed Oct 5, 2019
commit 552edf86988137c2a836f57259636476a0bebc29
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
/**
* @author Kohsuke Kawaguchi
*/
public abstract class AbstractGitHubApiTestBase extends AbstractGitHubApiWireMockTest {
public abstract class AbstractGitHubApiTestBase extends AbstractGitHubWireMockTest {

@Before
public void setUp() throws Exception {
assumeTrue("All tests inheriting from this class are not guaranteed to work without proxy", githubApi.isUseProxy());
assumeTrue("All tests inheriting from this class are not guaranteed to work without proxy", mockGitHub.isUseProxy());
}

protected void kohsuke() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
package org.kohsuke.github;

import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.Response;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestWatcher;
import org.kohsuke.github.junit.GitHubApiWireMockRule;
import org.kohsuke.github.junit.WireMockRule;
import org.kohsuke.github.junit.GitHubWireMockRule;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

/**
* @author Liam Newman
*/
public abstract class AbstractGitHubApiWireMockTest extends Assert {
public abstract class AbstractGitHubWireMockTest extends Assert {

private final GitHubBuilder githubBuilder = createGitHubBuilder();

Expand Down Expand Up @@ -56,10 +46,10 @@ public abstract class AbstractGitHubApiWireMockTest extends Assert {
protected final String baseRecordPath = "src/test/resources/" + baseFilesClassPath + "/wiremock";

@Rule
public final GitHubApiWireMockRule githubApi;
public final GitHubWireMockRule mockGitHub;

public AbstractGitHubApiWireMockTest() {
githubApi = new GitHubApiWireMockRule(
public AbstractGitHubWireMockTest() {
mockGitHub = new GitHubWireMockRule(
this.getWireMockOptions()
);
}
Expand Down Expand Up @@ -103,7 +93,7 @@ private static GitHubBuilder createGitHubBuilder() {
protected GitHubBuilder getGitHubBuilder() {
GitHubBuilder builder = githubBuilder.clone();

if (!githubApi.isUseProxy()) {
if (!mockGitHub.isUseProxy()) {
// This sets the user and password to a placeholder for wiremock testing
// This makes the tests believe they are running with permissions
// The recorded stubs will behave like they running with permissions
Expand All @@ -117,14 +107,14 @@ protected GitHubBuilder getGitHubBuilder() {
@Before
public void wireMockSetup() throws Exception {
GitHubBuilder builder = getGitHubBuilder()
.withEndpoint(githubApi.baseUrl());
.withEndpoint(mockGitHub.apiServer().baseUrl());

if (useDefaultGitHub) {
gitHub = builder
.build();
}

if (githubApi.isUseProxy()) {
if (mockGitHub.isUseProxy()) {
gitHubBeforeAfter = getGitHubBuilder()
.withEndpoint("https://api.github.com/")
.build();
Expand All @@ -134,11 +124,11 @@ public void wireMockSetup() throws Exception {
}

protected void snapshotNotAllowed() {
assumeFalse("Test contains hand written mappings. Only valid when not taking a snapshot.", githubApi.isTakeSnapshot());
assumeFalse("Test contains hand written mappings. Only valid when not taking a snapshot.", mockGitHub.isTakeSnapshot());
}

protected void requireProxy(String reason) {
assumeTrue("Test only valid when proxying (-Dtest.github.useProxy to enable): " + reason, githubApi.isUseProxy());
assumeTrue("Test only valid when proxying (-Dtest.github.useProxy to enable): " + reason, mockGitHub.isUseProxy());
}

protected GHUser getUser() {
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Unit test for simple App.
*/
public class AppTest extends AbstractGitHubApiWireMockTest {
public class AppTest extends AbstractGitHubWireMockTest {
static final String GITHUB_API_TEST_REPO = "github-api-test";

private String getTestRepositoryName() throws IOException {
Expand Down Expand Up @@ -59,15 +59,15 @@ public void testRepositoryWithAutoInitializationCRUD() throws Exception {
r.enableIssueTracker(false);
r.enableDownloads(false);
r.enableWiki(false);
if (githubApi.isUseProxy()) {
if (mockGitHub.isUseProxy()) {
Thread.sleep(3000);
}
assertNotNull(r.getReadme());
getUser().getRepository(name).delete();
}

private void cleanupRepository(final String name) throws IOException {
if (githubApi.isUseProxy()) {
if (mockGitHub.isUseProxy()) {
GHRepository repository = getUser(gitHubBeforeAfter).getRepository(name);
if (repository != null) {
repository.delete();
Expand Down Expand Up @@ -154,7 +154,7 @@ public void testGetIssues() throws Exception {


private GHRepository getTestRepository() throws IOException {
if (githubApi.isUseProxy()) {
if (mockGitHub.isUseProxy()) {
GHRepository repository = gitHubBeforeAfter
.getOrganization(GITHUB_API_TEST_ORG)
.getRepository(GITHUB_API_TEST_REPO);
Expand Down Expand Up @@ -460,7 +460,7 @@ public void tryHook() throws Exception {
GHHook hook = r.createWebHook(new URL("http://www.google.com/"));
System.out.println(hook);

if (githubApi.isUseProxy()) {
if (mockGitHub.isUseProxy()) {
r = gitHubBeforeAfter.getOrganization(GITHUB_API_TEST_ORG).getRepository("github-api");
for (GHHook h : r.getHooks()) {
h.delete();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/CommitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* @author Kohsuke Kawaguchi
*/
public class CommitTest extends AbstractGitHubApiWireMockTest {
public class CommitTest extends AbstractGitHubWireMockTest {
@Test // issue 152
public void lastStatus() throws IOException {
GHTag t = gitHub.getRepository("stapler/stapler").listTags().iterator().next();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHAppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @author Paulo Miguel Almeida
*/
public class GHAppTest extends AbstractGitHubApiWireMockTest {
public class GHAppTest extends AbstractGitHubWireMockTest {

protected GitHubBuilder getGitHubBuilder() {
return super.getGitHubBuilder()
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/kohsuke/github/GHOrganizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.io.IOException;

public class GHOrganizationTest extends AbstractGitHubApiWireMockTest {
public class GHOrganizationTest extends AbstractGitHubWireMockTest {

public static final String GITHUB_API_TEST = "github-api-test";

Expand Down Expand Up @@ -36,7 +36,7 @@ public void testCreateRepositoryWithAutoInitialization() throws IOException {

@After
public void cleanUp() throws IOException {
if (githubApi.isUseProxy()) {
if (mockGitHub.isUseProxy()) {
GHRepository repository = gitHubBeforeAfter.getOrganization(GITHUB_API_TEST_ORG).getRepository(GITHUB_API_TEST);
if (repository != null) {
repository.delete();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/kohsuke/github/GHProjectCardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @author Gunnar Skjold
*/
public class GHProjectCardTest extends AbstractGitHubApiWireMockTest {
public class GHProjectCardTest extends AbstractGitHubWireMockTest {
private GHOrganization org;
private GHProject project;
private GHProjectColumn column;
Expand Down Expand Up @@ -72,7 +72,7 @@ public void testDeleteCard() throws IOException {

@After
public void after() throws IOException {
if(githubApi.isUseProxy()) {
if(mockGitHub.isUseProxy()) {
if (card != null) {
card = gitHubBeforeAfter.getProjectCard(card.getId());
try {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/kohsuke/github/GHProjectColumnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @author Gunnar Skjold
*/
public class GHProjectColumnTest extends AbstractGitHubApiWireMockTest {
public class GHProjectColumnTest extends AbstractGitHubWireMockTest {
private GHProject project;
private GHProjectColumn column;

Expand Down Expand Up @@ -48,7 +48,7 @@ public void testDeleteColumn() throws IOException {

@After
public void after() throws IOException {
if(githubApi.isUseProxy()) {
if(mockGitHub.isUseProxy()) {
if (column != null) {
column = gitHubBeforeAfter
.getProjectColumn(column.getId());
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/kohsuke/github/GHProjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @author Gunnar Skjold
*/
public class GHProjectTest extends AbstractGitHubApiWireMockTest {
public class GHProjectTest extends AbstractGitHubWireMockTest {
private GHProject project;

@Before
Expand Down Expand Up @@ -69,7 +69,7 @@ public void testDeleteProject() throws IOException {

@After
public void after() throws IOException {
if (githubApi.isUseProxy()) {
if (mockGitHub.isUseProxy()) {
if (project != null) {
project = gitHubBeforeAfter
.getProject(project.getId());
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/kohsuke/github/GHPullRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
/**
* @author Kohsuke Kawaguchi
*/
public class GHPullRequestTest extends AbstractGitHubApiWireMockTest {
public class GHPullRequestTest extends AbstractGitHubWireMockTest {

@Before
@After
public void cleanUp() throws Exception {
// Cleanup is only needed when proxying
if (!githubApi.isUseProxy()) {
if (!mockGitHub.isUseProxy()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @author Liam Newman
*/
public class GHRepositoryTest extends AbstractGitHubApiWireMockTest {
public class GHRepositoryTest extends AbstractGitHubWireMockTest {

@Test
public void archive() throws Exception {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GHTeamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.io.IOException;
import java.util.Random;

public class GHTeamTest extends AbstractGitHubApiWireMockTest {
public class GHTeamTest extends AbstractGitHubWireMockTest {

@Test
public void testSetDescription() throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GistTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @author Kohsuke Kawaguchi
*/
public class GistTest extends AbstractGitHubApiWireMockTest {
public class GistTest extends AbstractGitHubWireMockTest {
/**
* CRUD operation.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* @author Kohsuke Kawaguchi
*/
public class UserTest extends AbstractGitHubApiWireMockTest {
public class UserTest extends AbstractGitHubWireMockTest {
@Test
public void listFollowsAndFollowers() throws IOException {
GHUser u = gitHub.getUser("rtyler");
Expand Down
30 changes: 15 additions & 15 deletions src/test/java/org/kohsuke/github/WireMockStatusReporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@


/**
* Tests in this class are meant to show the behavior of {@link AbstractGitHubApiWireMockTest} with proxying on or off.
* Tests in this class are meant to show the behavior of {@link AbstractGitHubWireMockTest} with proxying on or off.
* <p>
* The wiremock data for these tests should only be modified by hand - thus most are skipped when snapshotting.
*
* @author Liam Newman
*/
public class WireMockStatusReporterTest extends AbstractGitHubApiWireMockTest {
public class WireMockStatusReporterTest extends AbstractGitHubWireMockTest {

@Test
public void user_whenProxying_AuthCorrectlyConfigured() throws Exception {
Expand Down Expand Up @@ -45,7 +45,7 @@ public void user_whenProxying_AuthCorrectlyConfigured() throws Exception {
public void user_whenNotProxying_Stubbed() throws Exception {
snapshotNotAllowed();

assumeFalse("Test only valid when not proxying", githubApi.isUseProxy());
assumeFalse("Test only valid when not proxying", mockGitHub.isUseProxy());

assertThat(gitHub.isAnonymous(), is(false));
assertThat(gitHub.login, equalTo(STUBBED_USER_LOGIN));
Expand All @@ -59,10 +59,11 @@ public void user_whenNotProxying_Stubbed() throws Exception {
System.out.println("GitHub proxying and user auth correctly configured for user login: " + user.getLogin());
}

@Ignore("Can't run this as WireMock will report failure after the test method completes.")
@Test
public void BasicBehaviors_whenNotProxying() throws Exception {
snapshotNotAllowed();
assumeFalse("Test only valid when not proxying", githubApi.isUseProxy());
assumeFalse("Test only valid when not proxying", mockGitHub.isUseProxy());

Exception e = null;
GHRepository repo = null;
Expand All @@ -71,18 +72,18 @@ public void BasicBehaviors_whenNotProxying() throws Exception {
repo = gitHub.getRepository("github-api/github-api");
assertThat(repo.getDescription(), equalTo("this is a stubbed description"));

// Valid repository, without stub - fails 500 when not proxying
// Invalid repository, without stub - fails 404 when not proxying
try {
gitHub.getRepository("jenkinsci/jenkins");
fail();
} catch (Exception ex) {
e = ex;
}
assertThat(e, Matchers.<Exception>instanceOf(HttpException.class));
assertThat("Status should be 500 for missing stubs", ((HttpException) e).getResponseCode(), equalTo(500));
assertThat(e.getMessage(), equalTo("Stubbed data not found. Set test.github.use-proxy to have WireMock proxy to github"));

// Invalid repository, without stub - fails 500 when not proxying
assertThat(e, Matchers.<Exception>instanceOf(GHFileNotFoundException.class));
assertThat(e.getMessage(), containsString("Request was not matched"));

// Invalid repository, without stub - fails 404 when not proxying
e = null;
try {
gitHub.getRepository("github-api/non-existant-repository");
Expand All @@ -91,9 +92,8 @@ public void BasicBehaviors_whenNotProxying() throws Exception {
e = ex;
}

assertThat(e, Matchers.<Exception>instanceOf(HttpException.class));
assertThat("Status should be 500 for missing stubs", ((HttpException) e).getResponseCode(), equalTo(500));
assertThat(e.getMessage(), equalTo("Stubbed data not found. Set test.github.use-proxy to have WireMock proxy to github"));
assertThat(e, Matchers.<Exception>instanceOf(GHFileNotFoundException.class));
assertThat(e.getMessage(), containsString("Request was not matched"));
}

@Test
Expand Down Expand Up @@ -126,15 +126,15 @@ public void BasicBehaviors_whenProxying() throws Exception {

@Test
public void whenSnapshot_EnsureProxy() throws Exception {
assumeTrue("Test only valid when Snapshotting (-Dtest.github.takeSnapshot to enable)", githubApi.isTakeSnapshot());
assumeTrue("Test only valid when Snapshotting (-Dtest.github.takeSnapshot to enable)", mockGitHub.isTakeSnapshot());

assertTrue("When taking a snapshot, proxy should automatically be enabled", githubApi.isUseProxy());
assertTrue("When taking a snapshot, proxy should automatically be enabled", mockGitHub.isUseProxy());
}

@Ignore("Not implemented yet")
@Test
public void whenSnapshot_EnsureRecordToExpectedLocation() throws Exception {
assumeTrue("Test only valid when Snapshotting (-Dtest.github.takeSnapshot to enable)", githubApi.isTakeSnapshot());
assumeTrue("Test only valid when Snapshotting (-Dtest.github.takeSnapshot to enable)", mockGitHub.isTakeSnapshot());

}
}
Loading