use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project google-api-java-client by google.
the class MockGoogleCredential method newMockHttpTransportWithSampleTokenResponse.
/**
* Returns a new {@link MockHttpTransport} with a sample {@link MockLowLevelHttpResponse}. The
* response includes sample TokenResponse content as specified in DEFAULT_TOKEN_RESPONSE_JSON. This
* is meant to produce a minimal implementation that allows methods such as
* {@link GoogleCredential#executeRefreshToken()} to be called without failing abruptly. This
* content is static. If you are making assertions based on the content of the request, then
* MockTokenServerTransport should be used instead.
*
* @return mockHttpTransport
*/
public static MockHttpTransport newMockHttpTransportWithSampleTokenResponse() {
MockLowLevelHttpResponse mockLowLevelHttpResponse = new MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE).setContent(DEFAULT_TOKEN_RESPONSE_JSON);
MockLowLevelHttpRequest request = new MockLowLevelHttpRequest().setResponse(mockLowLevelHttpResponse);
return new MockHttpTransport.Builder().setLowLevelHttpRequest(request).build();
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project copybara by google.
the class GithubPrDestinationTest method checkWrite.
private void checkWrite(String groupId) throws ValidationException, RepoException, IOException {
gitApiMockHttpTransport = new GitApiMockHttpTransport() {
@Override
protected byte[] getContent(String method, String url, MockLowLevelHttpRequest request) throws IOException {
boolean isPulls = "https://api.github.com/repos/foo/pulls".equals(url);
if ("GET".equals(method) && isPulls) {
return "[]".getBytes(UTF_8);
} else if ("POST".equals(method) && isPulls) {
assertThat(request.getContentAsString()).isEqualTo("{\"base\":\"master\",\"body\":\"test summary\",\"head\":\"feature\",\"title\":\"test summary\"}");
return ("{\n" + " \"id\": 1,\n" + " \"number\": 12345,\n" + " \"state\": \"open\",\n" + " \"title\": \"test summary\",\n" + " \"body\": \"test summary\"" + "}").getBytes();
}
fail(method + " " + url);
throw new IllegalStateException();
}
};
GithubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + " url = 'https://github.com/foo'" + ")");
Writer<GitRevision> writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
false, groupId, /*oldWriter=*/
null);
GitRepository remote = localHubRepo("foo");
addFiles(remote, null, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").build());
Files.write(this.workdir.resolve("test.txt"), "some content".getBytes());
writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), console);
Files.write(this.workdir.resolve("test.txt"), "other content".getBytes());
writer.write(TransformResults.of(this.workdir, new DummyRevision("two")), console);
// Use a new writer that shares the old state
writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
false, groupId, /*oldWriter=*/
writer);
Files.write(this.workdir.resolve("test.txt"), "and content".getBytes());
writer.write(TransformResults.of(this.workdir, new DummyRevision("three")), console);
console.assertThat().timesInLog(1, MessageType.INFO, "Pull Request https://github.com/foo/pull/12345 created using branch 'feature'.");
assertThat(remote.refExists("feature")).isTrue();
assertThat(Iterables.transform(remote.log("feature").run(), GitLogEntry::getBody)).containsExactly("first change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: one\n", "test summary\n" + "\n" + "DummyOrigin-RevId: two\n", "test summary\n" + "\n" + "DummyOrigin-RevId: three\n");
// If we don't keep writer state (same as a new migration). We do a rebase of
// all the changes.
writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
false, groupId, /*oldWriter=*/
null);
Files.write(this.workdir.resolve("test.txt"), "and content".getBytes());
writer.write(TransformResults.of(this.workdir, new DummyRevision("four")), console);
assertThat(Iterables.transform(remote.log("feature").run(), GitLogEntry::getBody)).containsExactly("first change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: four\n");
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project copybara by google.
the class GithubPrDestinationTest method testWriteNoMaster.
@Test
public void testWriteNoMaster() throws ValidationException, IOException, RepoException {
gitApiMockHttpTransport = new GitApiMockHttpTransport() {
@Override
protected byte[] getContent(String method, String url, MockLowLevelHttpRequest request) throws IOException {
boolean isPulls = "https://api.github.com/repos/foo/pulls".equals(url);
if ("GET".equals(method) && isPulls) {
return "[]".getBytes(UTF_8);
} else if ("POST".equals(method) && isPulls) {
assertThat(request.getContentAsString()).isEqualTo("{\"base\":\"other\",\"body\":\"test summary\",\"head\":\"feature\",\"title\":\"test summary\"}");
return ("{\n" + " \"id\": 1,\n" + " \"number\": 12345,\n" + " \"state\": \"open\",\n" + " \"title\": \"test summary\",\n" + " \"body\": \"test summary\"" + "}").getBytes();
}
fail(method + " " + url);
throw new IllegalStateException();
}
};
GithubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + " url = 'https://github.com/foo'," + " destination_ref = 'other'," + ")");
Writer<GitRevision> writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
false, "feature", /*oldWriter=*/
null);
GitRepository remote = localHubRepo("foo");
addFiles(remote, "master", "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").build());
addFiles(remote, "other", "second change", ImmutableMap.<String, String>builder().put("foo.txt", "test").build());
Files.write(this.workdir.resolve("test.txt"), "some content".getBytes());
writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), console);
assertThat(remote.refExists("feature")).isTrue();
assertThat(Iterables.transform(remote.log("feature").run(), GitLogEntry::getBody)).containsExactly("first change\n", "second change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: one\n");
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project copybara by google.
the class GithubPrOriginTest method testHookForGitHubPr.
@Test
public void testHookForGitHubPr() throws Exception {
GitRepository remote = localHubRepo("google/example");
GitRepository destination = localHubRepo("destination");
addFiles(remote, "base", ImmutableMap.<String, String>builder().put("test.txt", "a").build());
String lastRev = remote.parseRef("HEAD");
addFiles(remote, "one", ImmutableMap.<String, String>builder().put("test.txt", "b").build());
addFiles(remote, "two", ImmutableMap.<String, String>builder().put("test.txt", "c").build());
String prHeadSha1 = remote.parseRef("HEAD");
remote.simpleCommand("update-ref", GithubUtil.asHeadRef(123), prHeadSha1);
gitApiMockHttpTransport = new GitApiMockHttpTransport() {
@Override
protected byte[] getContent(String method, String url, MockLowLevelHttpRequest request) throws IOException {
if (url.contains("/status")) {
return ("{\n" + " state : 'success',\n" + " context : 'the_context'\n" + "}").getBytes(UTF_8);
}
return new MockPullRequest(123, ImmutableList.of(), "open").getContent(method, url, request);
}
};
Path dest = Files.createTempDirectory("");
options.folderDestination.localFolder = dest.toString();
options.setWorkdirToRealTempDir();
options.setForce(true);
options.setLastRevision(lastRev);
options.gitDestination.committerEmail = "commiter@email";
options.gitDestination.committerName = "Bara Kopi";
Workflow<GitRevision, ?> workflow = workflow("" + "def update_commit_status(ctx):\n" + " for effect in ctx.effects:\n" + " for origin_change in effect.origin_refs:\n" + " if effect.type == 'CREATED' or effect.type == 'UPDATED':\n" + " status = ctx.origin.create_status(\n" + " sha = origin_change.ref,\n" + " state = 'success',\n" + " context = 'copybara/import',\n" + " description = 'Migration success at ' " + "+ effect.destination_ref.id,\n" + " )\n" + "core.workflow(\n" + " name = 'default',\n" + " origin = git.github_pr_origin(\n" + " url = 'https://github.com/google/example',\n" + " ),\n" + " authoring = authoring.pass_thru('foo <foo@foo.com>'),\n" + " destination = git.destination(\n" + " url = '" + destination.getGitDir() + "'\n" + " ),\n" + " after_migration = [\n" + " update_commit_status" + " ]" + ")");
workflow.run(workdir, "123");
assertThat(gitApiMockHttpTransport.requests).hasSize(3);
assertThat(gitApiMockHttpTransport.requests.get(1).getRequest()).contains("Migration success at");
assertThat(gitApiMockHttpTransport.requests.get(2).getRequest()).contains("Migration success at");
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project copybara by google.
the class GerritDestinationTest method changeAlreadyMergedTooOften.
@Test
public void changeAlreadyMergedTooOften() throws Exception {
fetch = "master";
Files.write(workdir.resolve("file"), "some content".getBytes());
options.setForce(true);
String firstChangeId = "I" + Hashing.sha1().newHasher().putString("origin_ref", StandardCharsets.UTF_8).putString(options.gitDestination.committerEmail, StandardCharsets.UTF_8).putInt(0).hash();
String secondChangeId = "I" + Hashing.sha1().newHasher().putString("origin_ref", Charsets.UTF_8).putString(options.gitDestination.committerEmail, StandardCharsets.UTF_8).putInt(1).hash();
gitApiMockHttpTransport = new GitApiMockHttpTransport() {
@Override
protected byte[] getContent(String method, String url, MockLowLevelHttpRequest request) throws IOException {
if (method.equals("GET") && url.startsWith("https://localhost:33333/changes/")) {
String change = changeIdFromRequest(url);
String result = "[" + "{" + " change_id : \"" + change + "\"," + " status : \"MERGED\"" + "}]";
return result.getBytes(UTF_8);
}
throw new IllegalArgumentException(method + " " + url);
}
};
try {
process(new DummyRevision("origin_ref"));
fail("Should have thrown RepoException");
} catch (RepoException expected) {
assertThat(expected.getMessage()).contains("Unable to find unmerged change for ");
}
}
Aggregations