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