View Javadoc
1   /**
2    * Copyright (c) 2013-2022, 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.net.HttpURLConnection;
40  import java.util.Collections;
41  import org.hamcrest.MatcherAssert;
42  import org.hamcrest.Matchers;
43  import org.junit.Rule;
44  import org.junit.Test;
45  
46  /**
47   * Test case for {@link RtGists}.
48   *
49   * @author Carlos Miranda (miranda.cma@gmail.com)
50   * @version $Id: cd2926178be8940d934dbfffceec2b0cb421d76a $
51   */
52  public final class RtGistsTest {
53  
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       * RtGists can create new files.
63       *
64       * @throws Exception if a problem occurs.
65       */
66      @Test
67      public void canCreateFiles() throws Exception {
68          try (
69          final MkContainer container = new MkGrizzlyContainer().next(
70              new MkAnswer.Simple(
71                  HttpURLConnection.HTTP_CREATED,
72                  "{\"id\":\"1\"}"
73              )
74          ).start(this.resource.port())) {
75              final Gists gists = new RtGists(
76                  new MkGithub(),
77                  new ApacheRequest(container.home())
78              );
79              MatcherAssert.assertThat(
80                  gists.create(Collections.singletonMap("test", ""), false),
81                  Matchers.notNullValue()
82              );
83              MatcherAssert.assertThat(
84                  container.take().body(),
85                  Matchers.startsWith("{\"files\":{\"test\":{\"content\":")
86              );
87              container.stop();
88          }
89      }
90  
91      /**
92       * RtGists can retrieve a specific Gist.
93       *
94       * @throws Exception if a problem occurs.
95       */
96      @Test
97      public void canRetrieveSpecificGist() throws Exception {
98          try (
99              final MkContainer container = new MkGrizzlyContainer().next(
100                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "testing")
101             ).start(this.resource.port())
102         ) {
103             final Gists gists = new RtGists(
104                 new MkGithub(),
105                 new ApacheRequest(container.home())
106             );
107             MatcherAssert.assertThat(
108                 gists.get("gist"),
109                 Matchers.notNullValue()
110             );
111             container.stop();
112         }
113     }
114 
115     /**
116      * RtGists can iterate through its contents.
117      *
118      * @throws Exception if a problem occurs.
119      */
120     @Test
121     public void canIterateThrouRtGists() throws Exception {
122         try (
123             final MkContainer container = new MkGrizzlyContainer().next(
124                 new MkAnswer.Simple(
125                     HttpURLConnection.HTTP_OK,
126                     "[{\"id\":\"hello\"}]"
127                 )
128             ).start(this.resource.port())
129         ) {
130             final Gists gists = new RtGists(
131                 new MkGithub(),
132                 new ApacheRequest(container.home())
133             );
134             MatcherAssert.assertThat(
135                 gists.iterate().iterator().next(),
136                 Matchers.notNullValue()
137             );
138             container.stop();
139         }
140     }
141     /**
142      * RtGists can remove a gist by name.
143      * @throws Exception - if something goes wrong.
144      */
145     @Test
146     public void removesGistByName() throws Exception {
147         try (
148             final MkContainer container = new MkGrizzlyContainer().next(
149                 new MkAnswer.Simple(
150                     HttpURLConnection.HTTP_NO_CONTENT,
151                     ""
152                 )
153             ).start(this.resource.port())) {
154             final Gists gists = new RtGists(
155                 new MkGithub(),
156                 new ApacheRequest(container.home())
157             );
158             gists.remove("12234");
159             final MkQuery query = container.take();
160             MatcherAssert.assertThat(
161                 query.method(),
162                 Matchers.equalTo(Request.DELETE)
163             );
164             container.stop();
165         }
166     }
167 }