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 java.net.URL;
33  import java.nio.charset.StandardCharsets;
34  import javax.json.Json;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.Test;
38  import org.mockito.Mockito;
39  
40  /**
41   * Test case for {@link Content}.
42   * @author Paul Polischuk (ppol@ua.fm)
43   * @version $Id: d8b9b4b8969c328bd07c6dd84c76264897440fe1 $
44   * @checkstyle MultipleStringLiterals (500 lines)
45   */
46  @SuppressWarnings("PMD.TooManyMethods")
47  public class ContentTest {
48      /**
49       * Content.Smart can fetch type property from Content.
50       * @throws Exception If some problem inside
51       */
52      @Test
53      public final void fetchesType() throws Exception {
54          final Content content = Mockito.mock(Content.class);
55          final String prop = "this is some type";
56          Mockito.doReturn(
57              Json.createObjectBuilder()
58                  .add("type", prop)
59                  .build()
60          ).when(content).json();
61          MatcherAssert.assertThat(
62              new Content.Smart(content).type(),
63              Matchers.is(prop)
64          );
65      }
66  
67      /**
68       * Content.Smart can fetch size property from Content.
69       * @throws Exception If some problem inside
70       */
71      @Test
72      public final void fetchesSize() throws Exception {
73          final Content content = Mockito.mock(Content.class);
74          final int prop = 5555;
75          Mockito.doReturn(
76              Json.createObjectBuilder()
77                  // @checkstyle MagicNumber (1 line)
78                  .add("size", prop)
79                  .build()
80          ).when(content).json();
81          MatcherAssert.assertThat(
82              new Content.Smart(content).size(),
83              // @checkstyle MagicNumber (1 line)
84              Matchers.is(prop)
85          );
86      }
87  
88      /**
89       * Content.Smart can fetch name property from Content.
90       * @throws Exception If some problem inside
91       */
92      @Test
93      public final void fetchesName() throws Exception {
94          final Content content = Mockito.mock(Content.class);
95          final String prop = "this is some name";
96          Mockito.doReturn(
97              Json.createObjectBuilder()
98                  .add("name", prop)
99                  .build()
100         ).when(content).json();
101         MatcherAssert.assertThat(
102             new Content.Smart(content).name(),
103             Matchers.is(prop)
104         );
105     }
106 
107     /**
108      * Content.Smart can fetch path property from Content.
109      */
110     @Test
111     public final void fetchesPath() {
112         final Content content = Mockito.mock(Content.class);
113         final String path = "this is some path";
114         Mockito.doReturn(path).when(content).path();
115         MatcherAssert.assertThat(
116             new Content.Smart(content).path(),
117             Matchers.is(path)
118         );
119     }
120 
121     /**
122      * Content.Smart can fetch sha property from Content.
123      * @throws Exception If some problem inside
124      */
125     @Test
126     public final void fetchesSha() throws Exception {
127         final Content content = Mockito.mock(Content.class);
128         final String prop = "this is some sha";
129         Mockito.doReturn(
130             Json.createObjectBuilder()
131                 .add("sha", prop)
132                 .build()
133         ).when(content).json();
134         MatcherAssert.assertThat(
135             new Content.Smart(content).sha(),
136             Matchers.is(prop)
137         );
138     }
139 
140     /**
141      * Content.Smart can fetch url property from Content.
142      * @throws Exception If some problem inside
143      */
144     @Test
145     public final void fetchesUrl() throws Exception {
146         final Content content = Mockito.mock(Content.class);
147         // @checkstyle LineLength (1 line)
148         final String prop = "https://api.github.com/repos/pengwynn/octokit/contents/README.md";
149         Mockito.doReturn(
150             Json.createObjectBuilder()
151                 .add("url", prop)
152                 .build()
153         ).when(content).json();
154         MatcherAssert.assertThat(
155             new Content.Smart(content).url(),
156             Matchers.is(new URL(prop))
157         );
158     }
159 
160     /**
161      * Content.Smart can fetch git_url property from Content.
162      * @throws Exception If some problem inside
163      */
164     @Test
165     public final void fetchesGitUrl() throws Exception {
166         final Content content = Mockito.mock(Content.class);
167         // @checkstyle LineLength (1 line)
168         final String prop = "https://api.github.com/repos/pengwynn/octokit/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1";
169         Mockito.doReturn(
170             Json.createObjectBuilder()
171                 .add("git_url", prop)
172                 .build()
173         ).when(content).json();
174         MatcherAssert.assertThat(
175             new Content.Smart(content).gitUrl(),
176             Matchers.is(new URL(prop))
177         );
178     }
179 
180     /**
181      * Content.Smart can fetch html_url property from Content.
182      * @throws Exception If some problem inside
183      */
184     @Test
185     public final void fetchesHtmlUrl() throws Exception {
186         final Content content = Mockito.mock(Content.class);
187         // @checkstyle LineLength (1 line)
188         final String prop = "https://github.com/pengwynn/octokit/blob/master/README.md";
189         Mockito.doReturn(
190             Json.createObjectBuilder()
191                 .add("html_url", prop)
192                 .build()
193         ).when(content).json();
194         MatcherAssert.assertThat(
195             new Content.Smart(content).htmlUrl(),
196             Matchers.is(new URL(prop))
197         );
198     }
199 
200     /**
201      * Content.Smart can fetch encoded content.
202      * @throws Exception If some problem inside
203      */
204     @Test
205     public final void fetchesContent() throws Exception {
206         final Content content = Mockito.mock(Content.class);
207         final String prop = "dGVzdCBlbmNvZGU=";
208         Mockito.doReturn(
209             Json.createObjectBuilder()
210                 .add("content", prop)
211                 .build()
212         ).when(content).json();
213         MatcherAssert.assertThat(
214             new Content.Smart(content).content(),
215             Matchers.is(prop)
216         );
217     }
218 
219     /**
220      * Content.Smart can fetch decoded content.
221      * @throws Exception If some problem inside
222      */
223     @Test
224     public final void fetchesDecoded() throws Exception {
225         final Content content = Mockito.mock(Content.class);
226         final String prop = "dGVzdCBlbmNvZGXigqw=";
227         Mockito.doReturn(
228             Json.createObjectBuilder()
229                 .add("content", prop)
230                 .build()
231         ).when(content).json();
232         MatcherAssert.assertThat(
233             new String(
234                 new Content.Smart(content).decoded(), StandardCharsets.UTF_8
235             ),
236             Matchers.is("test encode\u20ac")
237         );
238     }
239 
240     /**
241      * Content.Smart can get underlying repo.
242      */
243     @Test
244     public final void smartCanGetUnderlyingRepo() {
245         final Content content = Mockito.mock(Content.class);
246         final Repo repo = Mockito.mock(Repo.class);
247         Mockito.doReturn(repo).when(content).repo();
248         MatcherAssert.assertThat(
249             new Content.Smart(content).repo(),
250             Matchers.is(repo)
251         );
252     }
253 }