View Javadoc
1   /**
2    * Copyright (c) 2013-2023, 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.mock;
31  
32  import com.jcabi.github.Coordinates;
33  import com.jcabi.github.Issue;
34  import com.jcabi.github.Pull;
35  import com.jcabi.github.PullRef;
36  import com.jcabi.github.Repo;
37  import javax.json.Json;
38  import javax.json.JsonObject;
39  import org.hamcrest.MatcherAssert;
40  import org.hamcrest.Matchers;
41  import org.hamcrest.core.IsEqual;
42  import org.junit.Test;
43  import org.mockito.Mockito;
44  
45  /**
46   * Test case for {@link MkPull}.
47   *
48   * @author Carlos Miranda (miranda.cma@gmail.com)
49   * @version $Id $
50   * @checkstyle MultipleStringLiterals (500 lines)
51   * @checkstyle JavadocMethodCheck (500 lines)
52   */
53  @SuppressWarnings("PMD.TooManyMethods")
54  public final class MkPullTest {
55      /**
56       * Login of test user.
57       */
58      private static final String USERNAME = "patrick";
59      /**
60       * Base branch name.
61       */
62      private static final String BASE = "my-base-branch";
63      /**
64       * Head branch name.
65       */
66      private static final String HEAD = "my-head-branch";
67  
68      /**
69       * MkPull should be able to compare different instances.
70       *
71       * @throws Exception when a problem occurs.
72       */
73      @Test
74      public void canCompareInstances() throws Exception {
75          final MkPull less = new MkPull(
76              new MkStorage.InFile(),
77              "login-less",
78              Mockito.mock(Coordinates.class),
79              1
80          );
81          final MkPull greater = new MkPull(
82              new MkStorage.InFile(),
83              "login-greater",
84              Mockito.mock(Coordinates.class),
85              2
86          );
87          MatcherAssert.assertThat(
88              less.compareTo(greater),
89              Matchers.lessThan(0)
90          );
91          MatcherAssert.assertThat(
92              greater.compareTo(less),
93              Matchers.greaterThan(0)
94          );
95      }
96  
97      /**
98       * MkPull can get comments number if no comments.
99       *
100      * @throws Exception when a problem occurs.
101      */
102     @Test
103     public void canGetCommentsNumberIfZero() throws Exception {
104         final Pull pull = MkPullTest.pullRequest();
105         MatcherAssert.assertThat(
106             pull.json().getInt("comments"),
107             Matchers.is(0)
108         );
109     }
110 
111     /**
112      * MkPull can get comments number if some comments exist.
113      *
114      * @throws Exception when a problem occurs.
115      */
116     @Test
117     public void canGetCommentsNumberIfNonZero() throws Exception {
118         final Pull pull = MkPullTest.pullRequest();
119         pull.comments().post("comment1", "path1", "how are you?", 1);
120         pull.comments().post("comment2", "path2", "how are you2?", 2);
121         MatcherAssert.assertThat(
122             pull.json().getInt("comments"),
123             Matchers.is(2)
124         );
125     }
126 
127     /**
128      * MkPull can get comments.
129      *
130      * @throws Exception when a problem occurs.
131      */
132     @Test
133     public void canGetComments() throws Exception {
134         final Pull pull = MkPullTest.pullRequest();
135         MatcherAssert.assertThat(
136             pull.comments(),
137             Matchers.notNullValue()
138         );
139     }
140 
141     /**
142      * MkPull can get its base ref.
143      * @throws Exception If a problem occurs.
144      */
145     @Test
146     public void canGetBase() throws Exception {
147         final PullRef base = MkPullTest.pullRequest().base();
148         MatcherAssert.assertThat(base, Matchers.notNullValue());
149         MatcherAssert.assertThat(
150             base.ref(),
151             Matchers.equalTo(MkPullTest.BASE)
152         );
153     }
154 
155     /**
156      * MkPull can get its head ref.
157      * @throws Exception If a problem occurs.
158      */
159     @Test
160     public void canGetHead() throws Exception {
161         final PullRef head = MkPullTest.pullRequest().head();
162         MatcherAssert.assertThat(head, Matchers.notNullValue());
163         MatcherAssert.assertThat(
164             head.ref(),
165             Matchers.equalTo(MkPullTest.HEAD)
166         );
167     }
168 
169     /**
170      * MkPull can be represented as JSON.
171      *
172      * @throws Exception If a problem occurs.
173      */
174     @Test
175     public void canRetrieveAsJson() throws Exception {
176         final String head = "blah";
177         final String base = "aaa";
178         final Pull pull = MkPullTest.repo().pulls()
179             .create("Test Pull Json", head, base);
180         final JsonObject json = pull.json();
181         MatcherAssert.assertThat(
182             json.getInt("number"),
183             Matchers.equalTo(1)
184         );
185         MatcherAssert.assertThat(
186             json.getJsonObject("head").getString("label"),
187             Matchers.equalTo(
188                 String.format(
189                     "%s:%s",
190                     MkPullTest.USERNAME,
191                     head
192                 )
193             )
194         );
195         MatcherAssert.assertThat(
196             json.getJsonObject("base").getString("label"),
197             Matchers.equalTo(
198                 String.format(
199                     "%s:%s",
200                     MkPullTest.USERNAME,
201                     base
202                 )
203             )
204         );
205         MatcherAssert.assertThat(
206             json.getJsonObject("user").getString("login"),
207             Matchers.equalTo(MkPullTest.USERNAME)
208         );
209     }
210 
211     /**
212      * MkPull can perform JSON patch operation.
213      *
214      * @throws Exception If a problem occurs.
215      */
216     @Test
217     public void canPatchJson() throws Exception {
218         final Pull pull = MkPullTest.repo().pulls()
219             .create("Test Patch", "def", "abc");
220         final String value = "someValue";
221         pull.patch(
222             Json.createObjectBuilder().add("somekey", value).build()
223         );
224         MatcherAssert.assertThat(
225             pull.json().getString("somekey"),
226             new IsEqual<>(value)
227         );
228         final int lines = 20;
229         pull.patch(Json.createObjectBuilder().add("additions", lines).build());
230         MatcherAssert.assertThat(
231             pull.json().getString("additions"),
232             new IsEqual<>(Integer.toString(lines))
233         );
234     }
235 
236     @Test
237     public void issueIsPull() throws Exception {
238         final Pull pull = MkPullTest.pullRequest();
239         MatcherAssert.assertThat(
240             "Issue is not a pull request",
241             new Issue.Smart(pull.repo().issues().get(pull.number())).isPull(),
242             Matchers.is(true)
243         );
244     }
245 
246     @Test
247     public void retrievesAllChecks() throws Exception {
248         final Pull pull = MkPullTest.pullRequest();
249         MatcherAssert.assertThat(
250             pull.checks().all(),
251             Matchers.hasSize(0)
252         );
253     }
254 
255     /**
256      * Create an repo to work with.
257      * @return Repo
258      * @throws Exception If some problem inside
259      */
260     private static Repo repo() throws Exception {
261         return new MkGithub(MkPullTest.USERNAME).randomRepo();
262     }
263 
264     /**
265      * Create a pull request to work with.
266      * @return Repo
267      * @throws Exception If some problem inside
268      */
269     private static Pull pullRequest() throws Exception {
270         final Repo rpo = MkPullTest.repo();
271         final MkBranches branches = (MkBranches) rpo.branches();
272         branches.create(
273             MkPullTest.BASE,
274             "e11f7ffa797f8422f016576cb7c2f5bb6f66aa51"
275         );
276         branches.create(
277             MkPullTest.HEAD,
278             "5a8d0143b3fa9de883a5672d4a1f44d472657a8a"
279         );
280         return rpo.pulls().create(
281             "Test PR",
282             MkPullTest.HEAD,
283             MkPullTest.BASE
284         );
285     }
286 }