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 javax.json.Json;
33  import javax.json.JsonValue;
34  import org.hamcrest.MatcherAssert;
35  import org.hamcrest.Matchers;
36  import org.junit.Test;
37  import org.mockito.Mockito;
38  
39  /**
40   * Test case for {@link Release}.
41   * @author Denis Anisimov (denis.nix.anisimov@gmail.com)
42   * @checkstyle MultipleStringLiteralsCheck (400 lines)
43   * @version $Id: bf41f5d372a0e31f3301bd6bdd138d2275aefda7 $
44   */
45  @SuppressWarnings("PMD.TooManyMethods")
46  public final class ReleaseTest {
47  
48      /**
49       * Release.Smart can fetch url properties of an Release.
50       * @throws Exception If some problem inside
51       */
52      @Test
53      public void fetchesUrls() throws Exception {
54          final Release release = Mockito.mock(Release.class);
55          final String url = "http://url";
56          Mockito.doReturn(
57              Json.createObjectBuilder()
58                  .add("url", url)
59                  .build()
60          ).when(release).json();
61          final Release.Smart smart = new Release.Smart(release);
62          MatcherAssert.assertThat(
63              smart.url().toString(),
64              Matchers.equalTo(url)
65          );
66      }
67  
68      /**
69       * Release.Smart can fetch html url properties of an Release.
70       * @throws Exception If some problem inside
71       */
72      @Test
73      public void fetchesHtmlUrls() throws Exception {
74          final Release release = Mockito.mock(Release.class);
75          final String htmlurl = "http://html_url";
76          Mockito.doReturn(
77              Json.createObjectBuilder()
78                  .add("html_url", htmlurl)
79                  .build()
80          ).when(release).json();
81          final Release.Smart smart = new Release.Smart(release);
82          MatcherAssert.assertThat(
83              smart.htmlUrl().toString(),
84              Matchers.equalTo(htmlurl)
85          );
86      }
87  
88      /**
89       * Release.Smart can fetch assets url properties of an Release.
90       * @throws Exception If some problem inside
91       */
92      @Test
93      public void fetchesAssetsHtmlUrls() throws Exception {
94          final Release release = Mockito.mock(Release.class);
95          final String assetsurl = "http://assets_url";
96          Mockito.doReturn(
97              Json.createObjectBuilder()
98                  .add("assets_url", assetsurl)
99                  .build()
100         ).when(release).json();
101         final Release.Smart smart = new Release.Smart(release);
102         MatcherAssert.assertThat(
103             smart.assetsUrl().toString(),
104             Matchers.equalTo(assetsurl)
105         );
106     }
107 
108     /**
109      * Release.Smart can fetch upload url properties of an Release.
110      * @throws Exception If some problem inside
111      */
112     @Test
113     public void fetchesUploadHtmlUrls() throws Exception {
114         final Release release = Mockito.mock(Release.class);
115         final String uploadurl = "http://upload_url";
116         Mockito.doReturn(
117             Json.createObjectBuilder()
118                 .add("upload_url", uploadurl)
119                 .build()
120         ).when(release).json();
121         final Release.Smart smart = new Release.Smart(release);
122         MatcherAssert.assertThat(
123             smart.uploadUrl().toString(),
124             Matchers.equalTo(uploadurl)
125         );
126     }
127 
128     /**
129      * Release.Smart returns correct number of Release.
130      * @throws Exception If some problem inside
131      */
132     @Test
133     public void testId() throws Exception {
134         final Release release = Mockito.mock(Release.class);
135         Mockito.doReturn(1).when(release).number();
136         final Release.Smart smart = new Release.Smart(release);
137         MatcherAssert.assertThat(
138             smart.number(),
139             Matchers.equalTo(1)
140         );
141     }
142 
143     /**
144      * Release.Smart can fetch tag of an Release.
145      * @throws Exception If some problem inside
146      */
147     @Test
148     public void fetchTag() throws Exception {
149         final Release release = Mockito.mock(Release.class);
150         final String tag = "v1.0.0";
151         Mockito.doReturn(
152             Json.createObjectBuilder()
153                 .add("tag_name", tag)
154                 .build()
155         ).when(release).json();
156         final Release.Smart smart = new Release.Smart(release);
157         MatcherAssert.assertThat(
158             smart.tag(),
159             Matchers.equalTo(tag)
160         );
161     }
162 
163     /**
164      * Release.Smart can fetch commitish of an Release.
165      * @throws Exception If some problem inside
166      */
167     @Test
168     public void fetchProperties() throws Exception {
169         final Release release = Mockito.mock(Release.class);
170         final String master = "master";
171         Mockito.doReturn(
172             Json.createObjectBuilder()
173                 .add("target_commitish", master)
174                 .build()
175         ).when(release).json();
176         final Release.Smart smart = new Release.Smart(release);
177         MatcherAssert.assertThat(
178             smart.commitish(),
179             Matchers.equalTo(master)
180         );
181     }
182 
183     /**
184      * Release.Smart can fetch name of an Release.
185      * @throws Exception If some problem inside
186      */
187     @Test
188     public void fetchName() throws Exception {
189         final Release release = Mockito.mock(Release.class);
190         final String name = "v1";
191         // @checkstyle MultipleStringLiterals (3 lines)
192         Mockito.doReturn(
193             Json.createObjectBuilder()
194                 .add("name", name)
195                 .build()
196         ).when(release).json();
197         final Release.Smart smart = new Release.Smart(release);
198         MatcherAssert.assertThat(
199             smart.hasName(),
200             Matchers.is(true)
201         );
202         MatcherAssert.assertThat(
203             smart.name(),
204             Matchers.equalTo(name)
205         );
206     }
207 
208     /**
209      * Release.Smart can determine if the release does not have a name
210      * (NULL json value).
211      * @throws Exception If some problem inside
212      */
213     @Test
214     public void incidatesNoName() throws Exception {
215         final Release release = Mockito.mock(Release.class);
216         Mockito.doReturn(
217             Json.createObjectBuilder()
218                 .add("name", JsonValue.NULL)
219                 .build()
220         ).when(release).json();
221         final Release.Smart smart = new Release.Smart(release);
222         MatcherAssert.assertThat(
223             smart.hasName(),
224             Matchers.is(false)
225         );
226     }
227 
228     /**
229      * Release.Smart can fetch body of an Release.
230      * @throws Exception If some problem inside
231      */
232     @Test
233     public void fetchBody() throws Exception {
234         final Release release = Mockito.mock(Release.class);
235         final String description = "Description of the release";
236         Mockito.doReturn(
237             Json.createObjectBuilder()
238                 .add("body", description)
239                 .build()
240         ).when(release).json();
241         final Release.Smart smart = new Release.Smart(release);
242         MatcherAssert.assertThat(
243             smart.body(),
244             Matchers.equalTo(description)
245         );
246     }
247 
248     /**
249      * Release.Smart can fetch created date of an Release.
250      * @throws Exception If some problem inside
251      */
252     @Test
253     public void fetchDescription() throws Exception {
254         final Release release = Mockito.mock(Release.class);
255         final String created = "2013-02-27T19:35:32Z";
256         Mockito.doReturn(
257             Json.createObjectBuilder()
258                 .add("created_at", created)
259                 .build()
260         ).when(release).json();
261         final Release.Smart smart = new Release.Smart(release);
262         MatcherAssert.assertThat(
263             smart.createdAt(),
264             Matchers.equalTo(new Github.Time(created).date())
265         );
266     }
267 
268     /**
269      * Release.Smart can fetch published date of an Release.
270      * @throws Exception If some problem inside
271      */
272     @Test
273     public void fetchPublished() throws Exception {
274         final Release release = Mockito.mock(Release.class);
275         final String published = "2013-01-27T19:35:32Z";
276         Mockito.doReturn(
277             Json.createObjectBuilder()
278                 .add("published_at", published)
279                 .build()
280         ).when(release).json();
281         final Release.Smart smart = new Release.Smart(release);
282         MatcherAssert.assertThat(
283             smart.publishedAt(),
284             Matchers.equalTo(new Github.Time(published).date())
285         );
286     }
287 
288     /**
289      * Release.Smart can tell when the release is a prerelease.
290      * @throws Exception If problem inside
291      */
292     @Test
293     public void isPrerelease() throws Exception {
294         final Release release = Mockito.mock(Release.class);
295         Mockito.doReturn(
296             Json.createObjectBuilder().add("prerelease", Boolean.TRUE).build()
297         ).when(release).json();
298         MatcherAssert.assertThat(
299             new Release.Smart(release).prerelease(),
300             Matchers.is(Boolean.TRUE)
301         );
302     }
303 
304     /**
305      * Release.Smart can tell when the release is not a prerelease.
306      * @throws Exception If problem inside
307      */
308     @Test
309     public void isNotPrerelease() throws Exception {
310         final Release release = Mockito.mock(Release.class);
311         Mockito.doReturn(
312             Json.createObjectBuilder().add("prerelease", "false").build()
313         ).when(release).json();
314         MatcherAssert.assertThat(
315             new Release.Smart(release).prerelease(),
316             Matchers.is(Boolean.FALSE)
317         );
318     }
319 
320     /**
321      * Release.Smart counts prerelease as false if its missing.
322      * @throws Exception If problem inside
323      */
324     @Test
325     public void missingPrerelease() throws Exception {
326         final Release release = Mockito.mock(Release.class);
327         Mockito.doReturn(
328             Json.createObjectBuilder().build()
329         ).when(release).json();
330         MatcherAssert.assertThat(
331             new Release.Smart(release).prerelease(),
332             Matchers.is(Boolean.FALSE)
333         );
334     }
335 
336     /**
337      * Release.Smart can tell when the release is a draft.
338      * @throws Exception If problem inside
339      */
340     @Test
341     public void isDraft() throws Exception {
342         final Release release = Mockito.mock(Release.class);
343         Mockito.doReturn(
344             Json.createObjectBuilder().add("draft", Boolean.TRUE).build()
345         ).when(release).json();
346         MatcherAssert.assertThat(
347             new Release.Smart(release).draft(),
348             Matchers.is(Boolean.TRUE)
349         );
350     }
351 
352     /**
353      * Release.Smart can tell when the release is not a draft.
354      * @throws Exception If problem inside
355      */
356     @Test
357     public void isNotDraft() throws Exception {
358         final Release release = Mockito.mock(Release.class);
359         Mockito.doReturn(
360             Json.createObjectBuilder().add("draft", Boolean.FALSE).build()
361         ).when(release).json();
362         MatcherAssert.assertThat(
363             new Release.Smart(release).draft(),
364             Matchers.is(Boolean.FALSE)
365         );
366     }
367 
368     /**
369      * Release.Smart counts draft as false if its missing.
370      * @throws Exception If problem inside
371      */
372     @Test
373     public void missingDraft() throws Exception {
374         final Release release = Mockito.mock(Release.class);
375         Mockito.doReturn(
376             Json.createObjectBuilder().build()
377         ).when(release).json();
378         MatcherAssert.assertThat(
379             new Release.Smart(release).draft(),
380             Matchers.is(Boolean.FALSE)
381         );
382     }
383 }