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 javax.json.JsonObject;
43  import org.hamcrest.MatcherAssert;
44  import org.hamcrest.Matchers;
45  import org.hamcrest.collection.IsIterableWithSize;
46  import org.hamcrest.core.IsEqual;
47  import org.junit.Rule;
48  import org.junit.Test;
49  
50  /**
51   * Test case for {@link RtComment}.
52   *
53   * @author Carlos Miranda (miranda.cma@gmail.com)
54   * @version $Id: 6a6e3403d4d8a4380529e6babc6a750701c4b1ad $
55   * @checkstyle MultipleStringLiterals (500 lines)
56   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
57   */
58  public final class RtCommentTest {
59  
60      /**
61       * The rule for skipping test if there's BindException.
62       * @checkstyle VisibilityModifierCheck (3 lines)
63       */
64      @Rule
65      public final transient RandomPort resource = new RandomPort();
66  
67      /**
68       * RtComment should be able to compare different instances.
69       * @throws Exception when a problem occurs.
70       */
71      @Test
72      public void canCompareInstances() throws Exception {
73          final Repo repo = new MkGithub().randomRepo();
74          final Issue issue = repo.issues().create("title", "body");
75          final RtComment less = new RtComment(new FakeRequest(), issue, 1);
76          final RtComment greater = new RtComment(new FakeRequest(), issue, 2);
77          MatcherAssert.assertThat(
78              less.compareTo(greater), Matchers.lessThan(0)
79          );
80          MatcherAssert.assertThat(
81              greater.compareTo(less), Matchers.greaterThan(0)
82          );
83      }
84  
85      /**
86       * RtComment can return its issue (owner).
87       * @throws Exception - if anything goes wrong.
88       */
89      @Test
90      public void returnsItsIssue() throws Exception {
91          final Repo repo = new MkGithub().randomRepo();
92          final Issue issue = repo.issues().create("testing1", "issue1");
93          final RtComment comment = new RtComment(new FakeRequest(), issue, 1);
94          MatcherAssert.assertThat(comment.issue(), Matchers.is(issue));
95      }
96  
97      /**
98       * RtComment can return its number.
99       * @throws Exception - in case something goes wrong.
100      */
101     @Test
102     public void returnsItsNumber() throws Exception {
103         final Repo repo = new MkGithub().randomRepo();
104         final Issue issue = repo.issues().create("testing2", "issue2");
105         final int num = 10;
106         final RtComment comment = new RtComment(new FakeRequest(), issue, num);
107         MatcherAssert.assertThat(comment.number(), Matchers.is(num));
108     }
109 
110     /**
111      * This tests that the remove() method is working fine.
112      * @throws Exception - in case something goes wrong.
113      */
114     @Test
115     public void removesComment() throws Exception {
116         try (
117             final MkContainer container = new MkGrizzlyContainer().next(
118                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
119             ).start(this.resource.port())) {
120             final Repo repo = new MkGithub().randomRepo();
121             final Issue issue = repo.issues().create("testing3", "issue3");
122             final RtComment comment = new RtComment(
123                 new ApacheRequest(container.home()), issue, 10
124             );
125             comment.remove();
126             final MkQuery query = container.take();
127             MatcherAssert.assertThat(
128                 query.method(),
129                 Matchers.equalTo(Request.DELETE)
130             );
131         }
132     }
133 
134     /**
135      * RtComment can return its JSon description.
136      * @throws Exception - if something goes wrong.
137      */
138     @Test
139     public void returnsItsJSon() throws Exception {
140         final String body = "{\"body\":\"test5\"}";
141         try (
142             final MkContainer container = new MkGrizzlyContainer().next(
143                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body)
144             ).start(this.resource.port())) {
145             final Repo repo = new MkGithub().randomRepo();
146             final Issue issue = repo.issues().create("testing4", "issue4");
147             final RtComment comment = new RtComment(
148                 new ApacheRequest(container.home()), issue, 10
149             );
150             final JsonObject json = comment.json();
151             MatcherAssert.assertThat(
152                 json.getString("body"),
153                 Matchers.is("test5")
154             );
155         }
156     }
157 
158     /**
159      * RtComment can patch a comment.
160      * @throws Exception - if anything goes wrong.
161      */
162     @Test
163     public void patchesComment() throws Exception {
164         try (
165             final MkContainer container = new MkGrizzlyContainer().next(
166                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
167             ).start(this.resource.port())) {
168             final Repo repo = new MkGithub().randomRepo();
169             final Issue issue = repo.issues().create("testing5", "issue5");
170             final RtComment comment = new RtComment(
171                 new ApacheRequest(container.home()), issue, 10
172             );
173             final JsonObject jsonPatch = Json.createObjectBuilder()
174                 .add("title", "test comment").build();
175             comment.patch(jsonPatch);
176             final MkQuery query = container.take();
177             MatcherAssert.assertThat(
178                 query.method(), Matchers.equalTo(Request.PATCH)
179             );
180         }
181     }
182 
183     /**
184      * RtComment can add a reaction.
185      * @throws Exception - if anything goes wrong.
186      */
187     @Test
188     public void reacts() throws Exception {
189         try (
190             final MkContainer container = new MkGrizzlyContainer().next(
191                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
192             ).start(this.resource.port())) {
193             final Repo repo = new MkGithub().randomRepo();
194             final Issue issue = repo.issues().create(
195                 "Reaction adding test", "This is a test for adding a reaction"
196             );
197             final RtComment comment = new RtComment(
198                 new ApacheRequest(container.home()), issue, 10
199             );
200             comment.react(new Reaction.Simple(Reaction.HEART));
201             final MkQuery query = container.take();
202             MatcherAssert.assertThat(
203                 query.method(),
204                 new IsEqual<>(Request.POST)
205             );
206         }
207     }
208 
209     /**
210      * RtComment can list its reactions.
211      * @throws Exception - if anything goes wrong.
212      */
213     @Test
214     public void reactions() throws Exception {
215         try (
216             final MkContainer container = new MkGrizzlyContainer().next(
217                 new MkAnswer.Simple(
218                     HttpURLConnection.HTTP_OK,
219                      Json.createArrayBuilder()
220                     .add(
221                         Json.createObjectBuilder()
222                         .add("id", "1")
223                         .add("content", "heart")
224                         .build()
225                     ).build().toString()
226                 )
227             ).start(this.resource.port())) {
228             final Repo repo = new MkGithub().randomRepo();
229             final Issue issue = repo.issues().create(
230                 "Reaction Listing test", "This is a test for listing reactions"
231             );
232             final RtComment comment = new RtComment(
233                 new ApacheRequest(container.home()), issue, 10
234             );
235             MatcherAssert.assertThat(
236                 comment.reactions(),
237                 new IsIterableWithSize<>(new IsEqual<>(1))
238             );
239         }
240     }
241 
242     /**
243      * This tests that the toString() method is working fine.
244      * @throws Exception - if anything goes wrong.
245      */
246     @Test
247     public void givesToString() throws Exception {
248         try (
249             final MkContainer container = new MkGrizzlyContainer().next(
250                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
251             ).start(this.resource.port())) {
252             final Repo repo = new MkGithub().randomRepo();
253             final Issue issue = repo.issues().create("testing6", "issue6");
254             final RtComment comment = new RtComment(
255                 new ApacheRequest(container.home()), issue, 10
256             );
257             final String stringComment = comment.toString();
258             MatcherAssert.assertThat(
259                 stringComment, Matchers.not(Matchers.isEmptyOrNullString())
260             );
261             MatcherAssert.assertThat(stringComment, Matchers.endsWith("10"));
262         }
263     }
264 }