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 java.net.URL;
33  import javax.json.Json;
34  import org.hamcrest.MatcherAssert;
35  import org.hamcrest.Matchers;
36  import org.junit.Test;
37  import org.mockito.Mockito;
38  
39  /**
40   * Test case for {@link ReleaseAsset}.
41   * @author Andres Candal (andres.candal@rollasolution.com)
42   * @version $Id: bba1228650b6019d359eeefa8f6a7bef8d11dd09 $
43   * @checkstyle MultipleStringLiterals (150 lines)
44   */
45  @SuppressWarnings("PMD.TooManyMethods")
46  public class ReleaseAssetTest {
47  
48      /**
49       * ReleaseAsset.Smart can fetch url property from ReleaseAsset.
50       * @throws Exception If some problem inside
51       */
52      @Test
53      public final void fetchesUrl() throws Exception {
54          final ReleaseAsset releaseAsset = Mockito.mock(ReleaseAsset.class);
55          // @checkstyle LineLength (1 line)
56          final String prop = "https://api.github.com/repos/octo/Hello/releases/assets/1";
57          Mockito.doReturn(
58              Json.createObjectBuilder()
59                  .add("url", prop)
60                  .build()
61          ).when(releaseAsset).json();
62          MatcherAssert.assertThat(
63              new ReleaseAsset.Smart(releaseAsset).url(),
64              Matchers.is(new URL(prop))
65          );
66      }
67  
68      /**
69       * ReleaseAsset.Smart can fetch name property from ReleaseAsset.
70       * @throws Exception If some problem inside
71       */
72      @Test
73      public final void fetchesName() throws Exception {
74          final ReleaseAsset releaseAsset = Mockito.mock(ReleaseAsset.class);
75          final String prop = "assetname.ext";
76          Mockito.doReturn(
77              Json.createObjectBuilder()
78                  .add("name", prop)
79                  .build()
80          ).when(releaseAsset).json();
81          MatcherAssert.assertThat(
82              new ReleaseAsset.Smart(releaseAsset).name(),
83              Matchers.is(prop)
84          );
85      }
86  
87      /**
88       * ReleaseAsset.Smart can fetch label property from ReleaseAsset.
89       * @throws Exception If some problem inside
90       */
91      @Test
92      public final void fetchesLabel() throws Exception {
93          final ReleaseAsset releaseAsset = Mockito.mock(ReleaseAsset.class);
94          final String prop = "short description";
95          Mockito.doReturn(
96              Json.createObjectBuilder()
97                  .add("label", prop)
98                  .build()
99          ).when(releaseAsset).json();
100         MatcherAssert.assertThat(
101             new ReleaseAsset.Smart(releaseAsset).label(),
102             Matchers.is(prop)
103         );
104     }
105 
106     /**
107      * ReleaseAsset.Smart can fetch state property from ReleaseAsset.
108      * @throws Exception If some problem inside
109      */
110     @Test
111     public final void fetchesState() throws Exception {
112         final ReleaseAsset releaseAsset = Mockito.mock(ReleaseAsset.class);
113         final String prop = "uploaded";
114         Mockito.doReturn(
115             Json.createObjectBuilder()
116                 .add("state", prop)
117                 .build()
118         ).when(releaseAsset).json();
119         MatcherAssert.assertThat(
120             new ReleaseAsset.Smart(releaseAsset).state(),
121             Matchers.is(prop)
122         );
123     }
124 
125     /**
126      * ReleaseAsset.Smart can fetch content_type property from ReleaseAsset.
127      * @throws Exception If some problem inside
128      */
129     @Test
130     public final void fetchesContentType() throws Exception {
131         final ReleaseAsset releaseAsset = Mockito.mock(ReleaseAsset.class);
132         final String prop = "application/zip";
133         Mockito.doReturn(
134             Json.createObjectBuilder()
135                 .add("content_type", prop)
136                 .build()
137         ).when(releaseAsset).json();
138         MatcherAssert.assertThat(
139             new ReleaseAsset.Smart(releaseAsset).contentType(),
140             Matchers.is(prop)
141         );
142     }
143 
144     /**
145      * ReleaseAsset.Smart can fetch size property from ReleaseAsset.
146      * @throws Exception If some problem inside
147      */
148     @Test
149     public final void fetchesSize() throws Exception {
150         final ReleaseAsset releaseAsset = Mockito.mock(ReleaseAsset.class);
151         final int prop = 1024;
152         Mockito.doReturn(
153             Json.createObjectBuilder()
154                 .add("size", prop)
155                 .build()
156         ).when(releaseAsset).json();
157         MatcherAssert.assertThat(
158             new ReleaseAsset.Smart(releaseAsset).size(),
159             Matchers.is(prop)
160         );
161     }
162 
163     /**
164      * ReleaseAsset.Smart can fetch download_count property from ReleaseAsset.
165      * @throws Exception If some problem inside
166      */
167     @Test
168     public final void fetchesDownloadCount() throws Exception {
169         final ReleaseAsset releaseAsset = Mockito.mock(ReleaseAsset.class);
170         final int prop = 42;
171         Mockito.doReturn(
172             Json.createObjectBuilder()
173                 .add("download_count", prop)
174                 .build()
175         ).when(releaseAsset).json();
176         MatcherAssert.assertThat(
177             new ReleaseAsset.Smart(releaseAsset).downloadCount(),
178             Matchers.is(prop)
179         );
180     }
181 
182     /**
183      * ReleaseAsset.Smart can fetch created_at property from ReleaseAsset.
184      * @throws Exception If some problem inside
185      */
186     @Test
187     public final void fetchesCreatedAt() throws Exception {
188         final ReleaseAsset releaseAsset = Mockito.mock(ReleaseAsset.class);
189         final String prop = "2013-02-27T19:35:32Z";
190         Mockito.doReturn(
191             Json.createObjectBuilder()
192                 .add("created_at", prop)
193                 .build()
194         ).when(releaseAsset).json();
195         MatcherAssert.assertThat(
196             new ReleaseAsset.Smart(releaseAsset).createdAt(),
197             Matchers.equalTo(new Github.Time(prop).date())
198         );
199     }
200 
201     /**
202      * ReleaseAsset.Smart can fetch updated_at property from ReleaseAsset.
203      * @throws Exception If some problem inside
204      */
205     @Test
206     public final void fetchesUpdatedAt() throws Exception {
207         final ReleaseAsset releaseAsset = Mockito.mock(ReleaseAsset.class);
208         final String prop = "2013-02-27T19:35:32Z";
209         Mockito.doReturn(
210             Json.createObjectBuilder()
211                 .add("updated_at", prop)
212                 .build()
213         ).when(releaseAsset).json();
214         MatcherAssert.assertThat(
215             new ReleaseAsset.Smart(releaseAsset).updatedAt(),
216             Matchers.equalTo(new Github.Time(prop).date())
217         );
218     }
219 
220     /**
221      * ReleaseAsset.Smart can update the name property of ReleaseAsset.
222      * @throws Exception If a problem occurs.
223      */
224     @Test
225     public final void updatesName() throws Exception {
226         final ReleaseAsset releaseAsset = Mockito.mock(ReleaseAsset.class);
227         final String prop = "new_name";
228         new ReleaseAsset.Smart(releaseAsset).name(prop);
229         Mockito.verify(releaseAsset).patch(
230             Json.createObjectBuilder().add("name", prop).build()
231         );
232     }
233 
234     /**
235      * ReleaseAsset.Smart can update the label property of ReleaseAsset.
236      * @throws Exception If a problem occurs.
237      */
238     @Test
239     public final void updatesLabel() throws Exception {
240         final ReleaseAsset releaseAsset = Mockito.mock(ReleaseAsset.class);
241         final String prop = "new_label";
242         new ReleaseAsset.Smart(releaseAsset).label(prop);
243         Mockito.verify(releaseAsset).patch(
244             Json.createObjectBuilder().add("label", prop).build()
245         );
246     }
247 }