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.Response;
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.ApacheRequest;
37 import com.jcabi.http.request.FakeRequest;
38 import com.jcabi.http.response.JsonResponse;
39 import java.util.EnumMap;
40 import javax.json.Json;
41 import javax.json.JsonArrayBuilder;
42 import javax.json.JsonObject;
43 import javax.json.JsonObjectBuilder;
44 import org.hamcrest.MatcherAssert;
45 import org.hamcrest.Matchers;
46 import org.junit.Rule;
47 import org.junit.Test;
48
49
50
51
52
53
54
55
56
57
58 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
59 public final class RtSearchTest {
60
61
62
63
64 @Rule
65 public final transient RandomPort resource = new RandomPort();
66
67
68
69
70
71 @Test
72 public void canSearchForRepos() {
73 final String coords = "test-user1/test-repo1";
74 final Search search = new RtGithub(
75 new FakeRequest().withBody(
76 RtSearchTest.search(
77 Json.createObjectBuilder().add("full_name", coords).build()
78 ).toString()
79 )
80 ).search();
81 MatcherAssert.assertThat(
82 search.repos("test", "stars", Search.Order.DESC).iterator().next()
83 .coordinates().toString(),
84 Matchers.equalTo(coords)
85 );
86 }
87
88
89
90
91
92 @Test
93 public void canSearchForIssues() {
94 final int number = 1;
95 final Search search = new RtGithub(
96 new FakeRequest().withBody(
97 RtSearchTest.search(
98 Json.createObjectBuilder().add(
99 "url", String.format(
100
101 "https://api.github.com/repos/user/repo/issues/%s",
102 number
103 )
104 ).add("number", number).build()
105 ).toString()
106 )
107 ).search();
108 MatcherAssert.assertThat(
109 search.issues(
110 "test2",
111 "created",
112 Search.Order.DESC,
113 new EnumMap<>(Search.Qualifier.class)
114 ).iterator().next().number(),
115 Matchers.equalTo(number)
116 );
117 }
118
119
120
121
122
123
124 @Test
125 public void canSearchForUsers() throws Exception {
126 final String login = "test-user";
127 final Search search = new RtGithub(
128 new FakeRequest().withBody(
129 RtSearchTest.search(
130 Json.createObjectBuilder()
131 .add("login", login).build()
132 ).toString()
133 )
134 ).search();
135 MatcherAssert.assertThat(
136 search.users("test3", "joined", Search.Order.DESC)
137 .iterator().next().login(),
138 Matchers.equalTo(login)
139 );
140 }
141
142
143
144
145
146
147 @Test
148 public void canSearchForContents() throws Exception {
149 final JsonObject first = RtSearchTest.content(
150 "test/unit/attributes.js",
151 "attributes.js",
152
153 "https://api.github.com/repos/user/repo/contents/test/unit/attributes.js?ref=f3b89ba0820882bd4ce4404b7e7c819e7b506de5"
154 ).build();
155 final JsonObject second = RtSearchTest.content(
156 "src/attributes/classes.js",
157 "classes.js",
158
159 "https://api.github.com/repos/user/repo/contents/src/attributes/classes.js?ref=f3b89ba0820882bd4ce4404b7e7c819e7b506de5"
160 ).build();
161 try (
162 final MkContainer container = new MkGrizzlyContainer().next(
163 new MkAnswer.Simple(
164 RtSearchTest.search(first, second).toString()
165 )
166 ).next(new MkAnswer.Simple(first.toString()))
167 .next(new MkAnswer.Simple(second.toString()))
168 .start(this.resource.port())
169 ) {
170 final Search search = new RtGithub(
171 new ApacheRequest(container.home())
172 ).search();
173 MatcherAssert.assertThat(
174 search.codes("test4", "joined", Search.Order.DESC),
175 Matchers.<Content>iterableWithSize(2)
176 );
177 container.stop();
178 }
179 }
180
181
182
183
184
185 @Test
186 public void readNonUnicode() throws Exception {
187 final Response resp = new FakeRequest()
188 .withBody("{\"help\": \"\u001Fblah\u0001cwhoa\u0000!\"}").fetch();
189 final JsonResponse response = new JsonResponse(resp);
190 MatcherAssert.assertThat(
191 response.json().readObject().getString("help"),
192 Matchers.is("\u001Fblah\u0001cwhoa\u0000!")
193 );
194 }
195
196
197
198
199
200
201
202
203 private static JsonObjectBuilder content(
204 final String path, final String name, final String url) {
205 return Json.createObjectBuilder()
206 .add("path", path)
207 .add("name", name)
208 .add("url", url);
209 }
210
211
212
213
214
215
216 private static JsonObject search(
217 final JsonObject ... contents) {
218 final JsonArrayBuilder builder = Json.createArrayBuilder();
219 for (final JsonObject content : contents) {
220 builder.add(content);
221 }
222 return Json.createObjectBuilder()
223 .add("total_count", contents.length)
224 .add("items", builder).build();
225 }
226 }