1
2
3
4
5 package com.jcabi.github.mock;
6
7 import com.jcabi.github.PullComment;
8 import com.jcabi.github.PullComments;
9 import com.jcabi.github.Repo;
10 import com.jcabi.github.Repos;
11 import jakarta.json.JsonObject;
12 import java.io.IOException;
13 import java.util.Collections;
14 import java.util.List;
15 import org.hamcrest.MatcherAssert;
16 import org.hamcrest.Matchers;
17 import org.junit.jupiter.api.Test;
18
19
20
21
22
23 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
24 final class MkPullCommentsTest {
25
26 @Test
27 void fetchesPullComment() throws IOException {
28 final PullComments comments = MkPullCommentsTest.comments();
29 final PullComment comment = comments.post("comment", "commit", "/", 1);
30 MatcherAssert.assertThat(
31 "Values are not equal",
32 comments.get(comment.number()).number(),
33 Matchers.equalTo(comment.number())
34 );
35 }
36
37 @Test
38 void iteratesRepoPullComments() throws IOException {
39 final PullComments comments = MkPullCommentsTest.comments();
40 comments.pull()
41 .repo()
42 .pulls()
43 .create("new", "head-branch", "base-branch")
44 .comments()
45 .post("new pull comment", "new commit", "/p", 1);
46 comments.post("test 1", "tesst 1", "/test1", 1);
47 MatcherAssert.assertThat(
48 "Collection size is incorrect",
49 comments.iterate(
50 comments.pull().number(),
51 Collections.emptyMap()
52 ),
53 Matchers.iterableWithSize(1)
54 );
55 MatcherAssert.assertThat(
56 "Collection size is incorrect",
57 comments.iterate(
58 Collections.emptyMap()
59 ),
60 Matchers.iterableWithSize(2)
61 );
62 }
63
64 @Test
65 void iteratesPullRequestComments() throws IOException {
66 final PullComments comments = MkPullCommentsTest.comments();
67 comments.post("comment 1", "commit 1", "/commit1", 1);
68 comments.post("comment 2", "commit 2", "/commit2", 2);
69 MatcherAssert.assertThat(
70 "Collection size is incorrect",
71 comments.iterate(
72 comments.pull().number(),
73 Collections.emptyMap()
74 ),
75 Matchers.iterableWithSize(2)
76 );
77 }
78
79 @Test
80 void postsPullComment() throws IOException {
81 final MkStorage storage = new MkStorage.InFile();
82 final String commit = "commit_id";
83 final String path = "path";
84 final String bodytext = "some text as a body";
85 final String login = "jamie";
86 final String reponame = "incredible";
87 final Repo repo = new MkGitHub(storage, login).repos().create(
88 new Repos.RepoCreate(reponame, false)
89 );
90 repo.pulls()
91 .create("pullrequest1", "head", "base").comments()
92 .post(bodytext, commit, path, 1);
93 final String[] fields = {commit, path};
94 for (final String element : fields) {
95 MkPullCommentsTest.assertFieldContains(storage, repo, element);
96 }
97 final List<String> position = storage.xml().xpath(
98 String.format(
99
100 "/github/repos/repo[@coords='%s/%s']/pulls/pull/comments/comment/position/text()",
101 repo.coordinates().user(),
102 repo.coordinates().repo()
103 )
104 );
105 MatcherAssert.assertThat(
106 "Value is null",
107 position.get(0),
108 Matchers.notNullValue()
109 );
110 final List<String> body = storage.xml().xpath(
111 String.format(
112
113 "/github/repos/repo[@coords='%s/%s']/pulls/pull/comments/comment/body/text()",
114 repo.coordinates().user(),
115 repo.coordinates().repo()
116 )
117 );
118 MatcherAssert.assertThat(
119 "Values are not equal", body.get(0), Matchers.equalTo(bodytext)
120 );
121 }
122
123 @Test
124 void createsPullCommentReply() throws IOException {
125 final PullComments comments = MkPullCommentsTest.comments();
126 final int orig = comments.post(
127 "Orig Comment",
128 "6dcb09b5b57875f334f61aebed695e2e4193db5e",
129 "file1.txt",
130 1
131 ).number();
132 final String body = "Reply Comment";
133 final JsonObject reply = comments.reply(body, orig).json();
134 MatcherAssert.assertThat(
135 "Values are not equal",
136 reply.getString("body"),
137 Matchers.is(body)
138 );
139 MatcherAssert.assertThat(
140 "Values are not equal",
141 reply.getString("original_position"),
142 Matchers.is(Integer.toString(orig))
143 );
144 }
145
146 @Test
147 void removesPullComment() throws IOException {
148 final PullComments comments = MkPullCommentsTest.comments();
149 final int orig = comments.post(
150 "Origg Comment",
151 "6dcb09b5b57875f334f61aebed695e2e4193db5d",
152 "file2.txt",
153 1
154 ).number();
155 MatcherAssert.assertThat(
156 "Collection size is incorrect",
157 comments.iterate(
158 orig, Collections.emptyMap()
159 ),
160 Matchers.iterableWithSize(1)
161 );
162 comments.remove(orig);
163 MatcherAssert.assertThat(
164 "Collection size is incorrect",
165 comments.iterate(
166 orig, Collections.emptyMap()
167 ),
168 Matchers.iterableWithSize(0)
169 );
170 }
171
172
173
174
175
176
177 private static PullComments comments() throws IOException {
178
179 return new MkGitHub().randomRepo().pulls()
180 .create("hello", "awesome-head", "awesome-base")
181 .comments();
182 }
183
184
185
186
187
188
189
190
191 private static void assertFieldContains(
192 final MkStorage storage,
193 final Repo repo,
194 final String element) throws IOException {
195 final String xpath = String.format(
196
197 "/github/repos/repo[@coords='%s/%s']/pulls/pull/comments/comment/%s/text()",
198 repo.coordinates().user(),
199 repo.coordinates().repo(),
200 element
201 );
202 MatcherAssert.assertThat(
203 "Values are not equal",
204 storage.xml().xpath(xpath).get(0),
205 Matchers.is(element)
206 );
207 }
208
209 }