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 jakarta.json.Json;
8   import java.io.IOException;
9   import java.net.MalformedURLException;
10  import java.net.URI;
11  import java.net.URISyntaxException;
12  import org.hamcrest.MatcherAssert;
13  import org.hamcrest.Matchers;
14  import org.junit.jupiter.api.Test;
15  import org.mockito.Mockito;
16  
17  /**
18   * Test case for {@link RepoCommit}.
19   * @since 0.1
20   * @checkstyle MultipleStringLiterals (500 lines)
21   */
22  final class RepoCommitTest {
23  
24      @Test
25      void fetchesUrl() throws IOException, MalformedURLException, URISyntaxException {
26          final RepoCommit commit = Mockito.mock(RepoCommit.class);
27          // @checkstyle LineLength (1 line)
28          final String prop = "https://api.github.com/repos/pengwynn/octokit/contents/README.md";
29          Mockito.doReturn(
30              Json.createObjectBuilder()
31                  .add("url", prop)
32                  .build()
33          ).when(commit).json();
34          MatcherAssert.assertThat(
35              "Values are not equal",
36              new RepoCommit.Smart(commit).url(),
37              Matchers.is(new URI(prop).toURL())
38          );
39      }
40  
41      @Test
42      void fetchesMessage() throws IOException {
43          final RepoCommit commit = Mockito.mock(RepoCommit.class);
44          Mockito.doReturn(
45              Json.createObjectBuilder().add(
46                  "commit",
47                  Json.createObjectBuilder().add("message", "hello, world!")
48              ).build()
49          ).when(commit).json();
50          MatcherAssert.assertThat(
51              "String does not start with expected value",
52              new RepoCommit.Smart(commit).message(),
53              Matchers.startsWith("hello, ")
54          );
55      }
56  
57      /**
58       * RtRepoCommit can verify status.
59       * @throws IOException If fails
60       */
61      @Test
62      void verifiesStatus() throws IOException {
63          final RepoCommit commit = Mockito.mock(RepoCommit.class);
64          Mockito.doReturn(
65              Json.createObjectBuilder().add(
66                  "commit",
67                  Json.createObjectBuilder().add(
68                      "verification",
69                      Json.createObjectBuilder().add("verified", true)
70                  ).build()
71              ).build()
72          ).when(commit).json();
73          MatcherAssert.assertThat(
74              "Values are not equal",
75              new RepoCommit.Smart(commit).isVerified(),
76              Matchers.is(true)
77          );
78      }
79  
80      /**
81       * RtRepoCommit can read author's login.
82       * @throws IOException If fails
83       */
84      @Test
85      void readsAuthorLogin() throws IOException {
86          final RepoCommit commit = Mockito.mock(RepoCommit.class);
87          final String login = "jeff";
88          Mockito.doReturn(
89              Json.createObjectBuilder().add(
90                  "commit",
91                  Json.createObjectBuilder().add(
92                      "author",
93                      Json.createObjectBuilder().add("name", login)
94                  ).build()
95              ).build()
96          ).when(commit).json();
97          MatcherAssert.assertThat(
98              "Values are not equal",
99              new RepoCommit.Smart(commit).author(),
100             Matchers.equalTo(login)
101         );
102     }
103 }