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 package com.jcabi.github;
31
32 import com.jcabi.http.Request;
33 import com.jcabi.http.mock.MkAnswer;
34 import com.jcabi.http.mock.MkContainer;
35 import com.jcabi.http.mock.MkGrizzlyContainer;
36 import com.jcabi.http.request.ApacheRequest;
37 import com.jcabi.http.request.FakeRequest;
38 import java.net.HttpURLConnection;
39 import java.util.Iterator;
40 import javax.json.Json;
41 import javax.json.JsonObject;
42 import org.hamcrest.MatcherAssert;
43 import org.hamcrest.Matchers;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.mockito.Mockito;
47
48
49
50
51
52
53
54
55 @SuppressWarnings("PMD.TooManyMethods")
56 public final class RtRepoTest {
57
58
59
60
61
62 @Rule
63 public final transient RandomPort resource = new RandomPort();
64
65
66
67
68
69
70 @Test
71 public void iteratesEvents() throws Exception {
72 try (
73 final MkContainer container = new MkGrizzlyContainer().next(
74 new MkAnswer.Simple(
75 HttpURLConnection.HTTP_OK,
76 Json.createArrayBuilder()
77 .add(event(Event.ASSIGNED))
78 .add(event(Event.MENTIONED))
79 .build().toString()
80 )
81 ).start(this.resource.port())
82 ) {
83 final Repo repo = RtRepoTest.repo(
84 new ApacheRequest(container.home())
85 );
86 MatcherAssert.assertThat(
87 repo.issueEvents().iterate(),
88 Matchers.<Event>iterableWithSize(2)
89 );
90 container.stop();
91 }
92 }
93
94
95
96
97
98
99 @Test
100 public void fetchesLabels() throws Exception {
101 final Repo repo = RtRepoTest.repo(
102 new FakeRequest()
103 );
104 MatcherAssert.assertThat(
105 repo.labels(),
106 Matchers.notNullValue()
107 );
108 }
109
110
111
112
113
114
115 @Test
116 public void fetchesIssues() throws Exception {
117 final Repo repo = RtRepoTest.repo(
118 new FakeRequest()
119 );
120 MatcherAssert.assertThat(
121 repo.issues(),
122 Matchers.notNullValue()
123 );
124 }
125
126
127
128
129
130
131 @Test
132 public void fetchesBranches() throws Exception {
133 final Repo repo = RtRepoTest.repo(
134 new FakeRequest()
135 );
136 MatcherAssert.assertThat(
137 repo.branches(),
138 Matchers.notNullValue()
139 );
140 }
141
142
143
144
145
146
147 @Test
148 public void fetchesPulls() throws Exception {
149 final Repo repo = RtRepoTest.repo(
150 new FakeRequest()
151 );
152 MatcherAssert.assertThat(
153 repo.pulls(),
154 Matchers.notNullValue()
155 );
156 }
157
158
159
160
161
162
163 @Test
164 public void fetchHooks() throws Exception {
165 final Repo repo = RtRepoTest.repo(
166 new FakeRequest()
167 );
168 MatcherAssert.assertThat(
169 repo.hooks(),
170 Matchers.notNullValue()
171 );
172 }
173
174
175
176
177
178
179 @Test
180 public void fetchKeys() throws Exception {
181 final Repo repo = RtRepoTest.repo(
182 new FakeRequest()
183 );
184 MatcherAssert.assertThat(
185 repo.keys(),
186 Matchers.notNullValue()
187 );
188 }
189
190
191
192
193
194
195 @Test
196 public void fetchReleases() throws Exception {
197 final Repo repo = RtRepoTest.repo(
198 new FakeRequest()
199 );
200 MatcherAssert.assertThat(
201 repo.releases(),
202 Matchers.notNullValue()
203 );
204 }
205
206
207
208
209
210
211 @Test
212 public void fetchContents() throws Exception {
213 final Repo repo = RtRepoTest.repo(
214 new FakeRequest()
215 );
216 MatcherAssert.assertThat(
217 repo.contents(),
218 Matchers.notNullValue()
219 );
220 }
221
222
223
224
225
226 @Test
227 public void identifiesItself() throws Exception {
228 final Coordinates coords = new Coordinates.Simple("me", "me-branch");
229 final Repo repo = new RtRepo(
230 Mockito.mock(Github.class),
231 new FakeRequest(),
232 coords
233 );
234 MatcherAssert.assertThat(
235 repo.coordinates(),
236 Matchers.sameInstance(coords)
237 );
238 }
239
240
241
242
243
244
245 @Test
246 public void executePatchRequest() throws Exception {
247 try (
248 final MkContainer container = new MkGrizzlyContainer().next(
249 new MkAnswer.Simple(
250 HttpURLConnection.HTTP_OK,
251 event(Event.ASSIGNED).toString()
252 )
253 ).start(this.resource.port())
254 ) {
255 final Repo repo = RtRepoTest.repo(
256 new ApacheRequest(container.home())
257 );
258 repo.patch(RtRepoTest.event(Event.ASSIGNED));
259 MatcherAssert.assertThat(
260 container.take().method(),
261 Matchers.equalTo(Request.PATCH)
262 );
263 container.stop();
264 }
265 }
266
267
268
269
270
271
272 @Test
273 public void describeAsJson() throws Exception {
274 final Repo repo = RtRepoTest.repo(
275 new FakeRequest().withBody(
276 Json.createObjectBuilder()
277 .add("full_name", "octocat/Hello-World")
278 .add("fork", true)
279 .build()
280 .toString()
281 )
282 );
283 MatcherAssert.assertThat(
284 repo.json().toString(),
285 Matchers.equalTo(
286 "{\"full_name\":\"octocat/Hello-World\",\"fork\":true}"
287 )
288 );
289 }
290
291
292
293
294 @Test
295 public void fetchCommits() {
296 final Repo repo = RtRepoTest.repo(
297 new FakeRequest()
298 );
299 MatcherAssert.assertThat(repo.commits(), Matchers.notNullValue());
300 }
301
302
303
304
305 @Test
306 public void fetchesGit() {
307 final Repo repo = RtRepoTest.repo(
308 new FakeRequest()
309 );
310 MatcherAssert.assertThat(repo.git(), Matchers.notNullValue());
311 }
312
313
314
315
316 @Test
317 public void fetchStars() {
318 final Repo repo = RtRepoTest.repo(
319 new FakeRequest()
320 );
321 MatcherAssert.assertThat(repo.stars(), Matchers.notNullValue());
322 }
323
324
325
326
327 @Test
328 public void fetchNotifications() {
329 final Repo repo = RtRepoTest.repo(
330 new FakeRequest()
331 );
332 MatcherAssert.assertThat(repo.notifications(), Matchers.notNullValue());
333 }
334
335
336
337
338
339 @Test
340 public void fetchLanguages() throws Exception {
341 try (
342 final MkContainer container = new MkGrizzlyContainer().next(
343 new MkAnswer.Simple(
344 HttpURLConnection.HTTP_OK,
345 Json.createObjectBuilder()
346 .add("Ruby", 1)
347 .build().toString()
348 )
349 ).start(this.resource.port())
350 ) {
351 final Repo repo = RtRepoTest.repo(
352 new ApacheRequest(container.home())
353 );
354 MatcherAssert.assertThat(repo.languages(), Matchers.notNullValue());
355 container.stop();
356 }
357 }
358
359
360
361
362
363
364 @Test
365 public void iteratesLanguages() throws Exception {
366 final String lang = "C";
367 final String other = "Java";
368 try (
369 final MkContainer container = new MkGrizzlyContainer().next(
370 new MkAnswer.Simple(
371 HttpURLConnection.HTTP_OK,
372 Json.createObjectBuilder()
373 .add(lang, 1)
374 .add(other, 2)
375 .build().toString()
376 )
377 ).start(this.resource.port())
378 ) {
379 final Repo repo = RtRepoTest.repo(
380 new ApacheRequest(container.home())
381 );
382 final Iterator<Language> iter = repo.languages().iterator();
383 MatcherAssert.assertThat(
384 iter.hasNext(),
385 Matchers.is(true)
386 );
387 MatcherAssert.assertThat(
388 iter.next().name(),
389 Matchers.is(lang)
390 );
391 MatcherAssert.assertThat(
392 iter.hasNext(),
393 Matchers.is(true)
394 );
395 MatcherAssert.assertThat(
396 iter.next().name(),
397 Matchers.is(other)
398 );
399 MatcherAssert.assertThat(
400 iter.hasNext(),
401 Matchers.is(false)
402 );
403 container.stop();
404 }
405 }
406
407
408
409
410
411
412
413 private static JsonObject event(final String event) throws Exception {
414 return Json.createObjectBuilder()
415 .add("id", 1)
416 .add("event", event)
417 .build();
418 }
419
420
421
422
423
424
425 private static Repo repo(final Request request) {
426 return new RtRepo(
427 Mockito.mock(Github.class),
428 request,
429 new Coordinates.Simple("testuser", "testrepo")
430 );
431 }
432 }