View Javadoc
1   /**
2    * Copyright (c) 2013-2022, 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: f40cc7c9a43e35ed94e5d8b3da777c40435ffd82 $
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      * @throws Exception if any problem inside
154      */
155     @Test
156     public void readCorrectURL() throws Exception {
157         MatcherAssert.assertThat(
158             new RtRepoCommits(new FakeRequest(), repo())
159                 .compare("base", "head").toString(),
160             Matchers.endsWith(
161                 "/see-FakeRequest-class/repos/user/repo/compare/base...head"
162             )
163         );
164     }
165 
166     /**
167      * Create repository for tests.
168      * @return Repository
169      */
170     private static Repo repo() {
171         return new RtGithub().repos()
172             .get(new Coordinates.Simple("user", "repo"));
173     }
174 
175 }