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 com.jcabi.http.Request;
34  import com.jcabi.http.mock.MkAnswer;
35  import com.jcabi.http.mock.MkContainer;
36  import com.jcabi.http.mock.MkGrizzlyContainer;
37  import com.jcabi.http.request.ApacheRequest;
38  import com.jcabi.immutable.ArrayMap;
39  import java.net.HttpURLConnection;
40  import javax.json.Json;
41  import javax.json.JsonObject;
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 RtPulls}.
50   *
51   * @author Giang Le (giang@vn-smartsolutions.com)
52   * @version $Id: f5406c858088a87d19f9923185d5f9eba73f9521 $
53   * @checkstyle ClassDataAbstractionCouplingCheck (200 lines)
54   */
55  public final class RtPullsTest {
56  
57      /**
58       * The rule for skipping test if there's BindException.
59       * @checkstyle VisibilityModifierCheck (3 lines)
60       */
61      @Rule
62      public final transient RandomPort resource = new RandomPort();
63  
64      /**
65       * RtPulls can create a pull request.
66       *
67       * @throws Exception if some problem inside
68       */
69      @Test
70      public void createPull() throws Exception {
71          final String title = "new feature";
72          final String body = pull(title).toString();
73          try (
74              final MkContainer container = new MkGrizzlyContainer().next(
75                  new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
76              ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
77                  .start(this.resource.port())
78          ) {
79              final RtPulls pulls = new RtPulls(
80                  new ApacheRequest(container.home()),
81                  repo()
82              );
83              final Pull pull = pulls.create(title, "octocat", "master");
84              MatcherAssert.assertThat(
85                  container.take().method(),
86                  Matchers.equalTo(Request.POST)
87              );
88              MatcherAssert.assertThat(
89                  new Pull.Smart(pull).title(),
90                  Matchers.equalTo(title)
91              );
92              container.stop();
93          }
94      }
95  
96      /**
97       * RtPulls can get a single pull request.
98       * @throws Exception if some problem inside
99       */
100     @Test
101     public void getSinglePull() throws Exception {
102         final String title = "new-feature";
103         try (
104             final MkContainer container = new MkGrizzlyContainer().next(
105                 new MkAnswer.Simple(
106                     HttpURLConnection.HTTP_OK,
107                     pull(title).toString()
108                 )
109             ).start(this.resource.port())
110         ) {
111             final RtPulls pulls = new RtPulls(
112                 new ApacheRequest(container.home()),
113                 repo()
114             );
115             final Pull pull = pulls.get(Tv.BILLION);
116             MatcherAssert.assertThat(
117                 new Pull.Smart(pull).title(),
118                 Matchers.equalTo(title)
119             );
120             container.stop();
121         }
122     }
123 
124     /**
125      * RtPulls can iterate pulls.
126      * @throws Exception if there is any error
127      */
128     @Test
129     public void iteratePulls() throws Exception {
130         try (
131             final MkContainer container = new MkGrizzlyContainer().next(
132                 new MkAnswer.Simple(
133                     HttpURLConnection.HTTP_OK,
134                     Json.createArrayBuilder()
135                         .add(pull("new-topic"))
136                         .add(pull("Amazing new feature"))
137                         .build().toString()
138                 )
139             ).start(this.resource.port())
140         ) {
141             final RtPulls pulls = new RtPulls(
142                 new ApacheRequest(container.home()),
143                 repo()
144             );
145             MatcherAssert.assertThat(
146                 pulls.iterate(new ArrayMap<String, String>()),
147                 Matchers.<Pull>iterableWithSize(2)
148             );
149             container.stop();
150         }
151     }
152 
153     /**
154      * Create and return JsonObject to test.
155      * @param title The title of the pull request
156      * @return JsonObject
157      * @throws Exception If some problem inside
158      */
159     private static JsonObject pull(final String title) throws Exception {
160         return Json.createObjectBuilder()
161             .add("number", Tv.BILLION)
162             .add("state", Issue.OPEN_STATE)
163             .add("title", title)
164             .build();
165     }
166 
167     /**
168      * Create and return repo to test.
169      * @return Repo
170      * @throws Exception If some problem inside
171      */
172     private static Repo repo() throws Exception {
173         final Repo repo = Mockito.mock(Repo.class);
174         Mockito.doReturn(new Coordinates.Simple("mark", "test"))
175             .when(repo).coordinates();
176         return repo;
177     }
178 }