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 javax.json.Json;
34  import org.hamcrest.MatcherAssert;
35  import org.hamcrest.Matchers;
36  import org.junit.Test;
37  
38  /**
39   * Test case for {@link CommitsComparison}.
40   * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
41   * @version $Id: 1ad941802169435d57ab6bc673066ced2dd7c796 $
42   * @checkstyle MultipleStringLiterals (75 lines)
43   */
44  public final class CommitsComparisonTest {
45  
46      /**
47       * CommitsComparison.Smart can fetch commits.
48       * @throws Exception If some problem inside
49       */
50      @Test
51      public void fetchesCommits() throws Exception {
52          final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db50";
53          final CommitsComparison.Smart comparison = new CommitsComparison.Smart(
54              new RtCommitsComparison(
55                  new FakeRequest().withBody(
56                      Json.createObjectBuilder()
57                          .add("base_commit", Json.createObjectBuilder())
58                          .add(
59                              "commits",
60                              Json.createArrayBuilder()
61                                  .add(Json.createObjectBuilder().add("sha", sha))
62                          )
63                          .add("files", Json.createArrayBuilder())
64                          .build().toString()
65                  ),
66                  CommitsComparisonTest.repo(),
67                  "6dcb09b5b57875f334f61aebed695e2e4193db51",
68                  "6dcb09b5b57875f334f61aebed695e2e4193db52"
69              )
70          );
71          MatcherAssert.assertThat(
72              comparison.commits().iterator().next().sha(), Matchers.equalTo(sha)
73          );
74      }
75  
76      /**
77       * CommitsComparison.Smart can fetch files.
78       * @throws Exception If some problem inside
79       */
80      @Test
81      public void fetchesFiles() throws Exception {
82          final String filename = "file.txt";
83          final CommitsComparison.Smart comparison = new CommitsComparison.Smart(
84              new RtCommitsComparison(
85                  new FakeRequest().withBody(
86                      Json.createObjectBuilder()
87                          .add("base_commit", Json.createObjectBuilder())
88                          .add("commits", Json.createArrayBuilder())
89                          .add(
90                              "files",
91                              Json.createArrayBuilder().add(
92                                  Json.createObjectBuilder()
93                                      .add("filename", filename)
94                              )
95                          )
96                          .build().toString()
97                  ),
98                  CommitsComparisonTest.repo(),
99                  "6dcb09b5b57875f334f61aebed695e2e4193db53",
100                 "6dcb09b5b57875f334f61aebed695e2e4193db54"
101             )
102         );
103         MatcherAssert.assertThat(
104             new FileChange.Smart(
105                 comparison.files().iterator().next()
106             ).filename(),
107             Matchers.equalTo(filename)
108         );
109     }
110 
111     /**
112      * Return repo for tests.
113      * @return Repository
114      */
115     private static Repo repo() {
116         return new RtGithub().repos()
117             .get(new Coordinates.Simple("user", "repo"));
118     }
119 
120 }