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;
31
32 import com.jcabi.github.mock.MkGithub;
33 import com.jcabi.http.Request;
34 import com.jcabi.http.mock.MkAnswer;
35 import com.jcabi.http.mock.MkContainer;
36 import com.jcabi.http.mock.MkGrizzlyContainer;
37 import com.jcabi.http.mock.MkQuery;
38 import com.jcabi.http.request.ApacheRequest;
39 import com.jcabi.http.request.FakeRequest;
40 import java.net.HttpURLConnection;
41 import javax.json.Json;
42 import javax.json.JsonObject;
43 import org.hamcrest.MatcherAssert;
44 import org.hamcrest.Matchers;
45 import org.hamcrest.collection.IsIterableWithSize;
46 import org.hamcrest.core.IsEqual;
47 import org.junit.Rule;
48 import org.junit.Test;
49
50
51
52
53
54
55
56
57
58 public final class RtCommentTest {
59
60
61
62
63
64 @Rule
65 public final transient RandomPort resource = new RandomPort();
66
67
68
69
70
71 @Test
72 public void canCompareInstances() throws Exception {
73 final Repo repo = new MkGithub().randomRepo();
74 final Issue issue = repo.issues().create("title", "body");
75 final RtComment less = new RtComment(new FakeRequest(), issue, 1);
76 final RtComment greater = new RtComment(new FakeRequest(), issue, 2);
77 MatcherAssert.assertThat(
78 less.compareTo(greater), Matchers.lessThan(0)
79 );
80 MatcherAssert.assertThat(
81 greater.compareTo(less), Matchers.greaterThan(0)
82 );
83 }
84
85
86
87
88
89 @Test
90 public void returnsItsIssue() throws Exception {
91 final Repo repo = new MkGithub().randomRepo();
92 final Issue issue = repo.issues().create("testing1", "issue1");
93 final RtComment comment = new RtComment(new FakeRequest(), issue, 1);
94 MatcherAssert.assertThat(comment.issue(), Matchers.is(issue));
95 }
96
97
98
99
100
101 @Test
102 public void returnsItsNumber() throws Exception {
103 final Repo repo = new MkGithub().randomRepo();
104 final Issue issue = repo.issues().create("testing2", "issue2");
105 final int num = 10;
106 final RtComment comment = new RtComment(new FakeRequest(), issue, num);
107 MatcherAssert.assertThat(comment.number(), Matchers.is(num));
108 }
109
110
111
112
113
114 @Test
115 public void removesComment() throws Exception {
116 try (
117 final MkContainer container = new MkGrizzlyContainer().next(
118 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
119 ).start(this.resource.port())) {
120 final Repo repo = new MkGithub().randomRepo();
121 final Issue issue = repo.issues().create("testing3", "issue3");
122 final RtComment comment = new RtComment(
123 new ApacheRequest(container.home()), issue, 10
124 );
125 comment.remove();
126 final MkQuery query = container.take();
127 MatcherAssert.assertThat(
128 query.method(),
129 Matchers.equalTo(Request.DELETE)
130 );
131 }
132 }
133
134
135
136
137
138 @Test
139 public void returnsItsJSon() throws Exception {
140 final String body = "{\"body\":\"test5\"}";
141 try (
142 final MkContainer container = new MkGrizzlyContainer().next(
143 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body)
144 ).start(this.resource.port())) {
145 final Repo repo = new MkGithub().randomRepo();
146 final Issue issue = repo.issues().create("testing4", "issue4");
147 final RtComment comment = new RtComment(
148 new ApacheRequest(container.home()), issue, 10
149 );
150 final JsonObject json = comment.json();
151 MatcherAssert.assertThat(
152 json.getString("body"),
153 Matchers.is("test5")
154 );
155 }
156 }
157
158
159
160
161
162 @Test
163 public void patchesComment() throws Exception {
164 try (
165 final MkContainer container = new MkGrizzlyContainer().next(
166 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
167 ).start(this.resource.port())) {
168 final Repo repo = new MkGithub().randomRepo();
169 final Issue issue = repo.issues().create("testing5", "issue5");
170 final RtComment comment = new RtComment(
171 new ApacheRequest(container.home()), issue, 10
172 );
173 final JsonObject jsonPatch = Json.createObjectBuilder()
174 .add("title", "test comment").build();
175 comment.patch(jsonPatch);
176 final MkQuery query = container.take();
177 MatcherAssert.assertThat(
178 query.method(), Matchers.equalTo(Request.PATCH)
179 );
180 }
181 }
182
183
184
185
186
187 @Test
188 public void reacts() throws Exception {
189 try (
190 final MkContainer container = new MkGrizzlyContainer().next(
191 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
192 ).start(this.resource.port())) {
193 final Repo repo = new MkGithub().randomRepo();
194 final Issue issue = repo.issues().create(
195 "Reaction adding test", "This is a test for adding a reaction"
196 );
197 final RtComment comment = new RtComment(
198 new ApacheRequest(container.home()), issue, 10
199 );
200 comment.react(new Reaction.Simple(Reaction.HEART));
201 final MkQuery query = container.take();
202 MatcherAssert.assertThat(
203 query.method(),
204 new IsEqual<>(Request.POST)
205 );
206 }
207 }
208
209
210
211
212
213 @Test
214 public void reactions() throws Exception {
215 try (
216 final MkContainer container = new MkGrizzlyContainer().next(
217 new MkAnswer.Simple(
218 HttpURLConnection.HTTP_OK,
219 Json.createArrayBuilder()
220 .add(
221 Json.createObjectBuilder()
222 .add("id", "1")
223 .add("content", "heart")
224 .build()
225 ).build().toString()
226 )
227 ).start(this.resource.port())) {
228 final Repo repo = new MkGithub().randomRepo();
229 final Issue issue = repo.issues().create(
230 "Reaction Listing test", "This is a test for listing reactions"
231 );
232 final RtComment comment = new RtComment(
233 new ApacheRequest(container.home()), issue, 10
234 );
235 MatcherAssert.assertThat(
236 comment.reactions(),
237 new IsIterableWithSize<>(new IsEqual<>(1))
238 );
239 }
240 }
241
242
243
244
245
246 @Test
247 public void givesToString() throws Exception {
248 try (
249 final MkContainer container = new MkGrizzlyContainer().next(
250 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
251 ).start(this.resource.port())) {
252 final Repo repo = new MkGithub().randomRepo();
253 final Issue issue = repo.issues().create("testing6", "issue6");
254 final RtComment comment = new RtComment(
255 new ApacheRequest(container.home()), issue, 10
256 );
257 final String stringComment = comment.toString();
258 MatcherAssert.assertThat(
259 stringComment,
260 Matchers.not(Matchers.is(Matchers.emptyOrNullString()))
261 );
262 MatcherAssert.assertThat(stringComment, Matchers.endsWith("10"));
263 }
264 }
265 }