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.google.common.base.Optional;
33 import com.jcabi.http.request.FakeRequest;
34 import javax.json.Json;
35 import javax.json.JsonObject;
36 import org.hamcrest.MatcherAssert;
37 import org.hamcrest.Matchers;
38 import org.junit.Test;
39
40
41
42
43
44
45 public final class RtCommitsComparisonTest {
46
47
48
49
50
51
52
53 @Test
54 public void fetchesJson() throws Exception {
55 final String sha = "fffffffffffffffffffffffffffffffffffffffe";
56 final String filename = "bar/quux.txt";
57
58 final int additions = 7;
59 final int deletions = 2;
60 final int changes = 9;
61 final String patch = "some diff here";
62
63 final String bloburl = "https://api.jcabi-github.invalid/johndoe/my-repo/blob/fffffffffffffffffffffffffffffffffffffffe/bar/quux.txt";
64 final String rawurl = "https://api.jcabi-github.invalid/johndoe/my-repo/raw/fffffffffffffffffffffffffffffffffffffffe/bar/quux.txt";
65 final String contentsurl = "https://api.github.invalid/repos/johndoe/my-repo/contents/bar/quux.txt?ref=fffffffffffffffffffffffffffffffffffffffe";
66 final CommitsComparison comparison = new RtCommitsComparison(
67 new FakeRequest().withBody(
68 Json.createObjectBuilder()
69 .add("base_commit", Json.createObjectBuilder())
70 .add("commits", Json.createArrayBuilder())
71 .add(
72 "files",
73 Json.createArrayBuilder()
74 .add(
75 Json.createObjectBuilder()
76 .add("sha", sha)
77 .add("filename", filename)
78 .add("status", "added")
79 .add("additions", additions)
80 .add("deletions", deletions)
81 .add("changes", changes)
82 .add("patch", patch)
83 .add("blob_url", bloburl)
84 .add("raw_url", rawurl)
85 .add("contents_url", contentsurl)
86 .build()
87 )
88 .build()
89 )
90 .build().toString()
91 ),
92 RtCommitsComparisonTest.repo(),
93 "6dcb09b5b57875f334f61aebed695e2e4193db51",
94 "6dcb09b5b57875f334f61aebed695e2e4193db52"
95 );
96 final JsonObject json = comparison.json();
97 MatcherAssert.assertThat(
98 json.getJsonObject("base_commit"), Matchers.notNullValue()
99 );
100 MatcherAssert.assertThat(
101 json.getJsonArray("commits"), Matchers.notNullValue()
102 );
103 MatcherAssert.assertThat(
104 comparison.files(),
105 Matchers.<FileChange>iterableWithSize(1)
106 );
107 final FileChange.Smart file = new FileChange.Smart(
108 comparison.files().iterator().next()
109 );
110 MatcherAssert.assertThat(file.sha(), Matchers.equalTo(sha));
111 MatcherAssert.assertThat(file.filename(), Matchers.equalTo(filename));
112 MatcherAssert.assertThat(file.additions(), Matchers.equalTo(additions));
113 MatcherAssert.assertThat(file.deletions(), Matchers.equalTo(deletions));
114 MatcherAssert.assertThat(file.changes(), Matchers.equalTo(changes));
115 MatcherAssert.assertThat(
116 file.status(),
117 Matchers.equalTo(FileChange.Status.ADDED)
118 );
119 MatcherAssert.assertThat(
120 file.patch(),
121 Matchers.equalTo(Optional.of(patch))
122 );
123 }
124
125
126
127
128
129 private static Repo repo() {
130 return new RtGithub().repos()
131 .get(new Coordinates.Simple("user", "repo"));
132 }
133
134 }