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.mock;
31  
32  import com.jcabi.github.Release;
33  import com.jcabi.github.ReleaseAsset;
34  import com.jcabi.github.ReleaseAssets;
35  import java.nio.charset.StandardCharsets;
36  import javax.json.Json;
37  import javax.xml.bind.DatatypeConverter;
38  import org.apache.commons.io.IOUtils;
39  import org.hamcrest.MatcherAssert;
40  import org.hamcrest.Matchers;
41  import org.junit.Test;
42  
43  /**
44   * Test case for {@link MkReleaseAsset}.
45   *
46   * @author Carlos Miranda (miranda.cma@gmail.com)
47   * @version $Id: 289ecefabc77b15b6d8e6b2e30f64121d2bace91 $
48   * @since 0.8
49   * @checkstyle MultipleStringLiteralsCheck (200 lines)
50   */
51  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
52  public final class MkReleaseAssetTest {
53  
54      /**
55       * MkReleaseAsset can fetch its own Release.
56       *
57       * @throws Exception If a problem occurs.
58       */
59      @Test
60      public void fetchesRelease() throws Exception {
61          final Release rel = release();
62          MatcherAssert.assertThat(
63              rel.assets().get(1).release(),
64              Matchers.is(rel)
65          );
66      }
67  
68      /**
69       * MkReleaseAsset can fetch its own number.
70       *
71       * @throws Exception If a problem occurs.
72       */
73      @Test
74      public void fetchesNumber() throws Exception {
75          final Release rel = release();
76          MatcherAssert.assertThat(
77              rel.assets().get(1).number(),
78              Matchers.is(1)
79          );
80      }
81  
82      /**
83       * MkReleaseAsset can be removed.
84       *
85       * @throws Exception If a problem occurs.
86       */
87      @Test
88      public void removesAsset() throws Exception {
89          final ReleaseAssets assets = release().assets();
90          final ReleaseAsset asset = assets.upload(
91              "testRemove".getBytes(), "text/plain", "remove.txt"
92          );
93          MatcherAssert.assertThat(
94              assets.iterate(),
95              Matchers.<ReleaseAsset>iterableWithSize(1)
96          );
97          asset.remove();
98          MatcherAssert.assertThat(
99              assets.iterate(),
100             Matchers.emptyIterable()
101         );
102     }
103 
104     /**
105      * MkReleaseAsset can be removed several times.
106      *
107      * @throws Exception If a problem occurs.
108      */
109     @Test
110     public void removesSeveralAssets() throws Exception {
111         final ReleaseAssets assets = release().assets();
112         // @checkstyle MagicNumberCheck (1 line)
113         final int limit = 3;
114         final ReleaseAsset[] bodies = new ReleaseAsset[limit];
115         for (int idx = 0; idx < limit; ++idx) {
116             bodies[idx] = assets.upload(
117                 "testRemove".getBytes(), "text/plain", "remove.txt"
118             );
119         }
120         MatcherAssert.assertThat(
121             assets.iterate(),
122             Matchers.<ReleaseAsset>iterableWithSize(limit)
123         );
124         for (int idx = 0; idx < limit; ++idx) {
125             bodies[idx].remove();
126         }
127         MatcherAssert.assertThat(
128             assets.iterate(),
129             Matchers.emptyIterable()
130         );
131     }
132 
133     /**
134      * MkReleaseAsset can be represented in JSON format.
135      *
136      * @throws Exception If a problem occurs.
137      */
138     @Test
139     public void canRepresentAsJson() throws Exception {
140         final String name = "json.txt";
141         final String type = "text/plain";
142         final ReleaseAsset asset = release().assets().upload(
143             "testJson".getBytes(), type, name
144         );
145         MatcherAssert.assertThat(
146             asset.json().getString("content_type"),
147             Matchers.is(type)
148         );
149         MatcherAssert.assertThat(
150             asset.json().getString("name"),
151             Matchers.is(name)
152         );
153     }
154 
155     /**
156      * MkReleaseAsset can patch its JSON representation.
157      *
158      * @throws Exception If a problem occurs.
159      */
160     @Test
161     public void canPatchJson() throws Exception {
162         final String orig = "orig.txt";
163         final ReleaseAsset asset = release().assets().upload(
164             "testPatch".getBytes(), "text/plain", orig
165         );
166         final String attribute = "name";
167         MatcherAssert.assertThat(
168             asset.json().getString(attribute),
169             Matchers.is(orig)
170         );
171         final String patched = "patched.txt";
172         asset.patch(
173             Json.createObjectBuilder().add(attribute, patched).build()
174         );
175         MatcherAssert.assertThat(
176             asset.json().getString(attribute),
177             Matchers.is(patched)
178         );
179     }
180 
181     /**
182      * Should return the Base64-encoded value of the input contents. When
183      * decoded, should be equal to the input.
184      *
185      * @throws Exception if some problem inside
186      */
187     @Test
188     public void fetchesRawRepresentation() throws Exception {
189         final String test = "This is a test asset.";
190         final ReleaseAsset asset = new MkGithub().randomRepo().releases()
191             .create("v1.0")
192             .assets()
193             .upload(test.getBytes(), "type", "name");
194         MatcherAssert.assertThat(
195             new String(
196               DatatypeConverter.parseBase64Binary(
197                   IOUtils.toString(asset.raw(), StandardCharsets.UTF_8)
198               )
199             ),
200             Matchers.is(test)
201         );
202     }
203 
204     /**
205      * Create a Release to work with.
206      * @return Repo
207      * @throws Exception If some problem inside
208      */
209     private static Release release() throws Exception {
210         return new MkGithub().randomRepo().releases().create("v1.0");
211     }
212 }