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.http.Request;
33 import com.jcabi.http.mock.MkAnswer;
34 import com.jcabi.http.mock.MkContainer;
35 import com.jcabi.http.mock.MkGrizzlyContainer;
36 import com.jcabi.http.request.JdkRequest;
37 import com.jcabi.immutable.ArrayMap;
38 import java.net.HttpURLConnection;
39 import java.util.EnumMap;
40 import javax.json.Json;
41 import javax.json.JsonObject;
42 import org.hamcrest.MatcherAssert;
43 import org.hamcrest.Matchers;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.mockito.Mockito;
47
48
49
50
51
52
53
54
55 public final class RtIssuesTest {
56
57
58
59
60
61 @Rule
62 public final transient RandomPort resource = new RandomPort();
63
64
65
66
67
68
69 @Test
70 public void createIssue() throws Exception {
71 final String title = "Found a bug";
72 final String body = issue(title).toString();
73 try (
74 final MkContainer container = new MkGrizzlyContainer().next(
75 new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
76 ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
77 .start(this.resource.port())
78 ) {
79 final RtIssues issues = new RtIssues(
80 new JdkRequest(container.home()),
81 repo()
82 );
83 final Issue issue = issues.create(
84 title, "having a problem with it."
85 );
86 MatcherAssert.assertThat(
87 container.take().method(),
88 Matchers.equalTo(Request.POST)
89 );
90 MatcherAssert.assertThat(
91 new Issue.Smart(issue).title(),
92 Matchers.equalTo(title)
93 );
94 container.stop();
95 }
96 }
97
98
99
100
101
102 @Test
103 public void getSingleIssue() throws Exception {
104 final String title = "Unit test";
105 try (
106 final MkContainer container = new MkGrizzlyContainer().next(
107 new MkAnswer.Simple(
108 HttpURLConnection.HTTP_OK,
109 issue(title).toString()
110 )
111 ).start(this.resource.port())
112 ) {
113 final RtIssues issues = new RtIssues(
114 new JdkRequest(container.home()),
115 repo()
116 );
117 final Issue issue = issues.get(1);
118 MatcherAssert.assertThat(
119 new Issue.Smart(issue).title(),
120 Matchers.equalTo(title)
121 );
122 container.stop();
123 }
124 }
125
126
127
128
129
130 @Test
131 public void iterateIssues() throws Exception {
132 try (
133 final MkContainer container = new MkGrizzlyContainer().next(
134 new MkAnswer.Simple(
135 HttpURLConnection.HTTP_OK,
136 Json.createArrayBuilder()
137 .add(issue("new issue"))
138 .add(issue("code issue"))
139 .build().toString()
140 )
141 ).start(this.resource.port())
142 ) {
143 final RtIssues issues = new RtIssues(
144 new JdkRequest(container.home()),
145 repo()
146 );
147 MatcherAssert.assertThat(
148 issues.iterate(new ArrayMap<>()),
149 Matchers.<Issue>iterableWithSize(2)
150 );
151 container.stop();
152 }
153 }
154
155
156
157
158
159 @Test
160 public void searchIssues() throws Exception {
161 try (
162 final MkContainer container = new MkGrizzlyContainer().next(
163 new MkAnswer.Simple(
164 HttpURLConnection.HTTP_OK,
165 Json.createArrayBuilder()
166 .add(issue("some issue"))
167 .add(issue("some other issue"))
168 .build().toString()
169 )
170 ).start(this.resource.port())
171 ) {
172 final RtIssues issues = new RtIssues(
173 new JdkRequest(container.home()),
174 repo()
175 );
176 MatcherAssert.assertThat(
177 issues.search(
178 Issues.Sort.UPDATED,
179 Search.Order.ASC,
180 new EnumMap<>(
181 Issues.Qualifier.class
182 )
183 ),
184 Matchers.<Issue>iterableWithSize(2)
185 );
186 container.stop();
187 }
188 }
189
190
191
192
193
194
195 private static JsonObject issue(final String title) {
196 return Json.createObjectBuilder()
197 .add("number", 1)
198 .add("state", Issue.OPEN_STATE)
199 .add("title", title)
200 .build();
201 }
202
203
204
205
206
207 private static Repo repo() {
208 final Repo repo = Mockito.mock(Repo.class);
209 Mockito.doReturn(new Coordinates.Simple("mark", "test"))
210 .when(repo).coordinates();
211 return repo;
212 }
213 }