1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
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  
47  
48  
49  
50  
51  
52  
53  @SuppressWarnings("PMD.TooManyMethods")
54  public final class MkPullTest {
55      
56  
57  
58      private static final String USERNAME = "patrick";
59      
60  
61  
62      private static final String BASE = "my-base-branch";
63      
64  
65  
66      private static final String HEAD = "my-head-branch";
67  
68      
69  
70  
71  
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  
99  
100 
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 
113 
114 
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 
129 
130 
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 
143 
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 
157 
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 
171 
172 
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 
213 
214 
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 
257 
258 
259 
260     private static Repo repo() throws Exception {
261         return new MkGithub(MkPullTest.USERNAME).randomRepo();
262     }
263 
264     
265 
266 
267 
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 }