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.IsCollectionWithSize;
46 import org.hamcrest.core.IsEqual;
47 import org.junit.Ignore;
48 import org.junit.Rule;
49 import org.junit.Test;
50 import org.mockito.Mockito;
51
52
53
54
55
56
57
58
59
60 public final class RtPullCommentTest {
61
62
63
64
65
66 @Rule
67 public final transient RandomPort resource = new RandomPort();
68
69
70
71
72 @Test
73 public void canCompareInstances() throws Exception {
74 final Pull pull = Mockito.mock(Pull.class);
75 Mockito.doReturn(new MkGithub().randomRepo()).when(pull).repo();
76 final RtPullComment less =
77 new RtPullComment(new FakeRequest(), pull, 1);
78 final RtPullComment greater =
79 new RtPullComment(new FakeRequest(), pull, 2);
80 MatcherAssert.assertThat(
81 less.compareTo(greater), Matchers.lessThan(0)
82 );
83 MatcherAssert.assertThat(
84 greater.compareTo(less), Matchers.greaterThan(0)
85 );
86 MatcherAssert.assertThat(
87 less.compareTo(less), Matchers.equalTo(0)
88 );
89 }
90
91
92
93
94
95 @Test
96 public void canDescribeAsJson() throws Exception {
97 final String body = "{\"body\":\"test\"}";
98 try (
99 final MkContainer container = new MkGrizzlyContainer().next(
100 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body)
101 ).start(this.resource.port())
102 ) {
103 final Pull pull = Mockito.mock(Pull.class);
104 Mockito.doReturn(repo()).when(pull).repo();
105 final RtPullComment comment =
106 new RtPullComment(new ApacheRequest(container.home()), pull, 1);
107 final JsonObject json = comment.json();
108 MatcherAssert.assertThat(
109 json.getString("body"),
110 Matchers.is("test")
111 );
112 MatcherAssert.assertThat(
113 container.take().uri().toString(),
114 Matchers.endsWith("/repos/joe/blueharvest/pulls/comments/1")
115 );
116 container.stop();
117 }
118 }
119
120
121
122
123
124 @Test
125 public void patchesComment() throws Exception {
126 try (
127 final MkContainer container = new MkGrizzlyContainer().next(
128 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
129 ).start(this.resource.port())
130 ) {
131 final Pull pull = Mockito.mock(Pull.class);
132 Mockito.doReturn(repo()).when(pull).repo();
133 final RtPullComment comment =
134 new RtPullComment(new ApacheRequest(container.home()), pull, 2);
135 final JsonObject json = Json.createObjectBuilder()
136 .add("body", "test comment").build();
137 comment.patch(json);
138 final MkQuery query = container.take();
139 MatcherAssert.assertThat(
140 query.method(), Matchers.equalTo(Request.PATCH)
141 );
142 MatcherAssert.assertThat(
143 query.body(),
144 Matchers.containsString("{\"body\":\"test comment\"}")
145 );
146 MatcherAssert.assertThat(
147 query.uri().toString(),
148 Matchers.endsWith("/repos/joe/blueharvest/pulls/comments/2")
149 );
150 container.stop();
151 }
152 }
153
154
155
156
157
158 @Test
159 @Ignore
160 public void reacts() throws Exception {
161 try (
162 final MkContainer container = new MkGrizzlyContainer().next(
163 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
164 ).start(this.resource.port())) {
165 final Repo repo = new MkGithub().randomRepo();
166 final Pull pull = repo.pulls().create(
167 "Reaction adding test",
168 "This is a test for adding a reaction",
169 "Base"
170 );
171 final RtPullComment comment = new RtPullComment(
172 new ApacheRequest(container.home()), pull, 2
173 );
174 comment.react(new Reaction.Simple(Reaction.HEART));
175 MatcherAssert.assertThat(
176 "Pull comment was unable to react",
177 comment.reactions(),
178 new IsCollectionWithSize<>(
179 new IsEqual<>(1)
180 )
181 );
182 }
183 }
184
185
186
187
188
189
190 private static Repo repo() throws Exception {
191 return new MkGithub("joe").repos().create(
192 new Repos.RepoCreate("blueharvest", false)
193 );
194 }
195 }