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 RtPullRef}.
42   *
43   * @author Chris Rebert (github@rebertia.com)
44   * @version $Id: 24e8b43dcd22f39fe10acbe29b3f49e882136ef2 $
45   * @since 0.24
46   */
47  public final class RtPullRefTest {
48      /**
49       * Test commit SHA.
50       */
51      private static final String SHA =
52          "7a1f68e743e8a81e158136c8661011fb55abd703";
53      /**
54       * Test ref.
55       */
56      private static final String REF = "some-branch";
57  
58      /**
59       * RtPullRef can fetch its repo.
60       * @throws IOException If there is an I/O problem.
61       */
62      @Test
63      public void fetchesRepo() throws IOException {
64          final Repo repo = new MkGithub().randomRepo();
65          MatcherAssert.assertThat(
66              RtPullRefTest.pullRef(repo).repo().coordinates(),
67              Matchers.equalTo(repo.coordinates())
68          );
69      }
70  
71      /**
72       * RtPullRef can fetch its ref.
73       * @throws IOException If there is an I/O problem.
74       */
75      @Test
76      public void fetchesRef() throws IOException {
77          MatcherAssert.assertThat(
78              RtPullRefTest.pullRef().ref(),
79              Matchers.equalTo(REF)
80          );
81      }
82  
83      /**
84       * RtPullRef can fetch its commit SHA.
85       * @throws IOException If there is an I/O problem.
86       */
87      @Test
88      public void fetchesSha() throws IOException {
89          MatcherAssert.assertThat(
90              RtPullRefTest.pullRef().sha(),
91              Matchers.equalTo(SHA)
92          );
93      }
94  
95      /**
96       * Returns an RtPullRef for testing.
97       * @return Test RtPullRef
98       * @throws IOException If there is an I/O problem.
99       */
100     private static PullRef pullRef() throws IOException {
101         return RtPullRefTest.pullRef(new MkGithub().randomRepo());
102     }
103 
104     /**
105      * Returns an RtPullRef in the given repo for testing.
106      * @param repo Repo to create the pull request ref in
107      * @return Test RtPullRef
108      */
109     private static PullRef pullRef(final Repo repo) {
110         final Coordinates coords = repo.coordinates();
111         final JsonObject user = Json.createObjectBuilder()
112             .add("login", coords.user())
113             .build();
114         return new RtPullRef(
115             repo.github(),
116             Json.createObjectBuilder()
117                 .add("ref", REF)
118                 .add("sha", SHA)
119                 .add("user", user)
120                 .add(
121                     "repo",
122                     Json.createObjectBuilder()
123                         .add("owner", user)
124                         .add("name", coords.repo())
125                         .add("full_name", coords.toString())
126                         .build()
127                 ).build()
128         );
129     }
130 }