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 jakarta.json.Json;
15 import java.io.IOException;
16 import java.net.HttpURLConnection;
17 import org.hamcrest.MatcherAssert;
18 import org.hamcrest.Matchers;
19 import org.junit.jupiter.api.Test;
20 import org.junit.jupiter.api.extension.ExtendWith;
21
22
23
24
25
26 @ExtendWith(RandomPort.class)
27 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
28 final class RtGistTest {
29
30
31
32
33
34 @Test
35 void readsFileWithContents() throws IOException {
36 try (
37 MkContainer container = new MkGrizzlyContainer().next(
38 new MkAnswer.Simple(
39 HttpURLConnection.HTTP_OK,
40 "{\"files\":{\"hello\":{\"raw_url\":\"world\"}}}"
41 )
42 ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "success!"))
43 .start(RandomPort.port())) {
44 final RtGist gist = new RtGist(
45 new MkGitHub(),
46 new ApacheRequest(container.home()),
47 "test"
48 );
49 MatcherAssert.assertThat(
50 "Values are not equal",
51 gist.read("hello"),
52 Matchers.equalTo("success!")
53 );
54 container.stop();
55 }
56 }
57
58 @Test
59 void writesFileContents() throws IOException {
60 try (
61 MkContainer container = new MkGrizzlyContainer().next(
62 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "testFileWrite")
63 ).start(RandomPort.port())
64 ) {
65 final RtGist gist = new RtGist(
66 new MkGitHub(),
67 new ApacheRequest(container.home()),
68 "testWrite"
69 );
70 gist.write("testFile", "testContent");
71 MatcherAssert.assertThat(
72 "String does not contain expected value",
73 container.take().body(),
74 Matchers.containsString(
75 "\"testFile\":{\"content\":\"testContent\"}"
76 )
77 );
78 container.stop();
79 }
80 }
81
82
83
84
85
86 @Test
87 void fork() throws IOException {
88 try (MkContainer container = new MkGrizzlyContainer()) {
89 container.next(
90 new MkAnswer.Simple(
91 HttpURLConnection.HTTP_OK,
92 "{\"files\":{\"hello\":{\"raw_url\":\"world\"}}}"
93 )
94 );
95 final String success = "success";
96 container.next(
97 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, success)
98 );
99 container.next(
100 new MkAnswer.Simple(
101 HttpURLConnection.HTTP_CREATED,
102 "{\"id\": \"forked\"}"
103 )
104 );
105 container.next(
106 new MkAnswer.Simple(
107 HttpURLConnection.HTTP_OK,
108 "{\"files\":{\"hello\":{\"raw_url\":\"world\"}}}"
109 )
110 );
111 container.next(
112 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, success)
113 );
114 container.start(RandomPort.port());
115 final Gist gist = new RtGist(
116 new MkGitHub(),
117 new ApacheRequest(container.home()),
118 "test"
119 );
120 final String content = gist.read("hello");
121 final Gist fork = gist.fork();
122 MatcherAssert.assertThat(
123 "Values are not equal",
124 fork.read("hello"),
125 Matchers.equalTo(content)
126 );
127 container.stop();
128 }
129 }
130
131 @Test
132 void canIterateFiles() throws IOException {
133 try (
134 MkContainer container = new MkGrizzlyContainer().next(
135 new MkAnswer.Simple(
136 HttpURLConnection.HTTP_OK,
137 "{\"files\":{\"something\":{\"filename\":\"not null\"}}}"
138 )
139 ).start(RandomPort.port())
140 ) {
141 final Gist.Smart smart = new Gist.Smart(
142 new RtGist(
143 new MkGitHub(),
144 new ApacheRequest(container.home()),
145 "testGetFiles"
146 )
147 );
148 MatcherAssert.assertThat(
149 "Value is null",
150 smart.files(),
151 Matchers.notNullValue()
152 );
153 MatcherAssert.assertThat(
154 "String does not end with expected value",
155 container.take().uri().toString(),
156 Matchers.endsWith("/gists/testGetFiles")
157 );
158 container.stop();
159 }
160 }
161
162 @Test
163 void canRepresentAsString() throws IOException {
164 try (
165 MkContainer container = new MkGrizzlyContainer()
166 .start(RandomPort.port())
167 ) {
168 final RtGist gist = new RtGist(
169 new MkGitHub(),
170 new ApacheRequest(container.home()),
171 "testToString"
172 );
173 MatcherAssert.assertThat(
174 "String does not end with expected value",
175 gist.toString(),
176 Matchers.endsWith("/gists/testToString")
177 );
178 container.stop();
179 }
180 }
181
182 @Test
183 void canUnstarAGist() throws IOException {
184 try (
185 MkContainer container = new MkGrizzlyContainer().next(
186 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
187 ).start(RandomPort.port())
188 ) {
189 final RtGist gist = new RtGist(
190 new MkGitHub(),
191 new ApacheRequest(container.home()),
192 "unstar"
193 );
194 gist.unstar();
195 final MkQuery query = container.take();
196 MatcherAssert.assertThat(
197 "Values are not equal",
198 query.method(),
199 Matchers.equalTo(Request.DELETE)
200 );
201 MatcherAssert.assertThat(
202 "Values are not equal",
203 query.body(),
204 Matchers.is(Matchers.emptyOrNullString())
205 );
206 container.stop();
207 }
208 }
209
210 @Test
211 void executePatchRequest() throws IOException {
212 try (
213 MkContainer container = new MkGrizzlyContainer().next(
214 new MkAnswer.Simple(
215 HttpURLConnection.HTTP_OK, "{\"msg\":\"hi\"}"
216 )
217 ).start(RandomPort.port())
218 ) {
219 final RtGist gist = new RtGist(
220 new MkGitHub(),
221 new ApacheRequest(container.home()),
222 "patch"
223 );
224 gist.patch(
225 Json.createObjectBuilder()
226 .add("content", "hi you!")
227 .build()
228 );
229 MatcherAssert.assertThat(
230 "Values are not equal",
231 container.take().method(),
232 Matchers.equalTo(Request.PATCH)
233 );
234 container.stop();
235 }
236 }
237 }