1
2
3
4
5 package com.jcabi.github.mock;
6
7 import com.jcabi.github.Coordinates;
8 import com.jcabi.github.Issue;
9 import com.jcabi.github.Pull;
10 import com.jcabi.github.PullRef;
11 import com.jcabi.github.Repo;
12 import jakarta.json.Json;
13 import jakarta.json.JsonObject;
14 import java.io.IOException;
15 import org.hamcrest.MatcherAssert;
16 import org.hamcrest.Matchers;
17 import org.hamcrest.core.IsEqual;
18 import org.junit.jupiter.api.Test;
19 import org.mockito.Mockito;
20
21
22
23
24
25
26
27 @SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"})
28 final class MkPullTest {
29
30
31
32 private static final String USERNAME = "patrick";
33
34
35
36
37 private static final String BASE = "my-base-branch";
38
39
40
41
42 private static final String HEAD = "my-head-branch";
43
44 @Test
45 void canCompareInstances() throws IOException {
46 final MkPull less = new MkPull(
47 new MkStorage.InFile(),
48 "login-less",
49 Mockito.mock(Coordinates.class),
50 1
51 );
52 final MkPull greater = new MkPull(
53 new MkStorage.InFile(),
54 "login-greater",
55 Mockito.mock(Coordinates.class),
56 2
57 );
58 MatcherAssert.assertThat(
59 "Value is not less than expected",
60 less.compareTo(greater),
61 Matchers.lessThan(0)
62 );
63 MatcherAssert.assertThat(
64 "Value is not greater than expected",
65 greater.compareTo(less),
66 Matchers.greaterThan(0)
67 );
68 }
69
70
71
72
73
74 @Test
75 void canGetCommentsNumberIfZero() throws Exception {
76 final Pull pull = MkPullTest.pullRequest();
77 MatcherAssert.assertThat(
78 "Values are not equal",
79 pull.json().getInt("comments"),
80 Matchers.is(0)
81 );
82 }
83
84
85
86
87
88 @Test
89 void canGetCommentsNumberIfNonZero() throws Exception {
90 final Pull pull = MkPullTest.pullRequest();
91 pull.comments().post("comment1", "path1", "how are you?", 1);
92 pull.comments().post("comment2", "path2", "how are you2?", 2);
93 MatcherAssert.assertThat(
94 "Values are not equal",
95 pull.json().getInt("comments"),
96 Matchers.is(2)
97 );
98 }
99
100
101
102
103
104 @Test
105 void canGetComments() throws Exception {
106 final Pull pull = MkPullTest.pullRequest();
107 MatcherAssert.assertThat(
108 "Value is null",
109 pull.comments(),
110 Matchers.notNullValue()
111 );
112 }
113
114
115
116
117
118 @Test
119 void canGetBase() throws Exception {
120 final PullRef base = MkPullTest.pullRequest().base();
121 MatcherAssert.assertThat(
122 "Value is null", base, Matchers.notNullValue()
123 );
124 MatcherAssert.assertThat(
125 "Values are not equal",
126 base.ref(),
127 Matchers.equalTo(MkPullTest.BASE)
128 );
129 }
130
131
132
133
134
135 @Test
136 void canGetHead() throws Exception {
137 final PullRef head = MkPullTest.pullRequest().head();
138 MatcherAssert.assertThat(
139 "Value is null", head, Matchers.notNullValue()
140 );
141 MatcherAssert.assertThat(
142 "Values are not equal",
143 head.ref(),
144 Matchers.equalTo(MkPullTest.HEAD)
145 );
146 }
147
148
149
150
151
152 @Test
153 void canRetrieveAsJson() throws Exception {
154 final String head = "blah";
155 final String base = "aaa";
156 final Pull pull = MkPullTest.repo().pulls()
157 .create("Test Pull Json", head, base);
158 final JsonObject json = pull.json();
159 MatcherAssert.assertThat(
160 "Values are not equal",
161 json.getInt("number"),
162 Matchers.equalTo(1)
163 );
164 MatcherAssert.assertThat(
165 "Values are not equal",
166 json.getJsonObject("head").getString("label"),
167 Matchers.equalTo(
168 String.format(
169 "%s:%s",
170 MkPullTest.USERNAME,
171 head
172 )
173 )
174 );
175 MatcherAssert.assertThat(
176 "Values are not equal",
177 json.getJsonObject("base").getString("label"),
178 Matchers.equalTo(
179 String.format(
180 "%s:%s",
181 MkPullTest.USERNAME,
182 base
183 )
184 )
185 );
186 MatcherAssert.assertThat(
187 "Values are not equal",
188 json.getJsonObject("user").getString("login"),
189 Matchers.equalTo(MkPullTest.USERNAME)
190 );
191 }
192
193
194
195
196
197 @Test
198 void canPatchJson() throws Exception {
199 final Pull pull = MkPullTest.repo().pulls()
200 .create("Test Patch", "def", "abc");
201 final String value = "someValue";
202 pull.patch(
203 Json.createObjectBuilder().add("somekey", value).build()
204 );
205 MatcherAssert.assertThat(
206 "Assertion failed",
207 pull.json().getString("somekey"),
208 new IsEqual<>(value)
209 );
210 final int lines = 20;
211 pull.patch(Json.createObjectBuilder().add("additions", lines).build());
212 MatcherAssert.assertThat(
213 "Assertion failed",
214 pull.json().getString("additions"),
215 new IsEqual<>(Integer.toString(lines))
216 );
217 }
218
219 @Test
220 void issueIsPull() throws Exception {
221 final Pull pull = MkPullTest.pullRequest();
222 MatcherAssert.assertThat(
223 "Issue is not a pull request",
224 new Issue.Smart(pull.repo().issues().get(pull.number())).isPull(),
225 Matchers.is(true)
226 );
227 }
228
229 @Test
230 void retrievesAllChecks() throws Exception {
231 final Pull pull = MkPullTest.pullRequest();
232 MatcherAssert.assertThat(
233 "Collection size is incorrect",
234 pull.checks().all(),
235 Matchers.hasSize(0)
236 );
237 }
238
239
240
241
242
243 private static Repo repo() throws IOException {
244 return new MkGitHub(MkPullTest.USERNAME).randomRepo();
245 }
246
247
248
249
250
251
252 private static Pull pullRequest() throws Exception {
253 final Repo rpo = MkPullTest.repo();
254 final MkBranches branches = (MkBranches) rpo.branches();
255 branches.create(
256 MkPullTest.BASE,
257 "e11f7ffa797f8422f016576cb7c2f5bb6f66aa51"
258 );
259 branches.create(
260 MkPullTest.HEAD,
261 "5a8d0143b3fa9de883a5672d4a1f44d472657a8a"
262 );
263 return rpo.pulls().create(
264 "Test PR",
265 MkPullTest.HEAD,
266 MkPullTest.BASE
267 );
268 }
269 }