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.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: 73d49a74efae088bb7b2dfd2934bb257160c5223 $
51   */
52  @OAuthScope(Scope.REPO)
53  public class RtRepoCommitsITCase {
54  
55      /**
56       * RtRepoCommits can fetch repo commits.
57       * @throws Exception if there is no github key provided
58       */
59      @Test
60      public final void fetchCommits() throws Exception {
61          final Iterator<RepoCommit> iterator =
62              RtRepoCommitsITCase.repo().commits().iterate(
63                  new ArrayMap<String, String>()
64                      .with("since", "2014-01-26T00:00:00Z")
65                      .with("until", "2014-01-27T00:00:00Z")
66              ).iterator();
67          final List<String> shas = new ArrayList<String>(5);
68          shas.add("1aa4af45aa2c56421c3d911a0a06da513a7316a0");
69          shas.add("940dd5081fada0ead07762933036bf68a005cc40");
70          shas.add("05940dbeaa6124e4a87d9829fb2fce80b713dcbe");
71          shas.add("51cabb8e759852a6a40a7a2a76ef0afd4beef96d");
72          shas.add("11bd4d527236f9cb211bc6667df06fde075beded");
73          int found = 0;
74          while (iterator.hasNext()) {
75              if (shas.contains(iterator.next().sha())) {
76                  found += 1;
77              }
78          }
79          MatcherAssert.assertThat(
80              found,
81              Matchers.equalTo(shas.size())
82          );
83      }
84  
85      /**
86       * RtRepoCommits can compare two commits and return result in patch mode.
87       * @throws Exception if there is no github key provided
88       */
89      @Test
90      public final void compareCommitsPatch() throws Exception {
91          final String patch = RtRepoCommitsITCase.repo().commits().patch(
92              "5339b8e35b",
93              "9b2e6efde9"
94          );
95          MatcherAssert.assertThat(
96              patch,
97              Matchers.startsWith(
98                  "From 9b2e6efde94fabec5876dc481b38811e8b4e992f"
99              )
100         );
101         MatcherAssert.assertThat(
102             patch,
103             Matchers.containsString(
104                 "Issue #430 RepoCommit interface was added"
105             )
106         );
107     }
108 
109     /**
110      * RtRepoCommits can compare two commits and return result in diff mode.
111      * @throws Exception if there is no github key provided
112      */
113     @Test
114     public final void compareCommitsDiff() throws Exception {
115         final String diff = RtRepoCommitsITCase.repo().commits().diff(
116             "2b3814e",
117             "b828dfa"
118         );
119         MatcherAssert.assertThat(
120             diff,
121             Matchers.startsWith("diff --git")
122         );
123     }
124 
125     /**
126      * Check that commit actually got.
127      * @throws Exception If some problem inside
128      */
129     @Test
130     public final void getCommit() throws Exception {
131         final String sha = "94e4216";
132         MatcherAssert.assertThat(
133             RtRepoCommitsITCase.repo().commits().get(sha).sha(),
134             Matchers.equalTo(sha)
135         );
136     }
137 
138     /**
139      * Create and return repo to test.
140      * @return Repo
141      */
142     private static Repo repo() {
143         return new GithubIT().connect().repos().get(
144             new Coordinates.Simple("jcabi", "jcabi-github")
145         );
146     }
147 }