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.mock.MkAnswer;
33 import com.jcabi.http.mock.MkContainer;
34 import com.jcabi.http.mock.MkGrizzlyContainer;
35 import com.jcabi.http.request.ApacheRequest;
36 import java.net.HttpURLConnection;
37 import javax.json.Json;
38 import javax.json.JsonObject;
39 import org.hamcrest.MatcherAssert;
40 import org.hamcrest.Matchers;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44
45
46
47
48
49
50
51 public final class RtUsersTest {
52
53
54
55
56
57 @Rule
58 public final transient RandomPort resource = new RandomPort();
59
60
61
62
63
64 @Test
65 public void iterateUsers() throws Exception {
66 final String identifier = "1";
67 final MkContainer container = new MkGrizzlyContainer().next(
68 new MkAnswer.Simple(
69 HttpURLConnection.HTTP_OK,
70 Json.createArrayBuilder()
71 .add(RtUsersTest.json("octocat", identifier))
72 .add(RtUsersTest.json("dummy", "2"))
73 .build().toString()
74 )
75 ).start(this.resource.port());
76 final Users users = new RtUsers(
77 Mockito.mock(Github.class),
78 new ApacheRequest(container.home())
79 );
80 MatcherAssert.assertThat(
81 users.iterate(identifier),
82 Matchers.<User>iterableWithSize(2)
83 );
84 container.stop();
85 }
86
87
88
89
90
91
92 @Test
93 public void getSingleUser() throws Exception {
94 final String login = "mark";
95 final MkContainer container = new MkGrizzlyContainer().next(
96 new MkAnswer.Simple(
97 HttpURLConnection.HTTP_OK,
98 RtUsersTest.json(login, "3").toString()
99 )
100 ).start(this.resource.port());
101 final Users users = new RtUsers(
102 Mockito.mock(Github.class),
103 new ApacheRequest(container.home())
104 );
105 MatcherAssert.assertThat(
106 users.get(login).login(),
107 Matchers.equalTo(login)
108 );
109 container.stop();
110 }
111
112
113
114
115
116
117 @Test
118 public void getCurrentUser() throws Exception {
119 final String login = "kendy";
120 final MkContainer container = new MkGrizzlyContainer().next(
121 new MkAnswer.Simple(
122 HttpURLConnection.HTTP_OK,
123 RtUsersTest.json(login, "4").toString()
124 )
125 ).start(this.resource.port());
126 final Users users = new RtUsers(
127 Mockito.mock(Github.class),
128 new ApacheRequest(container.home())
129 );
130 MatcherAssert.assertThat(
131 users.self().login(),
132 Matchers.equalTo(login)
133 );
134 container.stop();
135 }
136
137
138
139
140
141
142
143 private static JsonObject json(
144 final String login, final String identifier
145 ) {
146 return Json.createObjectBuilder()
147 .add("id", Integer.valueOf(identifier))
148 .add("login", login)
149 .build();
150 }
151 }