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.Request;
33 import com.jcabi.http.mock.MkAnswer;
34 import com.jcabi.http.mock.MkContainer;
35 import com.jcabi.http.mock.MkGrizzlyContainer;
36 import com.jcabi.http.mock.MkQuery;
37 import com.jcabi.http.request.ApacheRequest;
38 import com.jcabi.http.request.FakeRequest;
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.Ignore;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.mockito.Mockito;
48
49
50
51
52
53
54
55 public final class RtPullTest {
56
57
58
59 private static final String REF_PROP = "ref";
60
61
62
63 private static final String SHA_PROP = "sha";
64
65
66
67
68
69 @Rule
70 public final transient RandomPort resource = new RandomPort();
71
72
73
74
75
76
77 @Test
78 public void fetchesCommits() throws Exception {
79 try (
80 final MkContainer container = new MkGrizzlyContainer().next(
81 new MkAnswer.Simple(
82 HttpURLConnection.HTTP_OK,
83 "[{\"commits\":\"test\"}]"
84 )
85 ).start(this.resource.port())
86 ) {
87 final RtPull pull = new RtPull(
88 new ApacheRequest(container.home()),
89 this.repo(),
90 1
91 );
92 MatcherAssert.assertThat(
93 pull.commits(),
94 Matchers.notNullValue()
95 );
96 container.stop();
97 }
98 }
99
100
101
102
103
104
105 @Test
106 public void fetchesFiles() throws Exception {
107 try (
108 final MkContainer container = new MkGrizzlyContainer().next(
109 new MkAnswer.Simple(
110 HttpURLConnection.HTTP_OK,
111 "[{\"file1\":\"testFile\"}]"
112 )
113 ).start(this.resource.port())
114 ) {
115 final RtPull pull = new RtPull(
116 new ApacheRequest(container.home()),
117 this.repo(),
118 2
119 );
120 MatcherAssert.assertThat(
121 pull.files().iterator().next().getString("file1"),
122 Matchers.equalTo("testFile")
123 );
124 container.stop();
125 }
126 }
127
128
129
130
131
132 @Test
133 public void fetchesBase() throws IOException {
134 final String ref = "sweet-feature-branch";
135 final String sha = "e93c6a2216c69daa574abc16e7c14767fce44ad6";
136 try (
137 final MkContainer container = new MkGrizzlyContainer().next(
138 new MkAnswer.Simple(
139 HttpURLConnection.HTTP_OK,
140 Json.createObjectBuilder()
141 .add(
142 "base",
143 Json.createObjectBuilder()
144 .add(RtPullTest.REF_PROP, ref)
145 .add(RtPullTest.SHA_PROP, sha)
146 .build()
147 )
148 .build()
149 .toString()
150 )
151 ).start(this.resource.port())
152 ) {
153 final RtPull pull = new RtPull(
154 new ApacheRequest(container.home()),
155 this.repo(),
156 1
157 );
158 final PullRef base = pull.base();
159 MatcherAssert.assertThat(
160 base,
161 Matchers.notNullValue()
162 );
163 MatcherAssert.assertThat(
164 base.ref(),
165 Matchers.equalTo(ref)
166 );
167 MatcherAssert.assertThat(
168 base.sha(),
169 Matchers.equalTo(sha)
170 );
171 container.stop();
172 }
173 }
174
175
176
177
178
179 @Test
180 public void fetchesHead() throws IOException {
181 final String ref = "neat-other-branch";
182 final String sha = "9c717b4716e4fc4d917f546e8e6b562e810e3922";
183 try (
184 final MkContainer container = new MkGrizzlyContainer().next(
185 new MkAnswer.Simple(
186 HttpURLConnection.HTTP_OK,
187 Json.createObjectBuilder()
188 .add(
189 "head",
190 Json.createObjectBuilder()
191 .add(RtPullTest.REF_PROP, ref)
192 .add(RtPullTest.SHA_PROP, sha)
193 .build()
194 )
195 .build()
196 .toString()
197 )
198 ).start(this.resource.port())
199 ) {
200 final RtPull pull = new RtPull(
201 new ApacheRequest(container.home()),
202 this.repo(),
203 1
204 );
205 final PullRef head = pull.head();
206 MatcherAssert.assertThat(
207 head,
208 Matchers.notNullValue()
209 );
210 MatcherAssert.assertThat(
211 head.ref(),
212 Matchers.equalTo(ref)
213 );
214 MatcherAssert.assertThat(
215 head.sha(),
216 Matchers.equalTo(sha)
217 );
218 container.stop();
219 }
220 }
221
222
223
224
225
226
227 @Test
228 public void executeMerge() throws Exception {
229 try (
230 final MkContainer container = new MkGrizzlyContainer().next(
231 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "testMerge")
232 ).start(this.resource.port())
233 ) {
234 final RtPull pull = new RtPull(
235 new ApacheRequest(container.home()),
236 this.repo(),
237 3
238 );
239 pull.merge("Test commit.");
240 final MkQuery query = container.take();
241 MatcherAssert.assertThat(
242 query.method(),
243 Matchers.equalTo(Request.PUT)
244 );
245 MatcherAssert.assertThat(
246 query.body(),
247 Matchers.equalTo("{\"commit_message\":\"Test commit.\"}")
248 );
249 container.stop();
250 }
251 }
252
253
254
255
256
257
258 @Test
259 public void canCompareInstances() throws Exception {
260 final RtPull less = new RtPull(new FakeRequest(), this.repo(), 1);
261 final RtPull greater = new RtPull(new FakeRequest(), this.repo(), 2);
262 MatcherAssert.assertThat(
263 less.compareTo(greater), Matchers.lessThan(0)
264 );
265 MatcherAssert.assertThat(
266 greater.compareTo(less), Matchers.greaterThan(0)
267 );
268 }
269
270
271
272
273
274
275 @Test
276 @Ignore
277 public void canFetchComments() throws Exception {
278
279 }
280
281
282
283
284
285 private Repo repo() {
286 final Repo repo = Mockito.mock(Repo.class);
287 final Coordinates coords = Mockito.mock(Coordinates.class);
288 Mockito.doReturn(coords).when(repo).coordinates();
289 Mockito.doReturn("/user").when(coords).user();
290 Mockito.doReturn("/repo").when(coords).repo();
291 return repo;
292 }
293
294 }