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.mock.MkQuery;
37  import com.jcabi.http.request.ApacheRequest;
38  import com.jcabi.http.request.FakeRequest;
39  import com.jcabi.http.request.JdkRequest;
40  import java.io.IOException;
41  import java.net.HttpURLConnection;
42  import javax.json.Json;
43  import javax.json.JsonObject;
44  import org.hamcrest.MatcherAssert;
45  import org.hamcrest.Matchers;
46  import org.junit.Rule;
47  import org.junit.Test;
48  import org.mockito.Mockito;
49  
50  /**
51   * Test case for {@link RtReleases}.
52   * @author Paul Polishchuk (ppol@ua.fm)
53   * @author Paulo Lobo (pauloeduardolobo@gmail.com)
54   * @version $Id: 0271e91e06e226b2820ff9ed004cc80f3ce74100 $
55   * @checkstyle MultipleStringLiterals (500 lines)
56   * @since 0.8
57   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
58   */
59  public final class RtReleasesTest {
60  
61      /**
62       * The rule for skipping test if there's BindException.
63       * @checkstyle VisibilityModifierCheck (3 lines)
64       */
65      @Rule
66      public final transient RandomPort resource = new RandomPort();
67  
68      /**
69       * RtReleases can fetch empty list of releases.
70       * @throws Exception if some problem inside
71       */
72      @Test
73      public void canFetchEmptyListOfReleases() throws Exception {
74          final Releases releases = new RtReleases(
75              new FakeRequest().withBody("[]"),
76              RtReleasesTest.repo()
77          );
78          MatcherAssert.assertThat(
79              releases.iterate(),
80              Matchers.emptyIterable()
81          );
82      }
83  
84      /**
85       * RtReleases can fetch non empty list of releases.
86       */
87      @Test
88      public void canFetchNonEmptyListOfReleases() {
89          final int number = 1;
90          final Releases releases = new RtReleases(
91              new FakeRequest().withBody(
92                  Json.createArrayBuilder().add(
93                      Json.createObjectBuilder()
94                          .add("id", number)
95                          .add("tag_name", "v1.0.0")
96                          .add("name", "v1.0.0")
97                          .add("body", "Release")
98                  ).build().toString()
99              ),
100             RtReleasesTest.repo()
101         );
102         MatcherAssert.assertThat(
103             releases.iterate().iterator().next().number(),
104             Matchers.equalTo(number)
105         );
106     }
107 
108     /**
109      * RtReleases can fetch a single release.
110      * @throws IOException If some problem inside
111      */
112     @Test
113     public void canFetchSingleRelease() throws IOException {
114         final Releases releases = new RtReleases(
115             new FakeRequest(), RtReleasesTest.repo()
116         );
117         MatcherAssert.assertThat(releases.get(1), Matchers.notNullValue());
118     }
119 
120     /**
121      * RtReleases can create a release.
122      * @throws Exception If some problem inside
123      */
124     @Test
125     public void canCreateRelease() throws Exception {
126         final String tag = "v1.0.0";
127         final String rel = release(tag).toString();
128         try (
129             final MkContainer container = new MkGrizzlyContainer().next(
130                 new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, rel)
131             ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, rel))
132                 .start(this.resource.port())
133         ) {
134             final RtReleases releases = new RtReleases(
135                 new JdkRequest(container.home()),
136                 repo()
137             );
138             final Release release = releases.create(tag);
139             MatcherAssert.assertThat(
140                 container.take().method(),
141                 Matchers.equalTo(Request.POST)
142             );
143             MatcherAssert.assertThat(
144                 release.json().getString("tag_name"),
145                 Matchers.equalTo(tag)
146             );
147             container.stop();
148         }
149     }
150 
151     /**
152      * RtReleases can delete a release.
153      * @throws Exception If some problem inside
154      */
155     @Test
156     public void canDeleteRelease() throws Exception {
157         try (
158             final MkContainer container = new MkGrizzlyContainer().next(
159                 new MkAnswer.Simple(
160                     HttpURLConnection.HTTP_NO_CONTENT,
161                     ""
162                 )
163             ).start(this.resource.port());
164         ) {
165             final Releases releases = new RtReleases(
166                 new ApacheRequest(container.home()),
167                 RtReleasesTest.repo()
168             );
169             releases.remove(1);
170             final MkQuery query = container.take();
171             MatcherAssert.assertThat(
172                 query.uri().toString(),
173                 Matchers.endsWith("/releases/1")
174             );
175             MatcherAssert.assertThat(
176                 query.method(),
177                 Matchers.equalTo(Request.DELETE)
178             );
179             container.stop();
180         }
181     }
182 
183     /**
184      * Create and return repo for testing.
185      * @return Repo
186      */
187     private static Repo repo() {
188         final Repo repo = Mockito.mock(Repo.class);
189         Mockito.doReturn(new Coordinates.Simple("test", "releases"))
190             .when(repo).coordinates();
191         return repo;
192     }
193 
194     /**
195      * Create and return JsonObject to test.
196      * @param tag The tag name of the release
197      * @return JsonObject
198      * @throws Exception If some problem inside
199      */
200     private static JsonObject release(final String tag) throws Exception {
201         return Json.createObjectBuilder()
202             .add("id", 1)
203             .add("tag_name", tag)
204             .build();
205     }
206 }