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.aspects.Tv;
33 import com.jcabi.github.mock.MkGithub;
34 import com.jcabi.http.Request;
35 import com.jcabi.http.mock.MkAnswer;
36 import com.jcabi.http.mock.MkContainer;
37 import com.jcabi.http.mock.MkGrizzlyContainer;
38 import com.jcabi.http.request.ApacheRequest;
39 import com.jcabi.http.request.FakeRequest;
40 import java.net.HttpURLConnection;
41 import java.util.Date;
42 import javax.json.Json;
43 import org.hamcrest.MatcherAssert;
44 import org.hamcrest.Matchers;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.mockito.Mockito;
48
49
50
51
52
53
54
55
56
57
58
59 @SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods" })
60 public final class RtUserTest {
61
62
63
64
65
66 @Rule
67 public final transient RandomPort resource = new RandomPort();
68
69
70
71
72
73 @Test
74 public void checksWhoAmI() throws Exception {
75 final String login = "monalia";
76 final RtUser user = new RtUser(
77 Mockito.mock(Github.class),
78 new FakeRequest().withBody(
79 Json.createObjectBuilder()
80 .add("login", login)
81 .build().toString()
82 )
83 );
84 MatcherAssert.assertThat(
85 user.login(),
86 Matchers.equalTo(login)
87 );
88 }
89
90
91
92
93
94 @Test
95 public void checksIfHeHasAName() throws Exception {
96 final User.Smart smart = new User.Smart(
97 new RtUser(
98 Mockito.mock(Github.class),
99 new FakeRequest().withBody(
100 Json.createObjectBuilder()
101 .add("name", "octoc")
102 .build()
103 .toString()
104 ),
105 "octoc"
106 )
107 );
108 MatcherAssert.assertThat(
109 smart.hasName(),
110 Matchers.equalTo(true)
111 );
112 }
113
114
115
116
117
118 @Test
119 public void checksIfHeHasNoName() throws Exception {
120 final User.Smart smart = new User.Smart(
121 new RtUser(
122 Mockito.mock(Github.class),
123 new FakeRequest().withBody(
124 Json.createObjectBuilder()
125 .build()
126 .toString()
127 ),
128 "octoc"
129 )
130 );
131 MatcherAssert.assertThat(
132 smart.hasName(),
133 Matchers.equalTo(false)
134 );
135 }
136
137
138
139
140
141
142 @Test
143 public void describeAsJson() throws Exception {
144 final RtUser user = new RtUser(
145 Mockito.mock(Github.class),
146 new FakeRequest().withBody(
147 Json.createObjectBuilder()
148 .add("name", "monalisa")
149 .add("email", "octocat@github.com")
150 .build()
151 .toString()
152 ),
153 "octoc"
154 );
155 MatcherAssert.assertThat(
156 user.json().toString(),
157 Matchers.equalTo(
158 "{\"name\":\"monalisa\",\"email\":\"octocat@github.com\"}"
159 )
160 );
161 }
162
163
164
165
166
167
168 @Test
169 public void executePatchRequest() throws Exception {
170 final MkContainer container = new MkGrizzlyContainer().next(
171 new MkAnswer.Simple(
172 HttpURLConnection.HTTP_OK,
173 "{\"login\":\"octocate\"}"
174 )
175 ).start(this.resource.port());
176 final RtUser json = new RtUser(
177 Mockito.mock(Github.class),
178 new ApacheRequest(container.home())
179 );
180 json.patch(
181 Json.createObjectBuilder()
182 .add("location", "San Francisco")
183 .build()
184 );
185 MatcherAssert.assertThat(
186 container.take().method(),
187 Matchers.equalTo(Request.PATCH)
188 );
189 container.stop();
190 }
191
192
193
194
195 @Test
196 public void fetchesEmails() {
197 final Github github = Mockito.mock(Github.class);
198 Mockito.when(github.entry()).thenReturn(new FakeRequest());
199 final User user = new RtUser(github, new FakeRequest());
200 MatcherAssert.assertThat(user.emails(), Matchers.notNullValue());
201 }
202
203
204
205
206 @Test
207 public void fetchesOrganizations() {
208 final Github github = Mockito.mock(Github.class);
209 Mockito.when(github.entry()).thenReturn(new FakeRequest());
210 final User user = new RtUser(github, new FakeRequest());
211 MatcherAssert.assertThat(user.organizations(), Matchers.notNullValue());
212 }
213
214
215
216
217
218 @Test
219 public void hasHtmlUrl() throws Exception {
220 final String value = "http://github.example.com";
221 final User.Smart smart = this.userWith("html_url", value);
222 MatcherAssert.assertThat(smart.htmlUrl(), Matchers.is(value));
223 }
224
225
226
227
228
229 @Test
230 public void hasFollwersUrl() throws Exception {
231 final String value = "http://github.example.com/followers";
232 final User.Smart smart = this.userWith("followers_url", value);
233 MatcherAssert.assertThat(smart.followersUrl(), Matchers.is(value));
234 }
235
236
237
238
239
240 @Test
241 public void hasFollowingUrl() throws Exception {
242 final String value = "http://github.example.com/following";
243 final User.Smart smart = this.userWith("following_url", value);
244 MatcherAssert.assertThat(smart.followingUrl(), Matchers.is(value));
245 }
246
247
248
249
250
251 @Test
252 public void hasGistsUrl() throws Exception {
253 final String value = "http://github.example.com/gists";
254 final User.Smart smart = this.userWith("gists_url", value);
255 MatcherAssert.assertThat(smart.gistsUrl(), Matchers.is(value));
256 }
257
258
259
260
261
262 @Test
263 public void hasStarredUrl() throws Exception {
264 final String value = "http://github.example.com/starred";
265 final User.Smart smart = this.userWith("starred_url", value);
266 MatcherAssert.assertThat(smart.starredUrl(), Matchers.is(value));
267 }
268
269
270
271
272
273 @Test
274 public void hasSubscriptionsUrl() throws Exception {
275 final String value = "http://github.example.com/subscriptions";
276 final User.Smart smart = this.userWith("subscriptions_url", value);
277 MatcherAssert.assertThat(smart.subscriptionsUrl(), Matchers.is(value));
278 }
279
280
281
282
283
284 @Test
285 public void hasOrganizationsUrl() throws Exception {
286 final String value = "http://github.example.com/organizations";
287 final User.Smart smart = this.userWith("organizations_url", value);
288 MatcherAssert.assertThat(smart.organizationsUrl(), Matchers.is(value));
289 }
290
291
292
293
294
295 @Test
296 public void hasReposUrl() throws Exception {
297 final String value = "http://github.example.com/repos";
298 final User.Smart smart = this.userWith("repos_url", value);
299 MatcherAssert.assertThat(smart.reposUrl(), Matchers.is(value));
300 }
301
302
303
304
305
306 @Test
307 public void hasEventsUrl() throws Exception {
308 final String value = "http://github.example.com/events";
309 final User.Smart smart = this.userWith("events_url", value);
310 MatcherAssert.assertThat(smart.eventsUrl(), Matchers.is(value));
311 }
312
313
314
315
316
317 @Test
318 public void hasReceivedEventsUrl() throws Exception {
319 final String value = "http://github.example.com/received_events";
320 final User.Smart smart = this.userWith("received_events_url", value);
321 MatcherAssert.assertThat(smart.receivedEventsUrl(), Matchers.is(value));
322 }
323
324
325
326
327
328 @Test
329 public void hasType() throws Exception {
330 final String value = "http://github.example.com/organizations";
331 final User.Smart smart = this.userWith("type", value);
332 MatcherAssert.assertThat(smart.type(), Matchers.is(value));
333 }
334
335
336
337
338
339 @Test
340 public void hasSiteAdmin() throws Exception {
341 final User.Smart smart = this.userWith("site_admin", "true");
342 MatcherAssert.assertThat(smart.siteAdmin(), Matchers.is(true));
343 }
344
345
346
347
348
349 @Test
350 public void hasBlog() throws Exception {
351 final String value = "http://blog.example.com";
352 final User.Smart smart = this.userWith("blog", value);
353 MatcherAssert.assertThat(smart.blog(), Matchers.is(value));
354 }
355
356
357
358
359
360 @Test
361 public void hasHireable() throws Exception {
362 final User.Smart smart = this.userWith("hireable", "true");
363 MatcherAssert.assertThat(smart.hireable(), Matchers.is(true));
364 }
365
366
367
368
369
370 @Test
371 public void hasBio() throws Exception {
372 final String value = "http://github.example.com/bio";
373 final User.Smart smart = this.userWith("bio", value);
374 MatcherAssert.assertThat(smart.bio(), Matchers.is(value));
375 }
376
377
378
379
380
381 @Test
382 public void hasPublicRepos() throws Exception {
383 final int value = Tv.THREE;
384 final User.Smart smart = this.userWith(
385 "public_repos",
386 String.valueOf(value)
387 );
388 MatcherAssert.assertThat(smart.publicRepos(), Matchers.is(value));
389 }
390
391
392
393
394
395 @Test
396 public void hasPublicGists() throws Exception {
397 final int value = Tv.FOUR;
398 final User.Smart smart = this.userWith(
399 "public_gists",
400 String.valueOf(value)
401 );
402 MatcherAssert.assertThat(smart.publicGists(), Matchers.is(value));
403 }
404
405
406
407
408
409 @Test
410 public void hasFollowersCount() throws Exception {
411 final int value = Tv.FIVE;
412 final User.Smart smart = this.userWith(
413 "followers",
414 String.valueOf(value)
415 );
416 MatcherAssert.assertThat(smart.followersCount(), Matchers.is(value));
417 }
418
419
420
421
422
423 @Test
424 public void hasFollowingCount() throws Exception {
425 final int value = Tv.SIX;
426 final User.Smart smart = this.userWith(
427 "following",
428 String.valueOf(value)
429 );
430 MatcherAssert.assertThat(smart.followingCount(), Matchers.is(value));
431 }
432
433
434
435
436
437 @Test
438 public void hasCreated() throws Exception {
439 final Github.Time value = new Github.Time("2014-07-04T15:29:43Z");
440 final User.Smart smart = this.userWith("created_at", value.toString());
441 MatcherAssert.assertThat(smart.created(), Matchers.is(value));
442 }
443
444
445
446
447
448 @Test
449 public void hasUpdated() throws Exception {
450 final Github.Time value = new Github.Time("2014-07-04T15:29:43Z");
451 final User.Smart smart = this.userWith("updated_at", value.toString());
452 MatcherAssert.assertThat(smart.updated(), Matchers.is(value));
453 }
454
455
456
457
458
459 @Test
460 public void notifications() throws Exception {
461 MatcherAssert.assertThat(
462 new RtUser(
463 new MkGithub(),
464 new FakeRequest()
465 ).notifications(),
466 Matchers.not(Matchers.nullValue())
467 );
468 }
469
470
471
472
473
474
475 @Test
476 public void markAsReadOkIfResponseStatusIs205() throws Exception {
477 MkContainer container = null;
478 try {
479 container = new MkGrizzlyContainer().next(
480 new MkAnswer.Simple(HttpURLConnection.HTTP_RESET)
481 ).start(this.resource.port());
482 final Request req = new ApacheRequest(container.home());
483 final Github github = Mockito.mock(Github.class);
484 Mockito.when(github.entry()).thenReturn(req);
485 new RtUser(
486 github,
487 req
488 ).markAsRead(new Date());
489 } finally {
490 container.close();
491 }
492 }
493
494
495
496
497
498
499 @Test(expected = AssertionError.class)
500 public void markAsReadErrorIfResponseStatusIsNot205() throws Exception {
501 MkContainer container = null;
502 try {
503 container = new MkGrizzlyContainer().next(
504 new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR)
505 ).start(this.resource.port());
506 final Request req = new ApacheRequest(container.home());
507 final Github github = Mockito.mock(Github.class);
508 Mockito.when(github.entry()).thenReturn(req);
509 new RtUser(
510 github,
511 req
512 ).markAsRead(new Date());
513 } finally {
514 container.close();
515 }
516 }
517
518
519
520
521
522
523
524 private User.Smart userWith(final String property, final String value) {
525 return new User.Smart(
526 new RtUser(
527 Mockito.mock(Github.class),
528 new FakeRequest().withBody(
529 Json.createObjectBuilder()
530 .add(property, value)
531 .build()
532 .toString()
533 ),
534 "octoc"
535 )
536 );
537 }
538 }