Skip to content

Commit 19754fe

Browse files
committed
test: Fix tests
1 parent 4ef2a54 commit 19754fe

File tree

7 files changed

+161
-143
lines changed

7 files changed

+161
-143
lines changed

src/main/java/com/spotify/github/http/okhttp/OkHttpHttpResponse.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class OkHttpHttpResponse implements HttpResponse {
3939
private final String body;
4040
private final Map<String, List<String>> headers;
4141

42-
public OkHttpHttpResponse(final HttpRequest request, final Response response) throws IOException {
42+
public OkHttpHttpResponse(final HttpRequest request, final Response response) {
4343
this.request = request;
4444
this.statusCode = response.code();
4545
this.statusMessage = response.message();
@@ -90,6 +90,9 @@ public boolean isSuccessful() {
9090
}
9191

9292
private static String responseBodyUnchecked(final Response response) {
93+
if (response.body() == null) {
94+
return null;
95+
}
9396
try (ResponseBody body = response.body()) {
9497
return body.string();
9598
} catch (IOException e) {

src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static org.mockito.Mockito.*;
3434

3535
import com.google.common.io.Resources;
36+
import com.spotify.github.http.HttpRequest;
3637
import com.spotify.github.tracing.Span;
3738
import com.spotify.github.tracing.Tracer;
3839
import com.spotify.github.v3.checks.CheckSuiteResponseList;
@@ -129,21 +130,12 @@ public void testSearchIssue() throws Throwable {
129130
.build();
130131

131132
when(client.newCall(any())).thenReturn(call);
132-
when(tracer.createTracedClient(client))
133-
.thenReturn(
134-
new Call.Factory() {
135-
@NotNull
136-
@Override
137-
public Call newCall(@NotNull final Request request) {
138-
return call;
139-
}
140-
});
141133
IssueClient issueClient =
142134
github.withTracer(tracer).createRepositoryClient("testorg", "testrepo").createIssueClient();
143135

144136
CompletableFuture<Void> maybeSucceeded = issueClient.editComment(1, "some comment");
145137
capture.getValue().onResponse(call, response);
146-
verify(tracer, times(1)).span(any(Request.class));
138+
verify(tracer, times(1)).span(any(HttpRequest.class));
147139

148140
Exception exception = assertThrows(ExecutionException.class, maybeSucceeded::get);
149141
Assertions.assertEquals(ReadOnlyRepositoryException.class, exception.getCause().getClass());
@@ -267,16 +259,6 @@ public void testGetWorkflow() throws Throwable {
267259
.request(new Request.Builder().url("http://localhost/").build())
268260
.build();
269261

270-
when(tracer.createTracedClient(any(OkHttpClient.class)))
271-
.thenReturn(
272-
new Call.Factory() {
273-
@NotNull
274-
@Override
275-
public Call newCall(@NotNull final Request request) {
276-
return call;
277-
}
278-
});
279-
280262
when(client.newCall(any())).thenReturn(call);
281263
WorkflowsClient client =
282264
github

src/test/java/com/spotify/github/v3/clients/IssueClientTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.google.common.io.Resources;
4444
import com.spotify.github.async.Async;
4545
import com.spotify.github.async.AsyncPage;
46+
import com.spotify.github.http.HttpResponse;
4647
import com.spotify.github.jackson.Json;
4748
import com.spotify.github.v3.ImmutableUser;
4849
import com.spotify.github.v3.comment.Comment;
@@ -83,13 +84,13 @@ public void testCommentPaginationSpliterator() throws IOException {
8384
"<https://github.com/api/v3/repos/someowner/somerepo/issues/123/comments?page=2>; rel=\"next\", <https://github.com/api/v3/repos/someowner/somerepo/issues/123/comments?page=2>; rel=\"last\"";
8485
final String firstPageBody =
8586
Resources.toString(getResource(this.getClass(), "comments_page1.json"), defaultCharset());
86-
final Response firstPageResponse = createMockResponse(firstPageLink, firstPageBody);
87+
final HttpResponse firstPageResponse = createMockResponse(firstPageLink, firstPageBody);
8788

8889
final String lastPageLink =
8990
"<https://github.com/api/v3/repos/someowner/somerepo/issues/123/comments>; rel=\"first\", <https://github.com/api/v3/repos/someowner/somerepo/issues/123/comments>; rel=\"prev\"";
9091
final String lastPageBody =
9192
Resources.toString(getResource(this.getClass(), "comments_page2.json"), defaultCharset());
92-
final Response lastPageResponse = createMockResponse(lastPageLink, lastPageBody);
93+
final HttpResponse lastPageResponse = createMockResponse(lastPageLink, lastPageBody);
9394

9495
when(github.request(format(COMMENTS_URI_NUMBER_TEMPLATE, "someowner", "somerepo", "123")))
9596
.thenReturn(completedFuture(firstPageResponse));
@@ -112,13 +113,13 @@ public void testCommentPaginationForeach() throws IOException {
112113
"<https://github.com/api/v3/repos/someowner/somerepo/issues/123/comments?page=2>; rel=\"next\", <https://github.com/api/v3/repos/someowner/somerepo/issues/123/comments?page=2>; rel=\"last\"";
113114
final String firstPageBody =
114115
Resources.toString(getResource(this.getClass(), "comments_page1.json"), defaultCharset());
115-
final Response firstPageResponse = createMockResponse(firstPageLink, firstPageBody);
116+
final HttpResponse firstPageResponse = createMockResponse(firstPageLink, firstPageBody);
116117

117118
final String lastPageLink =
118119
"<https://github.com/api/v3/repos/someowner/somerepo/issues/123/comments>; rel=\"first\", <https://github.com/api/v3/repos/someowner/somerepo/issues/123/comments>; rel=\"prev\"";
119120
final String lastPageBody =
120121
Resources.toString(getResource(this.getClass(), "comments_page2.json"), defaultCharset());
121-
final Response lastPageResponse = createMockResponse(lastPageLink, lastPageBody);
122+
final HttpResponse lastPageResponse = createMockResponse(lastPageLink, lastPageBody);
122123

123124
when(github.request(format(COMMENTS_URI_NUMBER_TEMPLATE, "someowner", "somerepo", "123")))
124125
.thenReturn(completedFuture(firstPageResponse));
@@ -142,7 +143,7 @@ public void testCommentPaginationForeach() throws IOException {
142143
@Test
143144
public void testCommentCreated() throws IOException {
144145
final String fixture = loadFixture("clients/comment_created.json");
145-
final Response response = createMockResponse("", fixture);
146+
final HttpResponse response = createMockResponse("", fixture);
146147
final String path = format(COMMENTS_URI_NUMBER_TEMPLATE, "someowner", "somerepo", 10);
147148
when(github.post(anyString(), anyString(), eq(Comment.class))).thenCallRealMethod();
148149
when(github.post(eq(path), anyString())).thenReturn(completedFuture(response));
@@ -154,7 +155,7 @@ public void testCommentCreated() throws IOException {
154155
@Test
155156
public void testCommentCreatedWithLargeId() throws IOException {
156157
final String fixture = loadFixture("clients/comment_created_long_id.json");
157-
final Response response = createMockResponse("", fixture);
158+
final HttpResponse response = createMockResponse("", fixture);
158159
final String path = format(COMMENTS_URI_NUMBER_TEMPLATE, "someowner", "somerepo", 10);
159160
when(github.post(anyString(), anyString(), eq(Comment.class))).thenCallRealMethod();
160161
when(github.post(eq(path), anyString())).thenReturn(completedFuture(response));
@@ -210,8 +211,8 @@ public void testDeleteIssueCommentReaction() {
210211
long reactionId = 385825;
211212
final String path =
212213
format(COMMENTS_REACTION_ID_TEMPLATE, "someowner", "somerepo", issueNumber, reactionId);
213-
Response mockResponse = mock(Response.class);
214-
when(mockResponse.code()).thenReturn(204);
214+
HttpResponse mockResponse = mock(HttpResponse.class);
215+
when(mockResponse.statusCode()).thenReturn(204);
215216
when(github.delete(eq(path))).thenReturn(completedFuture(mockResponse));
216217

217218
final var response = issueClient.deleteCommentReaction(issueNumber, reactionId).join();
@@ -239,7 +240,7 @@ public void testListIssueCommentReaction() throws IOException {
239240
"<https://github.com/api/v3/repos/someowner/somerepo/comments/%s/reactions?page=1>; rel=\"last\"",
240241
commentId);
241242
final String firstPageBody = github.json().toJsonUnchecked(listResponse.join().toArray());
242-
final Response firstPageResponse = createMockResponse(firstPageLink, firstPageBody);
243+
final HttpResponse firstPageResponse = createMockResponse(firstPageLink, firstPageBody);
243244

244245
when(github.request(eq(path))).thenReturn(completedFuture(firstPageResponse));
245246
final List<CommentReaction> listCommentReactions = Lists.newArrayList();

src/test/java/com/spotify/github/v3/clients/MockHelper.java

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,26 +20,78 @@
2020

2121
package com.spotify.github.v3.clients;
2222

23-
import static org.mockito.Mockito.mock;
24-
import static org.mockito.Mockito.when;
25-
23+
import com.spotify.github.http.HttpRequest;
24+
import com.spotify.github.http.HttpResponse;
2625
import java.io.IOException;
27-
import okhttp3.Headers;
28-
import okhttp3.Response;
29-
import okhttp3.ResponseBody;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Optional;
3029

3130
public class MockHelper {
32-
public static Response createMockResponse(final String headerLinksFixture, final String bodyFixture)
33-
throws IOException {
34-
final ResponseBody body = mock(ResponseBody.class);
35-
when(body.string()).thenReturn(bodyFixture);
36-
37-
final Headers headers = mock(Headers.class);
38-
when(headers.get("Link")).thenReturn(headerLinksFixture);
39-
40-
final Response response = mock(Response.class);
41-
when(response.headers()).thenReturn(headers);
42-
when(response.body()).thenReturn(body);
43-
return response;
31+
private static final int HTTP_OK = 200;
32+
private static final int HTTP_BAD_REQUEST = 400;
33+
34+
// public static Response createMockResponse(final String headerLinksFixture, final String
35+
// bodyFixture)
36+
// throws IOException {
37+
// final ResponseBody body = mock(ResponseBody.class);
38+
// when(body.string()).thenReturn(bodyFixture);
39+
//
40+
// final Headers headers = mock(Headers.class);
41+
// when(headers.get("Link")).thenReturn(headerLinksFixture);
42+
//
43+
// final Response response = mock(Response.class);
44+
// when(response.headers()).thenReturn(headers);
45+
// when(response.body()).thenReturn(body);
46+
// return response;
47+
// }
48+
49+
public static HttpResponse createMockResponse(
50+
final String headerLinksFixture, final String bodyFixture) throws IOException {
51+
int statusCode = 200;
52+
return createMockHttpResponse(
53+
"", statusCode, bodyFixture, Map.of("Link", List.of(headerLinksFixture)));
54+
}
55+
56+
public static HttpResponse createMockHttpResponse(
57+
final String url,
58+
final int statusCode,
59+
final String body,
60+
final Map<String, List<String>> headers) {
61+
return new HttpResponse() {
62+
@Override
63+
public HttpRequest request() {
64+
return ImmutableHttpRequest.builder.url(url).build();
65+
}
66+
67+
@Override
68+
public int statusCode() {
69+
return statusCode;
70+
}
71+
72+
@Override
73+
public String statusMessage() {
74+
return "";
75+
}
76+
77+
@Override
78+
public String body() {
79+
return body;
80+
}
81+
82+
@Override
83+
public Map<String, List<String>> headers() {
84+
return Optional.ofNullable(headers).orElse(Map.of());
85+
}
86+
87+
@Override
88+
public boolean isSuccessful() {
89+
return MockHelper.isSuccessful(statusCode);
90+
}
91+
};
92+
}
93+
94+
private static boolean isSuccessful(final int statusCode) {
95+
return statusCode >= HTTP_OK && statusCode < HTTP_BAD_REQUEST;
4496
}
45-
}
97+
}

0 commit comments

Comments
 (0)