/
GHIssueTest.java
325 lines (281 loc) · 12.3 KB
/
GHIssueTest.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
package org.kohsuke.github;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
// TODO: Auto-generated Javadoc
/**
* The Class GHIssueTest.
*
* @author Kohsuke Kawaguchi
* @author Yoann Rodiere
*/
public class GHIssueTest extends AbstractGitHubWireMockTest {
/**
* Clean up.
*
* @throws Exception the exception
*/
@Before
@After
public void cleanUp() throws Exception {
// Cleanup is only needed when proxying
if (!mockGitHub.isUseProxy()) {
return;
}
for (GHIssue issue : getRepository(this.getNonRecordingGitHub()).getIssues(GHIssueState.OPEN)) {
issue.close();
}
}
/**
* Creates the issue.
*
* @throws Exception the exception
*/
@Test
public void createIssue() throws Exception {
String name = "createIssue";
GHRepository repo = getRepository();
GHIssue issue = repo.createIssue(name).body("## test").create();
assertThat(issue.getTitle(), equalTo(name));
}
/**
* Issue comment.
*
* @throws Exception the exception
*/
@Test
public void issueComment() throws Exception {
String name = "createIssueComment";
GHIssue issue = getRepository().createIssue(name).body("## test").create();
List<GHIssueComment> comments;
comments = issue.listComments().toList();
assertThat(comments, hasSize(0));
comments = issue.queryComments().list().toList();
assertThat(comments, hasSize(0));
GHIssueComment firstComment = issue.comment("First comment");
Date firstCommentCreatedAt = firstComment.getCreatedAt();
Date firstCommentCreatedAtPlus1Second = Date
.from(firstComment.getCreatedAt().toInstant().plus(1, ChronoUnit.SECONDS));
comments = issue.listComments().toList();
assertThat(comments, hasSize(1));
assertThat(comments, contains(hasProperty("body", equalTo("First comment"))));
comments = issue.queryComments().list().toList();
assertThat(comments, hasSize(1));
assertThat(comments, contains(hasProperty("body", equalTo("First comment"))));
// Test "since"
comments = issue.queryComments().since(firstCommentCreatedAt).list().toList();
assertThat(comments, hasSize(1));
assertThat(comments, contains(hasProperty("body", equalTo("First comment"))));
comments = issue.queryComments().since(firstCommentCreatedAtPlus1Second).list().toList();
assertThat(comments, hasSize(0));
// "since" is only precise up to the second,
// so if we want to differentiate comments, we need to be completely sure they're created
// at least 1 second from each other.
// Waiting 2 seconds to avoid edge cases.
Thread.sleep(2000);
GHIssueComment secondComment = issue.comment("Second comment");
Date secondCommentCreatedAt = secondComment.getCreatedAt();
Date secondCommentCreatedAtPlus1Second = Date
.from(secondComment.getCreatedAt().toInstant().plus(1, ChronoUnit.SECONDS));
assertThat(
"There's an error in the setup of this test; please fix it."
+ " The second comment should be created at least one second after the first one.",
firstCommentCreatedAtPlus1Second.getTime() <= secondCommentCreatedAt.getTime());
comments = issue.listComments().toList();
assertThat(comments, hasSize(2));
assertThat(comments,
contains(hasProperty("body", equalTo("First comment")),
hasProperty("body", equalTo("Second comment"))));
comments = issue.queryComments().list().toList();
assertThat(comments, hasSize(2));
assertThat(comments,
contains(hasProperty("body", equalTo("First comment")),
hasProperty("body", equalTo("Second comment"))));
// Test "since"
comments = issue.queryComments().since(firstCommentCreatedAt).list().toList();
assertThat(comments, hasSize(2));
assertThat(comments,
contains(hasProperty("body", equalTo("First comment")),
hasProperty("body", equalTo("Second comment"))));
comments = issue.queryComments().since(firstCommentCreatedAtPlus1Second).list().toList();
assertThat(comments, hasSize(1));
assertThat(comments, contains(hasProperty("body", equalTo("Second comment"))));
comments = issue.queryComments().since(secondCommentCreatedAt).list().toList();
assertThat(comments, hasSize(1));
assertThat(comments, contains(hasProperty("body", equalTo("Second comment"))));
comments = issue.queryComments().since(secondCommentCreatedAtPlus1Second).list().toList();
assertThat(comments, hasSize(0));
// Test "since" with timestamp instead of Date
comments = issue.queryComments().since(secondCommentCreatedAt.getTime()).list().toList();
assertThat(comments, hasSize(1));
assertThat(comments, contains(hasProperty("body", equalTo("Second comment"))));
}
/**
* Close issue.
*
* @throws Exception the exception
*/
@Test
public void closeIssue() throws Exception {
String name = "closeIssue";
GHIssue issue = getRepository().createIssue(name).body("## test").create();
assertThat(issue.getTitle(), equalTo(name));
assertThat(getRepository().getIssue(issue.getNumber()).getState(), equalTo(GHIssueState.OPEN));
issue.close();
assertThat(getRepository().getIssue(issue.getNumber()).getState(), equalTo(GHIssueState.CLOSED));
}
/**
* Sets the labels.
*
* @throws Exception the exception
*/
@Test
// Requires push access to the test repo to pass
public void setLabels() throws Exception {
GHIssue issue = getRepository().createIssue("setLabels").body("## test").create();
String label = "setLabels_label_name";
issue.setLabels(label);
Collection<GHLabel> labels = getRepository().getIssue(issue.getNumber()).getLabels();
assertThat(labels.size(), equalTo(1));
GHLabel savedLabel = labels.iterator().next();
assertThat(savedLabel.getName(), equalTo(label));
assertThat(savedLabel.getId(), notNullValue());
assertThat(savedLabel.getNodeId(), notNullValue());
assertThat(savedLabel.isDefault(), is(false));
}
/**
* Adds the labels.
*
* @throws Exception the exception
*/
@Test
// Requires push access to the test repo to pass
public void addLabels() throws Exception {
GHIssue issue = getRepository().createIssue("addLabels").body("## test").create();
String addedLabel1 = "addLabels_label_name_1";
String addedLabel2 = "addLabels_label_name_2";
String addedLabel3 = "addLabels_label_name_3";
List<GHLabel> resultingLabels = issue.addLabels(addedLabel1);
assertThat(resultingLabels.size(), equalTo(1));
GHLabel ghLabel = resultingLabels.get(0);
assertThat(ghLabel.getName(), equalTo(addedLabel1));
int requestCount = mockGitHub.getRequestCount();
resultingLabels = issue.addLabels(addedLabel2, addedLabel3);
// multiple labels can be added with one api call
assertThat(mockGitHub.getRequestCount(), equalTo(requestCount + 1));
assertThat(resultingLabels.size(), equalTo(3));
assertThat(resultingLabels,
containsInAnyOrder(hasProperty("name", equalTo(addedLabel1)),
hasProperty("name", equalTo(addedLabel2)),
hasProperty("name", equalTo(addedLabel3))));
// Adding a label which is already present does not throw an error
resultingLabels = issue.addLabels(ghLabel);
assertThat(resultingLabels.size(), equalTo(3));
}
/**
* Adds the labels concurrency issue.
*
* @throws Exception the exception
*/
@Test
// Requires push access to the test repo to pass
public void addLabelsConcurrencyIssue() throws Exception {
String addedLabel1 = "addLabelsConcurrencyIssue_label_name_1";
String addedLabel2 = "addLabelsConcurrencyIssue_label_name_2";
GHIssue issue1 = getRepository().createIssue("addLabelsConcurrencyIssue").body("## test").create();
issue1.getLabels();
GHIssue issue2 = getRepository().getIssue(issue1.getNumber());
issue2.addLabels(addedLabel2);
Collection<GHLabel> labels = issue1.addLabels(addedLabel1);
assertThat(labels.size(), equalTo(2));
assertThat(labels,
containsInAnyOrder(hasProperty("name", equalTo(addedLabel1)),
hasProperty("name", equalTo(addedLabel2))));
}
/**
* Removes the labels.
*
* @throws Exception the exception
*/
@Test
// Requires push access to the test repo to pass
public void removeLabels() throws Exception {
GHIssue issue = getRepository().createIssue("removeLabels").body("## test").create();
String label1 = "removeLabels_label_name_1";
String label2 = "removeLabels_label_name_2";
String label3 = "removeLabels_label_name_3";
issue.setLabels(label1, label2, label3);
Collection<GHLabel> labels = getRepository().getIssue(issue.getNumber()).getLabels();
assertThat(labels.size(), equalTo(3));
GHLabel ghLabel3 = labels.stream().filter(label -> label3.equals(label.getName())).findFirst().get();
int requestCount = mockGitHub.getRequestCount();
List<GHLabel> resultingLabels = issue.removeLabels(label2, label3);
// each label deleted is a separate api call
assertThat(mockGitHub.getRequestCount(), equalTo(requestCount + 2));
assertThat(resultingLabels.size(), equalTo(1));
assertThat(resultingLabels.get(0).getName(), equalTo(label1));
// Removing some labels that are not present does not throw
// This is consistent with earlier behavior and with addLabels()
issue.removeLabels(ghLabel3);
// Calling removeLabel() on label that is not present will throw
try {
issue.removeLabel(label3);
fail("Expected GHFileNotFoundException");
} catch (GHFileNotFoundException e) {
assertThat(e.getMessage(), containsString("Label does not exist"));
}
}
/**
* Sets the assignee.
*
* @throws Exception the exception
*/
@Test
// Requires push access to the test repo to pass
public void setAssignee() throws Exception {
GHIssue issue = getRepository().createIssue("setAssignee").body("## test").create();
GHMyself user = gitHub.getMyself();
issue.assignTo(user);
assertThat(getRepository().getIssue(issue.getNumber()).getAssignee(), equalTo(user));
}
/**
* Gets the user test.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
@Test
public void getUserTest() throws IOException {
GHIssue issue = getRepository().createIssue("getUserTest").create();
GHIssue issueSingle = getRepository().getIssue(issue.getNumber());
assertThat(issueSingle.getUser().root(), notNullValue());
PagedIterable<GHIssue> ghIssues = getRepository().listIssues(GHIssueState.OPEN);
for (GHIssue otherIssue : ghIssues) {
assertThat(otherIssue.getUser().root(), notNullValue());
}
}
/**
* Gets the repository.
*
* @return the repository
* @throws IOException Signals that an I/O exception has occurred.
*/
protected GHRepository getRepository() throws IOException {
return getRepository(gitHub);
}
private GHRepository getRepository(GitHub gitHub) throws IOException {
return gitHub.getOrganization(GITHUB_API_TEST_ORG).getRepository("GHIssueTest");
}
}