|
20 | 20 |
|
21 | 21 | package com.spotify.github.v3.clients;
|
22 | 22 |
|
| 23 | +import static com.google.common.collect.ImmutableMap.of; |
23 | 24 | import static com.google.common.io.Resources.getResource;
|
24 | 25 | import static java.nio.charset.Charset.defaultCharset;
|
| 26 | +import static java.util.concurrent.CompletableFuture.completedFuture; |
| 27 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | +import static org.hamcrest.core.Is.is; |
25 | 29 | import static org.junit.Assert.assertEquals;
|
26 | 30 | import static org.mockito.ArgumentMatchers.any;
|
27 | 31 | import static org.mockito.Mockito.doNothing;
|
28 | 32 | import static org.mockito.Mockito.mock;
|
29 | 33 | import static org.mockito.Mockito.when;
|
30 | 34 |
|
31 | 35 | import com.google.common.collect.ImmutableList;
|
| 36 | +import com.google.common.collect.ImmutableMap; |
32 | 37 | import com.google.common.io.Resources;
|
| 38 | +import com.spotify.github.jackson.Json; |
33 | 39 | import com.spotify.github.v3.exceptions.RequestNotOkException;
|
| 40 | +import com.spotify.github.v3.git.Reference; |
34 | 41 | import com.spotify.github.v3.prs.ImmutableRequestReviewParameters;
|
| 42 | +import com.spotify.github.v3.prs.PullRequest; |
35 | 43 | import com.spotify.github.v3.prs.ReviewRequests;
|
| 44 | +import com.spotify.github.v3.prs.requests.ImmutablePullRequestCreate; |
| 45 | +import com.spotify.github.v3.prs.requests.ImmutablePullRequestUpdate; |
| 46 | +import com.spotify.github.v3.prs.requests.PullRequestCreate; |
| 47 | +import com.spotify.github.v3.prs.requests.PullRequestUpdate; |
36 | 48 | import java.io.IOException;
|
37 | 49 | import java.net.URI;
|
38 | 50 | import java.util.concurrent.CompletableFuture;
|
@@ -64,6 +76,88 @@ public void setUp() {
|
64 | 76 | github = GitHubClient.create(client, URI.create("http://bogus"), "token");
|
65 | 77 | }
|
66 | 78 |
|
| 79 | + @Test |
| 80 | + public void createPullRequest() throws Exception { |
| 81 | + final String title = "Amazing new feature"; |
| 82 | + final String body = "Please pull these awesome changes in!"; |
| 83 | + final String head = "octocat:new-topic"; |
| 84 | + final String base = "master"; |
| 85 | + |
| 86 | + final Call call = mock(Call.class); |
| 87 | + final ArgumentCaptor<Callback> capture = ArgumentCaptor.forClass(Callback.class); |
| 88 | + doNothing().when(call).enqueue(capture.capture()); |
| 89 | + |
| 90 | + final Response response = |
| 91 | + new Response.Builder() |
| 92 | + .code(201) |
| 93 | + .protocol(Protocol.HTTP_1_1) |
| 94 | + .message("Created") |
| 95 | + .body( |
| 96 | + ResponseBody.create( |
| 97 | + MediaType.get("application/json"), |
| 98 | + getFixture("pull_request.json"))) |
| 99 | + .request(new Request.Builder().url("http://localhost/").build()) |
| 100 | + .build(); |
| 101 | + |
| 102 | + when(client.newCall(any())).thenReturn(call); |
| 103 | + |
| 104 | + final PullRequestClient pullRequestClient = |
| 105 | + PullRequestClient.create(github, "owner", "repo"); |
| 106 | + |
| 107 | + final PullRequestCreate request = ImmutablePullRequestCreate.builder().title(title).body(body) |
| 108 | + .head(head).base(base).build(); |
| 109 | + |
| 110 | + final CompletableFuture<PullRequest> result = pullRequestClient.create(request); |
| 111 | + |
| 112 | + capture.getValue().onResponse(call, response); |
| 113 | + |
| 114 | + PullRequest pullRequest = result.get(); |
| 115 | + |
| 116 | + assertThat(pullRequest.title(), is(title)); |
| 117 | + assertThat(pullRequest.body().get(), is(body)); |
| 118 | + assertThat(pullRequest.head().label().get(), is(head)); |
| 119 | + assertThat(pullRequest.base().ref(), is(base)); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void updatePullRequest() throws Exception { |
| 124 | + final String title = "Amazing new feature"; |
| 125 | + final String body = "Please pull these awesome changes in!"; |
| 126 | + |
| 127 | + final Call call = mock(Call.class); |
| 128 | + final ArgumentCaptor<Callback> capture = ArgumentCaptor.forClass(Callback.class); |
| 129 | + doNothing().when(call).enqueue(capture.capture()); |
| 130 | + |
| 131 | + final Response response = |
| 132 | + new Response.Builder() |
| 133 | + .code(200) |
| 134 | + .protocol(Protocol.HTTP_1_1) |
| 135 | + .message("OK") |
| 136 | + .body( |
| 137 | + ResponseBody.create( |
| 138 | + MediaType.get("application/json"), |
| 139 | + getFixture("pull_request.json"))) |
| 140 | + .request(new Request.Builder().url("http://localhost/").build()) |
| 141 | + .build(); |
| 142 | + |
| 143 | + when(client.newCall(any())).thenReturn(call); |
| 144 | + |
| 145 | + final PullRequestClient pullRequestClient = |
| 146 | + PullRequestClient.create(github, "owner", "repo"); |
| 147 | + |
| 148 | + final PullRequestUpdate request = ImmutablePullRequestUpdate.builder().title(title).body(body) |
| 149 | + .build(); |
| 150 | + |
| 151 | + final CompletableFuture<PullRequest> result = pullRequestClient.update(1, request); |
| 152 | + |
| 153 | + capture.getValue().onResponse(call, response); |
| 154 | + |
| 155 | + PullRequest pullRequest = result.get(); |
| 156 | + |
| 157 | + assertThat(pullRequest.title(), is(title)); |
| 158 | + assertThat(pullRequest.body().get(), is(body)); |
| 159 | + } |
| 160 | + |
67 | 161 | @Test
|
68 | 162 | public void testListReviewRequests() throws Throwable {
|
69 | 163 | final Call call = mock(Call.class);
|
|
0 commit comments