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 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: 11a834d0915cc7c3f1fef1caf16d2e69e8551661 $
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      * @throws Exception If some problem inside
110      */
111     @Test
112     public final void fetchesPath() throws Exception {
113         final Content content = Mockito.mock(Content.class);
114         final String path = "this is some path";
115         Mockito.doReturn(path).when(content).path();
116         MatcherAssert.assertThat(
117             new Content.Smart(content).path(),
118             Matchers.is(path)
119         );
120     }
121 
122     /**
123      * Content.Smart can fetch sha property from Content.
124      * @throws Exception If some problem inside
125      */
126     @Test
127     public final void fetchesSha() throws Exception {
128         final Content content = Mockito.mock(Content.class);
129         final String prop = "this is some sha";
130         Mockito.doReturn(
131             Json.createObjectBuilder()
132                 .add("sha", prop)
133                 .build()
134         ).when(content).json();
135         MatcherAssert.assertThat(
136             new Content.Smart(content).sha(),
137             Matchers.is(prop)
138         );
139     }
140 
141     /**
142      * Content.Smart can fetch url property from Content.
143      * @throws Exception If some problem inside
144      */
145     @Test
146     public final void fetchesUrl() throws Exception {
147         final Content content = Mockito.mock(Content.class);
148         // @checkstyle LineLength (1 line)
149         final String prop = "https://api.github.com/repos/pengwynn/octokit/contents/README.md";
150         Mockito.doReturn(
151             Json.createObjectBuilder()
152                 .add("url", prop)
153                 .build()
154         ).when(content).json();
155         MatcherAssert.assertThat(
156             new Content.Smart(content).url(),
157             Matchers.is(new URL(prop))
158         );
159     }
160 
161     /**
162      * Content.Smart can fetch git_url property from Content.
163      * @throws Exception If some problem inside
164      */
165     @Test
166     public final void fetchesGitUrl() throws Exception {
167         final Content content = Mockito.mock(Content.class);
168         // @checkstyle LineLength (1 line)
169         final String prop = "https://api.github.com/repos/pengwynn/octokit/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1";
170         Mockito.doReturn(
171             Json.createObjectBuilder()
172                 .add("git_url", prop)
173                 .build()
174         ).when(content).json();
175         MatcherAssert.assertThat(
176             new Content.Smart(content).gitUrl(),
177             Matchers.is(new URL(prop))
178         );
179     }
180 
181     /**
182      * Content.Smart can fetch html_url property from Content.
183      * @throws Exception If some problem inside
184      */
185     @Test
186     public final void fetchesHtmlUrl() throws Exception {
187         final Content content = Mockito.mock(Content.class);
188         // @checkstyle LineLength (1 line)
189         final String prop = "https://github.com/pengwynn/octokit/blob/master/README.md";
190         Mockito.doReturn(
191             Json.createObjectBuilder()
192                 .add("html_url", prop)
193                 .build()
194         ).when(content).json();
195         MatcherAssert.assertThat(
196             new Content.Smart(content).htmlUrl(),
197             Matchers.is(new URL(prop))
198         );
199     }
200 
201     /**
202      * Content.Smart can fetch encoded content.
203      * @throws Exception If some problem inside
204      */
205     @Test
206     public final void fetchesContent() throws Exception {
207         final Content content = Mockito.mock(Content.class);
208         final String prop = "dGVzdCBlbmNvZGU=";
209         Mockito.doReturn(
210             Json.createObjectBuilder()
211                 .add("content", prop)
212                 .build()
213         ).when(content).json();
214         MatcherAssert.assertThat(
215             new Content.Smart(content).content(),
216             Matchers.is(prop)
217         );
218     }
219 
220     /**
221      * Content.Smart can fetch decoded content.
222      * @throws Exception If some problem inside
223      */
224     @Test
225     public final void fetchesDecoded() throws Exception {
226         final Content content = Mockito.mock(Content.class);
227         final String prop = "dGVzdCBlbmNvZGXigqw=";
228         Mockito.doReturn(
229             Json.createObjectBuilder()
230                 .add("content", prop)
231                 .build()
232         ).when(content).json();
233         MatcherAssert.assertThat(
234             new String(
235                 new Content.Smart(content).decoded(), StandardCharsets.UTF_8
236             ),
237             Matchers.is("test encode\u20ac")
238         );
239     }
240 
241     /**
242      * Content.Smart can get underlying repo.
243      * @throws Exception If some problem inside
244      */
245     @Test
246     public final void smartCanGetUnderlyingRepo() throws Exception {
247         final Content content = Mockito.mock(Content.class);
248         final Repo repo = Mockito.mock(Repo.class);
249         Mockito.doReturn(repo).when(content).repo();
250         MatcherAssert.assertThat(
251             new Content.Smart(content).repo(),
252             Matchers.is(repo)
253         );
254     }
255 }