Skip to content

Commit a68a34d

Browse files
committed
review and extended tests
1 parent 55c011b commit a68a34d

4 files changed

Lines changed: 211 additions & 3 deletions

File tree

src/main/java/com/spotify/github/v3/clients/GitHubClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ CompletableFuture<Response> delete(final String path) {
500500
* Make an http DELETE request for the given path.
501501
*
502502
* @param path relative to the Github base url
503+
* @param data request body as stringified JSON
503504
* @return response body as String
504505
*/
505506
CompletableFuture<Response> delete(final String path, final String data) {
@@ -654,7 +655,7 @@ public void onResponse(final Call call, final Response response) {
654655

655656
private RequestNotOkException mapException(final Response res, final Request request)
656657
throws IOException {
657-
String bodyString = res.body().string();
658+
String bodyString = res.body() != null ? res.body().string() : "";
658659
if (res.code() == FORBIDDEN) {
659660
if (bodyString.contains("Repository was archived so is read-only")) {
660661
return new ReadOnlyRepositoryException(request.url().encodedPath(), res.code(), bodyString);

src/main/java/com/spotify/github/v3/clients/PullRequestClient.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public CompletableFuture<PullRequest> requestReview(final int number, final Requ
211211
public CompletableFuture<Void> removeRequestedReview(final int number, final RequestReviewParameters properties) {
212212
final String path = String.format(PR_REVIEW_REQUESTS_TEMPLATE, owner, repo, number);
213213
final String jsonPayload = github.json().toJsonUnchecked(properties);
214-
log.debug("Requesting reviews for PR: " + path);
214+
log.debug("Removing requested reviews for PR: " + path);
215215
return github.delete(path, jsonPayload).thenAccept(IGNORE_RESPONSE_CONSUMER);
216216
}
217217

@@ -235,5 +235,4 @@ private CompletableFuture<List<PullRequestItem>> list(final String parameterPath
235235
return github.request(path, LIST_PR_TYPE_REFERENCE);
236236
}
237237

238-
239238
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.clients;
22+
23+
import static com.google.common.io.Resources.getResource;
24+
import static java.nio.charset.Charset.defaultCharset;
25+
import static org.junit.Assert.assertEquals;
26+
import static org.mockito.ArgumentMatchers.any;
27+
import static org.mockito.Mockito.doNothing;
28+
import static org.mockito.Mockito.mock;
29+
import static org.mockito.Mockito.when;
30+
31+
import com.google.common.collect.ImmutableList;
32+
import com.google.common.io.Resources;
33+
import com.spotify.github.v3.exceptions.RequestNotOkException;
34+
import com.spotify.github.v3.prs.ImmutableRequestReviewParameters;
35+
import com.spotify.github.v3.prs.ReviewRequests;
36+
import java.io.IOException;
37+
import java.net.URI;
38+
import java.util.concurrent.CompletableFuture;
39+
import java.util.concurrent.ExecutionException;
40+
import okhttp3.Call;
41+
import okhttp3.Callback;
42+
import okhttp3.MediaType;
43+
import okhttp3.OkHttpClient;
44+
import okhttp3.Protocol;
45+
import okhttp3.Request;
46+
import okhttp3.Response;
47+
import okhttp3.ResponseBody;
48+
import org.junit.Before;
49+
import org.junit.Test;
50+
import org.mockito.ArgumentCaptor;
51+
52+
public class PullRequestClientTest {
53+
54+
private GitHubClient github;
55+
private OkHttpClient client;
56+
57+
private static String getFixture(String resource) throws IOException {
58+
return Resources.toString(getResource(PullRequestClientTest.class, resource), defaultCharset());
59+
}
60+
61+
@Before
62+
public void setUp() {
63+
client = mock(OkHttpClient.class);
64+
github = GitHubClient.create(client, URI.create("http://bogus"), "token");
65+
}
66+
67+
@Test
68+
public void testListReviewRequests() throws Throwable {
69+
final Call call = mock(Call.class);
70+
final ArgumentCaptor<Callback> capture = ArgumentCaptor.forClass(Callback.class);
71+
doNothing().when(call).enqueue(capture.capture());
72+
73+
final Response response =
74+
new Response.Builder()
75+
.code(200)
76+
.protocol(Protocol.HTTP_1_1)
77+
.message("OK")
78+
.body(
79+
ResponseBody.create(
80+
MediaType.get("application/json"),
81+
getFixture("requestedReviews.json")))
82+
.request(new Request.Builder().url("http://localhost/").build())
83+
.build();
84+
85+
when(client.newCall(any())).thenReturn(call);
86+
87+
final PullRequestClient pullRequestClient =
88+
PullRequestClient.create(github, "owner", "repo");
89+
90+
final CompletableFuture<ReviewRequests> result =
91+
pullRequestClient.listReviewRequests(1);
92+
93+
capture.getValue().onResponse(call, response);
94+
95+
ReviewRequests reviewRequests = result.get();
96+
97+
assertEquals(1, reviewRequests.users().size());
98+
assertEquals("octocat", reviewRequests.users().get(0).login());
99+
assertEquals(1, reviewRequests.teams().size());
100+
assertEquals("justice-league", reviewRequests.teams().get(0).slug());
101+
}
102+
103+
@Test
104+
public void testRemoveRequestedReview() throws Throwable {
105+
106+
final Call call = mock(Call.class);
107+
final ArgumentCaptor<Callback> capture = ArgumentCaptor.forClass(Callback.class);
108+
doNothing().when(call).enqueue(capture.capture());
109+
110+
final Response response =
111+
new Response.Builder()
112+
.code(200)
113+
.protocol(Protocol.HTTP_1_1)
114+
.message("OK")
115+
.request(new Request.Builder().url("http://localhost/").build())
116+
.build();
117+
118+
when(client.newCall(any())).thenReturn(call);
119+
120+
PullRequestClient pullRequestClient =
121+
PullRequestClient.create(github, "owner", "repo");
122+
123+
CompletableFuture<Void> result =
124+
pullRequestClient.removeRequestedReview(1, ImmutableRequestReviewParameters.builder()
125+
.reviewers(ImmutableList.of("user1", "user2"))
126+
.build());
127+
128+
capture.getValue().onResponse(call, response);
129+
130+
result.get();
131+
// Passes without throwing
132+
}
133+
134+
@Test(expected = RequestNotOkException.class)
135+
public void testRemoveRequestedReview_failure() throws Throwable {
136+
137+
final Call call = mock(Call.class);
138+
final ArgumentCaptor<Callback> capture = ArgumentCaptor.forClass(Callback.class);
139+
doNothing().when(call).enqueue(capture.capture());
140+
141+
final Response response =
142+
new Response.Builder()
143+
.code(400)
144+
.protocol(Protocol.HTTP_1_1)
145+
.message("Failed")
146+
.request(new Request.Builder().url("http://localhost/").build())
147+
.build();
148+
149+
when(client.newCall(any())).thenReturn(call);
150+
151+
PullRequestClient pullRequestClient =
152+
PullRequestClient.create(github, "owner", "repo");
153+
154+
CompletableFuture<Void> result =
155+
pullRequestClient.removeRequestedReview(1, ImmutableRequestReviewParameters.builder()
156+
.reviewers(ImmutableList.of("user1", "user2"))
157+
.build());
158+
159+
capture.getValue().onResponse(call, response);
160+
161+
try {
162+
result.get();
163+
} catch (ExecutionException e) {
164+
throw e.getCause();
165+
// expecting RequestNotOkException
166+
}
167+
}
168+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"users": [
3+
{
4+
"login": "octocat",
5+
"id": 1,
6+
"node_id": "MDQ6VXNlcjE=",
7+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
8+
"gravatar_id": "",
9+
"url": "https://api.github.com/users/octocat",
10+
"html_url": "https://github.com/octocat",
11+
"followers_url": "https://api.github.com/users/octocat/followers",
12+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
13+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
14+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
15+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
16+
"organizations_url": "https://api.github.com/users/octocat/orgs",
17+
"repos_url": "https://api.github.com/users/octocat/repos",
18+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
19+
"received_events_url": "https://api.github.com/users/octocat/received_events",
20+
"type": "User",
21+
"site_admin": false
22+
}
23+
],
24+
"teams": [
25+
{
26+
"id": 1,
27+
"node_id": "MDQ6VGVhbTE=",
28+
"url": "https://api.github.com/teams/1",
29+
"html_url": "https://api.github.com/teams/justice-league",
30+
"name": "Justice League",
31+
"slug": "justice-league",
32+
"description": "A great team.",
33+
"privacy": "closed",
34+
"permission": "admin",
35+
"members_url": "https://api.github.com/teams/1/members{/member}",
36+
"repositories_url": "https://api.github.com/teams/1/repos",
37+
"parent": null
38+
}
39+
]
40+
}

0 commit comments

Comments
 (0)