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.IsCollectionWithSize;
46  import org.hamcrest.core.IsEqual;
47  import org.junit.Ignore;
48  import org.junit.Rule;
49  import org.junit.Test;
50  import org.mockito.Mockito;
51  
52  /**
53   * Test case for {@link RtPullComment}.
54   *
55   * @author Carlos Miranda (miranda.cma@gmail.com)
56   * @version $Id: 5bd9425b43520b079fc919177ba803905b087a22 $
57   * @checkstyle MultipleStringLiteralsCheck (200 lines)
58   * @checkstyle ClassDataAbstractionCouplingCheck (3 lines)
59   */
60  public final class RtPullCommentTest {
61  
62      /**
63       * The rule for skipping test if there's BindException.
64       * @checkstyle VisibilityModifierCheck (3 lines)
65       */
66      @Rule
67      public final transient RandomPort resource = new RandomPort();
68      /**
69       * RtPullComment should be able to compare different instances.
70       * @throws Exception If a problem occurs.
71       */
72      @Test
73      public void canCompareInstances() throws Exception {
74          final Pull pull = Mockito.mock(Pull.class);
75          Mockito.doReturn(new MkGithub().randomRepo()).when(pull).repo();
76          final RtPullComment less =
77              new RtPullComment(new FakeRequest(), pull, 1);
78          final RtPullComment greater =
79              new RtPullComment(new FakeRequest(), pull, 2);
80          MatcherAssert.assertThat(
81              less.compareTo(greater), Matchers.lessThan(0)
82          );
83          MatcherAssert.assertThat(
84              greater.compareTo(less), Matchers.greaterThan(0)
85          );
86          MatcherAssert.assertThat(
87              less.compareTo(less), Matchers.equalTo(0)
88          );
89      }
90  
91      /**
92       * RtPullComment can return its JSON description.
93       * @throws Exception If a problem occurs.
94       */
95      @Test
96      public void canDescribeAsJson() throws Exception {
97          final String body = "{\"body\":\"test\"}";
98          try (
99              final MkContainer container = new MkGrizzlyContainer().next(
100                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body)
101             ).start(this.resource.port())
102         ) {
103             final Pull pull = Mockito.mock(Pull.class);
104             Mockito.doReturn(repo()).when(pull).repo();
105             final RtPullComment comment =
106                 new RtPullComment(new ApacheRequest(container.home()), pull, 1);
107             final JsonObject json = comment.json();
108             MatcherAssert.assertThat(
109                 json.getString("body"),
110                 Matchers.is("test")
111             );
112             MatcherAssert.assertThat(
113                 container.take().uri().toString(),
114                 Matchers.endsWith("/repos/joe/blueharvest/pulls/comments/1")
115             );
116             container.stop();
117         }
118     }
119 
120     /**
121      * RtPullComment can create a patch request.
122      * @throws Exception If a problem occurs.
123      */
124     @Test
125     public void patchesComment() throws Exception {
126         try (
127             final MkContainer container = new MkGrizzlyContainer().next(
128                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
129             ).start(this.resource.port())
130         ) {
131             final Pull pull = Mockito.mock(Pull.class);
132             Mockito.doReturn(repo()).when(pull).repo();
133             final RtPullComment comment =
134                 new RtPullComment(new ApacheRequest(container.home()), pull, 2);
135             final JsonObject json = Json.createObjectBuilder()
136                 .add("body", "test comment").build();
137             comment.patch(json);
138             final MkQuery query = container.take();
139             MatcherAssert.assertThat(
140                 query.method(), Matchers.equalTo(Request.PATCH)
141             );
142             MatcherAssert.assertThat(
143                 query.body(),
144                 Matchers.containsString("{\"body\":\"test comment\"}")
145             );
146             MatcherAssert.assertThat(
147                 query.uri().toString(),
148                 Matchers.endsWith("/repos/joe/blueharvest/pulls/comments/2")
149             );
150             container.stop();
151         }
152     }
153 
154     /**
155      * RtPullComment can add a reaction.
156      * @throws Exception - if anything goes wrong.
157      */
158     @Test
159     @Ignore
160     public void reacts() throws Exception {
161         try (
162             final MkContainer container = new MkGrizzlyContainer().next(
163                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
164             ).start(this.resource.port())) {
165             final Repo repo = new MkGithub().randomRepo();
166             final Pull pull = repo.pulls().create(
167                 "Reaction adding test",
168                 "This is a test for adding a reaction",
169                 "Base"
170             );
171             final RtPullComment comment = new RtPullComment(
172                 new ApacheRequest(container.home()), pull, 2
173             );
174             comment.react(new Reaction.Simple(Reaction.HEART));
175             MatcherAssert.assertThat(
176                 "Pull comment was unable to react",
177                 comment.reactions(),
178                 new IsCollectionWithSize<>(
179                     new IsEqual<>(1)
180                 )
181             );
182         }
183     }
184 
185     /**
186      * This method returns a Repo for testing.
187      * @return Repo - a repo to be used for test.
188      * @throws Exception - if anything goes wrong.
189      */
190     private static Repo repo() throws Exception {
191         return new MkGithub("joe").repos().create(
192             new Repos.RepoCreate("blueharvest", false)
193         );
194     }
195 }