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.mock;
31  
32  import com.jcabi.github.PullComment;
33  import com.jcabi.github.PullComments;
34  import com.jcabi.github.Repo;
35  import com.jcabi.github.Repos;
36  import java.io.IOException;
37  import java.util.Collections;
38  import java.util.List;
39  import javax.json.JsonObject;
40  import org.hamcrest.MatcherAssert;
41  import org.hamcrest.Matchers;
42  import org.junit.Test;
43  
44  /**
45   * Test case for {@link MkPullComments}.
46   *
47   * @author Carlos Miranda (miranda.cma@gmail.com)
48   * @version $Id: fa6c2953b65942272e9ddcb92d1a57332f9a3bca $
49   */
50  public final class MkPullCommentsTest {
51  
52      /**
53       * MkPullComments can fetch a single comment.
54       *
55       * @throws Exception If something goes wrong.
56       */
57      @Test
58      public void fetchesPullComment() throws Exception {
59          final PullComments comments = this.comments();
60          final PullComment comment = comments.post("comment", "commit", "/", 1);
61          MatcherAssert.assertThat(
62              comments.get(comment.number()).number(),
63              Matchers.equalTo(comment.number())
64          );
65      }
66  
67      /**
68       * MkPullComments can fetch all pull comments for a repo.
69       *
70       * @throws Exception If something goes wrong.
71       */
72      @Test
73      public void iteratesRepoPullComments() throws Exception {
74          final PullComments comments = comments();
75          comments.pull()
76              .repo()
77              .pulls()
78              .create("new", "head-branch", "base-branch")
79              .comments()
80              .post("new pull comment", "new commit", "/p", 1);
81          comments.post("test 1", "tesst 1", "/test1", 1);
82          MatcherAssert.assertThat(
83              comments.iterate(
84                  comments.pull().number(),
85                  Collections.<String, String>emptyMap()
86              ),
87              Matchers.<PullComment>iterableWithSize(1)
88          );
89          MatcherAssert.assertThat(
90              comments.iterate(
91                  Collections.<String, String>emptyMap()
92              ),
93              Matchers.<PullComment>iterableWithSize(2)
94          );
95      }
96  
97      /**
98       * MkPullComments can fetch pull comments for a pull request.
99       *
100      * @throws Exception If something goes wrong.
101      */
102     @Test
103     public void iteratesPullRequestComments() throws Exception {
104         final PullComments comments = comments();
105         comments.post("comment 1", "commit 1", "/commit1", 1);
106         comments.post("comment 2", "commit 2", "/commit2", 2);
107         MatcherAssert.assertThat(
108             comments.iterate(
109                 comments.pull().number(),
110                 Collections.<String, String>emptyMap()
111             ),
112             Matchers.<PullComment>iterableWithSize(2)
113         );
114     }
115 
116     /**
117      * MkPullComments can create a pull comment.
118      *
119      * @throws Exception If something goes wrong.
120      */
121     @Test
122     public void postsPullComment() throws Exception {
123         final MkStorage storage = new MkStorage.InFile();
124         final String commit = "commit_id";
125         final String path = "path";
126         final String bodytext = "some text as a body";
127         final String login = "jamie";
128         final String reponame = "incredible";
129         final Repo repo = new MkGithub(storage, login).repos().create(
130             new Repos.RepoCreate(reponame, false)
131         );
132         repo.pulls()
133             .create("pullrequest1", "head", "base").comments()
134             .post(bodytext, commit, path, 1);
135         final String[] fields = {commit, path};
136         for (final String element : fields) {
137             MkPullCommentsTest.assertFieldContains(storage, repo, element);
138         }
139         final List<String> position = storage.xml().xpath(
140             String.format(
141                 // @checkstyle LineLength (1 line)
142                 "/github/repos/repo[@coords='%s/%s']/pulls/pull/comments/comment/position/text()",
143                 repo.coordinates().user(),
144                 repo.coordinates().repo()
145             )
146         );
147         MatcherAssert.assertThat(
148             position.get(0), Matchers.notNullValue()
149         );
150         final List<String> body = storage.xml().xpath(
151             String.format(
152                 // @checkstyle LineLength (1 line)
153                 "/github/repos/repo[@coords='%s/%s']/pulls/pull/comments/comment/body/text()",
154                 repo.coordinates().user(),
155                 repo.coordinates().repo()
156             )
157         );
158         MatcherAssert.assertThat(body.get(0), Matchers.equalTo(bodytext));
159     }
160 
161     /**
162      * MkPullComments can reply to an existing pull comment.
163      *
164      * @throws Exception If something goes wrong.
165      */
166     @Test
167     public void createsPullCommentReply() throws Exception {
168         final PullComments comments = this.comments();
169         final int orig = comments.post(
170             "Orig Comment",
171             "6dcb09b5b57875f334f61aebed695e2e4193db5e",
172             "file1.txt",
173             1
174         ).number();
175         final String body = "Reply Comment";
176         final JsonObject reply = comments.reply(body, orig).json();
177         MatcherAssert.assertThat(
178             reply.getString("body"),
179             Matchers.is(body)
180         );
181         MatcherAssert.assertThat(
182             reply.getString("original_position"),
183             Matchers.is(Integer.toString(orig))
184         );
185     }
186 
187     /**
188      * MkPullComments can remove a pull comment.
189      *
190      * @throws Exception If something goes wrong.
191      */
192     @Test
193     public void removesPullComment() throws Exception {
194         final PullComments comments = this.comments();
195         final int orig = comments.post(
196             "Origg Comment",
197             "6dcb09b5b57875f334f61aebed695e2e4193db5d",
198             "file2.txt",
199             1
200         ).number();
201         MatcherAssert.assertThat(
202             comments.iterate(
203                 orig, Collections.<String, String>emptyMap()
204             ),
205             Matchers.<PullComment>iterableWithSize(1)
206         );
207         comments.remove(orig);
208         MatcherAssert.assertThat(
209             comments.iterate(
210                 orig, Collections.<String, String>emptyMap()
211             ),
212             Matchers.<PullComment>iterableWithSize(0)
213         );
214     }
215 
216     /**
217      * Generate pull comments for test.
218      * @return The pull comments
219      * @throws IOException If an IO Exception occurs
220      */
221     private PullComments comments() throws IOException {
222         // @checkstyle MultipleStringLiteralsCheck (1 line)
223         return new MkGithub().randomRepo().pulls()
224             .create("hello", "awesome-head", "awesome-base")
225             .comments();
226     }
227 
228     /**
229      * Assert if fields doesn't contain value.
230      * @param storage The storage
231      * @param repo The repo
232      * @param element The element to be tested and the value.
233      * @throws IOException If any I/O error occurs.
234      */
235     private static void assertFieldContains(
236         final MkStorage storage,
237         final Repo repo,
238         final String element) throws IOException {
239         final String xpath = String.format(
240             // @checkstyle LineLength (1 line)
241             "/github/repos/repo[@coords='%s/%s']/pulls/pull/comments/comment/%s/text()",
242             repo.coordinates().user(),
243             repo.coordinates().repo(),
244             element
245         );
246         MatcherAssert.assertThat(
247             storage.xml().xpath(xpath).get(0),
248             Matchers.is(element)
249         );
250     }
251 
252 }