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.PullComment;
33 import com.jcabi.github.PullComments;
34 import com.jcabi.github.Repo;
35 import com.jcabi.github.Repos;
36 import java.io.IOException;
37 import java.util.Collections;
38 import java.util.List;
39 import javax.json.JsonObject;
40 import org.hamcrest.MatcherAssert;
41 import org.hamcrest.Matchers;
42 import org.junit.Test;
43
44
45
46
47
48
49
50 public final class MkPullCommentsTest {
51
52
53
54
55
56
57 @Test
58 public void fetchesPullComment() throws Exception {
59 final PullComments comments = this.comments();
60 final PullComment comment = comments.post("comment", "commit", "/", 1);
61 MatcherAssert.assertThat(
62 comments.get(comment.number()).number(),
63 Matchers.equalTo(comment.number())
64 );
65 }
66
67
68
69
70
71
72 @Test
73 public void iteratesRepoPullComments() throws Exception {
74 final PullComments comments = comments();
75 comments.pull()
76 .repo()
77 .pulls()
78 .create("new", "head-branch", "base-branch")
79 .comments()
80 .post("new pull comment", "new commit", "/p", 1);
81 comments.post("test 1", "tesst 1", "/test1", 1);
82 MatcherAssert.assertThat(
83 comments.iterate(
84 comments.pull().number(),
85 Collections.<String, String>emptyMap()
86 ),
87 Matchers.<PullComment>iterableWithSize(1)
88 );
89 MatcherAssert.assertThat(
90 comments.iterate(
91 Collections.<String, String>emptyMap()
92 ),
93 Matchers.<PullComment>iterableWithSize(2)
94 );
95 }
96
97
98
99
100
101
102 @Test
103 public void iteratesPullRequestComments() throws Exception {
104 final PullComments comments = comments();
105 comments.post("comment 1", "commit 1", "/commit1", 1);
106 comments.post("comment 2", "commit 2", "/commit2", 2);
107 MatcherAssert.assertThat(
108 comments.iterate(
109 comments.pull().number(),
110 Collections.<String, String>emptyMap()
111 ),
112 Matchers.<PullComment>iterableWithSize(2)
113 );
114 }
115
116
117
118
119
120
121 @Test
122 public void postsPullComment() throws Exception {
123 final MkStorage storage = new MkStorage.InFile();
124 final String commit = "commit_id";
125 final String path = "path";
126 final String bodytext = "some text as a body";
127 final String login = "jamie";
128 final String reponame = "incredible";
129 final Repo repo = new MkGithub(storage, login).repos().create(
130 new Repos.RepoCreate(reponame, false)
131 );
132 repo.pulls()
133 .create("pullrequest1", "head", "base").comments()
134 .post(bodytext, commit, path, 1);
135 final String[] fields = {commit, path};
136 for (final String element : fields) {
137 MkPullCommentsTest.assertFieldContains(storage, repo, element);
138 }
139 final List<String> position = storage.xml().xpath(
140 String.format(
141
142 "/github/repos/repo[@coords='%s/%s']/pulls/pull/comments/comment/position/text()",
143 repo.coordinates().user(),
144 repo.coordinates().repo()
145 )
146 );
147 MatcherAssert.assertThat(
148 position.get(0), Matchers.notNullValue()
149 );
150 final List<String> body = storage.xml().xpath(
151 String.format(
152
153 "/github/repos/repo[@coords='%s/%s']/pulls/pull/comments/comment/body/text()",
154 repo.coordinates().user(),
155 repo.coordinates().repo()
156 )
157 );
158 MatcherAssert.assertThat(body.get(0), Matchers.equalTo(bodytext));
159 }
160
161
162
163
164
165
166 @Test
167 public void createsPullCommentReply() throws Exception {
168 final PullComments comments = this.comments();
169 final int orig = comments.post(
170 "Orig Comment",
171 "6dcb09b5b57875f334f61aebed695e2e4193db5e",
172 "file1.txt",
173 1
174 ).number();
175 final String body = "Reply Comment";
176 final JsonObject reply = comments.reply(body, orig).json();
177 MatcherAssert.assertThat(
178 reply.getString("body"),
179 Matchers.is(body)
180 );
181 MatcherAssert.assertThat(
182 reply.getString("original_position"),
183 Matchers.is(Integer.toString(orig))
184 );
185 }
186
187
188
189
190
191
192 @Test
193 public void removesPullComment() throws Exception {
194 final PullComments comments = this.comments();
195 final int orig = comments.post(
196 "Origg Comment",
197 "6dcb09b5b57875f334f61aebed695e2e4193db5d",
198 "file2.txt",
199 1
200 ).number();
201 MatcherAssert.assertThat(
202 comments.iterate(
203 orig, Collections.<String, String>emptyMap()
204 ),
205 Matchers.<PullComment>iterableWithSize(1)
206 );
207 comments.remove(orig);
208 MatcherAssert.assertThat(
209 comments.iterate(
210 orig, Collections.<String, String>emptyMap()
211 ),
212 Matchers.<PullComment>iterableWithSize(0)
213 );
214 }
215
216
217
218
219
220
221 private PullComments comments() throws IOException {
222
223 return new MkGithub().randomRepo().pulls()
224 .create("hello", "awesome-head", "awesome-base")
225 .comments();
226 }
227
228
229
230
231
232
233
234
235 private static void assertFieldContains(
236 final MkStorage storage,
237 final Repo repo,
238 final String element) throws IOException {
239 final String xpath = String.format(
240
241 "/github/repos/repo[@coords='%s/%s']/pulls/pull/comments/comment/%s/text()",
242 repo.coordinates().user(),
243 repo.coordinates().repo(),
244 element
245 );
246 MatcherAssert.assertThat(
247 storage.xml().xpath(xpath).get(0),
248 Matchers.is(element)
249 );
250 }
251
252 }