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.github.mock.MkFileChange;
34 import java.io.IOException;
35 import javax.json.Json;
36 import org.hamcrest.MatcherAssert;
37 import org.hamcrest.Matchers;
38 import org.junit.Test;
39
40
41
42
43
44
45
46 public final class FileChangeTest {
47
48
49
50
51 @Test
52 public void getsStatus() throws IOException {
53 final String status = "status";
54 MatcherAssert.assertThat(
55 FileChangeTest.stringFileChange(status, "added").status(),
56 Matchers.equalTo(FileChange.Status.ADDED)
57 );
58 MatcherAssert.assertThat(
59 FileChangeTest.stringFileChange(status, "modified").status(),
60 Matchers.equalTo(FileChange.Status.MODIFIED)
61 );
62 MatcherAssert.assertThat(
63 FileChangeTest.stringFileChange(status, "removed").status(),
64 Matchers.equalTo(FileChange.Status.REMOVED)
65 );
66 MatcherAssert.assertThat(
67 FileChangeTest.stringFileChange(status, "renamed").status(),
68 Matchers.equalTo(FileChange.Status.RENAMED)
69 );
70 }
71
72
73
74
75
76 @Test
77 public void getsFilename() throws IOException {
78 final String filename = "foo/bar.txt";
79 MatcherAssert.assertThat(
80 FileChangeTest.stringFileChange("filename", filename).filename(),
81 Matchers.equalTo(filename)
82 );
83 }
84
85
86
87
88
89 @Test
90 public void getsSha() throws IOException {
91 final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db51";
92 MatcherAssert.assertThat(
93 FileChangeTest.stringFileChange("sha", sha).sha(),
94 Matchers.equalTo(sha)
95 );
96 }
97
98
99
100
101
102 @Test
103 public void getsAdditions() throws IOException {
104
105 final int adds = 42;
106 MatcherAssert.assertThat(
107 FileChangeTest.intFileChange("additions", adds).additions(),
108 Matchers.equalTo(adds)
109 );
110 }
111
112
113
114
115
116 @Test
117 public void getsDeletions() throws IOException {
118
119 final int deletions = 97;
120 MatcherAssert.assertThat(
121 FileChangeTest.intFileChange("deletions", deletions).deletions(),
122 Matchers.equalTo(deletions)
123 );
124 }
125
126
127
128
129
130 @Test
131 public void getsChanges() throws IOException {
132
133 final int changes = 11;
134 MatcherAssert.assertThat(
135 FileChangeTest.intFileChange("changes", changes).changes(),
136 Matchers.equalTo(changes)
137 );
138 }
139
140
141
142
143
144
145 @Test
146 public void getsAbsentPatch() throws IOException {
147 MatcherAssert.assertThat(
148 new FileChange.Smart(
149 new MkFileChange(
150 Json.createObjectBuilder().build()
151 )
152 ).patch(),
153 Matchers.equalTo(Optional.<String>absent())
154 );
155 }
156
157
158
159
160
161
162 @Test
163 public void getsPresentPatch() throws IOException {
164
165 final String patch = "@@ -120,7 +120,7 @@ class Test1 @@ -1000,7 +1000,7 @@ class Test1";
166 MatcherAssert.assertThat(
167 stringFileChange(
168 "patch",
169 patch
170 ).patch(),
171 Matchers.equalTo(Optional.of(patch))
172 );
173 }
174
175
176
177
178
179 @Test
180 public void getsRawUrl() throws IOException {
181
182 final String url = "https://api.jcabi-github.invalid/octocat/Hello-World/raw/6dcb09b5b57875f334f61aebed695e2e4193db51/foo/bar.txt";
183 MatcherAssert.assertThat(
184 stringFileChange(
185 "raw_url",
186 url
187 ).rawUrl(),
188 Matchers.equalTo(url)
189 );
190 }
191
192
193
194
195
196 @Test
197 public void getsBlobUrl() throws IOException {
198
199 final String url = "https://api.jcabi-github.invalid/octocat/Hello-World/blob/6dcb09b5b57875f334f61aebed695e2e4193db51/foo/bar.txt";
200 MatcherAssert.assertThat(
201 stringFileChange(
202 "blob_url",
203 url
204 ).blobUrl(),
205 Matchers.equalTo(url)
206 );
207 }
208
209
210
211
212
213 @Test
214 public void getsContentsUrl() throws IOException {
215
216 final String url = "https://api.jcabi-github.invalid/repos/octocat/Hello-World/contents/foo/bar.txt?ref=6dcb09b5b57875f334f61aebed695e2e4193db51";
217 MatcherAssert.assertThat(
218 FileChangeTest.stringFileChange("contents_url", url)
219 .contentsUrl(),
220 Matchers.equalTo(url)
221 );
222 }
223
224
225
226
227
228
229
230
231 private static FileChange.Smart stringFileChange(
232 final String key,
233 final String value
234 ) {
235 return new FileChange.Smart(
236 new MkFileChange(
237 Json.createObjectBuilder()
238 .add(key, value)
239 .build()
240 )
241 );
242 }
243
244
245
246
247
248
249
250
251 private static FileChange.Smart intFileChange(
252 final String key,
253 final int value
254 ) {
255 return new FileChange.Smart(
256 new MkFileChange(
257 Json.createObjectBuilder()
258 .add(key, value)
259 .build()
260 )
261 );
262 }
263 }