View Javadoc
1   /**
2    * Copyright (c) 2013-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
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 java.io.IOException;
40  import java.net.HttpURLConnection;
41  import javax.json.Json;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.Rule;
45  import org.junit.Test;
46  
47  /**
48   * Test case for {@link RtGist}.
49   *
50   * @author Carlos Miranda (miranda.cma@gmail.com)
51   * @version $Id: a5d7559d1f038e4e1433b0911b5702ad876c0cc7 $
52   */
53  public final class RtGistTest {
54      /**
55       * The rule for skipping test if there's BindException.
56       * @checkstyle VisibilityModifierCheck (3 lines)
57       */
58      @Rule
59      public final transient RandomPort resource = new RandomPort();
60  
61      /**
62       * RtGist should be able to do reads.
63       *
64       * @throws Exception if there is a problem.
65       * @checkstyle MultipleStringLiteralsCheck (20 lines)
66       */
67      @Test
68      public void readsFileWithContents() throws Exception {
69          try (
70              final MkContainer container = new MkGrizzlyContainer().next(
71                  new MkAnswer.Simple(
72                      HttpURLConnection.HTTP_OK,
73                      "{\"files\":{\"hello\":{\"raw_url\":\"world\"}}}"
74                  )
75              ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "success!"))
76                  .start(this.resource.port())) {
77              final RtGist gist = new RtGist(
78                  new MkGithub(),
79                  new ApacheRequest(container.home()),
80                  "test"
81              );
82              MatcherAssert.assertThat(
83                  gist.read("hello"),
84                  Matchers.equalTo("success!")
85              );
86              container.stop();
87          }
88      }
89  
90      /**
91       * RtGist should be able to do writes.
92       * @throws Exception if there is a problem.
93       */
94      @Test
95      public void writesFileContents() throws Exception {
96          try (
97              final MkContainer container = new MkGrizzlyContainer().next(
98                  new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "testFileWrite")
99              ).start(this.resource.port())
100         ) {
101             final RtGist gist = new RtGist(
102                 new MkGithub(),
103                 new ApacheRequest(container.home()),
104                 "testWrite"
105             );
106             gist.write("testFile", "testContent");
107             MatcherAssert.assertThat(
108                 container.take().body(),
109                 Matchers.containsString(
110                     "\"testFile\":{\"content\":\"testContent\"}"
111                 )
112             );
113             container.stop();
114         }
115     }
116 
117     /**
118      * RtGist can fork itself.
119      *
120      * @throws IOException If there is a problem.
121      */
122     @Test
123     public void fork() throws IOException {
124         final String fileContent = "success";
125         try (final MkContainer container = new MkGrizzlyContainer()) {
126             container.next(
127                 new MkAnswer.Simple(
128                     HttpURLConnection.HTTP_OK,
129                     "{\"files\":{\"hello\":{\"raw_url\":\"world\"}}}"
130                 )
131             );
132             container.next(
133                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, fileContent)
134             );
135             container.next(
136                 new MkAnswer.Simple(
137                     HttpURLConnection.HTTP_CREATED,
138                     "{\"id\": \"forked\"}"
139                 )
140             );
141             container.next(
142                 new MkAnswer.Simple(
143                     HttpURLConnection.HTTP_OK,
144                     "{\"files\":{\"hello\":{\"raw_url\":\"world\"}}}"
145                 )
146             );
147             container.next(
148                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, fileContent)
149             );
150             container.start(this.resource.port());
151             final Gist gist = new RtGist(
152                 new MkGithub(),
153                 new ApacheRequest(container.home()),
154                 "test"
155             );
156             final String content = gist.read("hello");
157             final Gist forkedGist = gist.fork();
158             MatcherAssert.assertThat(
159                 forkedGist.read("hello"),
160                 Matchers.equalTo(content)
161             );
162             container.stop();
163         }
164     }
165 
166     /**
167      * Gist.Smart can iterate through its files.
168      *
169      * @throws Exception if something goes wrong.
170      */
171     @Test
172     public void canIterateFiles() throws Exception {
173         try (
174             final MkContainer container = new MkGrizzlyContainer().next(
175                 new MkAnswer.Simple(
176                     HttpURLConnection.HTTP_OK,
177                     "{\"files\":{\"something\":{\"filename\":\"not null\"}}}"
178                 )
179             ).start(this.resource.port())
180         ) {
181             final Gist.Smart smart = new Gist.Smart(
182                 new RtGist(
183                     new MkGithub(),
184                     new ApacheRequest(container.home()),
185                     "testGetFiles"
186                 )
187             );
188             MatcherAssert.assertThat(
189                 smart.files(),
190                 Matchers.notNullValue()
191             );
192             MatcherAssert.assertThat(
193                 container.take().uri().toString(),
194                 Matchers.endsWith("/gists/testGetFiles")
195             );
196             container.stop();
197         }
198     }
199 
200     /**
201      * RtGist can return a String representation correctly reflecting its URI.
202      *
203      * @throws Exception If something goes wrong.
204      */
205     @Test
206     public void canRepresentAsString() throws Exception {
207         try (
208             final MkContainer container = new MkGrizzlyContainer()
209             .start(this.resource.port())
210         ) {
211             final RtGist gist = new RtGist(
212                 new MkGithub(),
213                 new ApacheRequest(container.home()),
214                 "testToString"
215             );
216             MatcherAssert.assertThat(
217                 gist.toString(),
218                 Matchers.endsWith("/gists/testToString")
219             );
220             container.stop();
221         }
222     }
223 
224     /**
225      * RtGist can unstar a starred Gist.
226      * @throws Exception If something goes wrong.
227      */
228     @Test
229     public void canUnstarAGist() throws Exception {
230         try (
231             final MkContainer container = new MkGrizzlyContainer().next(
232                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
233             ).start(this.resource.port())
234         ) {
235             final RtGist gist = new RtGist(
236                 new MkGithub(),
237                 new ApacheRequest(container.home()),
238                 "unstar"
239             );
240             gist.unstar();
241             final MkQuery query = container.take();
242             MatcherAssert.assertThat(
243                 query.method(),
244                 Matchers.equalTo(Request.DELETE)
245             );
246             MatcherAssert.assertThat(
247                 query.body(),
248                 Matchers.is(Matchers.emptyOrNullString())
249             );
250             container.stop();
251         }
252     }
253 
254     /**
255      * RtGist can execute PATCH request.
256      *
257      * @throws Exception if there is any problem
258      */
259     @Test
260     public void executePatchRequest() throws Exception {
261         try (
262             final MkContainer container = new MkGrizzlyContainer().next(
263                 new MkAnswer.Simple(
264                     HttpURLConnection.HTTP_OK, "{\"msg\":\"hi\"}"
265                 )
266             ).start(this.resource.port())
267         ) {
268             final RtGist gist = new RtGist(
269                 new MkGithub(),
270                 new ApacheRequest(container.home()),
271                 "patch"
272             );
273             gist.patch(
274                 Json.createObjectBuilder()
275                     .add("content", "hi you!")
276                     .build()
277             );
278             MatcherAssert.assertThat(
279                 container.take().method(),
280                 Matchers.equalTo(Request.PATCH)
281             );
282             container.stop();
283         }
284     }
285 }