-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
MockClientTest.java
326 lines (278 loc) · 11.6 KB
/
MockClientTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright 2012-2023 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign.mock;
import static feign.Util.UTF_8;
import static feign.Util.toByteArray;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import feign.Body;
import feign.Feign;
import feign.FeignException;
import feign.Headers;
import feign.Param;
import feign.Request;
import feign.RequestLine;
import feign.Response;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import feign.gson.GsonDecoder;
class MockClientTest {
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
@RequestLine("GET /repos/{owner}/{repo}/contributors?client_id={client_id}")
List<Contributor> contributors(@Param("client_id") String clientId,
@Param("owner") String owner,
@Param("repo") String repo);
@RequestLine("PATCH /repos/{owner}/{repo}/contributors")
List<Contributor> patchContributors(@Param("owner") String owner, @Param("repo") String repo);
@RequestLine("POST /repos/{owner}/{repo}/contributors")
@Headers({"Content-Type: application/json"})
@Body("%7B\"login\":\"{login}\",\"type\":\"{type}\"%7D")
Contributor create(@Param("owner") String owner,
@Param("repo") String repo,
@Param("login") String login,
@Param("type") String type);
}
static class Contributor {
String login;
int contributions;
}
class AssertionDecoder implements Decoder {
private final Decoder delegate;
public AssertionDecoder(Decoder delegate) {
this.delegate = delegate;
}
@Override
public Object decode(Response response, Type type)
throws IOException, DecodeException, FeignException {
assertThat(response.request()).isNotNull();
return delegate.decode(response, type);
}
}
private GitHub github;
private MockClient mockClient;
@BeforeEach
void setup() throws IOException {
try (InputStream input = getClass().getResourceAsStream("/fixtures/contributors.json")) {
byte[] data = toByteArray(input);
RequestKey postContributorKey =
RequestKey.builder(HttpMethod.POST, "/repos/netflix/feign/contributors")
.charset(UTF_8)
.headers(RequestHeaders.builder()
.add("Content-Length", "55")
.add("Content-Type", "application/json")
.build())
.body("{\"login\":\"velo_at_github\",\"type\":\"preposterous hacker\"}")
.build();
mockClient = new MockClient();
github = Feign.builder().decoder(new AssertionDecoder(new GsonDecoder()))
.client(mockClient.ok(HttpMethod.GET, "/repos/netflix/feign/contributors", data)
.ok(HttpMethod.GET, "/repos/netflix/feign/contributors?client_id=55")
.ok(HttpMethod.GET, "/repos/netflix/feign/contributors?client_id=7 7",
new ByteArrayInputStream(data))
.ok(postContributorKey, "{\"login\":\"velo\",\"contributions\":0}")
.noContent(HttpMethod.PATCH, "/repos/velo/feign-mock/contributors")
.add(HttpMethod.GET, "/repos/netflix/feign/contributors?client_id=1234567890",
HttpURLConnection.HTTP_NOT_FOUND)
.add(HttpMethod.GET, "/repos/netflix/feign/contributors?client_id=123456789",
HttpURLConnection.HTTP_INTERNAL_ERROR, new ByteArrayInputStream(data))
.add(HttpMethod.GET, "/repos/netflix/feign/contributors?client_id=123456789",
HttpURLConnection.HTTP_INTERNAL_ERROR, "")
.add(HttpMethod.GET, "/repos/netflix/feign/contributors?client_id=123456789",
HttpURLConnection.HTTP_INTERNAL_ERROR, data))
.target(new MockTarget<>(GitHub.class));
}
}
@Test
void hitMock() {
List<Contributor> contributors = github.contributors("netflix", "feign");
assertThat(contributors).hasSize(30);
mockClient.verifyStatus();
}
@Test
void missMock() {
try {
github.contributors("velo", "feign-mock");
fail("");
} catch (FeignException e) {
assertThat(e.getMessage()).contains("404");
}
}
@Test
void missHttpMethod() {
try {
github.patchContributors("netflix", "feign");
fail("");
} catch (FeignException e) {
assertThat(e.getMessage()).contains("404");
}
}
@Test
void paramsEncoding() {
List<Contributor> contributors = github.contributors("7 7", "netflix", "feign");
assertThat(contributors).hasSize(30);
mockClient.verifyStatus();
}
@Test
void verifyInvocation() {
RequestKey testRequestKey =
RequestKey.builder(HttpMethod.POST, "/repos/netflix/feign/contributors")
.headers(RequestHeaders.builder()
.add("Content-Length", "55")
.add("Content-Type", "application/json")
.build())
.body("{\"login\":\"velo_at_github\",\"type\":\"preposterous hacker\"}")
.build();
Contributor contribution =
github.create("netflix", "feign", "velo_at_github", "preposterous hacker");
// making sure it received a proper response
assertThat(contribution).isNotNull();
assertThat(contribution.login).isEqualTo("velo");
assertThat(contribution.contributions).isEqualTo(0);
List<Request> results =
mockClient.verifyTimes(HttpMethod.POST, "/repos/netflix/feign/contributors", 1);
assertThat(results).hasSize(1);
results = mockClient.verifyTimes(testRequestKey, 1);
assertThat(results).hasSize(1);
assertThat(mockClient.verifyOne(testRequestKey).body()).isNotNull();
byte[] body = mockClient.verifyOne(HttpMethod.POST, "/repos/netflix/feign/contributors")
.body();
assertThat(body).isNotNull();
String message = new String(body);
assertThat(message).contains("velo_at_github");
assertThat(message).contains("preposterous hacker");
mockClient.verifyStatus();
}
@Test
void verifyNone() {
RequestKey testRequestKey;
github.create("netflix", "feign", "velo_at_github", "preposterous hacker");
mockClient.verifyTimes(HttpMethod.POST, "/repos/netflix/feign/contributors", 1);
testRequestKey =
RequestKey.builder(HttpMethod.POST, "/repos/netflix/feign/contributors")
.charset(UTF_8)
.headers(RequestHeaders.builder()
.add("Content-Length", "55")
.add("Content-Type", "application/json")
.build())
// body is not equal
.body("{\"login\":\"velo[at]github\",\"type\":\"preposterous hacker\"}")
.build();
try {
mockClient.verifyOne(testRequestKey);
fail("");
} catch (VerificationAssertionError e) {
assertThat(e.getMessage()).contains("Wanted");
assertThat(e.getMessage()).contains("POST");
assertThat(e.getMessage()).contains("/repos/netflix/feign/contributors");
}
mockClient.verifyNever(testRequestKey);
testRequestKey =
RequestKey.builder(HttpMethod.POST, "/repos/netflix/feign/contributors")
.charset(UTF_8)
.headers(RequestHeaders.builder()
.add("Content-Length", "55")
.add("Content-Type", "application/json")
// headers are not equal
.add("X-Header", "qwerty")
.build())
.body("{\"login\":\"velo_at_github\",\"type\":\"preposterous hacker\"}")
.build();
try {
mockClient.verifyOne(testRequestKey);
fail("");
} catch (VerificationAssertionError e) {
assertThat(e.getMessage()).contains("Wanted");
assertThat(e.getMessage()).contains("POST");
assertThat(e.getMessage()).contains("/repos/netflix/feign/contributors");
}
mockClient.verifyNever(testRequestKey);
try {
mockClient.verifyTimes(HttpMethod.POST, "/repos/netflix/feign/contributors", 0);
fail("");
} catch (VerificationAssertionError e) {
assertThat(e.getMessage()).contains("Do not wanted");
assertThat(e.getMessage()).contains("POST");
assertThat(e.getMessage()).contains("/repos/netflix/feign/contributors");
}
try {
mockClient.verifyTimes(HttpMethod.POST, "/repos/netflix/feign/contributors", 3);
fail("");
} catch (VerificationAssertionError e) {
assertThat(e.getMessage()).contains("Wanted");
assertThat(e.getMessage()).contains("POST");
assertThat(e.getMessage()).contains("/repos/netflix/feign/contributors");
assertThat(e.getMessage()).contains("'3'");
assertThat(e.getMessage()).contains("'1'");
}
}
@Test
void verifyNotInvoked() {
mockClient.verifyNever(HttpMethod.POST, "/repos/netflix/feign/contributors");
List<Request> results =
mockClient.verifyTimes(HttpMethod.POST, "/repos/netflix/feign/contributors", 0);
assertThat(results).hasSize(0);
try {
mockClient.verifyOne(HttpMethod.POST, "/repos/netflix/feign/contributors");
fail("");
} catch (VerificationAssertionError e) {
assertThat(e.getMessage()).contains("Wanted");
assertThat(e.getMessage()).contains("POST");
assertThat(e.getMessage()).contains("/repos/netflix/feign/contributors");
assertThat(e.getMessage()).contains("never invoked");
}
}
@Test
void verifyNegative() {
try {
mockClient.verifyTimes(HttpMethod.POST, "/repos/netflix/feign/contributors", -1);
fail("");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("non negative");
}
}
@Test
void verifyMultipleRequests() {
mockClient.verifyNever(HttpMethod.POST, "/repos/netflix/feign/contributors");
github.create("netflix", "feign", "velo_at_github", "preposterous hacker");
Request result = mockClient.verifyOne(HttpMethod.POST, "/repos/netflix/feign/contributors");
assertThat(result).isNotNull();
github.create("netflix", "feign", "velo_at_github", "preposterous hacker");
List<Request> results =
mockClient.verifyTimes(HttpMethod.POST, "/repos/netflix/feign/contributors", 2);
assertThat(results).hasSize(2);
github.create("netflix", "feign", "velo_at_github", "preposterous hacker");
results = mockClient.verifyTimes(HttpMethod.POST, "/repos/netflix/feign/contributors", 3);
assertThat(results).hasSize(3);
mockClient.verifyStatus();
}
@Test
void resetRequests() {
mockClient.verifyNever(HttpMethod.POST, "/repos/netflix/feign/contributors");
github.create("netflix", "feign", "velo_at_github", "preposterous hacker");
Request result = mockClient.verifyOne(HttpMethod.POST, "/repos/netflix/feign/contributors");
assertThat(result).isNotNull();
mockClient.resetRequests();
mockClient.verifyNever(HttpMethod.POST, "/repos/netflix/feign/contributors");
}
}