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 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   * Test case for {@link RtPull}.
51   *
52   * @author Carlos Miranda (miranda.cma@gmail.com)
53   * @version $Id: 52244a6a34ba43038c04bb9deca025aa8e0b1011 $
54   */
55  public final class RtPullTest {
56      /**
57       * Property name for ref name in pull request ref JSON object.
58       */
59      private static final String REF_PROP = "ref";
60      /**
61       * Property name for commit SHA in pull request ref JSON object.
62       */
63      private static final String SHA_PROP = "sha";
64  
65      /**
66       * The rule for skipping test if there's BindException.
67       * @checkstyle VisibilityModifierCheck (3 lines)
68       */
69      @Rule
70      public final transient RandomPort resource = new RandomPort();
71  
72      /**
73       * RtPull should be able to retrieve commits.
74       *
75       * @throws Exception when a problem occurs.
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      * RtPull should be able to retrieve files.
102      *
103      * @throws Exception when a problem occurs.
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      * RtPull can fetch its base ref.
130      * @throws IOException If some I/O problem occurs
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      * RtPull can fetch its head ref.
177      * @throws IOException If some I/O problem occurs
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      * RtPull should be able to perform a merge.
224      *
225      * @throws Exception when a problem occurs.
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      * RtPull should be able to compare different instances.
255      *
256      * @throws Exception when a problem occurs.
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      * RtPull should be able to fetch pull comments.
272      *
273      * @throws Exception when a problem occurs.
274      */
275     @Test
276     @Ignore
277     public void canFetchComments() throws Exception {
278         //to be implemented
279     }
280 
281     /**
282      * Mock repository for testing purposes.
283      * @return Repo the mock repository.
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 }