View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github;
6   
7   import java.io.IOException;
8   import org.apache.commons.lang3.RandomStringUtils;
9   import org.hamcrest.MatcherAssert;
10  import org.hamcrest.Matchers;
11  import org.junit.jupiter.api.AfterAll;
12  import org.junit.jupiter.api.BeforeAll;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Integration test for {@link RtReleaseAssets}.
17   * @since 0.8
18   * @checkstyle MultipleStringLiteralsCheck (200 lines)
19   */
20  @OAuthScope(OAuthScope.Scope.REPO)
21  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
22  final class RtReleaseAssetsITCase {
23  
24      /**
25       * Test repos.
26       */
27      private static Repos repos;
28  
29      /**
30       * Test repo.
31       */
32      private static Repo repo;
33  
34      /**
35       * RepoRule.
36       * @checkstyle VisibilityModifierCheck (3 lines)
37       */
38      private static RepoRule rule = new RepoRule();
39  
40      /**
41       * Set up test fixtures.
42       */
43      @BeforeAll
44      static void setUp() throws IOException {
45          final GitHub github = GitHubIT.connect();
46          RtReleaseAssetsITCase.repos = github.repos();
47          RtReleaseAssetsITCase.repo = RtReleaseAssetsITCase.rule.repo(RtReleaseAssetsITCase.repos);
48          RtReleaseAssetsITCase.repo.releases().create(
49              RandomStringUtils.secure().nextAlphanumeric(10)
50          );
51      }
52  
53      /**
54       * Tear down test fixtures.
55       */
56      @AfterAll
57      static void tearDown() throws IOException {
58          if (RtReleaseAssetsITCase.repos != null && RtReleaseAssetsITCase.repo != null) {
59              RtReleaseAssetsITCase.repos.remove(RtReleaseAssetsITCase.repo.coordinates());
60          }
61      }
62  
63      @Test
64      void uploadsAssets() throws IOException {
65          final Releases releases = RtReleaseAssetsITCase.repo.releases();
66          final Release release = releases
67              .create(RandomStringUtils.secure().nextAlphanumeric(10));
68          final ReleaseAssets assets = release.assets();
69          try {
70              final String name = "upload.txt";
71              final ReleaseAsset uploaded = assets.upload(
72                  "upload".getBytes(),
73                  "text/plain",
74                  name
75              );
76              MatcherAssert.assertThat(
77                  "Values are not equal",
78                  uploaded.json().getString("name"),
79                  Matchers.is(name)
80              );
81          } finally {
82              releases.remove(release.number());
83          }
84      }
85  
86      @Test
87      void uploadsTwoAssets() throws IOException {
88          final Releases releases = RtReleaseAssetsITCase.repo.releases();
89          final Release release = releases
90              .create(RandomStringUtils.secure().nextAlphanumeric(10));
91          final ReleaseAssets assets = release.assets();
92          try {
93              final String name = "upload.txt";
94              final ReleaseAsset uploaded = assets.upload(
95                  "upload".getBytes(),
96                  "text/plain",
97                  name
98              );
99              MatcherAssert.assertThat(
100                 "Values are not equal",
101                 uploaded.json().getString("name"),
102                 Matchers.is(name)
103             );
104             final String othername = "upload2.txt";
105             final ReleaseAsset other = assets.upload(
106                 "upload2".getBytes(),
107                 "text/plain",
108                 othername
109             );
110             MatcherAssert.assertThat(
111                 "Values are not equal",
112                 other.json().getString("name"),
113                 Matchers.is(othername)
114             );
115         } finally {
116             releases.remove(release.number());
117         }
118     }
119 
120     @Test
121     void uploadsSameAssetInTwoReleases() throws IOException {
122         final Releases releases = RtReleaseAssetsITCase.repo.releases();
123         final Release release = releases.create(
124             RandomStringUtils.secure().nextAlphanumeric(10)
125         );
126         final Release otherrelease = releases.create(
127             RandomStringUtils.secure().nextAlphanumeric(10)
128         );
129         final ReleaseAssets assets = release.assets();
130         final ReleaseAssets otherassets = otherrelease.assets();
131         try {
132             final String name = "upload.txt";
133             final ReleaseAsset uploaded = assets.upload(
134                 "upload".getBytes(),
135                 "text/plain",
136                 name
137             );
138             MatcherAssert.assertThat(
139                 "Values are not equal",
140                 uploaded.json().getString("name"),
141                 Matchers.is(name)
142             );
143             final ReleaseAsset other = otherassets.upload(
144                 "upload".getBytes(),
145                 "text/plain",
146                 name
147             );
148             MatcherAssert.assertThat(
149                 "Values are not equal",
150                 other.json().getString("name"),
151                 Matchers.is(name)
152             );
153         } finally {
154             releases.remove(release.number());
155             releases.remove(otherrelease.number());
156         }
157     }
158 
159     @Test
160     void fetchesAssets() throws IOException {
161         final Releases releases = RtReleaseAssetsITCase.repo.releases();
162         final Release release = releases
163             .create(RandomStringUtils.secure().nextAlphanumeric(10));
164         final ReleaseAssets assets = release.assets();
165         try {
166             final ReleaseAsset uploaded = assets.upload(
167                 "fetch".getBytes(),
168                 "text/plain",
169                 "fetch.txt"
170             );
171             MatcherAssert.assertThat(
172                 "Values are not equal",
173                 assets.get(uploaded.number()),
174                 Matchers.is(uploaded)
175             );
176         } finally {
177             releases.remove(release.number());
178         }
179     }
180 
181     @Test
182     void iteratesAssets() throws IOException {
183         final Releases releases = RtReleaseAssetsITCase.repo.releases();
184         final Release release = releases
185             .create(RandomStringUtils.secure().nextAlphanumeric(10));
186         final ReleaseAssets assets = release.assets();
187         try {
188             final ReleaseAsset first = assets.upload(
189                 "first".getBytes(),
190                 "text/plain",
191                 "first.txt"
192             );
193             final ReleaseAsset second = assets.upload(
194                 "second".getBytes(),
195                 "text/plain",
196                 "second.txt"
197             );
198             MatcherAssert.assertThat(
199                 "Assertion failed",
200                 assets.iterate(),
201                 Matchers.contains(first, second)
202             );
203         } finally {
204             releases.remove(release.number());
205         }
206     }
207 
208     @Test
209     void returnsNoAssets() throws IOException {
210         final Releases releases = RtReleaseAssetsITCase.repo.releases();
211         final Release release = releases
212             .create(RandomStringUtils.secure().nextAlphanumeric(10));
213         final ReleaseAssets assets = release.assets();
214         try {
215             MatcherAssert.assertThat(
216                 "Collection is not empty",
217                 assets.iterate(),
218                 Matchers.emptyIterable()
219             );
220         } finally {
221             releases.remove(release.number());
222         }
223     }
224 
225 }