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.http.request.FakeRequest;
8   import jakarta.json.Json;
9   import java.io.IOException;
10  import java.util.Collections;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Test case for {@link RtRepoCommits}.
17   * @since 0.1
18   */
19  final class RtRepoCommitsTest {
20  
21      /**
22       * RtRepoCommits can return commits' iterator.
23       */
24      @Test
25      void returnIterator() {
26          final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db51";
27          final RepoCommits commits = new RtRepoCommits(
28              new FakeRequest().withBody(
29                  Json.createArrayBuilder().add(
30                      // @checkstyle MultipleStringLiterals (1 line)
31                      Json.createObjectBuilder().add("sha", sha)
32                  ).build().toString()
33              ),
34              RtRepoCommitsTest.repo()
35          );
36          MatcherAssert.assertThat(
37              "Values are not equal",
38              commits.iterate(
39                  Collections.emptyMap()
40              ).iterator().next().sha(),
41              Matchers.equalTo(sha)
42          );
43      }
44  
45      @Test
46      void getCommit() {
47          final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db52";
48          final RepoCommits commits = new RtRepoCommits(
49              new FakeRequest().withBody(
50                  Json.createObjectBuilder()
51                      .add("sha", sha)
52                      .build()
53                      .toString()
54              ),
55              RtRepoCommitsTest.repo()
56          );
57          MatcherAssert.assertThat(
58              "Values are not equal",
59              commits.get(sha).sha(),
60              Matchers.equalTo(sha)
61          );
62      }
63  
64      @Test
65      void comparesCommits() {
66          final RepoCommits commits = new RtRepoCommits(
67              new FakeRequest().withBody(
68                  Json.createObjectBuilder()
69                      .add("base_commit", Json.createObjectBuilder())
70                      .add("commits", Json.createArrayBuilder())
71                      .add("files", Json.createArrayBuilder())
72                      .build().toString()
73              ),
74              RtRepoCommitsTest.repo()
75          );
76          MatcherAssert.assertThat(
77              "Value is null",
78              commits.compare(
79                  "6dcb09b5b57875f334f61aebed695e2e4193db53",
80                  "6dcb09b5b57875f334f61aebed695e2e4193db54"
81              ),
82              Matchers.notNullValue(CommitsComparison.class)
83          );
84      }
85  
86      @Test
87      void comparesCommitsDiffFormat() throws IOException {
88          final RepoCommits commits = new RtRepoCommits(
89              new FakeRequest().withBody("diff --git"),
90              RtRepoCommitsTest.repo()
91          );
92          MatcherAssert.assertThat(
93              "String does not start with expected value",
94              commits.diff(
95                  "6dcb09b5b57875f334f61aebed695e2e4193db55",
96                  "6dcb09b5b57875f334f61aebed695e2e4193db56"
97              ),
98              Matchers.startsWith("diff")
99          );
100     }
101 
102     @Test
103     void comparesCommitsPatchFormat() throws IOException {
104         final RepoCommits commits = new RtRepoCommits(
105             new FakeRequest().withBody(
106                 "From 6dcb09b5b57875f33"
107             ),
108             RtRepoCommitsTest.repo()
109         );
110         MatcherAssert.assertThat(
111             "String does not start with expected value",
112             commits.patch(
113                 "6dcb09b5b57875f334f61aebed695e2e4193db57",
114                 "6dcb09b5b57875f334f61aebed695e2e4193db58"
115             ),
116             Matchers.startsWith("From")
117         );
118     }
119 
120     @Test
121     void readCorrectUrl() {
122         MatcherAssert.assertThat(
123             "String does not end with expected value",
124             new RtRepoCommits(new FakeRequest(), RtRepoCommitsTest.repo())
125                 .compare("base", "head").toString(),
126             Matchers.endsWith(
127                 "/see-FakeRequest-class/repos/user/repo/compare/base...head"
128             )
129         );
130     }
131 
132     /**
133      * Create repository for tests.
134      * @return Repository
135      */
136     private static Repo repo() {
137         return new RtGitHub().repos()
138             .get(new Coordinates.Simple("user", "repo"));
139     }
140 
141 }