1
2
3
4
5 package com.jcabi.github;
6
7 import com.jcabi.github.mock.MkGitHub;
8 import com.jcabi.http.Request;
9 import com.jcabi.http.mock.MkAnswer;
10 import com.jcabi.http.mock.MkContainer;
11 import com.jcabi.http.mock.MkGrizzlyContainer;
12 import com.jcabi.http.mock.MkQuery;
13 import com.jcabi.http.request.ApacheRequest;
14 import com.jcabi.http.request.FakeRequest;
15 import jakarta.json.Json;
16 import jakarta.json.JsonObject;
17 import java.io.IOException;
18 import java.net.HttpURLConnection;
19 import org.hamcrest.MatcherAssert;
20 import org.hamcrest.Matchers;
21 import org.hamcrest.collection.IsIterableWithSize;
22 import org.hamcrest.core.IsEqual;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25
26
27
28
29
30
31
32 @ExtendWith(RandomPort.class)
33 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
34 final class RtCommentTest {
35
36
37
38
39
40 @Test
41 void canCompareInstances() throws IOException {
42 final Repo repo = new MkGitHub().randomRepo();
43 final Issue issue = repo.issues().create("title", "body");
44 final RtComment less = new RtComment(new FakeRequest(), issue, 1);
45 final RtComment greater = new RtComment(new FakeRequest(), issue, 2);
46 MatcherAssert.assertThat(
47 "Value is not less than expected",
48 less.compareTo(greater), Matchers.lessThan(0)
49 );
50 MatcherAssert.assertThat(
51 "Value is not greater than expected",
52 greater.compareTo(less), Matchers.greaterThan(0)
53 );
54 }
55
56
57
58
59 @Test
60 void returnsItsIssue() throws IOException {
61 final Repo repo = new MkGitHub().randomRepo();
62 final Issue issue = repo.issues().create("testing1", "issue1");
63 final RtComment comment = new RtComment(new FakeRequest(), issue, 1);
64 MatcherAssert.assertThat(
65 "Values are not equal", comment.issue(), Matchers.is(issue)
66 );
67 }
68
69 @Test
70 void returnsItsNumber() throws IOException {
71 final Repo repo = new MkGitHub().randomRepo();
72 final Issue issue = repo.issues().create("testing2", "issue2");
73 final long num = 10L;
74 final RtComment comment = new RtComment(new FakeRequest(), issue, num);
75 MatcherAssert.assertThat(
76 "Values are not equal", comment.number(), Matchers.is(num)
77 );
78 }
79
80
81
82
83 @Test
84 void removesComment() throws IOException {
85 try (
86 MkContainer container = new MkGrizzlyContainer().next(
87 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
88 ).start(RandomPort.port())) {
89 final Repo repo = new MkGitHub().randomRepo();
90 final Issue issue = repo.issues().create("testing3", "issue3");
91 final RtComment comment = new RtComment(
92 new ApacheRequest(container.home()), issue, 10
93 );
94 comment.remove();
95 final MkQuery query = container.take();
96 MatcherAssert.assertThat(
97 "Values are not equal",
98 query.method(),
99 Matchers.equalTo(Request.DELETE)
100 );
101 }
102 }
103
104 @Test
105 void returnsItsJSon() throws IOException {
106 final String body = "{\"body\":\"test5\"}";
107 try (
108 MkContainer container = new MkGrizzlyContainer().next(
109 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body)
110 ).start(RandomPort.port())) {
111 final Repo repo = new MkGitHub().randomRepo();
112 final Issue issue = repo.issues().create("testing4", "issue4");
113 final RtComment comment = new RtComment(
114 new ApacheRequest(container.home()), issue, 10
115 );
116 final JsonObject json = comment.json();
117 MatcherAssert.assertThat(
118 "Values are not equal",
119 json.getString("body"),
120 Matchers.is("test5")
121 );
122 }
123 }
124
125 @Test
126 void patchesComment() throws IOException {
127 try (
128 MkContainer container = new MkGrizzlyContainer().next(
129 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
130 ).start(RandomPort.port())) {
131 final Repo repo = new MkGitHub().randomRepo();
132 final Issue issue = repo.issues().create("testing5", "issue5");
133 final RtComment comment = new RtComment(
134 new ApacheRequest(container.home()), issue, 10
135 );
136 final JsonObject patch = Json.createObjectBuilder()
137 .add("title", "test comment").build();
138 comment.patch(patch);
139 final MkQuery query = container.take();
140 MatcherAssert.assertThat(
141 "Values are not equal",
142 query.method(), Matchers.equalTo(Request.PATCH)
143 );
144 }
145 }
146
147 @Test
148 void reacts() throws IOException {
149 try (
150 MkContainer container = new MkGrizzlyContainer().next(
151 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
152 ).start(RandomPort.port())) {
153 final Repo repo = new MkGitHub().randomRepo();
154 final Issue issue = repo.issues().create(
155 "Reaction adding test", "This is a test for adding a reaction"
156 );
157 final RtComment comment = new RtComment(
158 new ApacheRequest(container.home()), issue, 10
159 );
160 comment.react(new Reaction.Simple(Reaction.HEART));
161 final MkQuery query = container.take();
162 MatcherAssert.assertThat(
163 "Assertion failed",
164 query.method(),
165 new IsEqual<>(Request.POST)
166 );
167 }
168 }
169
170 @Test
171 void reactions() throws IOException {
172 try (
173 MkContainer container = new MkGrizzlyContainer().next(
174 new MkAnswer.Simple(
175 HttpURLConnection.HTTP_OK,
176 Json.createArrayBuilder()
177 .add(
178 Json.createObjectBuilder()
179 .add("id", "1")
180 .add("content", "heart")
181 .build()
182 ).build().toString()
183 )
184 ).start(RandomPort.port())) {
185 final Repo repo = new MkGitHub().randomRepo();
186 final Issue issue = repo.issues().create(
187 "Reaction Listing test", "This is a test for listing reactions"
188 );
189 final RtComment comment = new RtComment(
190 new ApacheRequest(container.home()), issue, 10
191 );
192 MatcherAssert.assertThat(
193 "Assertion failed",
194 comment.reactions(),
195 new IsIterableWithSize<>(new IsEqual<>(1))
196 );
197 }
198 }
199
200
201
202
203 @Test
204 void givesToString() throws IOException {
205 try (
206 MkContainer container = new MkGrizzlyContainer().next(
207 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
208 ).start(RandomPort.port())) {
209 final Repo repo = new MkGitHub().randomRepo();
210 final Issue issue = repo.issues().create("testing6", "issue6");
211 final RtComment comment = new RtComment(
212 new ApacheRequest(container.home()), issue, 10
213 );
214 final String text = comment.toString();
215 MatcherAssert.assertThat(
216 "Values are not equal",
217 text,
218 Matchers.not(Matchers.is(Matchers.emptyOrNullString()))
219 );
220 MatcherAssert.assertThat(
221 "String does not end with expected value",
222 text,
223 Matchers.endsWith("10")
224 );
225 }
226 }
227 }