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.github.mock.MkGithub;
33  import com.jcabi.http.mock.MkAnswer;
34  import com.jcabi.http.mock.MkContainer;
35  import com.jcabi.http.mock.MkGrizzlyContainer;
36  import com.jcabi.http.request.FakeRequest;
37  import com.jcabi.http.request.JdkRequest;
38  import java.io.IOException;
39  import java.net.HttpURLConnection;
40  import java.util.Iterator;
41  import javax.json.Json;
42  import javax.json.JsonObject;
43  import org.hamcrest.MatcherAssert;
44  import org.hamcrest.Matchers;
45  import org.hamcrest.core.IsEqual;
46  import org.junit.Rule;
47  import org.junit.Test;
48  
49  /**
50   * Test case for {@link RtBranches}.
51   *
52   * @author Chris Rebert (github@rebertia.com)
53   * @version $Id: 020e866056e500a5a52053c01cfa0230d3063330 $
54   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
55   */
56  public final class RtBranchesTest {
57  
58      /**
59       * The rule for skipping test if there's BindException.
60       * @checkstyle VisibilityModifierCheck (3 lines)
61       */
62      @Rule
63      public final transient RandomPort resource = new RandomPort();
64  
65      /**
66       * RtBranches can iterate over all branches.
67       * @throws Exception if there is any error
68       */
69      @Test
70      public void iteratesOverBranches() throws Exception {
71          final String firstname = "first";
72          final String firstsha = "a971b1aca044105897297b87b0b0983a54dd5817";
73          final String secondname = "second";
74          final String secondsha = "5d8dc2acf9c95d0d4e8881eebe04c2f0cbb249ff";
75          final MkAnswer answer = new MkAnswer.Simple(
76              HttpURLConnection.HTTP_OK,
77              Json.createArrayBuilder()
78                  .add(branch(firstname, firstsha))
79                  .add(branch(secondname, secondsha))
80                  .build().toString()
81          );
82          try (
83              final MkContainer container = new MkGrizzlyContainer()
84                  .next(answer)
85                  .next(answer)
86                  .start(this.resource.port())
87          ) {
88              final RtBranches branches = new RtBranches(
89                  new JdkRequest(container.home()),
90                  new MkGithub().randomRepo()
91              );
92              MatcherAssert.assertThat(
93                  branches.iterate(),
94                  Matchers.<Branch>iterableWithSize(2)
95              );
96              final Iterator<Branch> iter = branches.iterate().iterator();
97              final Branch first = iter.next();
98              MatcherAssert.assertThat(first.name(), Matchers.equalTo(firstname));
99              MatcherAssert.assertThat(
100                 first.commit().sha(),
101                 Matchers.equalTo(firstsha)
102             );
103             final Branch second = iter.next();
104             MatcherAssert.assertThat(
105                 second.name(),
106                 Matchers.equalTo(secondname)
107             );
108             MatcherAssert.assertThat(
109                 second.commit().sha(),
110                 Matchers.equalTo(secondsha)
111             );
112             container.stop();
113         }
114     }
115 
116     /**
117      * RtBranches can find one branck by name.
118      * @throws Exception if there is any error
119      */
120     @Test
121     public void findBranch() throws Exception {
122         final String thirdname = "third";
123         final String thirdsha = "297b87b0b0983a54dd5817a971b1aca044105897";
124         final String fourthname = "fourth";
125         final String fourthsha = "d0d4e8881eebe04c5d8dc2acf9c952f0cbb249ff";
126         final MkAnswer answer = new MkAnswer.Simple(
127             HttpURLConnection.HTTP_OK,
128             Json.createArrayBuilder()
129                 .add(branch(thirdname, thirdsha))
130                 .add(branch(fourthname, fourthsha))
131                 .build().toString()
132         );
133         try (
134             final MkContainer container = new MkGrizzlyContainer()
135                 .next(answer)
136                 .next(answer)
137                 .start(this.resource.port());
138         ) {
139             final RtBranches branches = new RtBranches(
140                 new JdkRequest(container.home()),
141                 new MkGithub().randomRepo()
142             );
143             MatcherAssert.assertThat(
144                 "could not find branch correctly",
145                 branches.find(fourthname).commit().sha(),
146                 new IsEqual<>(
147                     fourthsha
148                 )
149             );
150             container.stop();
151         }
152     }
153 
154     /**
155      * RtBranches can fetch its repository.
156      * @throws IOException If there is any I/O problem
157      */
158     @Test
159     public void fetchesRepo() throws IOException {
160         final Repo repo = new MkGithub().randomRepo();
161         final RtBranches branch = new RtBranches(new FakeRequest(), repo);
162         final Coordinates coords = branch.repo().coordinates();
163         MatcherAssert.assertThat(
164             coords.user(),
165             Matchers.equalTo(repo.coordinates().user())
166         );
167         MatcherAssert.assertThat(
168             coords.repo(),
169             Matchers.equalTo(repo.coordinates().repo())
170         );
171     }
172 
173     /**
174      * Create and return JsonObject to test.
175      * @param name Name of the branch
176      * @param sha Commit SHA of the branch
177      * @return JsonObject
178      * @throws Exception If some problem inside
179      */
180     private static JsonObject branch(final String name, final String sha)
181         throws Exception {
182         return Json.createObjectBuilder()
183             .add("name", name)
184             .add(
185                 "commit",
186                 Json.createObjectBuilder()
187                     .add("sha", sha)
188                     // @checkstyle LineLengthCheck (1 line)
189                     .add("url", String.format("https://api.jcabi-github.invalid/repos/user/repo/commits/%s", sha))
190             )
191             .build();
192     }
193 }