View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
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 java.io.IOException;
15  import java.net.HttpURLConnection;
16  import java.util.Collections;
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   * Test case for {@link RtGists}.
24   * @since 0.1
25   */
26  @ExtendWith(RandomPort.class)
27  final class RtGistsTest {
28  
29      /**
30       * The rule for skipping test if there's BindException.
31       * @checkstyle VisibilityModifierCheck (3 lines)
32       */
33      @Test
34      void canCreateFiles() throws IOException {
35          try (
36              MkContainer container = new MkGrizzlyContainer().next(
37                  new MkAnswer.Simple(
38                      HttpURLConnection.HTTP_CREATED,
39                      "{\"id\":\"1\"}"
40                  )
41              ).start(RandomPort.port())
42          ) {
43              final Gists gists = new RtGists(
44                  new MkGitHub(),
45                  new ApacheRequest(container.home())
46              );
47              MatcherAssert.assertThat(
48                  "Value is null",
49                  gists.create(Collections.singletonMap("test", ""), false),
50                  Matchers.notNullValue()
51              );
52              MatcherAssert.assertThat(
53                  "String does not start with expected value",
54                  container.take().body(),
55                  Matchers.startsWith("{\"files\":{\"test\":{\"content\":")
56              );
57              container.stop();
58          }
59      }
60  
61      @Test
62      void canRetrieveSpecificGist() throws IOException {
63          try (
64              MkContainer container = new MkGrizzlyContainer().next(
65                  new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "testing")
66              ).start(RandomPort.port())
67          ) {
68              final Gists gists = new RtGists(
69                  new MkGitHub(),
70                  new ApacheRequest(container.home())
71              );
72              MatcherAssert.assertThat(
73                  "Value is null",
74                  gists.get("gist"),
75                  Matchers.notNullValue()
76              );
77              container.stop();
78          }
79      }
80  
81      @Test
82      void canIterateThroughRtGists() throws IOException {
83          try (
84              MkContainer container = new MkGrizzlyContainer().next(
85                  new MkAnswer.Simple(
86                      HttpURLConnection.HTTP_OK,
87                      "[{\"id\":\"hello\"}]"
88                  )
89              ).start(RandomPort.port())
90          ) {
91              final Gists gists = new RtGists(
92                  new MkGitHub(),
93                  new ApacheRequest(container.home())
94              );
95              MatcherAssert.assertThat(
96                  "Value is null",
97                  gists.iterate().iterator().next(),
98                  Matchers.notNullValue()
99              );
100             container.stop();
101         }
102     }
103 
104     @Test
105     void removesGistByName() throws IOException {
106         try (
107             MkContainer container = new MkGrizzlyContainer().next(
108                 new MkAnswer.Simple(
109                     HttpURLConnection.HTTP_NO_CONTENT,
110                     ""
111                 )
112             ).start(RandomPort.port())) {
113             final Gists gists = new RtGists(
114                 new MkGitHub(),
115                 new ApacheRequest(container.home())
116             );
117             gists.remove("12234");
118             final MkQuery query = container.take();
119             MatcherAssert.assertThat(
120                 "Values are not equal",
121                 query.method(),
122                 Matchers.equalTo(Request.DELETE)
123             );
124             container.stop();
125         }
126     }
127 }