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.github.mock.MkGithub;
33 import com.jcabi.http.Request;
34 import com.jcabi.http.mock.MkAnswer;
35 import com.jcabi.http.mock.MkContainer;
36 import com.jcabi.http.mock.MkGrizzlyContainer;
37 import com.jcabi.http.mock.MkQuery;
38 import com.jcabi.http.request.ApacheRequest;
39 import java.io.IOException;
40 import java.net.HttpURLConnection;
41 import javax.json.Json;
42 import org.hamcrest.MatcherAssert;
43 import org.hamcrest.Matchers;
44 import org.junit.Rule;
45 import org.junit.Test;
46
47
48
49
50
51
52
53 public final class RtGistTest {
54
55
56
57
58 @Rule
59 public final transient RandomPort resource = new RandomPort();
60
61
62
63
64
65
66
67 @Test
68 public void readsFileWithContents() throws Exception {
69 try (
70 final MkContainer container = new MkGrizzlyContainer().next(
71 new MkAnswer.Simple(
72 HttpURLConnection.HTTP_OK,
73 "{\"files\":{\"hello\":{\"raw_url\":\"world\"}}}"
74 )
75 ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "success!"))
76 .start(this.resource.port())) {
77 final RtGist gist = new RtGist(
78 new MkGithub(),
79 new ApacheRequest(container.home()),
80 "test"
81 );
82 MatcherAssert.assertThat(
83 gist.read("hello"),
84 Matchers.equalTo("success!")
85 );
86 container.stop();
87 }
88 }
89
90
91
92
93
94 @Test
95 public void writesFileContents() throws Exception {
96 try (
97 final MkContainer container = new MkGrizzlyContainer().next(
98 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "testFileWrite")
99 ).start(this.resource.port())
100 ) {
101 final RtGist gist = new RtGist(
102 new MkGithub(),
103 new ApacheRequest(container.home()),
104 "testWrite"
105 );
106 gist.write("testFile", "testContent");
107 MatcherAssert.assertThat(
108 container.take().body(),
109 Matchers.containsString(
110 "\"testFile\":{\"content\":\"testContent\"}"
111 )
112 );
113 container.stop();
114 }
115 }
116
117
118
119
120
121
122 @Test
123 public void fork() throws IOException {
124 final String fileContent = "success";
125 try (final MkContainer container = new MkGrizzlyContainer()) {
126 container.next(
127 new MkAnswer.Simple(
128 HttpURLConnection.HTTP_OK,
129 "{\"files\":{\"hello\":{\"raw_url\":\"world\"}}}"
130 )
131 );
132 container.next(
133 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, fileContent)
134 );
135 container.next(
136 new MkAnswer.Simple(
137 HttpURLConnection.HTTP_CREATED,
138 "{\"id\": \"forked\"}"
139 )
140 );
141 container.next(
142 new MkAnswer.Simple(
143 HttpURLConnection.HTTP_OK,
144 "{\"files\":{\"hello\":{\"raw_url\":\"world\"}}}"
145 )
146 );
147 container.next(
148 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, fileContent)
149 );
150 container.start(this.resource.port());
151 final Gist gist = new RtGist(
152 new MkGithub(),
153 new ApacheRequest(container.home()),
154 "test"
155 );
156 final String content = gist.read("hello");
157 final Gist forkedGist = gist.fork();
158 MatcherAssert.assertThat(
159 forkedGist.read("hello"),
160 Matchers.equalTo(content)
161 );
162 container.stop();
163 }
164 }
165
166
167
168
169
170
171 @Test
172 public void canIterateFiles() throws Exception {
173 try (
174 final MkContainer container = new MkGrizzlyContainer().next(
175 new MkAnswer.Simple(
176 HttpURLConnection.HTTP_OK,
177 "{\"files\":{\"something\":{\"filename\":\"not null\"}}}"
178 )
179 ).start(this.resource.port())
180 ) {
181 final Gist.Smart smart = new Gist.Smart(
182 new RtGist(
183 new MkGithub(),
184 new ApacheRequest(container.home()),
185 "testGetFiles"
186 )
187 );
188 MatcherAssert.assertThat(
189 smart.files(),
190 Matchers.notNullValue()
191 );
192 MatcherAssert.assertThat(
193 container.take().uri().toString(),
194 Matchers.endsWith("/gists/testGetFiles")
195 );
196 container.stop();
197 }
198 }
199
200
201
202
203
204
205 @Test
206 public void canRepresentAsString() throws Exception {
207 try (
208 final MkContainer container = new MkGrizzlyContainer()
209 .start(this.resource.port())
210 ) {
211 final RtGist gist = new RtGist(
212 new MkGithub(),
213 new ApacheRequest(container.home()),
214 "testToString"
215 );
216 MatcherAssert.assertThat(
217 gist.toString(),
218 Matchers.endsWith("/gists/testToString")
219 );
220 container.stop();
221 }
222 }
223
224
225
226
227
228 @Test
229 public void canUnstarAGist() throws Exception {
230 try (
231 final MkContainer container = new MkGrizzlyContainer().next(
232 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
233 ).start(this.resource.port())
234 ) {
235 final RtGist gist = new RtGist(
236 new MkGithub(),
237 new ApacheRequest(container.home()),
238 "unstar"
239 );
240 gist.unstar();
241 final MkQuery query = container.take();
242 MatcherAssert.assertThat(
243 query.method(),
244 Matchers.equalTo(Request.DELETE)
245 );
246 MatcherAssert.assertThat(
247 query.body(),
248 Matchers.is(Matchers.emptyOrNullString())
249 );
250 container.stop();
251 }
252 }
253
254
255
256
257
258
259 @Test
260 public void executePatchRequest() throws Exception {
261 try (
262 final MkContainer container = new MkGrizzlyContainer().next(
263 new MkAnswer.Simple(
264 HttpURLConnection.HTTP_OK, "{\"msg\":\"hi\"}"
265 )
266 ).start(this.resource.port())
267 ) {
268 final RtGist gist = new RtGist(
269 new MkGithub(),
270 new ApacheRequest(container.home()),
271 "patch"
272 );
273 gist.patch(
274 Json.createObjectBuilder()
275 .add("content", "hi you!")
276 .build()
277 );
278 MatcherAssert.assertThat(
279 container.take().method(),
280 Matchers.equalTo(Request.PATCH)
281 );
282 container.stop();
283 }
284 }
285 }