|
| 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 | +} |
0 commit comments