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 org.hamcrest.MatcherAssert;
43 import org.hamcrest.Matchers;
44 import org.hamcrest.core.IsEqual;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.mockito.Mockito;
48
49
50
51
52
53
54
55
56
57 public final class RtIssueTest {
58
59
60
61
62
63 @Rule
64 public final transient RandomPort resource = new RandomPort();
65
66
67
68
69
70 @Test
71 public void fetchesComments() {
72 final RtIssue issue = new RtIssue(new FakeRequest(), this.repo(), 1);
73 MatcherAssert.assertThat(
74 issue.comments(),
75 Matchers.notNullValue()
76 );
77 }
78
79
80
81
82
83 @Test
84 public void fetchesLabels() {
85 final RtIssue issue = new RtIssue(new FakeRequest(), this.repo(), 1);
86 MatcherAssert.assertThat(
87 issue.labels(),
88 Matchers.notNullValue()
89 );
90 }
91
92
93
94
95
96 @Test
97 public void fetchesEvents() {
98 final RtIssue issue = new RtIssue(new FakeRequest(), this.repo(), 1);
99 MatcherAssert.assertThat(
100 issue.events(),
101 Matchers.notNullValue()
102 );
103 }
104
105
106
107
108
109
110 @Test
111 public void fetchIssueAsJson() throws Exception {
112 final RtIssue issue = new RtIssue(
113 new FakeRequest().withBody("{\"issue\":\"json\"}"),
114 this.repo(),
115 1
116 );
117 MatcherAssert.assertThat(
118 issue.json().getString("issue"),
119 Matchers.equalTo("json")
120 );
121 }
122
123
124
125
126
127
128 @Test
129 public void patchWithJson() throws Exception {
130 try (
131 final MkContainer container = new MkGrizzlyContainer().next(
132 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "response")
133 ).start(this.resource.port())
134 ) {
135 final RtIssue issue = new RtIssue(
136 new ApacheRequest(container.home()),
137 this.repo(),
138 1
139 );
140 issue.patch(
141 Json.createObjectBuilder().add("patch", "test").build()
142 );
143 final MkQuery query = container.take();
144 MatcherAssert.assertThat(
145 query.method(),
146 Matchers.equalTo(Request.PATCH)
147 );
148 MatcherAssert.assertThat(
149 query.body(),
150 Matchers.equalTo("{\"patch\":\"test\"}")
151 );
152 container.stop();
153 }
154 }
155
156
157
158
159
160 @Test
161 public void canCompareInstances() {
162 final RtIssue less = new RtIssue(new FakeRequest(), this.repo(), 1);
163 final RtIssue greater = new RtIssue(new FakeRequest(), this.repo(), 2);
164 MatcherAssert.assertThat(
165 less.compareTo(greater), Matchers.lessThan(0)
166 );
167 MatcherAssert.assertThat(
168 greater.compareTo(less), Matchers.greaterThan(0)
169 );
170 }
171
172
173
174
175
176 @Test
177 public void reacts() throws Exception {
178 try (
179 final MkContainer container = new MkGrizzlyContainer().next(
180 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
181 ).start(this.resource.port())) {
182 final Repo repo = new MkGithub().randomRepo();
183 final Issue issue = new RtIssue(
184 new ApacheRequest(container.home()),
185 repo,
186 10
187 );
188 issue.react(new Reaction.Simple(Reaction.HEART));
189 final MkQuery query = container.take();
190 MatcherAssert.assertThat(
191 "Issue was unable to react",
192 query.method(),
193 new IsEqual<>(Request.POST)
194 );
195 }
196 }
197
198
199
200
201
202 private Repo repo() {
203 final Repo repo = Mockito.mock(Repo.class);
204 final Coordinates coords = Mockito.mock(Coordinates.class);
205 Mockito.doReturn(coords).when(repo).coordinates();
206 Mockito.doReturn("user").when(coords).user();
207 Mockito.doReturn("repo").when(coords).repo();
208 return repo;
209 }
210
211 }