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.aspects.Immutable;
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 com.jcabi.http.request.FakeRequest;
40  import java.io.InputStream;
41  import java.net.HttpURLConnection;
42  import java.nio.charset.StandardCharsets;
43  import javax.json.Json;
44  import javax.ws.rs.core.HttpHeaders;
45  import org.apache.commons.io.IOUtils;
46  import org.hamcrest.MatcherAssert;
47  import org.hamcrest.Matchers;
48  import org.junit.Rule;
49  import org.junit.Test;
50  import org.mockito.Mockito;
51  
52  /**
53   * Test case for {@link RtContent}.
54   * @author Andres Candal (andres.candal@rollasolution.com)
55   * @version $Id: 5a49a7de8e8dd9b8299c17c515c57ee4957e020a $
56   * @since 0.8
57   */
58  @Immutable
59  public final class RtContentTest {
60  
61      /**
62       * The rule for skipping test if there's BindException.
63       * @checkstyle VisibilityModifierCheck (3 lines)
64       */
65      @Rule
66      public final transient RandomPort resource = new RandomPort();
67  
68      /**
69       * RtContent should be able to describe itself in JSON format.
70       *
71       * @throws Exception if a problem occurs.
72       */
73      @Test
74      public void fetchContentAsJson() throws Exception {
75          final RtContent content = new RtContent(
76              new FakeRequest().withBody("{\"content\":\"json\"}"),
77              this.repo(),
78              "blah"
79          );
80          MatcherAssert.assertThat(
81              content.json().getString("content"),
82              Matchers.equalTo("json")
83          );
84      }
85  
86      /**
87       * RtContent should be able to perform a patch request.
88       *
89       * @throws Exception if a problem occurs.
90       */
91      @Test
92      public void patchWithJson() throws Exception {
93          try (final MkContainer container = new MkGrizzlyContainer().next(
94              new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "response")
95          ).start(this.resource.port())) {
96              final RtContent content = new RtContent(
97                  new ApacheRequest(container.home()),
98                  this.repo(),
99                  "path"
100             );
101             content.patch(
102                 Json.createObjectBuilder().add("patch", "test").build()
103             );
104             final MkQuery query = container.take();
105             MatcherAssert.assertThat(
106                 query.method(),
107                 Matchers.equalTo(Request.PATCH)
108             );
109             MatcherAssert.assertThat(
110                 query.body(),
111                 Matchers.equalTo("{\"patch\":\"test\"}")
112             );
113         }
114     }
115 
116     /**
117      * RtContent should be able to compare different instances.
118      *
119      * @throws Exception when a problem occurs.
120      */
121     @Test
122     public void canCompareInstances() throws Exception {
123         final RtContent less = new RtContent(
124             new FakeRequest(),
125             this.repo(),
126             "aaa"
127         );
128         final RtContent greater = new RtContent(
129             new FakeRequest(),
130             this.repo(),
131             "zzz"
132         );
133         MatcherAssert.assertThat(
134             less.compareTo(greater), Matchers.lessThan(0)
135         );
136         MatcherAssert.assertThat(
137             greater.compareTo(less), Matchers.greaterThan(0)
138         );
139         MatcherAssert.assertThat(
140             greater.compareTo(greater), Matchers.is(0)
141         );
142     }
143 
144     /**
145      * RtContent should be able to fetch raw content.
146      *
147      * @throws Exception if a problem occurs.
148      */
149     @Test
150     public void fetchesRawContent() throws Exception {
151         final String raw = "the raw \u20ac";
152         try (final MkContainer container = new MkGrizzlyContainer().next(
153             new MkAnswer.Simple(HttpURLConnection.HTTP_OK, raw)
154         ).start(this.resource.port())) {
155             final InputStream stream = new RtContent(
156                 new ApacheRequest(container.home()),
157                 this.repo(),
158                 "raw"
159             ).raw();
160             MatcherAssert.assertThat(
161                 IOUtils.toString(stream, StandardCharsets.UTF_8),
162                 Matchers.is(raw)
163             );
164             MatcherAssert.assertThat(
165                 container.take().headers().get(HttpHeaders.ACCEPT).get(0),
166                 Matchers.is("application/vnd.github.v3.raw")
167             );
168         }
169     }
170 
171     /**
172      * Mock repo for GhIssue creation.
173      * @return The mock repo.
174      */
175     private Repo repo() {
176         final Repo repo = Mockito.mock(Repo.class);
177         final Coordinates coords = Mockito.mock(Coordinates.class);
178         Mockito.doReturn(coords).when(repo).coordinates();
179         Mockito.doReturn("user").when(coords).user();
180         Mockito.doReturn("repo").when(coords).repo();
181         return repo;
182     }
183 }