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.aspects.Tv;
33  import com.jcabi.github.OAuthScope.Scope;
34  import org.apache.commons.lang3.RandomStringUtils;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.AfterClass;
38  import org.junit.BeforeClass;
39  import org.junit.Test;
40  
41  /**
42   * Integration test for {@link RtReleaseAssets}.
43   *
44   * @author Carlos Miranda (miranda.cma@gmail.com)
45   * @version $Id: 6b7c6aa4c711502e36b2ee52d38cc5fbea50913c $
46   * @checkstyle MultipleStringLiteralsCheck (200 lines)
47   */
48  @OAuthScope(Scope.REPO)
49  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
50  public final class RtReleaseAssetsITCase {
51  
52      /**
53       * Test repos.
54       */
55      private static Repos repos;
56  
57      /**
58       * Test repo.
59       */
60      private static Repo repo;
61  
62      /**
63       * RepoRule.
64       * @checkstyle VisibilityModifierCheck (3 lines)
65       */
66      private static RepoRule rule = new RepoRule();
67  
68      /**
69       * Set up test fixtures.
70       * @throws Exception If some errors occurred.
71       */
72      @BeforeClass
73      public static void setUp() throws Exception {
74          final Github github = new GithubIT().connect();
75          repos = github.repos();
76          repo = rule.repo(repos);
77          repo.releases().create(
78              RandomStringUtils.randomAlphanumeric(Tv.TEN)
79          );
80      }
81  
82      /**
83       * Tear down test fixtures.
84       * @throws Exception If some errors occurred.
85       */
86      @AfterClass
87      public static void tearDown() throws Exception {
88          if (repos != null && repo != null) {
89              repos.remove(repo.coordinates());
90          }
91      }
92  
93      /**
94       * RtReleaseAssets can upload release assets.
95       * @throws Exception If an exception occurs.
96       */
97      @Test
98      public void uploadsAssets() throws Exception {
99          final Releases releases = repo.releases();
100         final Release release = releases
101             .create(RandomStringUtils.randomAlphanumeric(Tv.TEN));
102         final ReleaseAssets assets = release.assets();
103         try {
104             final String name = "upload.txt";
105             final ReleaseAsset uploaded = assets.upload(
106                 "upload".getBytes(),
107                 "text/plain",
108                 name
109             );
110             MatcherAssert.assertThat(
111                 uploaded.json().getString("name"),
112                 Matchers.is(name)
113             );
114         } finally {
115             releases.remove(release.number());
116         }
117     }
118 
119     /**
120      * RtReleaseAssets can upload two release assets.
121      * @throws Exception If an exception occurs.
122      */
123     @Test
124     public void uploadsTwoAssets() throws Exception {
125         final Releases releases = repo.releases();
126         final Release release = releases
127             .create(RandomStringUtils.randomAlphanumeric(Tv.TEN));
128         final ReleaseAssets assets = release.assets();
129         try {
130             final String name = "upload.txt";
131             final ReleaseAsset uploaded = assets.upload(
132                 "upload".getBytes(),
133                 "text/plain",
134                 name
135             );
136             MatcherAssert.assertThat(
137                 uploaded.json().getString("name"),
138                 Matchers.is(name)
139             );
140             final String othername = "upload2.txt";
141             final ReleaseAsset otheruploaded = assets.upload(
142                 "upload2".getBytes(),
143                 "text/plain",
144                 othername
145             );
146             MatcherAssert.assertThat(
147                 otheruploaded.json().getString("name"),
148                 Matchers.is(othername)
149             );
150         } finally {
151             releases.remove(release.number());
152         }
153     }
154 
155     /**
156      * RtReleaseAssets can upload one release assets to two releases.
157      * @throws Exception If an exception occurs.
158      */
159     @Test
160     public void uploadsSameAssetInTwoReleases() throws Exception {
161         final Releases releases = repo.releases();
162         final Release release = releases.create(
163             RandomStringUtils.randomAlphanumeric(Tv.TEN)
164         );
165         final Release otherrelease = releases.create(
166             RandomStringUtils.randomAlphanumeric(Tv.TEN)
167         );
168         final ReleaseAssets assets = release.assets();
169         final ReleaseAssets otherassets = otherrelease.assets();
170         try {
171             final String name = "upload.txt";
172             final ReleaseAsset uploaded = assets.upload(
173                 "upload".getBytes(),
174                 "text/plain",
175                 name
176             );
177             MatcherAssert.assertThat(
178                 uploaded.json().getString("name"),
179                 Matchers.is(name)
180             );
181             final ReleaseAsset otheruploaded = otherassets.upload(
182                 "upload".getBytes(),
183                 "text/plain",
184                 name
185             );
186             MatcherAssert.assertThat(
187                 otheruploaded.json().getString("name"),
188                 Matchers.is(name)
189             );
190         } finally {
191             releases.remove(release.number());
192             releases.remove(otherrelease.number());
193         }
194     }
195 
196     /**
197      * RtReleaseAssets can fetch release assets by asset ID.
198      * @throws Exception If an exception occurs.
199      */
200     @Test
201     public void fetchesAssets() throws Exception {
202         final Releases releases = repo.releases();
203         final Release release = releases
204             .create(RandomStringUtils.randomAlphanumeric(Tv.TEN));
205         final ReleaseAssets assets = release.assets();
206         try {
207             final ReleaseAsset uploaded = assets.upload(
208                 "fetch".getBytes(),
209                 "text/plain",
210                 "fetch.txt"
211             );
212             MatcherAssert.assertThat(
213                 assets.get(uploaded.number()),
214                 Matchers.is(uploaded)
215             );
216         } finally {
217             releases.remove(release.number());
218         }
219     }
220 
221     /**
222      * RtReleaseAssets can iterate through multiple release assets.
223      * @throws Exception If an exception occurs.
224      */
225     @Test
226     public void iteratesAssets() throws Exception {
227         final Releases releases = repo.releases();
228         final Release release = releases
229             .create(RandomStringUtils.randomAlphanumeric(Tv.TEN));
230         final ReleaseAssets assets = release.assets();
231         try {
232             final ReleaseAsset first = assets.upload(
233                 "first".getBytes(),
234                 "text/plain",
235                 "first.txt"
236             );
237             final ReleaseAsset second = assets.upload(
238                 "second".getBytes(),
239                 "text/plain",
240                 "second.txt"
241             );
242             MatcherAssert.assertThat(
243                 assets.iterate(),
244                 Matchers.contains(first, second)
245             );
246         } finally {
247             releases.remove(release.number());
248         }
249     }
250 
251     /**
252      * RtReleaseAssets can return an empty list of release assets.
253      * @throws Exception If an exception occurs.
254      */
255     @Test
256     public void returnsNoAssets() throws Exception {
257         final Releases releases = repo.releases();
258         final Release release = releases
259             .create(RandomStringUtils.randomAlphanumeric(Tv.TEN));
260         final ReleaseAssets assets = release.assets();
261         try {
262             MatcherAssert.assertThat(
263                 assets.iterate(),
264                 Matchers.emptyIterable()
265             );
266         } finally {
267             releases.remove(release.number());
268         }
269     }
270 
271 }