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.github.OAuthScope.Scope;
33  import com.jcabi.immutable.ArrayMap;
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  import org.hamcrest.MatcherAssert;
38  import org.hamcrest.Matchers;
39  import org.junit.Test;
40  
41  /**
42   * Integration case for {@link RepoCommits}.
43   *
44   * <p>
45   * WARNING: As there is no way to create Commit directly it was decided to use
46   * real commits from jcabi-github repository for integration testing of
47   * RtRepoCommits
48   *
49   * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
50   * @version $Id: f16c379f6face7318d778765e46b5f3758126dcb $
51   */
52  @OAuthScope(Scope.REPO)
53  public class RtRepoCommitsITCase {
54  
55      /**
56       * RtRepoCommits can fetch repo commits.
57       */
58      @Test
59      public final void fetchCommits() {
60          final Iterator<RepoCommit> iterator =
61              RtRepoCommitsITCase.repo().commits().iterate(
62                  new ArrayMap<String, String>()
63                      .with("since", "2014-01-26T00:00:00Z")
64                      .with("until", "2014-01-27T00:00:00Z")
65              ).iterator();
66          final List<String> shas = new ArrayList<>(5);
67          shas.add("1aa4af45aa2c56421c3d911a0a06da513a7316a0");
68          shas.add("940dd5081fada0ead07762933036bf68a005cc40");
69          shas.add("05940dbeaa6124e4a87d9829fb2fce80b713dcbe");
70          shas.add("51cabb8e759852a6a40a7a2a76ef0afd4beef96d");
71          shas.add("11bd4d527236f9cb211bc6667df06fde075beded");
72          int found = 0;
73          while (iterator.hasNext()) {
74              if (shas.contains(iterator.next().sha())) {
75                  found += 1;
76              }
77          }
78          MatcherAssert.assertThat(
79              found,
80              Matchers.equalTo(shas.size())
81          );
82      }
83  
84      /**
85       * RtRepoCommits can compare two commits and return result in patch mode.
86       * @throws Exception if there is no github key provided
87       */
88      @Test
89      public final void compareCommitsPatch() throws Exception {
90          final String patch = RtRepoCommitsITCase.repo().commits().patch(
91              "5339b8e35b",
92              "9b2e6efde9"
93          );
94          MatcherAssert.assertThat(
95              patch,
96              Matchers.startsWith(
97                  "From 9b2e6efde94fabec5876dc481b38811e8b4e992f"
98              )
99          );
100         MatcherAssert.assertThat(
101             patch,
102             Matchers.containsString(
103                 "Issue #430 RepoCommit interface was added"
104             )
105         );
106     }
107 
108     /**
109      * RtRepoCommits can compare two commits and return result in diff mode.
110      * @throws Exception if there is no github key provided
111      */
112     @Test
113     public final void compareCommitsDiff() throws Exception {
114         final String diff = RtRepoCommitsITCase.repo().commits().diff(
115             "2b3814e",
116             "b828dfa"
117         );
118         MatcherAssert.assertThat(
119             diff,
120             Matchers.startsWith("diff --git")
121         );
122     }
123 
124     /**
125      * Check that commit actually got.
126      */
127     @Test
128     public final void getCommit() {
129         final String sha = "94e4216";
130         MatcherAssert.assertThat(
131             RtRepoCommitsITCase.repo().commits().get(sha).sha(),
132             Matchers.equalTo(sha)
133         );
134     }
135 
136     /**
137      * Create and return repo to test.
138      * @return Repo
139      */
140     private static Repo repo() {
141         return new GithubIT().connect().repos().get(
142             new Coordinates.Simple("jcabi", "jcabi-github")
143         );
144     }
145 }