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 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: 7474b4553b07b8df2437fe31a2648d52ecfc9ca6 $
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      */
131     @Test
132     public void testId() {
133         final Release release = Mockito.mock(Release.class);
134         Mockito.doReturn(1).when(release).number();
135         final Release.Smart smart = new Release.Smart(release);
136         MatcherAssert.assertThat(
137             smart.number(),
138             Matchers.equalTo(1)
139         );
140     }
141 
142     /**
143      * Release.Smart can fetch tag of an Release.
144      * @throws Exception If some problem inside
145      */
146     @Test
147     public void fetchTag() throws Exception {
148         final Release release = Mockito.mock(Release.class);
149         final String tag = "v1.0.0";
150         Mockito.doReturn(
151             Json.createObjectBuilder()
152                 .add("tag_name", tag)
153                 .build()
154         ).when(release).json();
155         final Release.Smart smart = new Release.Smart(release);
156         MatcherAssert.assertThat(
157             smart.tag(),
158             Matchers.equalTo(tag)
159         );
160     }
161 
162     /**
163      * Release.Smart can fetch commitish of an Release.
164      * @throws Exception If some problem inside
165      */
166     @Test
167     public void fetchProperties() throws Exception {
168         final Release release = Mockito.mock(Release.class);
169         final String master = "master";
170         Mockito.doReturn(
171             Json.createObjectBuilder()
172                 .add("target_commitish", master)
173                 .build()
174         ).when(release).json();
175         final Release.Smart smart = new Release.Smart(release);
176         MatcherAssert.assertThat(
177             smart.commitish(),
178             Matchers.equalTo(master)
179         );
180     }
181 
182     /**
183      * Release.Smart can fetch name of an Release.
184      * @throws Exception If some problem inside
185      */
186     @Test
187     public void fetchName() throws Exception {
188         final Release release = Mockito.mock(Release.class);
189         final String name = "v1";
190         // @checkstyle MultipleStringLiterals (3 lines)
191         Mockito.doReturn(
192             Json.createObjectBuilder()
193                 .add("name", name)
194                 .build()
195         ).when(release).json();
196         final Release.Smart smart = new Release.Smart(release);
197         MatcherAssert.assertThat(
198             smart.hasName(),
199             Matchers.is(true)
200         );
201         MatcherAssert.assertThat(
202             smart.name(),
203             Matchers.equalTo(name)
204         );
205     }
206 
207     /**
208      * Release.Smart can determine if the release does not have a name
209      * (NULL json value).
210      * @throws Exception If some problem inside
211      */
212     @Test
213     public void incidatesNoName() throws Exception {
214         final Release release = Mockito.mock(Release.class);
215         Mockito.doReturn(
216             Json.createObjectBuilder()
217                 .add("name", JsonValue.NULL)
218                 .build()
219         ).when(release).json();
220         final Release.Smart smart = new Release.Smart(release);
221         MatcherAssert.assertThat(
222             smart.hasName(),
223             Matchers.is(false)
224         );
225     }
226 
227     /**
228      * Release.Smart can fetch body of an Release.
229      * @throws Exception If some problem inside
230      */
231     @Test
232     public void fetchBody() throws Exception {
233         final Release release = Mockito.mock(Release.class);
234         final String description = "Description of the release";
235         Mockito.doReturn(
236             Json.createObjectBuilder()
237                 .add("body", description)
238                 .build()
239         ).when(release).json();
240         final Release.Smart smart = new Release.Smart(release);
241         MatcherAssert.assertThat(
242             smart.body(),
243             Matchers.equalTo(description)
244         );
245     }
246 
247     /**
248      * Release.Smart can fetch created date of an Release.
249      * @throws Exception If some problem inside
250      */
251     @Test
252     public void fetchDescription() throws Exception {
253         final Release release = Mockito.mock(Release.class);
254         final String created = "2013-02-27T19:35:32Z";
255         Mockito.doReturn(
256             Json.createObjectBuilder()
257                 .add("created_at", created)
258                 .build()
259         ).when(release).json();
260         final Release.Smart smart = new Release.Smart(release);
261         MatcherAssert.assertThat(
262             smart.createdAt(),
263             Matchers.equalTo(new Github.Time(created).date())
264         );
265     }
266 
267     /**
268      * Release.Smart can fetch published date of an Release.
269      * @throws Exception If some problem inside
270      */
271     @Test
272     public void fetchPublished() throws Exception {
273         final Release release = Mockito.mock(Release.class);
274         final String published = "2013-01-27T19:35:32Z";
275         Mockito.doReturn(
276             Json.createObjectBuilder()
277                 .add("published_at", published)
278                 .build()
279         ).when(release).json();
280         final Release.Smart smart = new Release.Smart(release);
281         MatcherAssert.assertThat(
282             smart.publishedAt(),
283             Matchers.equalTo(new Github.Time(published).date())
284         );
285     }
286 
287     /**
288      * Release.Smart can tell when the release is a prerelease.
289      * @throws Exception If problem inside
290      */
291     @Test
292     public void isPrerelease() throws Exception {
293         final Release release = Mockito.mock(Release.class);
294         Mockito.doReturn(
295             Json.createObjectBuilder().add("prerelease", Boolean.TRUE).build()
296         ).when(release).json();
297         MatcherAssert.assertThat(
298             new Release.Smart(release).prerelease(),
299             Matchers.is(Boolean.TRUE)
300         );
301     }
302 
303     /**
304      * Release.Smart can tell when the release is not a prerelease.
305      * @throws Exception If problem inside
306      */
307     @Test
308     public void isNotPrerelease() throws Exception {
309         final Release release = Mockito.mock(Release.class);
310         Mockito.doReturn(
311             Json.createObjectBuilder().add("prerelease", "false").build()
312         ).when(release).json();
313         MatcherAssert.assertThat(
314             new Release.Smart(release).prerelease(),
315             Matchers.is(Boolean.FALSE)
316         );
317     }
318 
319     /**
320      * Release.Smart counts prerelease as false if its missing.
321      * @throws Exception If problem inside
322      */
323     @Test
324     public void missingPrerelease() throws Exception {
325         final Release release = Mockito.mock(Release.class);
326         Mockito.doReturn(
327             Json.createObjectBuilder().build()
328         ).when(release).json();
329         MatcherAssert.assertThat(
330             new Release.Smart(release).prerelease(),
331             Matchers.is(Boolean.FALSE)
332         );
333     }
334 
335     /**
336      * Release.Smart can tell when the release is a draft.
337      * @throws Exception If problem inside
338      */
339     @Test
340     public void isDraft() throws Exception {
341         final Release release = Mockito.mock(Release.class);
342         Mockito.doReturn(
343             Json.createObjectBuilder().add("draft", Boolean.TRUE).build()
344         ).when(release).json();
345         MatcherAssert.assertThat(
346             new Release.Smart(release).draft(),
347             Matchers.is(Boolean.TRUE)
348         );
349     }
350 
351     /**
352      * Release.Smart can tell when the release is not a draft.
353      * @throws Exception If problem inside
354      */
355     @Test
356     public void isNotDraft() throws Exception {
357         final Release release = Mockito.mock(Release.class);
358         Mockito.doReturn(
359             Json.createObjectBuilder().add("draft", Boolean.FALSE).build()
360         ).when(release).json();
361         MatcherAssert.assertThat(
362             new Release.Smart(release).draft(),
363             Matchers.is(Boolean.FALSE)
364         );
365     }
366 
367     /**
368      * Release.Smart counts draft as false if its missing.
369      * @throws Exception If problem inside
370      */
371     @Test
372     public void missingDraft() throws Exception {
373         final Release release = Mockito.mock(Release.class);
374         Mockito.doReturn(
375             Json.createObjectBuilder().build()
376         ).when(release).json();
377         MatcherAssert.assertThat(
378             new Release.Smart(release).draft(),
379             Matchers.is(Boolean.FALSE)
380         );
381     }
382 }