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