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.xml.bind.DatatypeConverter;
36  import org.apache.commons.io.IOUtils;
37  import org.hamcrest.MatcherAssert;
38  import org.hamcrest.Matchers;
39  import org.junit.Test;
40  
41  /**
42   * Test case for {@link MkReleaseAssets}.
43   *
44   * @author Carlos Miranda (miranda.cma@gmail.com)
45   * @version $Id: fa7ea9466e7f17ac345779ab10dfb858600f154e $
46   * @since 0.8
47   * @checkstyle MultipleStringLiteralsCheck (200 lines)
48   * @checkstyle MethodNameCheck (200 lines)
49   */
50  public final class MkReleaseAssetsTest {
51  
52      /**
53       * MkReleaseAssets can upload a new Release Asset.
54       *
55       * @throws Exception If a problem occurs.
56       */
57      @Test
58      public void uploadsNewAsset() throws Exception {
59          final ReleaseAssets assets = release().assets();
60          final ReleaseAsset asset = assets.upload(
61              "testUpload".getBytes(), "text/plain", "upload.txt"
62          );
63          MatcherAssert.assertThat(
64              asset.number(),
65              Matchers.is(1)
66          );
67      }
68  
69      /**
70       * MkReleaseAssets can fetch a single Release Asset.
71       *
72       * @throws Exception If a problem occurs.
73       */
74      @Test
75      public void fetchesSingleAsset() throws Exception {
76          final ReleaseAssets assets = release().assets();
77          final ReleaseAsset asset = assets.upload(
78              "testGet".getBytes(), "text/plain", "get.txt"
79          );
80          MatcherAssert.assertThat(
81              assets.get(asset.number()),
82              Matchers.is(asset)
83          );
84      }
85  
86      /**
87       * MkReleaseAssets can iterate through Release Assets.
88       *
89       * @throws Exception If a problem occurs.
90       */
91      @Test
92      public void iteratesAssets() throws Exception {
93          final ReleaseAssets assets = release().assets();
94          assets.upload(
95              "testIterate".getBytes(), "text/plain", "iterate.txt"
96          );
97          MatcherAssert.assertThat(
98              assets.iterate(),
99              Matchers.not(Matchers.emptyIterable())
100         );
101     }
102 
103     /**
104      * MkReleaseAssets can fetch its own Release.
105      *
106      * @throws Exception If a problem occurs.
107      */
108     @Test
109     public void fetchesRelease() throws Exception {
110         final Release rel = release();
111         MatcherAssert.assertThat(
112             rel.assets().release(),
113             Matchers.is(rel)
114         );
115     }
116 
117     /**
118      * Must encode the input bytes into Base64.
119      * @throws Exception Unexpected.
120      */
121     @Test
122     public void encodesContentsAsBase64() throws Exception {
123         final String test = "This is a test asset.";
124         final ReleaseAsset asset = new MkGithub().randomRepo().releases()
125             .create("v1.0")
126             .assets()
127             .upload(test.getBytes(), "type", "name");
128         MatcherAssert.assertThat(
129             IOUtils.toString(asset.raw()),
130             Matchers.is(DatatypeConverter.printBase64Binary(test.getBytes()))
131         );
132     }
133 
134     /**
135      * Create a Release to work with.
136      * @return Repo
137      * @throws Exception If some problem inside
138      */
139     private static Release release() throws Exception {
140         return new MkGithub().randomRepo().releases().create("v1.0");
141     }
142 }