View Javadoc
1   /**
2    * Copyright (c) 2013-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
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.request.ApacheRequest;
37  import com.jcabi.http.request.FakeRequest;
38  import java.net.HttpURLConnection;
39  import java.util.Iterator;
40  import javax.json.Json;
41  import javax.json.JsonObject;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.Rule;
45  import org.junit.Test;
46  import org.mockito.Mockito;
47  
48  /**
49   * Test case for {@link RtRepo}.
50   *
51   * @author Giang Le (giang@vn-smartsolutions.com)
52   * @author Paulo Lobo (pauloeduardolobo@gmail.com)
53   * @version $Id: 668483047e6241e9043e2c0e84069e50bb47412e $
54   */
55  @SuppressWarnings("PMD.TooManyMethods")
56  public final class RtRepoTest {
57  
58      /**
59       * The rule for skipping test if there's BindException.
60       * @checkstyle VisibilityModifierCheck (3 lines)
61       */
62      @Rule
63      public final transient RandomPort resource = new RandomPort();
64  
65      /**
66       * RtRepo can fetch events.
67       *
68       * @throws Exception If some problem inside
69       */
70      @Test
71      public void iteratesEvents() throws Exception {
72          try (
73              final MkContainer container = new MkGrizzlyContainer().next(
74                  new MkAnswer.Simple(
75                      HttpURLConnection.HTTP_OK,
76                      Json.createArrayBuilder()
77                          .add(event(Event.ASSIGNED))
78                          .add(event(Event.MENTIONED))
79                          .build().toString()
80                  )
81              ).start(this.resource.port())
82          ) {
83              final Repo repo = RtRepoTest.repo(
84                  new ApacheRequest(container.home())
85              );
86              MatcherAssert.assertThat(
87                  repo.issueEvents().iterate(),
88                  Matchers.<Event>iterableWithSize(2)
89              );
90              container.stop();
91          }
92      }
93  
94      /**
95       * RtRepo can fetch its labels.
96       *
97       * @throws Exception if a problem occurs.
98       */
99      @Test
100     public void fetchesLabels() throws Exception {
101         final Repo repo = RtRepoTest.repo(
102             new FakeRequest()
103         );
104         MatcherAssert.assertThat(
105             repo.labels(),
106             Matchers.notNullValue()
107         );
108     }
109 
110     /**
111      * RtRepo can fetch its issues.
112      *
113      * @throws Exception if a problem occurs.
114      */
115     @Test
116     public void fetchesIssues() throws Exception {
117         final Repo repo = RtRepoTest.repo(
118             new FakeRequest()
119         );
120         MatcherAssert.assertThat(
121             repo.issues(),
122             Matchers.notNullValue()
123         );
124     }
125 
126     /**
127      * RtRepo can fetch its branches.
128      *
129      * @throws Exception if a problem occurs.
130      */
131     @Test
132     public void fetchesBranches() throws Exception {
133         final Repo repo = RtRepoTest.repo(
134             new FakeRequest()
135         );
136         MatcherAssert.assertThat(
137             repo.branches(),
138             Matchers.notNullValue()
139         );
140     }
141 
142     /**
143      * RtRepo can fetch its pulls.
144      *
145      * @throws Exception if a problem occurs.
146      */
147     @Test
148     public void fetchesPulls() throws Exception {
149         final Repo repo = RtRepoTest.repo(
150             new FakeRequest()
151         );
152         MatcherAssert.assertThat(
153             repo.pulls(),
154             Matchers.notNullValue()
155         );
156     }
157 
158     /**
159      * RtRepo can fetch its hooks.
160      *
161      * @throws Exception if a problem occurs.
162      */
163     @Test
164     public void fetchHooks() throws Exception {
165         final Repo repo = RtRepoTest.repo(
166             new FakeRequest()
167         );
168         MatcherAssert.assertThat(
169             repo.hooks(),
170             Matchers.notNullValue()
171         );
172     }
173 
174     /**
175      * RtRepo can fetch its keys.
176      *
177      * @throws Exception if a problem occurs.
178      */
179     @Test
180     public void fetchKeys() throws Exception {
181         final Repo repo = RtRepoTest.repo(
182             new FakeRequest()
183         );
184         MatcherAssert.assertThat(
185             repo.keys(),
186             Matchers.notNullValue()
187         );
188     }
189 
190     /**
191      * RtRepo can fetch its releases.
192      *
193      * @throws Exception if a problem occurs.
194      */
195     @Test
196     public void fetchReleases() throws Exception {
197         final Repo repo = RtRepoTest.repo(
198             new FakeRequest()
199         );
200         MatcherAssert.assertThat(
201             repo.releases(),
202             Matchers.notNullValue()
203         );
204     }
205 
206     /**
207      * RtRepo can fetch its contents.
208      *
209      * @throws Exception if a problem occurs.
210      */
211     @Test
212     public void fetchContents() throws Exception {
213         final Repo repo = RtRepoTest.repo(
214             new FakeRequest()
215         );
216         MatcherAssert.assertThat(
217             repo.contents(),
218             Matchers.notNullValue()
219         );
220     }
221 
222     /**
223      * RtRepo can identify itself.
224      * @throws Exception If some problem inside
225      */
226     @Test
227     public void identifiesItself() throws Exception {
228         final Coordinates coords = new Coordinates.Simple("me", "me-branch");
229         final Repo repo = new RtRepo(
230             Mockito.mock(Github.class),
231             new FakeRequest(),
232             coords
233         );
234         MatcherAssert.assertThat(
235             repo.coordinates(),
236             Matchers.sameInstance(coords)
237         );
238     }
239 
240     /**
241      * RtRepo can execute PATCH request.
242      *
243      * @throws Exception if there is any problem
244      */
245     @Test
246     public void executePatchRequest() throws Exception {
247         try (
248             final MkContainer container = new MkGrizzlyContainer().next(
249                 new MkAnswer.Simple(
250                     HttpURLConnection.HTTP_OK,
251                     event(Event.ASSIGNED).toString()
252                 )
253             ).start(this.resource.port())
254         ) {
255             final Repo repo = RtRepoTest.repo(
256                 new ApacheRequest(container.home())
257             );
258             repo.patch(RtRepoTest.event(Event.ASSIGNED));
259             MatcherAssert.assertThat(
260                 container.take().method(),
261                 Matchers.equalTo(Request.PATCH)
262             );
263             container.stop();
264         }
265     }
266 
267     /**
268      * RtRepo can describe as a JSON object.
269      *
270      * @throws Exception if there is any problem
271      */
272     @Test
273     public void describeAsJson() throws Exception {
274         final Repo repo = RtRepoTest.repo(
275             new FakeRequest().withBody(
276                 Json.createObjectBuilder()
277                     .add("full_name", "octocat/Hello-World")
278                     .add("fork", true)
279                     .build()
280                     .toString()
281             )
282         );
283         MatcherAssert.assertThat(
284             repo.json().toString(),
285             Matchers.equalTo(
286                 "{\"full_name\":\"octocat/Hello-World\",\"fork\":true}"
287             )
288         );
289     }
290 
291     /**
292      * RtRepo can fetch commits.
293      */
294     @Test
295     public void fetchCommits() {
296         final Repo repo = RtRepoTest.repo(
297             new FakeRequest()
298         );
299         MatcherAssert.assertThat(repo.commits(), Matchers.notNullValue());
300     }
301 
302     /**
303      * RtRepo can fetch Git.
304      */
305     @Test
306     public void fetchesGit() {
307         final Repo repo = RtRepoTest.repo(
308             new FakeRequest()
309         );
310         MatcherAssert.assertThat(repo.git(), Matchers.notNullValue());
311     }
312 
313     /**
314      * RtRepo can fetch stars.
315      */
316     @Test
317     public void fetchStars() {
318         final Repo repo = RtRepoTest.repo(
319             new FakeRequest()
320         );
321         MatcherAssert.assertThat(repo.stars(), Matchers.notNullValue());
322     }
323 
324     /**
325      * RtRepo can fetch notifications.
326      */
327     @Test
328     public void fetchNotifications() {
329         final Repo repo = RtRepoTest.repo(
330             new FakeRequest()
331         );
332         MatcherAssert.assertThat(repo.notifications(), Matchers.notNullValue());
333     }
334 
335     /**
336      * RtRepo can fetch languages.
337      * @throws Exception if a problem occurs.
338      */
339     @Test
340     public void fetchLanguages() throws Exception {
341         try (
342             final MkContainer container = new MkGrizzlyContainer().next(
343                 new MkAnswer.Simple(
344                     HttpURLConnection.HTTP_OK,
345                     Json.createObjectBuilder()
346                         .add("Ruby", 1)
347                         .build().toString()
348                 )
349             ).start(this.resource.port())
350         ) {
351             final Repo repo = RtRepoTest.repo(
352                 new ApacheRequest(container.home())
353             );
354             MatcherAssert.assertThat(repo.languages(), Matchers.notNullValue());
355             container.stop();
356         }
357     }
358 
359     /**
360      * RtRepo can iterate languages.
361      *
362      * @throws Exception If some problem inside
363      */
364     @Test
365     public void iteratesLanguages() throws Exception {
366         final String lang = "C";
367         final String other = "Java";
368         try (
369             final MkContainer container = new MkGrizzlyContainer().next(
370                 new MkAnswer.Simple(
371                     HttpURLConnection.HTTP_OK,
372                     Json.createObjectBuilder()
373                         .add(lang, 1)
374                         .add(other, 2)
375                         .build().toString()
376                 )
377             ).start(this.resource.port())
378         ) {
379             final Repo repo = RtRepoTest.repo(
380                 new ApacheRequest(container.home())
381             );
382             final Iterator<Language> iter = repo.languages().iterator();
383             MatcherAssert.assertThat(
384                 iter.hasNext(),
385                 Matchers.is(true)
386             );
387             MatcherAssert.assertThat(
388                 iter.next().name(),
389                 Matchers.is(lang)
390             );
391             MatcherAssert.assertThat(
392                 iter.hasNext(),
393                 Matchers.is(true)
394             );
395             MatcherAssert.assertThat(
396                 iter.next().name(),
397                 Matchers.is(other)
398             );
399             MatcherAssert.assertThat(
400                 iter.hasNext(),
401                 Matchers.is(false)
402             );
403             container.stop();
404         }
405     }
406 
407     /**
408      * Create and return JsonObject to test.
409      * @param event Event type
410      * @return JsonObject
411      * @throws Exception if some problem inside
412      */
413     private static JsonObject event(final String event) throws Exception {
414         return Json.createObjectBuilder()
415             .add("id", 1)
416             .add("event", event)
417             .build();
418     }
419 
420     /**
421      * Create test Repo.
422      * @param request Request
423      * @return Repo
424      */
425     private static Repo repo(final Request request) {
426         return new RtRepo(
427             Mockito.mock(Github.class),
428             request,
429             new Coordinates.Simple("testuser", "testrepo")
430         );
431     }
432 }