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.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: 240acaf3918c06d9497bd09059c9e7e855fa9764 $
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      */
120     @Test
121     public void canCompareInstances() {
122         final RtContent less = new RtContent(
123             new FakeRequest(),
124             this.repo(),
125             "aaa"
126         );
127         final RtContent greater = new RtContent(
128             new FakeRequest(),
129             this.repo(),
130             "zzz"
131         );
132         MatcherAssert.assertThat(
133             less.compareTo(greater), Matchers.lessThan(0)
134         );
135         MatcherAssert.assertThat(
136             greater.compareTo(less), Matchers.greaterThan(0)
137         );
138         MatcherAssert.assertThat(
139             greater.compareTo(greater), Matchers.is(0)
140         );
141     }
142 
143     /**
144      * RtContent should be able to fetch raw content.
145      *
146      * @throws Exception if a problem occurs.
147      */
148     @Test
149     public void fetchesRawContent() throws Exception {
150         final String raw = "the raw \u20ac";
151         try (final MkContainer container = new MkGrizzlyContainer().next(
152             new MkAnswer.Simple(HttpURLConnection.HTTP_OK, raw)
153         ).start(this.resource.port())) {
154             final InputStream stream = new RtContent(
155                 new ApacheRequest(container.home()),
156                 this.repo(),
157                 "raw"
158             ).raw();
159             MatcherAssert.assertThat(
160                 IOUtils.toString(stream, StandardCharsets.UTF_8),
161                 Matchers.is(raw)
162             );
163             MatcherAssert.assertThat(
164                 container.take().headers().get(HttpHeaders.ACCEPT).get(0),
165                 Matchers.is("application/vnd.github.v3.raw")
166             );
167         }
168     }
169 
170     /**
171      * Mock repo for GhIssue creation.
172      * @return The mock repo.
173      */
174     private Repo repo() {
175         final Repo repo = Mockito.mock(Repo.class);
176         final Coordinates coords = Mockito.mock(Coordinates.class);
177         Mockito.doReturn(coords).when(repo).coordinates();
178         Mockito.doReturn("user").when(coords).user();
179         Mockito.doReturn("repo").when(coords).repo();
180         return repo;
181     }
182 }