View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github;
6   
7   import com.jcabi.github.mock.MkGitHub;
8   import jakarta.json.Json;
9   import jakarta.json.JsonObject;
10  import java.io.IOException;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Test case for {@link RtPullRef}.
17   * @since 0.24
18   */
19  final class RtPullRefTest {
20      /**
21       * Test commit SHA.
22       */
23      private static final String SHA =
24          "7a1f68e743e8a81e158136c8661011fb55abd703";
25  
26      /**
27       * Test ref.
28       */
29      private static final String REF = "some-branch";
30  
31      /**
32       * RtPullRef can fetch its repo.
33       * @throws IOException If there is an I/O problem.
34       */
35      @Test
36      void fetchesRepo() throws IOException {
37          final Repo repo = new MkGitHub().randomRepo();
38          MatcherAssert.assertThat(
39              "Values are not equal",
40              RtPullRefTest.pullRef(repo).repo().coordinates(),
41              Matchers.equalTo(repo.coordinates())
42          );
43      }
44  
45      /**
46       * RtPullRef can fetch its ref.
47       * @throws IOException If there is an I/O problem.
48       */
49      @Test
50      void fetchesRef() throws IOException {
51          MatcherAssert.assertThat(
52              "Values are not equal",
53              RtPullRefTest.pullRef().ref(),
54              Matchers.equalTo(RtPullRefTest.REF)
55          );
56      }
57  
58      /**
59       * RtPullRef can fetch its commit SHA.
60       * @throws IOException If there is an I/O problem.
61       */
62      @Test
63      void fetchesSha() throws IOException {
64          MatcherAssert.assertThat(
65              "Values are not equal",
66              RtPullRefTest.pullRef().sha(),
67              Matchers.equalTo(RtPullRefTest.SHA)
68          );
69      }
70  
71      /**
72       * Returns an RtPullRef for testing.
73       * @return Test RtPullRef
74       * @throws IOException If there is an I/O problem.
75       */
76      private static PullRef pullRef() throws IOException {
77          return RtPullRefTest.pullRef(new MkGitHub().randomRepo());
78      }
79  
80      /**
81       * Returns an RtPullRef in the given repo for testing.
82       * @param repo Repo to create the pull request ref in
83       * @return Test RtPullRef
84       */
85      private static PullRef pullRef(final Repo repo) {
86          final Coordinates coords = repo.coordinates();
87          final JsonObject user = Json.createObjectBuilder()
88              .add("login", coords.user())
89              .build();
90          return new RtPullRef(
91              repo.github(),
92              Json.createObjectBuilder()
93                  .add("ref", RtPullRefTest.REF)
94                  .add("sha", RtPullRefTest.SHA)
95                  .add("user", user)
96                  .add(
97                      "repo",
98                      Json.createObjectBuilder()
99                          .add("owner", user)
100                         .add("name", coords.repo())
101                         .add("full_name", coords.toString())
102                         .build()
103                 ).build()
104         );
105     }
106 }