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.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: a978dc01f426cd900c42a3c7c70c196fa41fe947 $
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       * @throws Exception if a problem occurs.
70       */
71      @Test
72      public void fetchesComments() throws Exception {
73          final RtIssue issue = new RtIssue(new FakeRequest(), this.repo(), 1);
74          MatcherAssert.assertThat(
75              issue.comments(),
76              Matchers.notNullValue()
77          );
78      }
79  
80      /**
81       * RtIssue should be able to fetch its labels.
82       *
83       * @throws Exception if a problem occurs.
84       */
85      @Test
86      public void fetchesLabels() throws Exception {
87          final RtIssue issue = new RtIssue(new FakeRequest(), this.repo(), 1);
88          MatcherAssert.assertThat(
89              issue.labels(),
90              Matchers.notNullValue()
91          );
92      }
93  
94      /**
95       * RtIssue should be able to fetch its events.
96       *
97       * @throws Exception if a problem occurs.
98       */
99      @Test
100     public void fetchesEvents() throws Exception {
101         final RtIssue issue = new RtIssue(new FakeRequest(), this.repo(), 1);
102         MatcherAssert.assertThat(
103             issue.events(),
104             Matchers.notNullValue()
105         );
106     }
107 
108     /**
109      * RtIssue should be able to describe itself in JSON format.
110      *
111      * @throws Exception if a problem occurs.
112      */
113     @Test
114     public void fetchIssueAsJson() throws Exception {
115         final RtIssue issue = new RtIssue(
116             new FakeRequest().withBody("{\"issue\":\"json\"}"),
117             this.repo(),
118             1
119         );
120         MatcherAssert.assertThat(
121             issue.json().getString("issue"),
122             Matchers.equalTo("json")
123         );
124     }
125 
126     /**
127      * RtIssue should be able to perform a patch request.
128      *
129      * @throws Exception if a problem occurs.
130      */
131     @Test
132     public void patchWithJson() throws Exception {
133         try (
134             final MkContainer container = new MkGrizzlyContainer().next(
135                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "response")
136             ).start(this.resource.port())
137         ) {
138             final RtIssue issue = new RtIssue(
139                 new ApacheRequest(container.home()),
140                 this.repo(),
141                 1
142             );
143             issue.patch(
144                 Json.createObjectBuilder().add("patch", "test").build()
145             );
146             final MkQuery query = container.take();
147             MatcherAssert.assertThat(
148                 query.method(),
149                 Matchers.equalTo(Request.PATCH)
150             );
151             MatcherAssert.assertThat(
152                 query.body(),
153                 Matchers.equalTo("{\"patch\":\"test\"}")
154             );
155             container.stop();
156         }
157     }
158 
159     /**
160      * RtIssue should be able to compare different instances.
161      *
162      * @throws Exception when a problem occurs.
163      */
164     @Test
165     public void canCompareInstances() throws Exception {
166         final RtIssue less = new RtIssue(new FakeRequest(), this.repo(), 1);
167         final RtIssue greater = new RtIssue(new FakeRequest(), this.repo(), 2);
168         MatcherAssert.assertThat(
169             less.compareTo(greater), Matchers.lessThan(0)
170         );
171         MatcherAssert.assertThat(
172             greater.compareTo(less), Matchers.greaterThan(0)
173         );
174     }
175 
176     /**
177      * RtIssue can add a reaction.
178      * @throws Exception - if anything goes wrong.
179      */
180     @Test
181     public void reacts() throws Exception {
182         try (
183             final MkContainer container = new MkGrizzlyContainer().next(
184                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
185             ).start(this.resource.port())) {
186             final Repo repo = new MkGithub().randomRepo();
187             final Issue issue = new RtIssue(
188                 new ApacheRequest(container.home()),
189                 repo,
190                 10
191             );
192             issue.react(new Reaction.Simple(Reaction.HEART));
193             final MkQuery query = container.take();
194             MatcherAssert.assertThat(
195                 "Issue was unable to react",
196                 query.method(),
197                 new IsEqual<>(Request.POST)
198             );
199         }
200     }
201 
202     /**
203      * Mock repo for GhIssue creation.
204      * @return The mock repo.
205      */
206     private Repo repo() {
207         final Repo repo = Mockito.mock(Repo.class);
208         final Coordinates coords = Mockito.mock(Coordinates.class);
209         Mockito.doReturn(coords).when(repo).coordinates();
210         Mockito.doReturn("user").when(coords).user();
211         Mockito.doReturn("repo").when(coords).repo();
212         return repo;
213     }
214 
215 }