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.google.common.base.Optional;
33  import com.jcabi.github.mock.MkFileChange;
34  import java.io.IOException;
35  import javax.json.Json;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.Matchers;
38  import org.junit.Test;
39  
40  /**
41   * Test case for {@link FileChange}.
42   * @author Chris Rebert (github@chrisrebert.com)
43   * @version $Id: d5fa8d434df4e2bae8fc9496ce53f7239e8e29fe $
44   * @since 0.24
45   */
46  public final class FileChangeTest {
47      /**
48       * FileChange.Smart can get the status of the file.
49       * @throws IOException If an I/O problem occurs
50       */
51      @Test
52      public void getsStatus() throws IOException {
53          final String status = "status";
54          MatcherAssert.assertThat(
55              FileChangeTest.stringFileChange(status, "added").status(),
56              Matchers.equalTo(FileChange.Status.ADDED)
57          );
58          MatcherAssert.assertThat(
59              FileChangeTest.stringFileChange(status, "modified").status(),
60              Matchers.equalTo(FileChange.Status.MODIFIED)
61          );
62          MatcherAssert.assertThat(
63              FileChangeTest.stringFileChange(status, "removed").status(),
64              Matchers.equalTo(FileChange.Status.REMOVED)
65          );
66          MatcherAssert.assertThat(
67              FileChangeTest.stringFileChange(status, "renamed").status(),
68              Matchers.equalTo(FileChange.Status.RENAMED)
69          );
70      }
71  
72      /**
73       * FileChange.Smart can get the filename of the file.
74       * @throws IOException If there is an I/O problem
75       */
76      @Test
77      public void getsFilename() throws IOException {
78          final String filename = "foo/bar.txt";
79          MatcherAssert.assertThat(
80              FileChangeTest.stringFileChange("filename", filename).filename(),
81              Matchers.equalTo(filename)
82          );
83      }
84  
85      /**
86       * FileChange.Smart can get the commit SHA of the file.
87       * @throws IOException If there is an I/O problem
88       */
89      @Test
90      public void getsSha() throws IOException {
91          final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db51";
92          MatcherAssert.assertThat(
93              FileChangeTest.stringFileChange("sha", sha).sha(),
94              Matchers.equalTo(sha)
95          );
96      }
97  
98      /**
99       * FileChange.Smart can get the file's count of lines added.
100      * @throws IOException If there is an I/O problem
101      */
102     @Test
103     public void getsAdditions() throws IOException {
104         // @checkstyle MagicNumberCheck (1 line)
105         final int adds = 42;
106         MatcherAssert.assertThat(
107             FileChangeTest.intFileChange("additions", adds).additions(),
108             Matchers.equalTo(adds)
109         );
110     }
111 
112     /**
113      * FileChange.Smart can get the file's count of lines deleted.
114      * @throws IOException If there is an I/O problem
115      */
116     @Test
117     public void getsDeletions() throws IOException {
118         // @checkstyle MagicNumberCheck (1 line)
119         final int deletions = 97;
120         MatcherAssert.assertThat(
121             FileChangeTest.intFileChange("deletions", deletions).deletions(),
122             Matchers.equalTo(deletions)
123         );
124     }
125 
126     /**
127      * FileChange.Smart can get the file's count of lines changed.
128      * @throws IOException If there is an I/O problem
129      */
130     @Test
131     public void getsChanges() throws IOException {
132         // @checkstyle MagicNumberCheck (1 line)
133         final int changes = 11;
134         MatcherAssert.assertThat(
135             FileChangeTest.intFileChange("changes", changes).changes(),
136             Matchers.equalTo(changes)
137         );
138     }
139 
140     /**
141      * FileChange.Smart does not fail when attempting to get the
142      * it is absent (which is normally the case for binary files).
143      * @throws IOException If there is an I/O problem
144      */
145     @Test
146     public void getsAbsentPatch() throws IOException {
147         MatcherAssert.assertThat(
148             new FileChange.Smart(
149                 new MkFileChange(
150                     Json.createObjectBuilder().build()
151                 )
152             ).patch(),
153             Matchers.equalTo(Optional.<String>absent())
154         );
155     }
156 
157     /**
158      * FileChange.Smart can get the file's diff patch string when
159      * it is present (which is normally the case for text files).
160      * @throws IOException If there is an I/O problem
161      */
162     @Test
163     public void getsPresentPatch() throws IOException {
164         // @checkstyle LineLength (1 line)
165         final String patch = "@@ -120,7 +120,7 @@ class Test1 @@ -1000,7 +1000,7 @@ class Test1";
166         MatcherAssert.assertThat(
167             stringFileChange(
168                 "patch",
169                 patch
170             ).patch(),
171             Matchers.equalTo(Optional.of(patch))
172         );
173     }
174 
175     /**
176      * FileChange.Smart can get the URL for the file's raw content.
177      * @throws IOException If there is an I/O problem
178      */
179     @Test
180     public void getsRawUrl() throws IOException {
181         // @checkstyle LineLength (1 line)
182         final String url = "https://api.jcabi-github.invalid/octocat/Hello-World/raw/6dcb09b5b57875f334f61aebed695e2e4193db51/foo/bar.txt";
183         MatcherAssert.assertThat(
184             stringFileChange(
185                 "raw_url",
186                 url
187             ).rawUrl(),
188             Matchers.equalTo(url)
189         );
190     }
191 
192     /**
193      * FileChange.Smart can get the URL of the file's blob.
194      * @throws IOException If there is an I/O problem
195      */
196     @Test
197     public void getsBlobUrl() throws IOException {
198         // @checkstyle LineLength (1 line)
199         final String url = "https://api.jcabi-github.invalid/octocat/Hello-World/blob/6dcb09b5b57875f334f61aebed695e2e4193db51/foo/bar.txt";
200         MatcherAssert.assertThat(
201             stringFileChange(
202                 "blob_url",
203                 url
204             ).blobUrl(),
205             Matchers.equalTo(url)
206         );
207     }
208 
209     /**
210      * FileChange.Smart can get the contents URL of the file.
211      * @throws IOException If there is an I/O problem
212      */
213     @Test
214     public void getsContentsUrl() throws IOException {
215         // @checkstyle LineLength (1 line)
216         final String url = "https://api.jcabi-github.invalid/repos/octocat/Hello-World/contents/foo/bar.txt?ref=6dcb09b5b57875f334f61aebed695e2e4193db51";
217         MatcherAssert.assertThat(
218             FileChangeTest.stringFileChange("contents_url", url)
219                 .contentsUrl(),
220             Matchers.equalTo(url)
221         );
222     }
223 
224     /**
225      * Make a new smart file change backed by a JSON object consisting of a
226      * single key-value pair, where the value is a string.
227      * @param key Key string
228      * @param value Value string
229      * @return FileChange.Smart
230      */
231     private static FileChange.Smart stringFileChange(
232         final String key,
233         final String value
234     ) {
235         return new FileChange.Smart(
236             new MkFileChange(
237                 Json.createObjectBuilder()
238                     .add(key, value)
239                     .build()
240             )
241         );
242     }
243 
244     /**
245      * Make a new smart file change backed by a JSON object consisting of a
246      * single key-value pair, where the value is an integer.
247      * @param key Key string
248      * @param value Value integer
249      * @return FileChange.Smart
250      */
251     private static FileChange.Smart intFileChange(
252         final String key,
253         final int value
254     ) {
255         return new FileChange.Smart(
256             new MkFileChange(
257                 Json.createObjectBuilder()
258                     .add(key, value)
259                     .build()
260             )
261         );
262     }
263 }