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.OAuthScope.Scope;
33  import com.jcabi.log.Logger;
34  import java.util.Date;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.hamcrest.core.IsEqual;
38  import org.junit.AfterClass;
39  import org.junit.Assume;
40  import org.junit.BeforeClass;
41  import org.junit.Ignore;
42  import org.junit.Test;
43  
44  /**
45   * Integration case for {@link Issue}.
46   * @author Yegor Bugayenko (yegor256@gmail.com)
47   * @version $Id: a3c935ee8df4f70461539d38b13cc9e8ce055c48 $
48   * @checkstyle ClassDataAbstractionCoupling (500 lines)
49   */
50  @OAuthScope(Scope.REPO)
51  public final class RtIssueITCase {
52      /**
53       * Test repos.
54       */
55      private static Repos repos;
56  
57      /**
58       * Test repo.
59       */
60      private static Repo repo;
61  
62      /**
63       * Set up test fixtures.
64       * @throws Exception If some errors occurred.
65       */
66      @BeforeClass
67      public static void setUp() throws Exception {
68          final Github github = new GithubIT().connect();
69          repos = github.repos();
70          repo = new RepoRule().repo(repos);
71      }
72  
73      /**
74       * Tear down test fixtures.
75       * @throws Exception If some errors occurred.
76       */
77      @AfterClass
78      public static void tearDown() throws Exception {
79          if (repos != null && repo != null) {
80              repos.remove(repo.coordinates());
81          }
82      }
83  
84      /**
85       * RtIssue can talk in github. This test is ignored because of bug
86       * https://github.com/jcabi/jcabi-github/issues/1178.
87       * @throws Exception If some problem inside
88       */
89      @Ignore
90      @Test
91      public void talksInGithubProject() throws Exception {
92          final Issue issue = RtIssueITCase.issue();
93          final Comment comment = issue.comments().post("hey, works?");
94          MatcherAssert.assertThat(
95              new Comment.Smart(comment).body(),
96              Matchers.startsWith("hey, ")
97          );
98          MatcherAssert.assertThat(
99              issue.comments().iterate(new Date(0L)),
100             Matchers.<Comment>iterableWithSize(1)
101         );
102         final User.Smart author = new User.Smart(
103             new Comment.Smart(comment)
104                 .author()
105         );
106         final User.Smart self = new User.Smart(
107             issue.repo().github().users().self()
108         );
109         if (author.hasName() && self.hasName()) {
110             MatcherAssert.assertThat(
111                 author.name(),
112                 Matchers.equalTo(
113                     self.name()
114                 )
115             );
116         }
117         comment.remove();
118     }
119 
120     /**
121      * RtIssue can change title and body.
122      * @throws Exception If some problem inside
123      */
124     @Test
125     public void changesTitleAndBody() throws Exception {
126         final Issue issue = RtIssueITCase.issue();
127         new Issue.Smart(issue).title("test one more time");
128         MatcherAssert.assertThat(
129             new Issue.Smart(issue).title(),
130             Matchers.startsWith("test o")
131         );
132         new Issue.Smart(issue).body("some new body of the issue");
133         MatcherAssert.assertThat(
134             new Issue.Smart(issue).body(),
135             Matchers.startsWith("some new ")
136         );
137     }
138 
139     /**
140      * RtIssue can change issue state.
141      * @throws Exception If some problem inside
142      */
143     @Test
144     public void changesIssueState() throws Exception {
145         final Issue issue = RtIssueITCase.issue();
146         new Issue.Smart(issue).close();
147         MatcherAssert.assertThat(
148             new Issue.Smart(issue).isOpen(),
149             Matchers.is(false)
150         );
151         new Issue.Smart(issue).open();
152         MatcherAssert.assertThat(
153             new Issue.Smart(issue).isOpen(),
154             Matchers.is(true)
155         );
156     }
157 
158     /**
159      * RtIssue can fetch assignee.
160      *
161      * <p> If you get AssertionError during this test execution and test was
162      *  ignored it means that something happened with account that you try to
163      *  edit with Issue.assign(). We had this problem when our account was
164      *  flagged as suspicious by Github. In this case you should contact Github
165      *  support and ask them to unblock account you use.
166      *
167      * @see <a href="https://github.com/jcabi/jcabi-github/issues/810">Why test is ignored?</a>
168      * @throws Exception if any problem inside.
169      */
170     @Test
171     public void identifyAssignee() throws Exception {
172         final Issue issue = RtIssueITCase.issue();
173         final String login = issue.repo().github().users().self().login();
174         try {
175             new Issue.Smart(issue).assign(login);
176         } catch (final AssertionError error) {
177             Logger.warn(this, "Test failed with error: %s", error.getMessage());
178             Assume.assumeFalse(
179                 "Something wrong with your test account. Read test's java-doc.",
180                 true
181             );
182         }
183         final User assignee = new Issue.Smart(issue).assignee();
184         MatcherAssert.assertThat(
185             assignee.login(),
186             Matchers.equalTo(login)
187         );
188     }
189     /**
190      * RtIssue can check whether it is a pull request.
191      * @throws Exception If some problem inside
192      */
193     @Test
194     public void checksForPullRequest() throws Exception {
195         final Issue issue = RtIssueITCase.issue();
196         MatcherAssert.assertThat(
197             new Issue.Smart(issue).isPull(),
198             Matchers.is(false)
199         );
200     }
201 
202     /**
203      * GhIssue can list issue events.
204      * @throws Exception If some problem inside
205      */
206     @Test
207     public void listsIssueEvents() throws Exception {
208         final Issue issue = RtIssueITCase.issue();
209         new Issue.Smart(issue).close();
210         MatcherAssert.assertThat(
211             new Event.Smart(issue.events().iterator().next()).type(),
212             Matchers.equalTo(Event.CLOSED)
213         );
214     }
215 
216     /**
217      * Issue.Smart can find the latest event.
218      * @throws Exception If some problem inside
219      */
220     @Test
221     public void findsLatestEvent() throws Exception {
222         final Issue.Smart issue = new Issue.Smart(RtIssueITCase.issue());
223         issue.close();
224         MatcherAssert.assertThat(
225             new Event.Smart(
226                 new Issue.Smart(issue).latestEvent(Event.CLOSED)
227             ).author().login(),
228             Matchers.equalTo(issue.author().login())
229         );
230     }
231 
232     /**
233      * RtIssue always exists in Github.
234      *
235      * @throws Exception when a problem occurs.
236      */
237     @Test
238     public void issueAlwaysExistsInGithub() throws Exception {
239         MatcherAssert.assertThat(
240             new Issue.Smart(RtIssueITCase.issue()).exists(), Matchers.is(true)
241         );
242     }
243 
244     /**
245      * RtIssue can lock conversation.
246      * @throws Exception If some problem inside
247      */
248     @Test
249     public void locks() throws Exception {
250         final Issue issue = new Issue.Smart(RtIssueITCase.issue());
251         issue.lock("off-topic");
252         MatcherAssert.assertThat(
253             issue.isLocked(),
254             new IsEqual<>(true)
255         );
256     }
257 
258     /**
259      * RtIssue can unlock conversation.
260      * @throws Exception If some problem inside
261      */
262     @Test
263     public void unlocks() throws Exception {
264         final Issue issue = new Issue.Smart(RtIssueITCase.issue());
265         issue.lock("too heated");
266         issue.unlock();
267         MatcherAssert.assertThat(
268             issue.isLocked(),
269             new IsEqual<>(false)
270         );
271     }
272 
273     /**
274      * Create and return issue to test.
275      * @return Issue
276      * @throws Exception If some problem inside
277      */
278     private static Issue issue() throws Exception {
279         return repo.issues().create("test issue title", "test issue body");
280     }
281 
282 }