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.github.mock.MkGithub;
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.mock.MkQuery;
38  import com.jcabi.http.request.ApacheRequest;
39  import com.jcabi.http.request.FakeRequest;
40  import java.net.HttpURLConnection;
41  import javax.json.Json;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.hamcrest.core.IsEqual;
45  import org.junit.Rule;
46  import org.junit.Test;
47  import org.mockito.Mockito;
48  
49  /**
50   * Test case for {@link RtIssue}.
51   *
52   * @author Carlos Miranda (miranda.cma@gmail.com)
53   * @author Paulo Lobo (pauloeduardolobo@gmail.com)
54   * @version $Id: 103ad8dcc0ac5e1fd225deff569658ba20dc40bc $
55   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
56   */
57  public final class RtIssueTest {
58  
59      /**
60       * The rule for skipping test if there's BindException.
61       * @checkstyle VisibilityModifierCheck (3 lines)
62       */
63      @Rule
64      public final transient RandomPort resource = new RandomPort();
65  
66      /**
67       * RtIssue should be able to fetch its comments.
68       *
69       */
70      @Test
71      public void fetchesComments() {
72          final RtIssue issue = new RtIssue(new FakeRequest(), this.repo(), 1);
73          MatcherAssert.assertThat(
74              issue.comments(),
75              Matchers.notNullValue()
76          );
77      }
78  
79      /**
80       * RtIssue should be able to fetch its labels.
81       *
82       */
83      @Test
84      public void fetchesLabels() {
85          final RtIssue issue = new RtIssue(new FakeRequest(), this.repo(), 1);
86          MatcherAssert.assertThat(
87              issue.labels(),
88              Matchers.notNullValue()
89          );
90      }
91  
92      /**
93       * RtIssue should be able to fetch its events.
94       *
95       */
96      @Test
97      public void fetchesEvents() {
98          final RtIssue issue = new RtIssue(new FakeRequest(), this.repo(), 1);
99          MatcherAssert.assertThat(
100             issue.events(),
101             Matchers.notNullValue()
102         );
103     }
104 
105     /**
106      * RtIssue should be able to describe itself in JSON format.
107      *
108      * @throws Exception if a problem occurs.
109      */
110     @Test
111     public void fetchIssueAsJson() throws Exception {
112         final RtIssue issue = new RtIssue(
113             new FakeRequest().withBody("{\"issue\":\"json\"}"),
114             this.repo(),
115             1
116         );
117         MatcherAssert.assertThat(
118             issue.json().getString("issue"),
119             Matchers.equalTo("json")
120         );
121     }
122 
123     /**
124      * RtIssue should be able to perform a patch request.
125      *
126      * @throws Exception if a problem occurs.
127      */
128     @Test
129     public void patchWithJson() throws Exception {
130         try (
131             final MkContainer container = new MkGrizzlyContainer().next(
132                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "response")
133             ).start(this.resource.port())
134         ) {
135             final RtIssue issue = new RtIssue(
136                 new ApacheRequest(container.home()),
137                 this.repo(),
138                 1
139             );
140             issue.patch(
141                 Json.createObjectBuilder().add("patch", "test").build()
142             );
143             final MkQuery query = container.take();
144             MatcherAssert.assertThat(
145                 query.method(),
146                 Matchers.equalTo(Request.PATCH)
147             );
148             MatcherAssert.assertThat(
149                 query.body(),
150                 Matchers.equalTo("{\"patch\":\"test\"}")
151             );
152             container.stop();
153         }
154     }
155 
156     /**
157      * RtIssue should be able to compare different instances.
158      *
159      */
160     @Test
161     public void canCompareInstances() {
162         final RtIssue less = new RtIssue(new FakeRequest(), this.repo(), 1);
163         final RtIssue greater = new RtIssue(new FakeRequest(), this.repo(), 2);
164         MatcherAssert.assertThat(
165             less.compareTo(greater), Matchers.lessThan(0)
166         );
167         MatcherAssert.assertThat(
168             greater.compareTo(less), Matchers.greaterThan(0)
169         );
170     }
171 
172     /**
173      * RtIssue can add a reaction.
174      * @throws Exception - if anything goes wrong.
175      */
176     @Test
177     public void reacts() throws Exception {
178         try (
179             final MkContainer container = new MkGrizzlyContainer().next(
180                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
181             ).start(this.resource.port())) {
182             final Repo repo = new MkGithub().randomRepo();
183             final Issue issue = new RtIssue(
184                 new ApacheRequest(container.home()),
185                 repo,
186                 10
187             );
188             issue.react(new Reaction.Simple(Reaction.HEART));
189             final MkQuery query = container.take();
190             MatcherAssert.assertThat(
191                 "Issue was unable to react",
192                 query.method(),
193                 new IsEqual<>(Request.POST)
194             );
195         }
196     }
197 
198     /**
199      * Mock repo for GhIssue creation.
200      * @return The mock repo.
201      */
202     private Repo repo() {
203         final Repo repo = Mockito.mock(Repo.class);
204         final Coordinates coords = Mockito.mock(Coordinates.class);
205         Mockito.doReturn(coords).when(repo).coordinates();
206         Mockito.doReturn("user").when(coords).user();
207         Mockito.doReturn("repo").when(coords).repo();
208         return repo;
209     }
210 
211 }