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.google.common.base.Optional;
33  import com.jcabi.http.request.FakeRequest;
34  import javax.json.Json;
35  import javax.json.JsonObject;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.Matchers;
38  import org.junit.Test;
39  
40  /**
41   * Test case for {@link RtCommitsComparison}.
42   * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
43   * @version $Id: 9e839916a708e502b41eeee5a395ba1ad9d3e131 $
44   */
45  public final class RtCommitsComparisonTest {
46  
47      /**
48       * RtCommitsComparison can fetch JSON.
49       * @throws Exception If some problem inside
50       * @checkstyle MultipleStringLiterals (75 lines)
51       * @checkstyle ExecutableStatementCountCheck (75 lines)
52       */
53      @Test
54      public void fetchesJson() throws Exception {
55          final String sha = "fffffffffffffffffffffffffffffffffffffffe";
56          final String filename = "bar/quux.txt";
57          // @checkstyle MagicNumberCheck (3 lines)
58          final int additions = 7;
59          final int deletions = 2;
60          final int changes = 9;
61          final String patch = "some diff here";
62          // @checkstyle LineLength (3 lines)
63          final String bloburl = "https://api.jcabi-github.invalid/johndoe/my-repo/blob/fffffffffffffffffffffffffffffffffffffffe/bar/quux.txt";
64          final String rawurl = "https://api.jcabi-github.invalid/johndoe/my-repo/raw/fffffffffffffffffffffffffffffffffffffffe/bar/quux.txt";
65          final String contentsurl = "https://api.github.invalid/repos/johndoe/my-repo/contents/bar/quux.txt?ref=fffffffffffffffffffffffffffffffffffffffe";
66          final CommitsComparison comparison = new RtCommitsComparison(
67              new FakeRequest().withBody(
68                  Json.createObjectBuilder()
69                      .add("base_commit", Json.createObjectBuilder())
70                      .add("commits", Json.createArrayBuilder())
71                      .add(
72                          "files",
73                          Json.createArrayBuilder()
74                              .add(
75                                  Json.createObjectBuilder()
76                                      .add("sha", sha)
77                                      .add("filename", filename)
78                                      .add("status", "added")
79                                      .add("additions", additions)
80                                      .add("deletions", deletions)
81                                      .add("changes", changes)
82                                      .add("patch", patch)
83                                      .add("blob_url", bloburl)
84                                      .add("raw_url", rawurl)
85                                      .add("contents_url", contentsurl)
86                                      .build()
87                              )
88                              .build()
89                      )
90                      .build().toString()
91              ),
92              RtCommitsComparisonTest.repo(),
93              "6dcb09b5b57875f334f61aebed695e2e4193db51",
94              "6dcb09b5b57875f334f61aebed695e2e4193db52"
95          );
96          final JsonObject json = comparison.json();
97          MatcherAssert.assertThat(
98              json.getJsonObject("base_commit"), Matchers.notNullValue()
99          );
100         MatcherAssert.assertThat(
101             json.getJsonArray("commits"), Matchers.notNullValue()
102         );
103         MatcherAssert.assertThat(
104             comparison.files(),
105             Matchers.<FileChange>iterableWithSize(1)
106         );
107         final FileChange.Smart file = new FileChange.Smart(
108             comparison.files().iterator().next()
109         );
110         MatcherAssert.assertThat(file.sha(), Matchers.equalTo(sha));
111         MatcherAssert.assertThat(file.filename(), Matchers.equalTo(filename));
112         MatcherAssert.assertThat(file.additions(), Matchers.equalTo(additions));
113         MatcherAssert.assertThat(file.deletions(), Matchers.equalTo(deletions));
114         MatcherAssert.assertThat(file.changes(), Matchers.equalTo(changes));
115         MatcherAssert.assertThat(
116             file.status(),
117             Matchers.equalTo(FileChange.Status.ADDED)
118         );
119         MatcherAssert.assertThat(
120             file.patch(),
121             Matchers.equalTo(Optional.of(patch))
122         );
123     }
124 
125     /**
126      * Return repo for tests.
127      * @return Repository
128      */
129     private static Repo repo() {
130         return new RtGithub().repos()
131             .get(new Coordinates.Simple("user", "repo"));
132     }
133 
134 }