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 com.jcabi.aspects.Tv;
33  import javax.json.Json;
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 Fork}.
41   * @author Carlos Miranda (miranda.cma@gmail.com)
42   * @version $Id: e6e1e2459957ac063680d5f87cacee5af0567e69 $
43   */
44  public class ForkTest {
45      /**
46       * Fork.Smart can fetch name property from Fork.
47       * @throws Exception If some problem inside
48       */
49      @Test
50      public final void fetchesName() throws Exception {
51          final Fork fork = Mockito.mock(Fork.class);
52          final String name = "this is some name";
53          Mockito.doReturn(
54              Json.createObjectBuilder()
55                  .add("name", name)
56                  .build()
57          ).when(fork).json();
58          MatcherAssert.assertThat(
59              new Fork.Smart(fork).name(),
60              Matchers.is(name)
61          );
62      }
63  
64      /**
65       * Fork.Smart can fetch full name property from Fork.
66       * @throws Exception If some problem inside
67       */
68      @Test
69      public final void fetchesFullName() throws Exception {
70          final Fork fork = Mockito.mock(Fork.class);
71          final String name = "test full name";
72          Mockito.doReturn(
73              Json.createObjectBuilder()
74                  .add("full_name", name)
75                  .build()
76          ).when(fork).json();
77          MatcherAssert.assertThat(
78              new Fork.Smart(fork).fullName(),
79              Matchers.is(name)
80          );
81      }
82  
83      /**
84       * Fork.Smart can description property from Fork.
85       * @throws Exception If some problem inside
86       */
87      @Test
88      public final void fetchesDescription() throws Exception {
89          final Fork fork = Mockito.mock(Fork.class);
90          final String description = "test description";
91          Mockito.doReturn(
92              Json.createObjectBuilder()
93                  .add("description", description)
94                  .build()
95          ).when(fork).json();
96          MatcherAssert.assertThat(
97              new Fork.Smart(fork).description(),
98              Matchers.is(description)
99          );
100     }
101     /**
102      * Fork.Smart can fetch size property from Fork.
103      * @throws Exception If some problem inside
104      */
105     @Test
106     public final void fetchesSize() throws Exception {
107         final Fork fork = Mockito.mock(Fork.class);
108         final int prop = Tv.HUNDRED;
109         Mockito.doReturn(
110             Json.createObjectBuilder()
111                 .add("size", prop)
112                 .build()
113         ).when(fork).json();
114         MatcherAssert.assertThat(
115             new Fork.Smart(fork).size(),
116             Matchers.is(prop)
117         );
118     }
119 
120     /**
121      * Fork.Smart can fetch the Fork URLs.
122      * @throws Exception If some problem inside
123      */
124     @Test
125     public final void fetchesUrls() throws Exception {
126         final Fork fork = Mockito.mock(Fork.class);
127         final String url = "https://api.github.com/repos/octocat/Hello-World";
128         final String html = "https://github.com/octocat/Hello-World";
129         final String clone = "https://github.com/octocat/Hello-World.git";
130         final String git = "git://github.com/octocat/Hello-World.git";
131         final String ssh = "git@github.com:octocat/Hello-World.git";
132         final String svn = "https://svn.github.com/octocat/Hello-World";
133         final String mirror = "git://git.example.com/octocat/Hello-World";
134         final String homepage = "https://github.com";
135         Mockito.doReturn(
136             Json.createObjectBuilder()
137                 .add("url", url)
138                 .add("html_url", html)
139                 .add("clone_url", clone)
140                 .add("git_url", git)
141                 .add("ssh_url", ssh)
142                 .add("svn_url", svn)
143                 .add("mirror_url", mirror)
144                 .add("homepage", homepage)
145                 .build()
146         ).when(fork).json();
147         final Fork.Smart smart = new Fork.Smart(fork);
148         MatcherAssert.assertThat(
149             smart.url().toString(),
150             Matchers.is(url)
151         );
152         MatcherAssert.assertThat(
153             smart.htmlUrl().toString(),
154             Matchers.is(html)
155         );
156         MatcherAssert.assertThat(
157             smart.cloneUrl().toString(),
158             Matchers.is(clone)
159         );
160         MatcherAssert.assertThat(
161             smart.gitUrl().toString(),
162             Matchers.is(git)
163         );
164         MatcherAssert.assertThat(
165             smart.sshUrl().toString(),
166             Matchers.is(ssh)
167         );
168         MatcherAssert.assertThat(
169             smart.svnUrl().toString(),
170             Matchers.is(svn)
171         );
172         MatcherAssert.assertThat(
173             smart.mirrorUrl().toString(),
174             Matchers.is(mirror)
175         );
176         MatcherAssert.assertThat(
177             smart.homeUrl().toString(),
178             Matchers.is(homepage)
179         );
180     }
181     /**
182      * Fork.Smart can fetch the number of forks, stargazers, and watchers
183      * from Fork.
184      * @throws Exception If some problem inside
185      */
186     @Test
187     public final void fetchesCounts() throws Exception {
188         final Fork fork = Mockito.mock(Fork.class);
189         final int forks = Tv.TEN;
190         final int stargazers = Tv.TWENTY;
191         final int watchers = Tv.THIRTY;
192         Mockito.doReturn(
193             Json.createObjectBuilder()
194                 .add("forks_count", forks)
195                 .add("stargazers_count", stargazers)
196                 .add("watchers_count", watchers)
197                 .build()
198         ).when(fork).json();
199         final Fork.Smart smart = new Fork.Smart(fork);
200         MatcherAssert.assertThat(
201             smart.forks(),
202             Matchers.is(forks)
203         );
204         MatcherAssert.assertThat(
205             smart.stargazers(),
206             Matchers.is(stargazers)
207         );
208         MatcherAssert.assertThat(
209             smart.watchers(),
210             Matchers.is(watchers)
211         );
212     }
213 
214     /**
215      * Fork.Smart can fetch the number of open issues from Fork.
216      * @throws Exception If some problem inside
217      */
218     @Test
219     public final void openIssues() throws Exception {
220         final Fork fork = Mockito.mock(Fork.class);
221         final int openIssues = Tv.TEN;
222         Mockito.doReturn(
223             Json.createObjectBuilder()
224                 .add("open_issues_count", openIssues)
225                 .build()
226         ).when(fork).json();
227         final Fork.Smart smart = new Fork.Smart(fork);
228         MatcherAssert.assertThat(
229             smart.openIssues(),
230             Matchers.is(openIssues)
231         );
232     }
233 
234     /**
235      * Fork.Smart can fetch the default branch from Fork.
236      * @throws Exception If some problem inside
237      */
238     @Test
239     public final void fetchesDefaultBranches() throws Exception {
240         final Fork fork = Mockito.mock(Fork.class);
241         final String master = "master";
242         Mockito.doReturn(
243             Json.createObjectBuilder()
244                 .add("default_branch", master)
245                 .build()
246         ).when(fork).json();
247         final Fork.Smart smart = new Fork.Smart(fork);
248         MatcherAssert.assertThat(
249             smart.defaultBranch(),
250             Matchers.is(master)
251         );
252     }
253 }