forked from hub4j/github-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGHRepositoryForkBuilderTest.java
280 lines (242 loc) · 8.39 KB
/
GHRepositoryForkBuilderTest.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
package org.kohsuke.github;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.time.Duration;
import java.util.Map;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.*;
// TODO: Auto-generated Javadoc
/**
* The Class GHRepositoryForkBuilderTest.
*/
public class GHRepositoryForkBuilderTest extends AbstractGitHubWireMockTest {
private GHRepository repo;
private static final String TARGET_ORG = "nts-api-test-org";
private int originalInterval;
/**
* Instantiates a new Gh repository fork builder test.
*/
public GHRepositoryForkBuilderTest() {
}
/**
* Sets up.
*
* @throws Exception
* the exception
*/
@Before
public void setUp() throws Exception {
repo = getTempRepository();
originalInterval = GHRepositoryForkBuilder.FORK_RETRY_INTERVAL;
GHRepositoryForkBuilder.FORK_RETRY_INTERVAL = 100;
if (mockGitHub.isUseProxy()) {
GitHub github = getNonRecordingGitHub();
GHRepository repo = github.getRepository(this.repo.getFullName());
String defaultBranch = repo.getDefaultBranch();
GHRef mainRef = repo.getRef("heads/" + defaultBranch);
String mainSha = mainRef.getObject().getSha();
String[] branchNames = { "test-branch1", "test-branch2", "test-branch3" };
for (String branchName : branchNames) {
repo.createRef("refs/heads/" + branchName, mainSha);
}
}
}
/**
* Tear down.
*/
@After
public void tearDown() {
GHRepositoryForkBuilder.FORK_RETRY_INTERVAL = originalInterval;
}
/**
* The type Test fork builder.
*/
class TestForkBuilder extends GHRepositoryForkBuilder {
/**
* The Sleep count.
*/
int sleepCount = 0;
/**
* The Last sleep millis.
*/
int lastSleepMillis = 0;
/**
* Instantiates a new Test fork builder.
*
* @param repo
* the repo
*/
TestForkBuilder(GHRepository repo) {
super(repo);
}
@Override
void sleep(int millis) throws IOException {
sleepCount++;
lastSleepMillis = millis;
try {
if (mockGitHub.isUseProxy()) {
Thread.sleep(millis);
} else {
Thread.sleep(1);
}
} catch (InterruptedException e) {
throw (IOException) new InterruptedIOException().initCause(e);
}
}
}
private TestForkBuilder createBuilder() {
return new TestForkBuilder(repo);
}
private void verifyBasicForkProperties(GHRepository original, GHRepository forked, String expectedName)
throws IOException {
GHRepository updatedFork = forked;
await().atMost(Duration.ofSeconds(30))
.pollInterval(Duration.ofSeconds(3))
.until(() -> gitHub.getRepository(forked.getFullName()).isFork());
assertThat(updatedFork, notNullValue());
assertThat(updatedFork.getName(), equalTo(expectedName));
assertThat(updatedFork.isFork(), is(true));
assertThat(updatedFork.getParent().getFullName(), equalTo(original.getFullName()));
}
private void verifyBranches(GHRepository forked, boolean defaultBranchOnly) throws IOException {
Map<String, GHBranch> branches = forked.getBranches();
if (defaultBranchOnly) {
assertThat(branches.size(), equalTo(1));
} else {
assertThat(branches.size(), greaterThan(1));
}
assertThat(branches.containsKey(forked.getDefaultBranch()), is(true));
}
/**
* Test fork.
*
* @throws Exception
* the exception
*/
@Test
public void testFork() throws Exception {
// cover the deprecated fork() method
GHRepository forkedRepo = repo.fork();
verifyBasicForkProperties(repo, forkedRepo, repo.getName());
verifyBranches(forkedRepo, false);
forkedRepo.delete();
}
/**
* Test fork to org.
*
* @throws Exception
* the exception
*/
@Test
public void testForkToOrg() throws Exception {
GHOrganization targetOrg = gitHub.getOrganization(TARGET_ORG);
// equivalent to the deprecated forkTo() method
TestForkBuilder builder = createBuilder();
GHRepository forkedRepo = builder.organization(targetOrg).create();
verifyBasicForkProperties(repo, forkedRepo, repo.getName());
verifyBranches(forkedRepo, false);
forkedRepo.delete();
}
/**
* Test fork default branch only.
*
* @throws Exception
* the exception
*/
@Test
public void testForkDefaultBranchOnly() throws Exception {
TestForkBuilder builder = createBuilder();
GHRepository forkedRepo = builder.defaultBranchOnly(true).create();
verifyBasicForkProperties(repo, forkedRepo, repo.getName());
verifyBranches(forkedRepo, true);
forkedRepo.delete();
}
/**
* Test fork changed name.
*
* @throws Exception
* the exception
*/
@Test
public void testForkChangedName() throws Exception {
String newRepoName = "test-fork-with-new-name";
TestForkBuilder builder = createBuilder();
GHRepository forkedRepo = builder.name(newRepoName).create();
assertThat(forkedRepo.getName(), equalTo(newRepoName));
verifyBasicForkProperties(repo, forkedRepo, newRepoName);
verifyBranches(forkedRepo, false);
forkedRepo.delete();
}
/**
* Test timeout message and sleep count.
*
* @throws Exception
* the exception
*/
@Test
public void testTimeoutMessage() throws Exception {
// For re-recording, use line below to create successful fork test copy, then comment it out and modify json
// response to 404
// repo.createFork().name("test-message").create();
String newRepoName = "test-message";
try {
TestForkBuilder builder = createBuilder();
try {
builder.name(newRepoName).create();
fail("Expected IOException for timeout");
} catch (IOException e) {
assertThat(builder.sleepCount, equalTo(10));
assertThat(builder.lastSleepMillis, equalTo(100));
assertThat(e.getMessage(),
allOf(containsString("was forked"),
containsString("with name " + newRepoName),
containsString("but can't find the new repository")));
}
} finally {
GHRepositoryForkBuilder.FORK_RETRY_INTERVAL = originalInterval;
}
}
/**
* Test timeout org message.
*
* @throws Exception
* the exception
*/
@Test
public void testTimeoutOrgMessage() throws Exception {
GHOrganization targetOrg = gitHub.getOrganization(TARGET_ORG);
// For re-recording, use line below to create successful fork test copy, then comment it out and modify json
// response to 404
// repo.createFork().organization(targetOrg).create();
try {
repo.createFork().organization(targetOrg).create();
fail("Expected IOException for timeout");
} catch (IOException e) {
assertThat(e.getMessage(),
allOf(containsString("was forked"),
containsString("into " + TARGET_ORG),
containsString("but can't find the new repository")));
}
}
/**
* Test sleep.
*
* @throws Exception
* the exception
*/
@Test
public void testSleep() throws Exception {
GHRepositoryForkBuilder builder = new GHRepositoryForkBuilder(repo);
Thread.currentThread().interrupt();
try {
builder.sleep(100);
fail("Expected InterruptedIOException");
} catch (InterruptedIOException e) {
assertThat(e, instanceOf(InterruptedIOException.class));
assertThat(e.getCause(), instanceOf(InterruptedException.class));
}
}
}