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 javax.json.JsonObject;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.Matchers;
38  import org.junit.Test;
39  
40  /**
41   * Test case for {@link PullRef}.
42   *
43   * @author Chris Rebert (github@rebertia.com)
44   * @version $Id: 1bcd3f4cd2c2457950b32efedbc88fc73d534595 $
45   * @since 0.24
46   */
47  public final class PullRefTest {
48      /**
49       * Test ref.
50       */
51      private static final String REF = "the-ref";
52      /**
53       * Test commit SHA.
54       * @checkstyle LineLength (2 lines)
55       */
56      private static final String SHA = "7a1f68e743e8a81e158136c8661011fb55abd703";
57      /**
58       * Test pull request ref label.
59       */
60      private static final String LABEL = "pr-ref-label";
61  
62      /**
63       * PullRef.Smart can fetch its repo.
64       * @throws IOException If there is an I/O problem.
65       */
66      @Test
67      public void fetchesRepo() throws IOException {
68          final Repo repo = new MkGithub().randomRepo();
69          MatcherAssert.assertThat(
70              PullRefTest.pullRef(repo).repo().coordinates(),
71              Matchers.equalTo(repo.coordinates())
72          );
73      }
74  
75      /**
76       * PullRef.Smart can fetch its ref name.
77       * @throws IOException If there is an I/O problem.
78       */
79      @Test
80      public void fetchesRef() throws IOException {
81          MatcherAssert.assertThat(
82              PullRefTest.pullRef().ref(),
83              Matchers.equalTo(PullRefTest.REF)
84          );
85      }
86  
87      /**
88       * PullRef.Smart can fetch its commit sha.
89       * @throws IOException If there is an I/O problem.
90       */
91      @Test
92      public void fetchesSha() throws IOException {
93          MatcherAssert.assertThat(
94              PullRefTest.pullRef().sha(),
95              Matchers.equalTo(PullRefTest.SHA)
96          );
97      }
98  
99      /**
100      * PullRef.Smart can fetch its label.
101      * @throws IOException If there is an I/O problem.
102      */
103     @Test
104     public void fetchesLabel() throws IOException {
105         MatcherAssert.assertThat(
106             PullRefTest.pullRef().label(),
107             Matchers.equalTo(PullRefTest.LABEL)
108         );
109     }
110 
111     /**
112      * PullRef.Smart can fetch its commit.
113      * @throws IOException If there is an I/O problem.
114      */
115     @Test
116     public void fetchesCommit() throws IOException {
117         final Repo repo = new MkGithub().randomRepo();
118         final Commit commit = PullRefTest.pullRef(repo).commit();
119         MatcherAssert.assertThat(
120             commit.repo().coordinates(),
121             Matchers.equalTo(repo.coordinates())
122         );
123         MatcherAssert.assertThat(
124             commit.sha(),
125             Matchers.equalTo(PullRefTest.SHA)
126         );
127     }
128 
129     /**
130      * PullRef.Smart can fetch its user.
131      * @throws IOException If there is an I/O problem.
132      */
133     @Test
134     public void fetchesUser() throws IOException {
135         final Repo repo = new MkGithub().randomRepo();
136         MatcherAssert.assertThat(
137             PullRefTest.pullRef(repo).user().login(),
138             Matchers.equalTo(repo.coordinates().user())
139         );
140     }
141 
142     /**
143      * Returns a smart pull request ref in the given repo for testing.
144      * @param repo Repo to create the pull request ref in
145      * @return PullRef.Smart
146      * @throws IOException If there is an I/O problem.
147      */
148     private static PullRef.Smart pullRef(final Repo repo) throws IOException {
149         final Coordinates coords = repo.coordinates();
150         final JsonObject user = Json.createObjectBuilder()
151             .add("login", coords.user())
152             .build();
153         return new PullRef.Smart(
154             new RtPullRef(
155                 repo.github(),
156                 Json.createObjectBuilder()
157                     .add("label", PullRefTest.LABEL)
158                     .add("ref", PullRefTest.REF)
159                     .add("sha", PullRefTest.SHA)
160                     .add("user", user)
161                     .add(
162                         "repo",
163                         Json.createObjectBuilder()
164                             .add("name", coords.repo())
165                             .add("full_name", coords.toString())
166                             .add("owner", user)
167                             .build()
168                     ).build()
169             )
170         );
171     }
172 
173     /**
174      * Returns a smart pull request ref for testing.
175      * @return PullRef.Smart
176      * @throws IOException If there is an I/O problem.
177      */
178     private static PullRef.Smart pullRef() throws IOException {
179         return PullRefTest.pullRef(new MkGithub().randomRepo());
180     }
181 }