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.Event;
34 import com.jcabi.github.Github;
35 import com.jcabi.github.Issue;
36 import com.jcabi.github.Label;
37 import com.jcabi.github.Repo;
38 import java.util.Collections;
39 import java.util.Iterator;
40 import org.hamcrest.CustomMatcher;
41 import org.hamcrest.MatcherAssert;
42 import org.hamcrest.Matchers;
43 import org.junit.Test;
44 import org.mockito.Mockito;
45
46
47
48
49
50
51
52
53 @SuppressWarnings("PMD.TooManyMethods")
54 public final class MkIssueTest {
55
56
57
58
59
60 @Test
61 public void opensAndCloses() throws Exception {
62 final Issue issue = this.issue();
63 MatcherAssert.assertThat(
64 new Issue.Smart(issue).isOpen(),
65 Matchers.is(true)
66 );
67 new Issue.Smart(issue).close();
68 MatcherAssert.assertThat(
69 new Issue.Smart(issue).isOpen(),
70 Matchers.is(false)
71 );
72 }
73
74
75
76
77
78 @Test
79 public void pointsToAnEmptyPullRequest() throws Exception {
80 final Issue issue = this.issue();
81 MatcherAssert.assertThat(
82 new Issue.Smart(issue).isPull(),
83 Matchers.is(false)
84 );
85 }
86
87
88
89
90
91 @Test
92 public void showsIssueAuthor() throws Exception {
93 final Issue issue = this.issue();
94 MatcherAssert.assertThat(
95 new Issue.Smart(issue).author().login(),
96 Matchers.notNullValue()
97 );
98 }
99
100
101
102
103
104 @Test
105 public void changesTitle() throws Exception {
106 final Issue issue = this.issue();
107 new Issue.Smart(issue).title("hey, works?");
108 MatcherAssert.assertThat(
109 new Issue.Smart(issue).title(),
110 Matchers.startsWith("hey, ")
111 );
112 }
113
114
115
116
117
118 @Test
119 public void changesBody() throws Exception {
120 final Issue issue = this.issue();
121 new Issue.Smart(issue).body("hey, body works?");
122 MatcherAssert.assertThat(
123 new Issue.Smart(issue).body(),
124 Matchers.startsWith("hey, b")
125 );
126 }
127
128
129
130
131
132 @Test
133 public void exponsesProperties() throws Exception {
134 final Issue.Smart issue = new Issue.Smart(this.issue());
135 MatcherAssert.assertThat(issue.createdAt(), Matchers.notNullValue());
136 MatcherAssert.assertThat(issue.updatedAt(), Matchers.notNullValue());
137 MatcherAssert.assertThat(issue.htmlUrl(), Matchers.notNullValue());
138 }
139
140
141
142
143
144 @Test
145 public void listsReadOnlyLabels() throws Exception {
146 final Issue issue = this.issue();
147 final String tag = "test-tag";
148 issue.repo().labels().create(tag, "c0c0c0");
149 issue.labels().add(Collections.singletonList(tag));
150 MatcherAssert.assertThat(
151 new Issue.Smart(issue).roLabels().iterate(),
152 Matchers.<Label>hasItem(
153 new CustomMatcher<Label>("label just created") {
154 @Override
155 public boolean matches(final Object item) {
156 return Label.class.cast(item).name().equals(tag);
157 }
158 }
159 )
160 );
161 }
162
163
164
165
166
167 @Test
168 public void canCompareInstances() throws Exception {
169 final MkIssue less = new MkIssue(
170 new MkStorage.InFile(),
171 "login-less",
172 Mockito.mock(Coordinates.class),
173 1
174 );
175 final MkIssue greater = new MkIssue(
176 new MkStorage.InFile(),
177 "login-greater",
178 Mockito.mock(Coordinates.class),
179 2
180 );
181 MatcherAssert.assertThat(
182 less.compareTo(greater),
183 Matchers.lessThan(0)
184 );
185 MatcherAssert.assertThat(
186 greater.compareTo(less),
187 Matchers.greaterThan(0)
188 );
189 }
190
191
192
193
194
195 @Test
196 public void canRememberItsAuthor() throws Exception {
197 final MkGithub first = new MkGithub("first");
198 final Github second = first.relogin("second");
199 final Repo repo = first.randomRepo();
200 final int number = second.repos()
201 .get(repo.coordinates())
202 .issues()
203 .create("", "")
204 .number();
205 final Issue issue = first.repos()
206 .get(repo.coordinates())
207 .issues()
208 .get(number);
209 MatcherAssert.assertThat(
210 new Issue.Smart(issue).author().login(),
211 Matchers.is("second")
212 );
213 }
214
215
216
217
218
219 @Test
220 public void canCheckIfIssueExists() throws Exception {
221 MatcherAssert.assertThat(this.issue().exists(), Matchers.is(true));
222 }
223
224
225
226
227
228 @Test
229 public void canCheckNonExistentIssue() throws Exception {
230 MatcherAssert.assertThat(
231 new MkIssue(
232 new MkStorage.InFile(),
233 "login",
234 new Coordinates.Simple("user", "repo"),
235 1
236 ).exists(),
237 Matchers.is(false)
238 );
239 }
240
241
242
243
244
245 @Test
246 public void assignsUser() throws Exception {
247 final Issue.Smart issue = new Issue.Smart(this.issue());
248 issue.assign("walter");
249 MatcherAssert.assertThat(
250 issue.assignee().login(),
251 Matchers.startsWith("wal")
252 );
253 }
254
255
256
257
258
259 @Test
260 public void createsClosedEvent() throws Exception {
261 final Issue.Smart issue = new Issue.Smart(this.issue());
262 issue.close();
263 MatcherAssert.assertThat(
264 issue.events(),
265 Matchers.<Event>iterableWithSize(1)
266 );
267 final Event.Smart closed = new Event.Smart(
268 issue.events().iterator().next()
269 );
270 MatcherAssert.assertThat(
271 closed.type(),
272 Matchers.equalTo(Event.CLOSED)
273 );
274 }
275
276
277
278
279
280 @Test
281 public void createsReopenedEvent() throws Exception {
282 final Issue.Smart issue = new Issue.Smart(this.issue());
283 issue.close();
284 issue.open();
285 MatcherAssert.assertThat(
286 issue.events(),
287 Matchers.<Event>iterableWithSize(2)
288 );
289 final Iterator<Event> events = issue.events().iterator();
290 final Event.Smart closed = new Event.Smart(events.next());
291 final Event.Smart reopened = new Event.Smart(events.next());
292 MatcherAssert.assertThat(
293 closed.type(),
294 Matchers.equalTo(Event.CLOSED)
295 );
296 MatcherAssert.assertThat(
297 reopened.type(),
298 Matchers.equalTo(Event.REOPENED)
299 );
300 }
301
302
303
304
305
306
307 private Issue issue() throws Exception {
308 return new MkGithub().randomRepo()
309 .issues().create("hey", "how are you?");
310 }
311
312 }