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.google.common.base.Optional;
8   import com.jcabi.http.request.FakeRequest;
9   import jakarta.json.Json;
10  import jakarta.json.JsonObject;
11  import java.io.IOException;
12  import org.hamcrest.MatcherAssert;
13  import org.hamcrest.Matchers;
14  import org.junit.jupiter.api.Test;
15  
16  /**
17   * Test case for {@link RtCommitsComparison}.
18   * @since 0.8
19   */
20  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
21  final class RtCommitsComparisonTest {
22  
23      /**
24       * RtCommitsComparison can fetch JSON.
25       * @checkstyle MultipleStringLiterals (75 lines)
26       * @checkstyle ExecutableStatementCountCheck (75 lines)
27       */
28      @Test
29      void fetchesJson() throws IOException {
30          final String sha = "fffffffffffffffffffffffffffffffffffffffe";
31          final String filename = "bar/quux.txt";
32          // @checkstyle MagicNumberCheck (3 lines)
33          final int additions = 7;
34          final int deletions = 2;
35          final int changes = 9;
36          final String patch = "some diff here";
37          final String bloburl = String.join(
38              "",
39              "https://api.jcabi-github.invalid/johndoe/my-repo/blob/",
40              "fffffffffffffffffffffffffffffffffffffffe/bar/quux.txt"
41          );
42          final String rawurl = String.join(
43              "",
44              "https://api.jcabi-github.invalid/johndoe/my-repo/raw/",
45              "fffffffffffffffffffffffffffffffffffffffe/bar/quux.txt"
46          );
47          final String contentsurl = String.join(
48              "",
49              "https://api.github.invalid/repos/johndoe/my-repo/contents/",
50              "bar/quux.txt?ref=fffffffffffffffffffffffffffffffffffffffe"
51          );
52          final CommitsComparison comparison = new RtCommitsComparison(
53              new FakeRequest().withBody(
54                  Json.createObjectBuilder()
55                      .add("base_commit", Json.createObjectBuilder())
56                      .add("commits", Json.createArrayBuilder())
57                      .add(
58                          "files",
59                          Json.createArrayBuilder()
60                              .add(
61                                  Json.createObjectBuilder()
62                                      .add("sha", sha)
63                                      .add("filename", filename)
64                                      .add("status", "added")
65                                      .add("additions", additions)
66                                      .add("deletions", deletions)
67                                      .add("changes", changes)
68                                      .add("patch", patch)
69                                      .add("blob_url", bloburl)
70                                      .add("raw_url", rawurl)
71                                      .add("contents_url", contentsurl)
72                                      .build()
73                              )
74                              .build()
75                      )
76                      .build().toString()
77              ),
78              RtCommitsComparisonTest.repo(),
79              "6dcb09b5b57875f334f61aebed695e2e4193db51",
80              "6dcb09b5b57875f334f61aebed695e2e4193db52"
81          );
82          final JsonObject json = comparison.json();
83          MatcherAssert.assertThat(
84              "Value is null",
85              json.getJsonObject("base_commit"),
86              Matchers.notNullValue()
87          );
88          MatcherAssert.assertThat(
89              "Value is null",
90              json.getJsonArray("commits"),
91              Matchers.notNullValue()
92          );
93          MatcherAssert.assertThat(
94              "Collection size is incorrect",
95              comparison.files(),
96              Matchers.iterableWithSize(1)
97          );
98          final FileChange.Smart file = new FileChange.Smart(
99              comparison.files().iterator().next()
100         );
101         MatcherAssert.assertThat(
102             "Values are not equal", file.sha(), Matchers.equalTo(sha)
103         );
104         MatcherAssert.assertThat(
105             "Values are not equal", file.filename(), Matchers.equalTo(filename)
106         );
107         MatcherAssert.assertThat(
108             "Values are not equal", file.additions(), Matchers.equalTo(additions)
109         );
110         MatcherAssert.assertThat(
111             "Values are not equal", file.deletions(), Matchers.equalTo(deletions)
112         );
113         MatcherAssert.assertThat(
114             "Values are not equal", file.changes(), Matchers.equalTo(changes)
115         );
116         MatcherAssert.assertThat(
117             "Values are not equal",
118             file.status(),
119             Matchers.equalTo(FileChange.Status.ADDED)
120         );
121         MatcherAssert.assertThat(
122             "Values are not equal",
123             file.patch(),
124             Matchers.equalTo(Optional.of(patch))
125         );
126     }
127 
128     /**
129      * Return repo for tests.
130      * @return Repository
131      */
132     private static Repo repo() {
133         return new RtGitHub().repos()
134             .get(new Coordinates.Simple("user", "repo"));
135     }
136 
137 }