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 com.jcabi.http.Request;
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.net.HttpURLConnection;
39  import javax.json.Json;
40  import javax.json.JsonObject;
41  import org.apache.commons.lang3.RandomStringUtils;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.Rule;
45  import org.junit.Test;
46  import org.mockito.Mockito;
47  
48  /**
49   * Test case for {@link RtForks}.
50   *
51   * @author Carlos Miranda (miranda.cma@gmail.com)
52   * @version $Id: ee7249a7c997a5e5746e715bd94b8792bf01cca8 $
53   */
54  public final class RtForksTest {
55  
56      /**
57       * Fork's organization name in JSON object.
58       */
59      public static final String ORGANIZATION = "organization";
60  
61      /**
62       * The rule for skipping test if there's BindException.
63       * @checkstyle VisibilityModifierCheck (3 lines)
64       */
65      @Rule
66      public final transient RandomPort resource = new RandomPort();
67  
68      /**
69       * RtForks should be able to iterate its forks.
70       *
71       */
72      @Test
73      public void retrievesForks() {
74          final RtForks forks = new RtForks(
75              new FakeRequest()
76                  .withBody("[]"), this.repo()
77          );
78          MatcherAssert.assertThat(
79              forks.iterate("newest"),
80              Matchers.<Fork>iterableWithSize(0)
81          );
82      }
83  
84      /**
85       * RtForks should be able to create a new fork.
86       *
87       * @throws Exception if a problem occurs.
88       */
89      @Test
90      public void createsFork() throws Exception {
91          final String organization = RandomStringUtils.randomAlphanumeric(10);
92          final MkAnswer answer = new MkAnswer.Simple(
93              HttpURLConnection.HTTP_OK,
94              fork(organization).toString()
95          );
96          try (
97              final MkContainer container = new MkGrizzlyContainer().next(
98                  new MkAnswer.Simple(
99                      HttpURLConnection.HTTP_ACCEPTED,
100                     fork(organization).toString()
101                 )
102             ).next(answer).start(this.resource.port())) {
103             final Repo owner = Mockito.mock(Repo.class);
104             final Coordinates coordinates = new Coordinates.Simple(
105                 "test_user", "test_repo"
106             );
107             Mockito.doReturn(coordinates).when(owner).coordinates();
108             final RtForks forks = new RtForks(
109                 new JdkRequest(container.home()),
110                 owner
111             );
112             final Fork fork = forks.create(organization);
113             MatcherAssert.assertThat(
114                 container.take().method(),
115                 Matchers.equalTo(Request.POST)
116             );
117             MatcherAssert.assertThat(
118                 fork.json().getString(ORGANIZATION),
119                 Matchers.equalTo(organization)
120             );
121         }
122     }
123 
124     /**
125      * Create and return repo for testing.
126      *
127      * @return Repo
128      */
129     private Repo repo() {
130         final Repo repo = Mockito.mock(Repo.class);
131         Mockito.doReturn(new Coordinates.Simple("test", "forks"))
132             .when(repo).coordinates();
133         return repo;
134     }
135 
136     /**
137      * Create and return JsonObject to test.
138      * @param organization The organization of the fork
139      * @return JsonObject
140      */
141     private static JsonObject fork(
142         final String organization) {
143         return Json.createObjectBuilder()
144             .add("id", 1)
145             .add(ORGANIZATION, organization)
146             .build();
147     }
148 }