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.mock;
31
32 import com.jcabi.github.Content;
33 import com.jcabi.github.Contents;
34 import com.jcabi.github.Repo;
35 import com.jcabi.github.RepoCommit;
36 import com.jcabi.xml.XML;
37 import java.io.IOException;
38 import javax.json.Json;
39 import javax.json.JsonObject;
40 import javax.json.JsonObjectBuilder;
41 import org.hamcrest.MatcherAssert;
42 import org.hamcrest.Matchers;
43 import org.junit.Test;
44
45
46
47
48
49
50
51
52 @SuppressWarnings({ "PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals" })
53 public final class MkContentsTest {
54
55
56
57
58 @Test
59 public void canFetchReadmeFile() throws Exception {
60 final Contents contents = new MkGithub().randomRepo().contents();
61 final String body = "Readme On Master";
62
63 contents.create(
64 content("README.md", "readme on master", body).build()
65 );
66 MatcherAssert.assertThat(
67 contents.readme().json().getString("content"),
68 Matchers.is(body)
69 );
70 }
71
72
73
74
75
76
77 @Test
78 public void canFetchReadmeFromBranch() throws Exception {
79 final String branch = "branch-1";
80 final Contents contents = new MkGithub().randomRepo().contents();
81 final String body = "Readme On Branch";
82 contents.create(
83 content("README.md", "readme on branch", body)
84 .add("ref", branch)
85 .build()
86 );
87 MatcherAssert.assertThat(
88 contents.readme(branch).json().getString("content"),
89 Matchers.is(body)
90 );
91 }
92
93
94
95
96
97
98 @Test
99 public void canCreateFile() throws Exception {
100 final String path = "file.txt";
101 final Content.Smart content = new Content.Smart(
102 this.createFile(new MkGithub().randomRepo(), path)
103 );
104 MatcherAssert.assertThat(
105 content.path(),
106 Matchers.is(path)
107 );
108 MatcherAssert.assertThat(
109 content.name(),
110 Matchers.is(path)
111 );
112 MatcherAssert.assertThat(
113 content.sha(),
114 Matchers.not(Matchers.is(Matchers.emptyOrNullString()))
115 );
116 }
117
118
119
120
121
122
123 @Test
124 public void canCreateFileInSomeBranch() throws Exception {
125 final String path = "file-in-branch.txt";
126 final String branch = "branch-2";
127 final String body = "some file";
128 final Content.Smart content = new Content.Smart(
129 new MkGithub().randomRepo().contents().create(
130 content(path, "some file", body)
131 .add("ref", branch)
132 .build()
133 )
134 );
135 MatcherAssert.assertThat(
136 content.path(),
137 Matchers.is(path)
138 );
139 MatcherAssert.assertThat(
140 content.name(),
141 Matchers.is(path)
142 );
143 MatcherAssert.assertThat(
144 content.sha(),
145 Matchers.not(Matchers.is(Matchers.emptyOrNullString()))
146 );
147 MatcherAssert.assertThat(
148 content.content(),
149 Matchers.is(body)
150 );
151 }
152
153
154
155
156
157
158 @Test
159 public void canRemoveFile() throws Exception {
160 final Repo repo = new MkGithub().randomRepo();
161 final String path = "removeme.txt";
162 this.createFile(repo, path);
163 final JsonObject json = MkContentsTest
164 .content(path, "theDeleteMessage")
165 .add("committer", MkContentsTest.committer())
166 .build();
167 final RepoCommit commit = repo.contents().remove(json);
168 MatcherAssert.assertThat(commit, Matchers.notNullValue());
169 MatcherAssert.assertThat(
170 commit.json().getString("message"),
171 Matchers.equalTo("theDeleteMessage")
172 );
173 }
174
175
176
177
178
179
180 @Test
181 public void canRemoveFileFromBranch() throws Exception {
182 final String branch = "branch-1";
183 final Repo repo = new MkGithub().randomRepo();
184 final String path = "removeme.txt";
185 this.createFile(repo, path);
186 final JsonObject json = MkContentsTest
187 .content(path, "theDeleteMessage")
188 .add("ref", branch)
189 .add("committer", MkContentsTest.committer())
190 .build();
191 final RepoCommit commit = repo.contents().remove(json);
192 MatcherAssert.assertThat(commit, Matchers.notNullValue());
193 MatcherAssert.assertThat(
194 commit.json().getString("message"),
195 Matchers.equalTo("theDeleteMessage")
196 );
197 }
198
199
200
201
202
203 @Test
204 public void updatesFile() throws Exception {
205 final String path = "file.txt";
206 final String message = "content message";
207 final String initial = "initial text";
208 final String updated = "updated text";
209 final String cont = "content";
210 final Contents contents = new MkGithub().randomRepo().contents();
211 MatcherAssert.assertThat(
212 contents.create(
213 MkContentsTest.content(path, message, initial).build()
214 ).json().getString(cont),
215 Matchers.is(initial)
216 );
217 contents.update(
218 path, MkContentsTest.content(path, message, updated).build()
219 );
220 MatcherAssert.assertThat(
221 contents.get(path, "master").json().getString(cont),
222 Matchers.is(updated)
223 );
224 }
225
226
227
228
229
230
231 @Test
232 public void updatesFileCreateCommit() throws Exception {
233 final MkStorage storage = new MkStorage.InFile();
234 final Contents contents = MkContentsTest.repo(storage).contents();
235 final String path = "file.txt";
236 final JsonObject json = MkContentsTest
237 .content(path, "theCreateMessage", "newContent")
238 .add("committer", MkContentsTest.committer())
239 .build();
240 contents.create(json);
241 final String xpath = "/github/repos/repo/commits/commit";
242 MatcherAssert.assertThat(
243 storage.xml().nodes(xpath),
244 Matchers.<XML>iterableWithSize(1)
245 );
246 final JsonObject update = MkContentsTest
247 .content(path, "theMessage", "blah")
248 .build();
249 MatcherAssert.assertThat(
250 new RepoCommit.Smart(contents.update(path, update)).sha(),
251 Matchers.not(Matchers.is(Matchers.emptyOrNullString()))
252 );
253 MatcherAssert.assertThat(
254 new Content.Smart(contents.get(path, "master")).path(),
255 Matchers.is(path)
256 );
257 MatcherAssert.assertThat(
258 storage.xml().nodes(xpath),
259 Matchers.<XML>iterableWithSize(2)
260 );
261 }
262
263
264
265
266
267 @Test
268 public void updateContent() throws Exception {
269 final String path = "content-to-update.txt";
270 final String message = "commit message";
271 final String initial = "Hello World!";
272 final String updated = "update content";
273 final String branch = "master";
274 final Contents contents = new MkGithub().randomRepo().contents();
275 final JsonObject content = MkContentsTest
276 .content(path, message, initial)
277 .add("ref", branch)
278 .build();
279 MatcherAssert.assertThat(
280 new Content.Smart(contents.create(content)).content(),
281 Matchers.is(initial)
282 );
283 contents.update(
284 path, MkContentsTest.content(path, message, updated)
285 .add("ref", branch).build()
286 );
287 MatcherAssert.assertThat(
288 new Content.Smart(contents.get(path, branch)).content(),
289 Matchers.is(updated)
290 );
291 }
292
293
294
295
296
297 @Test
298 public void checkExists() throws Exception {
299 final String path = "content-exist.txt";
300 final String branch = "rel.08";
301 final Contents contents = new MkGithub().randomRepo().contents();
302 contents.create(
303 MkContentsTest.content(path, "commit", "content exists")
304 .add("ref", branch)
305 .build()
306 );
307 MatcherAssert.assertThat(
308 contents.exists(path, branch),
309 Matchers.is(true)
310 );
311 MatcherAssert.assertThat(
312 contents.exists("content-not-exist.txt", branch),
313 Matchers.is(false)
314 );
315 }
316
317
318
319
320
321 @Test
322 public void getContentFromDefaultBranch() throws Exception {
323 final String path = "content-default-branch.txt";
324 final String message = "content default branch created";
325 final String text = "I'm content of default branch";
326 final Contents contents = new MkGithub().randomRepo().contents();
327 final JsonObject content = MkContentsTest
328 .content(path, message, text)
329 .build();
330 MatcherAssert.assertThat(
331 new Content.Smart(contents.create(content)).content(),
332 Matchers.is(text)
333 );
334 MatcherAssert.assertThat(
335 new Content.Smart(contents.get(path)).content(),
336 Matchers.is(text)
337 );
338 }
339
340
341
342
343
344 @Test
345 public void canIterate() throws IOException {
346 final MkStorage storage = new MkStorage.InFile();
347 final Repo repo = repo(storage);
348 final Content[] correct = this.addContent(
349 repo, "foo/bar/1", "foo/bar/2"
350 );
351 this.addContent(repo, "foo/baz", "foo/boo");
352 MatcherAssert.assertThat(
353 repo.contents().iterate("foo/bar", "ref-1"),
354 Matchers.contains(correct)
355 );
356 }
357
358
359
360
361
362
363
364
365 private Content[] addContent(final Repo repo,
366 final String... paths) throws IOException {
367 final Content[] result = new Content[paths.length];
368 int index = 0;
369 for (final String path : paths) {
370 result[index] = repo.contents().create(
371 Json.createObjectBuilder().add("ref", "ref-1")
372 .add("path", path).add("content", path)
373 .add("message", "msg").build()
374 );
375 index += 1;
376 }
377 return result;
378 }
379
380
381
382
383
384
385
386
387 private Content createFile(
388 final Repo repo, final String path) throws Exception {
389 final Contents contents = repo.contents();
390 final JsonObject json = MkContentsTest
391 .content(path, "theCreateMessage", "newContent")
392 .add("committer", MkContentsTest.committer())
393 .build();
394 return contents.create(json);
395 }
396
397
398
399
400
401
402
403 private static JsonObjectBuilder content(
404 final String path, final String message) {
405 return Json.createObjectBuilder()
406 .add("path", path)
407 .add("message", message);
408 }
409
410
411
412
413
414
415
416
417 private static JsonObjectBuilder content(
418 final String path, final String message, final String content) {
419 return Json.createObjectBuilder()
420 .add("path", path)
421 .add("message", message)
422 .add("content", content);
423 }
424
425
426
427
428
429 private static JsonObjectBuilder committer() {
430 return Json.createObjectBuilder()
431 .add("name", "joe")
432 .add("email", "joe@contents.com");
433 }
434
435
436
437
438
439
440
441 private static Repo repo(
442 final MkStorage storage) throws IOException {
443 final String login = "test";
444 return new MkGithub(storage, login).randomRepo();
445 }
446
447 }