View Javadoc
1   /**
2    * Copyright (c) 2013-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github;
31  
32  import com.jcabi.http.request.FakeRequest;
33  import java.util.Collections;
34  import javax.json.Json;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.Test;
38  
39  /**
40   * Test case for {@link RtRepoCommits}.
41   * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
42   * @version $Id: 346112b7a7c64352bb5084de30e952a4fd6e2dc0 $
43   */
44  public final class RtRepoCommitsTest {
45  
46      /**
47       * RtRepoCommits can return commits' iterator.
48       */
49      @Test
50      public void returnIterator() {
51          final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db51";
52          final RepoCommits commits = new RtRepoCommits(
53              new FakeRequest().withBody(
54                  Json.createArrayBuilder().add(
55                      // @checkstyle MultipleStringLiterals (1 line)
56                      Json.createObjectBuilder().add("sha", sha)
57                  ).build().toString()
58              ),
59              RtRepoCommitsTest.repo()
60          );
61          MatcherAssert.assertThat(
62              commits.iterate(
63                  Collections.<String, String>emptyMap()
64              ).iterator().next().sha(),
65              Matchers.equalTo(sha)
66          );
67      }
68  
69      /**
70       * RtRepoCommits can get commit.
71       */
72      @Test
73      public void getCommit() {
74          final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db52";
75          final RepoCommits commits = new RtRepoCommits(
76              new FakeRequest().withBody(
77                  Json.createObjectBuilder()
78                      .add("sha", sha)
79                      .build()
80                      .toString()
81              ),
82              RtRepoCommitsTest.repo()
83          );
84          MatcherAssert.assertThat(commits.get(sha).sha(), Matchers.equalTo(sha));
85      }
86  
87      /**
88       * RtRepoCommits can compare two commits.
89       */
90      @Test
91      public void comparesCommits() {
92          final RepoCommits commits = new RtRepoCommits(
93              new FakeRequest().withBody(
94                  Json.createObjectBuilder()
95                      .add("base_commit", Json.createObjectBuilder())
96                      .add("commits", Json.createArrayBuilder())
97                      .add("files", Json.createArrayBuilder())
98                      .build().toString()
99              ),
100             RtRepoCommitsTest.repo()
101         );
102         MatcherAssert.assertThat(
103             commits.compare(
104                 "6dcb09b5b57875f334f61aebed695e2e4193db53",
105                 "6dcb09b5b57875f334f61aebed695e2e4193db54"
106             ),
107             Matchers.notNullValue(CommitsComparison.class)
108         );
109     }
110 
111     /**
112      * RtRepoCommits can compare two commits and present result in diff format.
113      * @throws Exception If some problem inside
114      */
115     @Test
116     public void comparesCommitsDiffFormat() throws Exception {
117         final RepoCommits commits = new RtRepoCommits(
118             new FakeRequest().withBody("diff --git"),
119             RtRepoCommitsTest.repo()
120         );
121         MatcherAssert.assertThat(
122             commits.diff(
123                 "6dcb09b5b57875f334f61aebed695e2e4193db55",
124                 "6dcb09b5b57875f334f61aebed695e2e4193db56"
125             ),
126             Matchers.startsWith("diff")
127         );
128     }
129 
130     /**
131      * RtRepoCommits can compare two commits and present result in patch format.
132      * @throws Exception If some problem inside
133      */
134     @Test
135     public void comparesCommitsPatchFormat() throws Exception {
136         final RepoCommits commits = new RtRepoCommits(
137             new FakeRequest().withBody(
138                 "From 6dcb09b5b57875f33"
139             ),
140             RtRepoCommitsTest.repo()
141         );
142         MatcherAssert.assertThat(
143             commits.patch(
144                 "6dcb09b5b57875f334f61aebed695e2e4193db57",
145                 "6dcb09b5b57875f334f61aebed695e2e4193db58"
146             ),
147             Matchers.startsWith("From")
148         );
149     }
150 
151     /**
152      * RtRepoCommits can read correctly URL.
153      */
154     @Test
155     public void readCorrectURL() {
156         MatcherAssert.assertThat(
157             new RtRepoCommits(new FakeRequest(), repo())
158                 .compare("base", "head").toString(),
159             Matchers.endsWith(
160                 "/see-FakeRequest-class/repos/user/repo/compare/base...head"
161             )
162         );
163     }
164 
165     /**
166      * Create repository for tests.
167      * @return Repository
168      */
169     private static Repo repo() {
170         return new RtGithub().repos()
171             .get(new Coordinates.Simple("user", "repo"));
172     }
173 
174 }