-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathGitHubStaticTest.java
More file actions
464 lines (376 loc) · 20.8 KB
/
GitHubStaticTest.java
File metadata and controls
464 lines (376 loc) · 20.8 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package org.kohsuke.github;
import org.junit.Assert;
import org.junit.Test;
import org.kohsuke.github.connector.GitHubConnectorResponse;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThrows;
// TODO: Auto-generated Javadoc
/**
* Unit test for {@link GitHub} static helpers.
*
* @author Liam Newman
*/
public class GitHubStaticTest extends AbstractGitHubWireMockTest {
/**
* Format instant.
*
* @param instant
* the instant
* @param format
* the format
* @return the string
*/
static String formatInstant(Instant instant, String format) {
return formatZonedInstant(instant, format, "GMT");
}
/**
* Format zoned instant.
*
* @param instant
* the instant
* @param format
* the format
* @param timeZone
* the time zone
* @return the string
*/
static String formatZonedInstant(Instant instant, String format, String timeZone) {
return DateTimeFormatter.ofPattern(format, Locale.ENGLISH)
.format(instant.atZone(ZoneId.of(timeZone, ZoneId.SHORT_IDS)));
}
/**
* Create default GitHubStaticTest instance
*/
public GitHubStaticTest() {
}
/**
* Test from record.
*/
@Test
public void testFromRecord() {
final long stableInstantEpochSeconds = 11610674762L;
GHRateLimit rateLimit_none = GHRateLimit.fromRecord(new GHRateLimit.Record(9876,
5432,
(stableInstantEpochSeconds + Duration.ofMinutes(30).toMillis()) / 1000L), RateLimitTarget.NONE);
GHRateLimit rateLimit_core = GHRateLimit.fromRecord(new GHRateLimit.Record(9876,
5432,
(stableInstantEpochSeconds + Duration.ofMinutes(30).toMillis()) / 1000L), RateLimitTarget.CORE);
GHRateLimit rateLimit_search = GHRateLimit.fromRecord(new GHRateLimit.Record(19876,
15432,
(stableInstantEpochSeconds + Duration.ofHours(1).toMillis()) / 1000L), RateLimitTarget.SEARCH);
GHRateLimit rateLimit_graphql = GHRateLimit.fromRecord(new GHRateLimit.Record(29876,
25432,
(stableInstantEpochSeconds + Duration.ofHours(2).toMillis()) / 1000L), RateLimitTarget.GRAPHQL);
GHRateLimit rateLimit_integration = GHRateLimit.fromRecord(
new GHRateLimit.Record(39876,
35432,
(stableInstantEpochSeconds + Duration.ofHours(3).toMillis()) / 1000L),
RateLimitTarget.INTEGRATION_MANIFEST);
assertThat(rateLimit_none, equalTo(rateLimit_core));
assertThat(rateLimit_none, not(sameInstance(rateLimit_core)));
assertThat(rateLimit_none.hashCode(), equalTo(rateLimit_core.hashCode()));
assertThat(rateLimit_none, equalTo(rateLimit_core));
assertThat(rateLimit_none, not(equalTo(rateLimit_search)));
assertThat(rateLimit_none.getCore(), not(sameInstance(rateLimit_core.getCore())));
assertThat(rateLimit_core.getRecord(RateLimitTarget.NONE), instanceOf(GHRateLimit.UnknownLimitRecord.class));
assertThat(rateLimit_core.getRecord(RateLimitTarget.NONE),
sameInstance(rateLimit_none.getRecord(RateLimitTarget.NONE)));
assertThat(rateLimit_core.getRecord(RateLimitTarget.SEARCH), sameInstance(rateLimit_search.getGraphQL()));
assertThat(rateLimit_search.getRecord(RateLimitTarget.GRAPHQL),
sameInstance(rateLimit_graphql.getIntegrationManifest()));
assertThat(rateLimit_graphql.getRecord(RateLimitTarget.INTEGRATION_MANIFEST),
sameInstance(rateLimit_integration.getCore()));
assertThat(rateLimit_integration.getRecord(RateLimitTarget.CORE), sameInstance(rateLimit_core.getSearch()));
assertThat(rateLimit_none.getRecord(RateLimitTarget.CORE).getLimit(), equalTo(9876));
assertThat(rateLimit_core.getRecord(RateLimitTarget.CORE).getLimit(), equalTo(9876));
assertThat(rateLimit_search.getRecord(RateLimitTarget.SEARCH).getLimit(), equalTo(19876));
assertThat(rateLimit_graphql.getRecord(RateLimitTarget.GRAPHQL).getLimit(), equalTo(29876));
assertThat(rateLimit_integration.getRecord(RateLimitTarget.INTEGRATION_MANIFEST).getLimit(), equalTo(39876));
assertThat(rateLimit_core.toString(), containsString("GHRateLimit {core {remaining=5432, limit=9876"));
assertThat(rateLimit_core.toString(), containsString("search {remaining=999999, limit=1000000"));
}
/**
* Test git hub rate limit should replace rate limit.
*
* @throws Exception
* the exception
*/
@Test
public void testGitHubRateLimitShouldReplaceRateLimit() throws Exception {
GHRateLimit.UnknownLimitRecord.reset();
GHRateLimit.UnknownLimitRecord.unknownLimitResetSeconds = 5;
GHRateLimit.Record unknown0 = GHRateLimit.UnknownLimitRecord.current();
Thread.sleep(1500);
GHRateLimit.UnknownLimitRecord.reset();
GHRateLimit.UnknownLimitRecord.unknownLimitResetSeconds = 5;
// For testing, we create an new unknown.
GHRateLimit.Record unknown1 = GHRateLimit.UnknownLimitRecord.current();
assertThat("Valid unknown should not replace an existing one, regardless of created or reset time",
unknown1.currentOrUpdated(unknown0),
sameInstance(unknown1));
assertThat("Valid unknown should not replace an existing one, regardless of created or reset time",
unknown0.currentOrUpdated(unknown1),
sameInstance(unknown0));
// Sleep to make different created time
Thread.sleep(1500);
// To reduce object creation: There is only one valid Unknown record at a time.
assertThat("Unknown current should should limit the creation of new unknown records",
unknown1,
sameInstance(GHRateLimit.UnknownLimitRecord.current()));
long epochSeconds = Instant.now().getEpochSecond();
GHRateLimit.Record record0 = new GHRateLimit.Record(10, 10, epochSeconds + 10L);
GHRateLimit.Record record1 = new GHRateLimit.Record(10, 9, epochSeconds + 10L);
GHRateLimit.Record record2 = new GHRateLimit.Record(10, 2, epochSeconds + 10L);
GHRateLimit.Record record3 = new GHRateLimit.Record(10, 10, epochSeconds + 20L);
GHRateLimit.Record record4 = new GHRateLimit.Record(10, 5, epochSeconds + 20L);
GHRateLimit.Record recordExpired0 = new GHRateLimit.Record(10, 10, epochSeconds - 1L);
GHRateLimit.Record recordExpired1 = new GHRateLimit.Record(10, 10, epochSeconds + 2L);
// Sleep to make expired and different created time
Thread.sleep(4000);
GHRateLimit.Record recordWorst = new GHRateLimit.Record(Integer.MAX_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE);
GHRateLimit.Record record00 = new GHRateLimit.Record(10, 10, epochSeconds + 10L);
GHRateLimit.Record unknownExpired0 = unknown0;
GHRateLimit.Record unknownExpired1 = unknown1;
unknown0 = GHRateLimit.UnknownLimitRecord.current();
// Rate-limit records maybe created and returned in different orders.
// We should update to the unexpired regular records over unknowns.
// After that, we should update to the candidate if its limit is lower or its reset is later.
assertThat("Expired unknowns should not replace another expired one, regardless of created or reset time",
unknownExpired0.currentOrUpdated(unknownExpired1),
sameInstance(unknownExpired0));
assertThat("Expired unknowns should not replace another expired one, regardless of created or reset time",
unknownExpired1.currentOrUpdated(unknownExpired0),
sameInstance(unknownExpired1));
assertThat("Expired unknown should not be replaced by expired earlier normal record",
unknownExpired0.currentOrUpdated(recordExpired0),
sameInstance(unknownExpired0));
assertThat("Expired normal record should not be replaced an expired earlier unknown record",
recordExpired0.currentOrUpdated(unknownExpired0),
sameInstance(recordExpired0));
assertThat("Expired unknown should be replaced by expired later normal record",
unknownExpired0.currentOrUpdated(recordExpired1),
sameInstance(recordExpired1));
assertThat(
"Expired later normal record should not be replaced an expired unknown record, regardless of created or reset time",
recordExpired1.currentOrUpdated(unknownExpired0),
sameInstance(recordExpired1));
assertThat("Valid unknown should not be replaced by an expired unknown",
unknown0.currentOrUpdated(unknownExpired0),
sameInstance(unknown0));
assertThat("Expired unknown should be replaced by valid unknown",
unknownExpired0.currentOrUpdated(unknown0),
sameInstance(unknown0));
assertThat("Valid unknown should replace an expired normal record",
recordExpired1.currentOrUpdated(unknown0),
sameInstance(unknown0));
assertThat("Valid unknown record should not be replaced by expired normal record",
unknown0.currentOrUpdated(recordExpired1),
sameInstance(unknown0));
// In normal comparision, expiration doesn't matter
assertThat("Expired normal should not be replaced by an earlier expired one",
recordExpired1.currentOrUpdated(recordExpired0),
sameInstance(recordExpired1));
assertThat("Expired normal should be replaced by a later expired one",
recordExpired0.currentOrUpdated(recordExpired1),
sameInstance(recordExpired1));
assertThat("Later worst record should be replaced by earlier record",
recordWorst.currentOrUpdated(record0),
sameInstance(record0));
assertThat("Later worst record should not replace earlier",
record0.currentOrUpdated(recordWorst),
sameInstance(record0));
assertThat("Equivalent record should not replace other",
record00.currentOrUpdated(record0),
sameInstance(record00));
assertThat("Equivalent record should not replace other",
record0.currentOrUpdated(record00),
sameInstance(record0));
assertThat("Higher limit record should be replaced by lower",
record0.currentOrUpdated(record1),
sameInstance(record1));
assertThat("Higher limit record should be replaced by lower",
record1.currentOrUpdated(record2),
sameInstance(record2));
assertThat("Lower limit record should not be replaced higher",
record2.currentOrUpdated(record1),
sameInstance(record2));
assertThat("Lower limit record should be replaced by higher limit record with later reset",
record2.currentOrUpdated(record3),
sameInstance(record3));
assertThat("Higher limit record should be replaced by lower limit record with later reset",
record1.currentOrUpdated(record4),
sameInstance(record4));
assertThat("Higher limit record should not be replaced by lower limit record with earlier reset",
record4.currentOrUpdated(record2),
sameInstance(record4));
}
/**
* Test git hub request get api URL.
*/
@Test
public void testGitHubRequest_getApiURL() {
assertThat(GitHubRequest.getApiURL("github.com", "/endpoint").toString(),
equalTo("https://api.github.com/endpoint"));
// This URL is completely invalid but doesn't throw
assertThat(GitHubRequest.getApiURL("github.com", "//endpoint&?").toString(),
equalTo("https://api.github.com//endpoint&?"));
assertThat(GitHubRequest.getApiURL("ftp://whoa.github.com", "/endpoint").toString(),
equalTo("ftp://whoa.github.com/endpoint"));
assertThat(GitHubRequest.getApiURL(null, "ftp://api.test.github.com/endpoint").toString(),
equalTo("ftp://api.test.github.com/endpoint"));
assertThat(
GitHubRequest
.getApiURL(null,
"https://api.github.com/repositories/694641495/contents/Alfred.alfredpreferences/workflows/menubar-search%20[3rd-Party]/menu/Package.swift?ref=073e7b4493d088fdf1995a74cf5da201a5795181")
.toString(),
equalTo(
"https://api.github.com/repositories/694641495/contents/Alfred.alfredpreferences/workflows/menubar-search%20%5B3rd-Party%5D/menu/Package.swift?ref=073e7b4493d088fdf1995a74cf5da201a5795181"));
GHException e;
e = Assert.assertThrows(GHException.class,
() -> GitHubRequest.getApiURL("gopher://whoa.github.com", "/endpoint"));
assertThat(e.getMessage(), equalTo("Unable to build GitHub API URL"));
assertThat(e.getCause(), instanceOf(MalformedURLException.class));
assertThat(e.getCause().getMessage(), equalTo("unknown protocol: gopher"));
e = Assert.assertThrows(GHException.class, () -> GitHubRequest.getApiURL("bogus", "/endpoint"));
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
assertThat(e.getCause().getMessage(), equalTo("URI is not absolute"));
e = Assert.assertThrows(GHException.class,
() -> GitHubRequest.getApiURL(null, "gopher://api.test.github.com/endpoint"));
assertThat(e.getCause(), instanceOf(MalformedURLException.class));
assertThat(e.getCause().getMessage(), equalTo("unknown protocol: gopher"));
}
/**
* Test mapping reader writer.
*
* @throws Exception
* the exception
*/
@Test
public void testMappingReaderWriter() throws Exception {
// This test ensures that data objects can be written and read in a raw form from string.
// This behavior is completely unsupported and should not be used but given that some
// clients, such as Jenkins Blue Ocean, have already implemented their own Jackson
// Reader and Writer that bind this library's data objects from outside this library
// this makes sure they don't break.
GHRepository repo = getTempRepository();
assertThat(repo.root(), not(nullValue()));
assertThat(repo.getResponseHeaderFields(), not(nullValue()));
String repoString = GitHub.getMappingObjectWriter().writeValueAsString(repo);
assertThat(repoString, not(nullValue()));
assertThat(repoString, containsString("testMappingReaderWriter"));
GHRepository readRepo = GitHubClient.getMappingObjectReader((GitHubConnectorResponse) null)
.forType(GHRepository.class)
.readValue(repoString);
// This should never happen if the internal method isn't used
final GHRepository readRepoFinal = readRepo;
assertThrows(NullPointerException.class, () -> readRepoFinal.root());
assertThat(readRepoFinal.isOffline(), is(true));
assertThat(readRepo.getResponseHeaderFields(), nullValue());
readRepo = GitHub.getMappingObjectReader().forType(GHRepository.class).readValue(repoString);
// This should never happen if the internal method isn't used
assertThat(readRepo.getResponseHeaderFields(), nullValue());
String readRepoString = GitHub.getMappingObjectWriter().writeValueAsString(readRepo);
assertThat(readRepoString, equalTo(repoString));
}
/**
* Test parse instant.
*/
@Test
public void testParseInstant() {
assertThat(GitHubClient.parseInstant(null), nullValue());
}
/**
* Test parse URL.
*
* @throws Exception
* the exception
*/
@Test
public void testParseURL() throws Exception {
assertThat(GitHubClient.parseURL("https://api.github.com"), equalTo(new URL("https://api.github.com")));
assertThat(GitHubClient.parseURL(null), nullValue());
try {
GitHubClient.parseURL("bogus");
fail();
} catch (IllegalStateException e) {
assertThat(e.getMessage(), equalTo("Invalid URL: bogus"));
}
}
/**
* Test raw url path invalid.
*/
@Test
public void testRawUrlPathInvalid() {
try {
gitHub.createRequest().setRawUrlPath("invalid.path.com");
fail();
} catch (GHException e) {
assertThat(e.getMessage(), equalTo("Raw URL must start with 'http'"));
}
}
/**
* Time round trip.
*/
@Test
public void timeRoundTrip() {
final long stableInstantEpochMilli = 1533721222255L;
Instant instantNow = Instant.ofEpochMilli(stableInstantEpochMilli);
Instant instantSeconds = instantNow.truncatedTo(ChronoUnit.SECONDS);
Instant instantMillis = instantNow.truncatedTo(ChronoUnit.MILLIS);
String instantFormatSlash = formatZonedInstant(instantMillis, "yyyy/MM/dd HH:mm:ss Z", "PST");
assertThat(instantFormatSlash, equalTo("2018/08/08 02:40:22 -0700"));
String instantFormatDash = formatInstant(instantMillis, "yyyy-MM-dd'T'HH:mm:ss'Z'");
assertThat(instantFormatDash, equalTo("2018-08-08T09:40:22Z"));
String instantFormatMillis = formatInstant(instantMillis, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
assertThat(instantFormatMillis, equalTo("2018-08-08T09:40:22.255Z"));
String instantFormatMillisZoned = formatZonedInstant(instantMillis, "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ", "PST");
assertThat(instantFormatMillisZoned, equalTo("2018-08-08T02:40:22.255-07:00"));
String instantSecondsFormatMillis = formatInstant(instantSeconds, "yyyy-MM-dd'T'HH:mm:ss.S'Z'");
assertThat(instantSecondsFormatMillis, equalTo("2018-08-08T09:40:22.0Z"));
String instantSecondsFormatMillisZoned = formatZonedInstant(instantSeconds,
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
"PST");
assertThat(instantSecondsFormatMillisZoned, equalTo("2018-08-08T02:40:22.000-07:00"));
String instantBadFormat = formatInstant(instantMillis, "yy-MM-dd'T'HH:mm'Z'");
assertThat(instantBadFormat, equalTo("18-08-08T09:40Z"));
assertThat(GitHubClient.parseInstant(GitHubClient.printInstant(instantSeconds)),
equalTo(GitHubClient.parseInstant(GitHubClient.printInstant(instantMillis))));
assertThat(GitHubClient.printInstant(instantSeconds), equalTo("2018-08-08T09:40:22Z"));
assertThat(GitHubClient.printInstant(GitHubClient.parseInstant(instantFormatMillisZoned)),
equalTo("2018-08-08T09:40:22Z"));
assertThat(instantSeconds, equalTo(GitHubClient.parseInstant(GitHubClient.printInstant(instantSeconds))));
// printDate will truncate to the nearest second, so it should not be equal
assertThat(instantMillis, not(equalTo(GitHubClient.parseInstant(GitHubClient.printInstant(instantMillis)))));
assertThat(instantSeconds, equalTo(GitHubClient.parseInstant(instantFormatSlash)));
assertThat(instantSeconds, equalTo(GitHubClient.parseInstant(instantFormatDash)));
// This parser does not truncate to the nearest second, so it will be equal
assertThat(instantMillis, equalTo(GitHubClient.parseInstant(instantFormatMillis)));
assertThat(instantMillis, equalTo(GitHubClient.parseInstant(instantFormatMillisZoned)));
assertThat(instantSeconds, equalTo(GitHubClient.parseInstant(instantSecondsFormatMillis)));
assertThat(instantSeconds, equalTo(GitHubClient.parseInstant(instantSecondsFormatMillisZoned)));
try {
GitHubClient.parseInstant(instantBadFormat);
fail("Bad time format should throw.");
} catch (DateTimeParseException e) {
assertThat(e.getMessage(), equalTo("Text '" + instantBadFormat + "' could not be parsed at index 0"));
}
final GitHubBridgeAdapterObject bridge = new GitHubBridgeAdapterObject() {
};
assertThat(bridge.instantToDate(null, null), nullValue());
assertThat(bridge.instantToDate(Instant.ofEpochMilli(stableInstantEpochMilli), null),
equalTo(Date.from(instantNow)));
}
}