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 PullRef}.
17   * @since 0.24
18   */
19  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
20  final class PullRefTest {
21      /**
22       * Test ref.
23       */
24      private static final String REF = "the-ref";
25  
26      /**
27       * Test commit SHA.
28       * @checkstyle LineLength (2 lines)
29       */
30      private static final String SHA = "7a1f68e743e8a81e158136c8661011fb55abd703";
31  
32      /**
33       * Test pull request ref label.
34       */
35      private static final String LABEL = "pr-ref-label";
36  
37      /**
38       * PullRef.Smart can fetch its repo.
39       * @throws IOException If there is an I/O problem.
40       */
41      @Test
42      void fetchesRepo() throws IOException {
43          final Repo repo = new MkGitHub().randomRepo();
44          MatcherAssert.assertThat(
45              "Values are not equal",
46              PullRefTest.pullRef(repo).repo().coordinates(),
47              Matchers.equalTo(repo.coordinates())
48          );
49      }
50  
51      /**
52       * PullRef.Smart can fetch its ref name.
53       * @throws IOException If there is an I/O problem.
54       */
55      @Test
56      void fetchesRef() throws IOException {
57          MatcherAssert.assertThat(
58              "Values are not equal",
59              PullRefTest.pullRef().ref(),
60              Matchers.equalTo(PullRefTest.REF)
61          );
62      }
63  
64      /**
65       * PullRef.Smart can fetch its commit sha.
66       * @throws IOException If there is an I/O problem.
67       */
68      @Test
69      void fetchesSha() throws IOException {
70          MatcherAssert.assertThat(
71              "Values are not equal",
72              PullRefTest.pullRef().sha(),
73              Matchers.equalTo(PullRefTest.SHA)
74          );
75      }
76  
77      /**
78       * PullRef.Smart can fetch its label.
79       * @throws IOException If there is an I/O problem.
80       */
81      @Test
82      void fetchesLabel() throws IOException {
83          MatcherAssert.assertThat(
84              "Values are not equal",
85              PullRefTest.pullRef().label(),
86              Matchers.equalTo(PullRefTest.LABEL)
87          );
88      }
89  
90      /**
91       * PullRef.Smart can fetch its commit.
92       * @throws IOException If there is an I/O problem.
93       */
94      @Test
95      void fetchesCommit() throws IOException {
96          final Repo repo = new MkGitHub().randomRepo();
97          final Commit commit = PullRefTest.pullRef(repo).commit();
98          MatcherAssert.assertThat(
99              "Values are not equal",
100             commit.repo().coordinates(),
101             Matchers.equalTo(repo.coordinates())
102         );
103         MatcherAssert.assertThat(
104             "Values are not equal",
105             commit.sha(),
106             Matchers.equalTo(PullRefTest.SHA)
107         );
108     }
109 
110     /**
111      * PullRef.Smart can fetch its user.
112      * @throws IOException If there is an I/O problem.
113      */
114     @Test
115     void fetchesUser() throws IOException {
116         final Repo repo = new MkGitHub().randomRepo();
117         MatcherAssert.assertThat(
118             "Values are not equal",
119             PullRefTest.pullRef(repo).user().login(),
120             Matchers.equalTo(repo.coordinates().user())
121         );
122     }
123 
124     /**
125      * Returns a smart pull request ref in the given repo for testing.
126      * @param repo Repo to create the pull request ref in
127      * @return PullRef.Smart
128      */
129     private static PullRef.Smart pullRef(final Repo repo) {
130         final Coordinates coords = repo.coordinates();
131         final JsonObject user = Json.createObjectBuilder()
132             .add("login", coords.user())
133             .build();
134         return new PullRef.Smart(
135             new RtPullRef(
136                 repo.github(),
137                 Json.createObjectBuilder()
138                     .add("label", PullRefTest.LABEL)
139                     .add("ref", PullRefTest.REF)
140                     .add("sha", PullRefTest.SHA)
141                     .add("user", user)
142                     .add(
143                         "repo",
144                         Json.createObjectBuilder()
145                             .add("name", coords.repo())
146                             .add("full_name", coords.toString())
147                             .add("owner", user)
148                             .build()
149                     ).build()
150             )
151         );
152     }
153 
154     /**
155      * Returns a smart pull request ref for testing.
156      * @return PullRef.Smart
157      * @throws IOException If there is an I/O problem.
158      */
159     private static PullRef.Smart pullRef() throws IOException {
160         return PullRefTest.pullRef(new MkGitHub().randomRepo());
161     }
162 }