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.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: 5dfd61c05e02dfb9c801dfb21f709e28c42d791e $
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       * @throws Exception if a problem occurs.
72       */
73      @Test
74      public void retrievesForks() throws Exception {
75          final RtForks forks = new RtForks(
76              new FakeRequest()
77                  .withBody("[]"), this.repo()
78          );
79          MatcherAssert.assertThat(
80              forks.iterate("newest"),
81              Matchers.<Fork>iterableWithSize(0)
82          );
83      }
84  
85      /**
86       * RtForks should be able to create a new fork.
87       *
88       * @throws Exception if a problem occurs.
89       */
90      @Test
91      public void createsFork() throws Exception {
92          final String organization = RandomStringUtils.randomAlphanumeric(10);
93          final MkAnswer answer = new MkAnswer.Simple(
94              HttpURLConnection.HTTP_OK,
95              fork(organization).toString()
96          );
97          try (
98              final MkContainer container = new MkGrizzlyContainer().next(
99                  new MkAnswer.Simple(
100                     HttpURLConnection.HTTP_ACCEPTED,
101                     fork(organization).toString()
102                 )
103             ).next(answer).start(this.resource.port())) {
104             final Repo owner = Mockito.mock(Repo.class);
105             final Coordinates coordinates = new Coordinates.Simple(
106                 "test_user", "test_repo"
107             );
108             Mockito.doReturn(coordinates).when(owner).coordinates();
109             final RtForks forks = new RtForks(
110                 new JdkRequest(container.home()),
111                 owner
112             );
113             final Fork fork = forks.create(organization);
114             MatcherAssert.assertThat(
115                 container.take().method(),
116                 Matchers.equalTo(Request.POST)
117             );
118             MatcherAssert.assertThat(
119                 fork.json().getString(ORGANIZATION),
120                 Matchers.equalTo(organization)
121             );
122         }
123     }
124 
125     /**
126      * Create and return repo for testing.
127      *
128      * @return Repo
129      */
130     private Repo repo() {
131         final Repo repo = Mockito.mock(Repo.class);
132         Mockito.doReturn(new Coordinates.Simple("test", "forks"))
133             .when(repo).coordinates();
134         return repo;
135     }
136 
137     /**
138      * Create and return JsonObject to test.
139      * @param organization The organization of the fork
140      * @return JsonObject
141      */
142     private static JsonObject fork(
143         final String organization) {
144         return Json.createObjectBuilder()
145             .add("id", 1)
146             .add(ORGANIZATION, organization)
147             .build();
148     }
149 }