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 java.io.IOException;
34  import javax.json.Json;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.Test;
38  import org.mockito.Mockito;
39  
40  /**
41   * Tests for {@link Pull}.
42   * @author Paul Polishchuk (ppol@ua.fm)
43   * @version $Id: 4a6acdedec19eda4b060fe7ae72500a6ea261c19 $
44   * @checkstyle MultipleStringLiterals (500 lines)
45   */
46  public final class PullTest {
47  
48      /**
49       * Pull.Smart can fetch comments count from Pull.
50       *
51       * @throws Exception If some problem inside
52       */
53      @Test
54      public void canFetchCommentsCount() throws Exception {
55          final int number = 1;
56          final Pull pull = Mockito.mock(Pull.class);
57          Mockito.doReturn(
58              Json.createObjectBuilder()
59                  .add("comments", number)
60                  .build()
61          ).when(pull).json();
62          MatcherAssert.assertThat(
63              new Pull.Smart(pull).commentsCount(),
64              Matchers.is(number)
65          );
66      }
67  
68      /**
69       * Pull.Smart can get an issue where the pull request is submitted.
70       */
71      @Test
72      public void getsIssue() {
73          final int number = 2;
74          final Issue issue = Mockito.mock(Issue.class);
75          Mockito.when(issue.number()).thenReturn(number);
76          final Issues issues = Mockito.mock(Issues.class);
77          Mockito.when(issues.get(2)).thenReturn(issue);
78          final Repo repo = Mockito.mock(Repo.class);
79          Mockito.when(repo.issues()).thenReturn(issues);
80          final Pull pull = Mockito.mock(Pull.class);
81          Mockito.when(pull.number()).thenReturn(number);
82          Mockito.when(pull.repo()).thenReturn(repo);
83          MatcherAssert.assertThat(
84              new Pull.Smart(pull).issue().number(),
85              Matchers.equalTo(number)
86          );
87      }
88  
89      /**
90       * Pull.Smart can get pull comments.
91       * @throws IOException If some problem inside
92       */
93      @Test
94      public void getsPullComments() throws IOException {
95          final PullComments pullComments = Mockito.mock(PullComments.class);
96          final Pull pull = Mockito.mock(Pull.class);
97          Mockito.when(pull.comments()).thenReturn(pullComments);
98          MatcherAssert.assertThat(
99              new Pull.Smart(pull).comments(),
100             Matchers.equalTo(pullComments)
101         );
102     }
103 
104     /**
105      * Pull.Smart can get the author.
106      * @throws IOException If some problem inside
107      */
108     @Test
109     public void getsAuthor() throws IOException {
110         final String login = "rose";
111         final Repo repo = Mockito.mock(Repo.class);
112         Mockito.when(repo.github()).thenReturn(new MkGithub());
113         final Pull pull = Mockito.mock(Pull.class);
114         Mockito.when(pull.json()).thenReturn(
115             Json.createObjectBuilder()
116                 .add(
117                     "user",
118                     Json.createObjectBuilder()
119                         .add("login", login)
120                         .build()
121                 )
122                 .build()
123         );
124         Mockito.when(pull.repo()).thenReturn(repo);
125         MatcherAssert.assertThat(
126             new Pull.Smart(pull).author().login(),
127             Matchers.equalTo(login)
128         );
129     }
130 }