-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathGitHubTest.java
More file actions
465 lines (404 loc) · 17.1 KB
/
GitHubTest.java
File metadata and controls
465 lines (404 loc) · 17.1 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
465
package org.kohsuke.github;
import com.google.common.collect.Iterables;
import org.junit.Assert;
import org.junit.Test;
import org.kohsuke.github.example.dataobject.ReadOnlyObjects;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import static org.hamcrest.Matchers.*;
import static org.kohsuke.github.GHMarketplaceAccountType.ORGANIZATION;
// TODO: Auto-generated Javadoc
/**
* Unit test for {@link GitHub}.
*/
public class GitHubTest extends AbstractGitHubWireMockTest {
/**
* Create default GitHubTest instance
*/
public GitHubTest() {
}
/**
* Gets the meta.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getMeta() throws IOException {
GHMeta meta = gitHub.getMeta();
assertThat(meta.isVerifiablePasswordAuthentication(), is(true));
assertThat(meta.getSshKeyFingerprints().size(), equalTo(4));
assertThat(meta.getSshKeys().size(), equalTo(3));
assertThat(meta.getApi().size(), equalTo(19));
assertThat(meta.getGit().size(), equalTo(36));
assertThat(meta.getHooks().size(), equalTo(4));
assertThat(meta.getImporter().size(), equalTo(3));
assertThat(meta.getPages().size(), equalTo(6));
assertThat(meta.getWeb().size(), equalTo(20));
assertThat(meta.getPackages().size(), equalTo(25));
assertThat(meta.getActions().size(), equalTo(1739));
assertThat(meta.getDependabot().size(), equalTo(3));
// Also test examples here
Class[] examples = new Class[]{ ReadOnlyObjects.GHMetaPublic.class, ReadOnlyObjects.GHMetaPackage.class,
ReadOnlyObjects.GHMetaGettersUnmodifiable.class, ReadOnlyObjects.GHMetaGettersFinal.class,
ReadOnlyObjects.GHMetaGettersFinalCreator.class, };
for (Class metaClass : examples) {
ReadOnlyObjects.GHMetaExample metaExample = gitHub.createRequest()
.withUrlPath("/meta")
.fetch((Class<ReadOnlyObjects.GHMetaExample>) metaClass);
assertThat(metaExample.isVerifiablePasswordAuthentication(), is(true));
assertThat(metaExample.getApi().size(), equalTo(19));
assertThat(metaExample.getGit().size(), equalTo(36));
assertThat(metaExample.getHooks().size(), equalTo(4));
assertThat(metaExample.getImporter().size(), equalTo(3));
assertThat(metaExample.getPages().size(), equalTo(6));
assertThat(metaExample.getWeb().size(), equalTo(20));
}
}
/**
* Gets the my marketplace purchases.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getMyMarketplacePurchases() throws IOException {
List<GHMarketplaceUserPurchase> userPurchases = gitHub.getMyMarketplacePurchases().toList();
assertThat(userPurchases.size(), equalTo(2));
for (GHMarketplaceUserPurchase userPurchase : userPurchases) {
assertThat(userPurchase.isOnFreeTrial(), is(false));
assertThat(userPurchase.getFreeTrialEndsOn(), nullValue());
assertThat(userPurchase.getBillingCycle(), equalTo("monthly"));
assertThat(userPurchase.getNextBillingDate(),
equalTo(GitHubClient.parseInstant("2020-01-01T00:00:00.000+13:00")));
assertThat(userPurchase.getUpdatedAt(),
equalTo(GitHubClient.parseInstant("2019-12-02T00:00:00.000+13:00")));
GHMarketplacePlan plan = userPurchase.getPlan();
// GHMarketplacePlan - Non-nullable fields
assertThat(plan.getUrl(), notNullValue());
assertThat(plan.getAccountsUrl(), notNullValue());
assertThat(plan.getName(), notNullValue());
assertThat(plan.getDescription(), notNullValue());
assertThat(plan.getPriceModel(), notNullValue());
assertThat(plan.getState(), notNullValue());
// GHMarketplacePlan - primitive fields
assertThat(plan.getId(), not(0L));
assertThat(plan.getNumber(), not(0L));
assertThat(plan.getMonthlyPriceInCents(), greaterThanOrEqualTo(0L));
// GHMarketplacePlan - list
assertThat(plan.getBullets().size(), equalTo(2));
GHMarketplaceAccount account = userPurchase.getAccount();
// GHMarketplaceAccount - Non-nullable fields
assertThat(account.getLogin(), notNullValue());
assertThat(account.getUrl(), notNullValue());
assertThat(account.getType(), notNullValue());
// GHMarketplaceAccount - primitive fields
assertThat(account.getId(), not(0L));
/* logical combination tests */
// Rationale: organization_billing_email is only set when account type is ORGANIZATION.
if (account.getType() == ORGANIZATION)
assertThat(account.getOrganizationBillingEmail(), notNullValue());
else
assertThat(account.getOrganizationBillingEmail(), nullValue());
}
}
/**
* Gets the orgs.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getOrgs() throws IOException {
int iterations = 10;
Set<Long> orgIds = new HashSet<Long>();
for (GHOrganization org : Iterables.limit(gitHub.listOrganizations().withPageSize(2), iterations)) {
orgIds.add(org.getId());
// System.out.println(org.getName());
}
assertThat(orgIds.size(), equalTo(iterations));
GHOrganization org = gitHub.getOrganization("hub4j");
GHOrganization org2 = gitHub.getOrganization("hub4j");
assertThat(org.getLogin(), equalTo("hub4j"));
// caching
assertThat(org, sameInstance(org2));
gitHub.refreshCache();
org2 = gitHub.getOrganization("hub4j");
assertThat(org2.getLogin(), equalTo("hub4j"));
// cache cleared
assertThat(org, not(sameInstance(org2)));
}
/**
* Gets the repository.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getRepository() throws IOException {
GHRepository repo = gitHub.getRepository("hub4j/github-api");
assertThat(repo.getFullName(), equalTo("hub4j/github-api"));
GHRepository repo2 = gitHub.getRepositoryById(repo.getId());
assertThat(repo2.getFullName(), equalTo("hub4j/github-api"));
try {
gitHub.getRepository("hub4j_github-api");
fail();
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("Repository name must be in format owner/repo"));
}
try {
gitHub.getRepository("hub4j/github/api");
fail();
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("Repository name must be in format owner/repo"));
}
}
/**
* Gzip.
*
* @throws Exception
* the exception
*/
@Test
public void gzip() throws Exception {
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
// getResponseHeaderFields is deprecated but we'll use it for testing.
assertThat(org.getResponseHeaderFields(), notNullValue());
// WireMock should automatically gzip all responses
assertThat(org.getResponseHeaderFields().get("Content-Encoding").get(0), is("gzip"));
assertThat(org.getResponseHeaderFields().get("Content-eNcoding").get(0), is("gzip"));
}
/**
* Verifies that the `type` field is correctly fetched when listing organizations.
* <p>
* Since the `type` field is not included by default in the list of organizations, this test ensures that calling
* {@code getType()} retrieves the expected value.
* </p>
*
* @throws IOException
* if an I/O error occurs while fetching the organizations.
*/
@Test
public void listOrganizationsFetchesType() throws IOException {
String type = gitHub.listOrganizations().withPageSize(1).iterator().next().getType();
assertThat(type, equalTo("Organization"));
}
/**
* List users.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void listUsers() throws IOException {
for (GHUser u : Iterables.limit(gitHub.listUsers(), 10)) {
assert u.getName() != null;
// System.out.println(u.getName());
}
}
/**
* Search content.
*
* @throws Exception
* the exception
*/
@Test
public void searchContent() throws Exception {
PagedSearchIterable<GHContent> r = gitHub.searchContent()
.q("addClass")
.in("file")
.language("js")
.repo("jquery/jquery")
// ignored unless sort is also set
.order(GHDirection.DESC)
.list();
GHContent c = r.iterator().next();
assertThat(c.getGitUrl(), endsWith("/repositories/167174/git/blobs/796fbcc808ca15bbe771f8c9c1a7bab3388f6128"));
assertThat(c.getHtmlUrl(),
endsWith(
"https://github.com/jquery/jquery/blob/a684e6ba836f7c553968d7d026ed7941e1a612d8/src/attributes/classes.js"));
// System.out.println(c.getName());
assertThat(c.getDownloadUrl(), notNullValue());
assertThat(c.getOwner(), notNullValue());
assertThat(c.getOwner().getFullName(), equalTo("jquery/jquery"));
assertThat(r.getTotalCount(), greaterThan(5));
PagedSearchIterable<GHContent> r2 = gitHub.searchContent()
.q("addClass")
.in("file")
.language("js")
.repo("jquery/jquery")
// resets query sort back to default
.sort(GHContentSearchBuilder.Sort.INDEXED)
.sort(GHContentSearchBuilder.Sort.BEST_MATCH)
// ignored unless sort is also set to non-default
.order(GHDirection.ASC)
.list();
GHContent c2 = r2.iterator().next();
assertThat(c2.getPath(), equalTo(c.getPath()));
assertThat(r2.getTotalCount(), equalTo(r.getTotalCount()));
PagedSearchIterable<GHContent> r3 = gitHub.searchContent()
.q("addClass")
.in("file")
.language("js")
.repo("jquery/jquery")
.sort(GHContentSearchBuilder.Sort.INDEXED)
.order(GHDirection.ASC)
.list();
GHContent c3 = r3.iterator().next();
assertThat(c3.getPath(), not(equalTo(c2.getPath())));
assertThat(r3.getTotalCount(), equalTo(r2.getTotalCount()));
GHContentSearchBuilder searchBuilder = gitHub.searchContent()
.q("addClass")
.in("file")
.language("js")
.repo("jquery/jquery")
.sort(GHContentSearchBuilder.Sort.INDEXED)
.order(GHDirection.DESC);
PagedSearchIterable<GHContent> r4 = searchBuilder.list();
GHContent c4 = r4.iterator().next();
assertThat(c4.getPath(), not(equalTo(c2.getPath())));
assertThat(c4.getPath(), not(equalTo(c3.getPath())));
assertThat(r4.getTotalCount(), equalTo(r2.getTotalCount()));
// Verify qualifier not allowed to be empty
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class,
() -> searchBuilder.q("", "not valid"));
assertThat(e.getMessage(), equalTo("qualifier cannot be null or empty"));
}
/**
* Search content where the API item URL contains special characters in path segments.
*
* @throws Exception
* the exception
*/
@Test
public void searchContentSpecialCharactersInUrl() throws Exception {
GHContent content = gitHub.searchContent()
.q("filename:Package.swift repo:chrisgrieser/.config")
.list()
.iterator()
.next();
assertThat(content.getPath(),
equalTo("Alfred.alfredpreferences/workflows/menubar-search [3rd-Party]/menu/Package.swift"));
byte[] data;
try (java.io.InputStream inputStream = content.read()) {
data = inputStream.readAllBytes();
}
assertThat(new String(data, StandardCharsets.UTF_8), equalTo("// content with special path chars\n"));
}
/**
* Search content with forks.
*/
@Test
public void searchContentWithForks() {
final PagedSearchIterable<GHContent> results = gitHub.searchContent()
.q("addClass")
.language("js")
.sort(GHContentSearchBuilder.Sort.INDEXED)
.order(GHDirection.DESC)
.fork(GHFork.PARENT_ONLY)
.list();
final PagedSearchIterable<GHContent> resultsWithForks = gitHub.searchContent()
.q("addClass")
.language("js")
.sort(GHContentSearchBuilder.Sort.INDEXED)
.order(GHDirection.DESC)
.fork(GHFork.PARENT_AND_FORKS)
.list();
assertThat(results.getTotalCount(), lessThan(resultsWithForks.getTotalCount()));
// Do not record these.
// This will verify that the queries for the deprecated path are identical to the ones above.
if (!mockGitHub.isTakeSnapshot()) {
final PagedSearchIterable<GHContent> resultsDeprecated = gitHub.searchContent()
.q("addClass")
.language("js")
.sort(GHContentSearchBuilder.Sort.INDEXED)
.order(GHDirection.DESC)
.fork(GHFork.PARENT_ONLY)
.list();
final PagedSearchIterable<GHContent> resultsWithForksDeprecated = gitHub.searchContent()
.q("addClass")
.language("js")
.sort(GHContentSearchBuilder.Sort.INDEXED)
.order(GHDirection.DESC)
.fork(GHFork.PARENT_AND_FORKS)
.list();
assertThat(resultsDeprecated.getTotalCount(), equalTo(results.getTotalCount()));
assertThat(resultsWithForksDeprecated.getTotalCount(), equalTo(resultsWithForks.getTotalCount()));
}
}
/**
* Search users.
*/
@Test
public void searchUsers() {
PagedSearchIterable<GHUser> r = gitHub.searchUsers().q("tom").repos(">42").followers(">1000").list();
GHUser u = r.iterator().next();
// System.out.println(u.getName());
assertThat(u.getId(), notNullValue());
assertThat(r.getTotalCount(), greaterThan(0));
}
/**
* Test expect GitHub {@link ServiceDownException}
*
*/
@Test
public void testCatchServiceDownException() {
snapshotNotAllowed();
try {
GHRepository repo = gitHub.getRepository("hub4j-test-org/github-api");
repo.getFileContent("ghcontent-ro/service-down");
fail("Exception was expected");
} catch (IOException e) {
assertThat(e.getClass().getName(), equalToIgnoringCase(ServiceDownException.class.getName()));
}
}
/**
* Test header field name.
*
* @throws Exception
* the exception
*/
@Test
public void testHeaderFieldName() throws Exception {
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
// getResponseHeaderFields is deprecated but we'll use it for testing.
assertThat(org.getResponseHeaderFields(), notNullValue());
assertThat("Header field names must be case-insensitive",
org.getResponseHeaderFields().containsKey("CacHe-ContrOl"));
assertThat("KeySet from header fields should also be case-insensitive",
org.getResponseHeaderFields().keySet().contains("CacHe-ControL"));
assertThat(org.getResponseHeaderFields().get("cachE-cOntrol").get(0), is("private, max-age=60, s-maxage=60"));
}
/**
* Test list all repositories.
*/
@Test
public void testListAllRepositories() {
Iterator<GHRepository> itr = gitHub.listAllPublicRepositories().iterator();
for (int i = 0; i < 115; i++) {
assertThat(itr.hasNext(), is(true));
GHRepository r = itr.next();
// System.out.println(r.getFullName());
assertThat(r.getUrl(), notNullValue());
assertThat(r.getId(), not(0L));
}
// ensure the iterator throws as expected
try {
itr.remove();
fail();
} catch (UnsupportedOperationException e) {
assertThat(e, notNullValue());
}
}
/**
* Test list my authorizations.
*/
@Test
public void testListMyAuthorizations() {
PagedIterable<GHAuthorization> list = gitHub.listMyAuthorizations();
for (GHAuthorization auth : list) {
assertThat(auth.getAppName(), notNullValue());
}
}
}