Merge branch 'master' into task/cleanup-more

This commit is contained in:
Liam Newman
2020-05-20 21:00:31 -07:00
committed by GitHub
13 changed files with 877 additions and 38 deletions

View File

@@ -7,15 +7,17 @@ import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.TimeZone;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class GHEventPayloadTest {
public class GHEventPayloadTest extends AbstractGitHubWireMockTest {
@Rule
public final PayloadRule payload = new PayloadRule(".json");
public GHEventPayloadTest() {
useDefaultGitHub = false;
}
@Test
public void commit_comment() throws Exception {
GHEventPayload.CommitComment event = GitHub.offline()
@@ -283,6 +285,70 @@ public class GHEventPayloadTest {
assertThat(event.getSender().getLogin(), is("baxterthehacker"));
}
@Test
@Payload("push.fork")
public void pushToFork() throws Exception {
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl()).build();
GHEventPayload.Push event = GitHub.offline().parseEventPayload(payload.asReader(), GHEventPayload.Push.class);
assertThat(event.getRef(), is("refs/heads/changes"));
assertThat(event.getBefore(), is("85c44b352958bf6d81b74ab8b21920f1d313a287"));
assertThat(event.getHead(), is("1393706f1364742defbc28ba459082630ca979af"));
assertThat(event.isCreated(), is(false));
assertThat(event.isDeleted(), is(false));
assertThat(event.isForced(), is(false));
assertThat(event.getCommits().size(), is(1));
assertThat(event.getCommits().get(0).getSha(), is("1393706f1364742defbc28ba459082630ca979af"));
assertThat(event.getCommits().get(0).getAuthor().getEmail(), is("bitwiseman@gmail.com"));
assertThat(event.getCommits().get(0).getCommitter().getEmail(), is("bitwiseman@gmail.com"));
assertThat(event.getCommits().get(0).getAdded().size(), is(6));
assertThat(event.getCommits().get(0).getRemoved().size(), is(0));
assertThat(event.getCommits().get(0).getModified().size(), is(2));
assertThat(event.getCommits().get(0).getModified().get(0),
is("src/main/java/org/kohsuke/github/GHLicense.java"));
assertThat(event.getRepository().getName(), is("github-api"));
assertThat(event.getRepository().getOwnerName(), is("hub4j-test-org"));
assertThat(event.getRepository().getUrl().toExternalForm(), is("https://github.com/hub4j-test-org/github-api"));
assertThat(event.getPusher().getName(), is("bitwiseman"));
assertThat(event.getPusher().getEmail(), is("bitwiseman@gmail.com"));
assertThat(event.getSender().getLogin(), is("bitwiseman"));
assertThat(event.getRepository().isFork(), is(true));
// in offliine mode, we should not populate missing fields
assertThat(event.getRepository().getSource(), is(nullValue()));
assertThat(event.getRepository().getParent(), is(nullValue()));
assertThat(event.getRepository().getUrl().toString(), is("https://github.com/hub4j-test-org/github-api"));
assertThat(event.getRepository().getHttpTransportUrl().toString(),
is("https://github.com/hub4j-test-org/github-api.git"));
// Test repository populate
event = gitHub.parseEventPayload(payload.asReader(mockGitHub::mapToMockGitHub), GHEventPayload.Push.class);
assertThat(event.getRepository().getUrl().toString(), is("https://github.com/hub4j-test-org/github-api"));
assertThat(event.getRepository().getHttpTransportUrl(), is("https://github.com/hub4j-test-org/github-api.git"));
event.getRepository().populate();
// After populate the url is fixed to point to the correct API endpoint
assertThat(event.getRepository().getUrl().toString(),
is(mockGitHub.apiServer().baseUrl() + "/repos/hub4j-test-org/github-api"));
assertThat(event.getRepository().getHttpTransportUrl(), is("https://github.com/hub4j-test-org/github-api.git"));
// ensure that root has been bound after populate
event.getRepository().getSource().getRef("heads/master");
event.getRepository().getParent().getRef("heads/master");
// Source
event = gitHub.parseEventPayload(payload.asReader(mockGitHub::mapToMockGitHub), GHEventPayload.Push.class);
assertThat(event.getRepository().getSource().getFullName(), is("hub4j/github-api"));
// Parent
event = gitHub.parseEventPayload(payload.asReader(mockGitHub::mapToMockGitHub), GHEventPayload.Push.class);
assertThat(event.getRepository().getParent().getFullName(), is("hub4j/github-api"));
}
// TODO implement support classes and write test
// @Test
// public void release() throws Exception {}

View File

@@ -340,9 +340,8 @@ public class GHRateLimitTest extends AbstractGitHubWireMockTest {
fail("Invalid rate limit missing some records should throw");
} catch (Exception e) {
assertThat(e, instanceOf(HttpException.class));
assertThat(e.getCause(), instanceOf(IOException.class));
assertThat(e.getCause().getCause(), instanceOf(ValueInstantiationException.class));
assertThat(e.getCause().getCause().getMessage(),
assertThat(e.getCause(), instanceOf(ValueInstantiationException.class));
assertThat(e.getCause().getMessage(),
containsString(
"Cannot construct instance of `org.kohsuke.github.GHRateLimit`, problem: `java.lang.NullPointerException`"));
}
@@ -352,9 +351,8 @@ public class GHRateLimitTest extends AbstractGitHubWireMockTest {
fail("Invalid rate limit record missing a value should throw");
} catch (Exception e) {
assertThat(e, instanceOf(HttpException.class));
assertThat(e.getCause(), instanceOf(IOException.class));
assertThat(e.getCause().getCause(), instanceOf(MismatchedInputException.class));
assertThat(e.getCause().getCause().getMessage(),
assertThat(e.getCause(), instanceOf(MismatchedInputException.class));
assertThat(e.getCause().getMessage(),
containsString("Missing required creator property 'reset' (index 2)"));
}

View File

@@ -10,7 +10,11 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.function.Function;
import javax.annotation.Nonnull;
/**
* @author Stephen Connolly
@@ -79,6 +83,11 @@ public class PayloadRule implements TestRule {
return new InputStreamReader(asInputStream(), Charset.defaultCharset());
}
public Reader asReader(@Nonnull Function<String, String> transformer) throws IOException {
String payloadString = asString();
return new StringReader(transformer.apply(payloadString));
}
public Reader asReader(String encoding) throws IOException {
return new InputStreamReader(asInputStream(), encoding);
}

View File

@@ -1,14 +1,12 @@
package org.kohsuke.github.junit;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
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.http.*;
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
import com.github.tomakehurst.wiremock.verification.*;
import com.google.gson.*;
import java.io.File;
@@ -20,6 +18,8 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.common.Gzip.unGzipToString;
@@ -264,6 +264,24 @@ public class GitHubWireMockRule extends WireMockMultiServerRule {
return targetPath;
}
@Nonnull
public String mapToMockGitHub(String body) {
body = body.replace("https://api.github.com", this.apiServer().baseUrl());
if (this.rawServer() != null) {
body = body.replace("https://raw.githubusercontent.com", this.rawServer().baseUrl());
} else {
body = body.replace("https://raw.githubusercontent.com", this.apiServer().baseUrl() + "/raw");
}
if (this.uploadsServer() != null) {
body = body.replace("https://uploads.github.com", this.uploadsServer().baseUrl());
} else {
body = body.replace("https://uploads.github.com", this.apiServer().baseUrl() + "/uploads");
}
return body;
}
/**
* A number of modifications are needed as runtime to make responses target the WireMock server and not accidentally
* switch to using the live github servers.
@@ -286,19 +304,7 @@ public class GitHubWireMockRule extends WireMockMultiServerRule {
String body;
body = getBodyAsString(response, headers);
body = body.replace("https://api.github.com", rule.apiServer().baseUrl());
if (rule.rawServer() != null) {
body = body.replace("https://raw.githubusercontent.com", rule.rawServer().baseUrl());
} else {
body = body.replace("https://raw.githubusercontent.com", rule.apiServer().baseUrl() + "/raw");
}
if (rule.uploadsServer() != null) {
body = body.replace("https://uploads.github.com", rule.uploadsServer().baseUrl());
} else {
body = body.replace("https://uploads.github.com", rule.apiServer().baseUrl() + "/uploads");
}
body = rule.mapToMockGitHub(body);
builder.body(body);