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.Github;
33  import com.jcabi.github.Release;
34  import com.jcabi.github.Releases;
35  import java.io.IOException;
36  import javax.json.Json;
37  import javax.json.JsonString;
38  import javax.json.JsonValue;
39  import org.hamcrest.MatcherAssert;
40  import org.hamcrest.Matchers;
41  import org.junit.Test;
42  
43  /**
44   * Test case for {@link MkRelease}.
45   *
46   * @author Denis Anisimov (denis.nix.anisimov@gmail.com)
47   * @version $Id: c76bb89bd5a748b993a37e97c9d8c107670d041d $
48   */
49  @SuppressWarnings("PMD.TooManyMethods")
50  public final class MkReleaseTest {
51      /**
52       * Check if a release can be deleted.
53       * @throws Exception If any problems occur.
54       */
55      @Test
56      public void canDeleteRelease() throws Exception {
57          final Releases releases = MkReleaseTest.releases();
58          final Release release = releases.create("v1.0");
59          release.delete();
60          MatcherAssert.assertThat(
61              releases.iterate().iterator().hasNext(),
62              Matchers.is(false)
63          );
64      }
65  
66      /**
67       * Smart decorator returns url.
68       * @throws Exception If some problem inside
69       */
70      @Test
71      public void canGetUrl() throws Exception {
72          final Release release = MkReleaseTest.release();
73          final Release.Smart smart = new Release.Smart(release);
74          MatcherAssert.assertThat(
75              smart.url().toString(),
76              Matchers.equalTo(this.value(release, "url"))
77          );
78      }
79  
80      /**
81       * Smart decorator returns assets url.
82       * @throws Exception If some problem inside
83       */
84      @Test
85      public void canGetAssetsUrl() throws Exception {
86          final Release release = MkReleaseTest.release();
87          final Release.Smart smart = new Release.Smart(release);
88          MatcherAssert.assertThat(
89              smart.assetsUrl().toString(),
90              Matchers.equalTo(this.value(release, "assets_url"))
91          );
92      }
93  
94      /**
95       * Smart decorator returns html url.
96       * @throws Exception If some problem inside
97       */
98      @Test
99      public void canGetHtmlUrl() throws Exception {
100         final Release release = MkReleaseTest.release();
101         final Release.Smart smart = new Release.Smart(release);
102         MatcherAssert.assertThat(
103             smart.htmlUrl().toString(),
104             Matchers.equalTo(this.value(release, "html_url"))
105         );
106     }
107 
108     /**
109      * Smart decorator returns upload url.
110      * @throws Exception If some problem inside
111      */
112     @Test
113     public void canGetUploadUrl() throws Exception {
114         final Release release = MkReleaseTest.release();
115         final Release.Smart smart = new Release.Smart(release);
116         MatcherAssert.assertThat(
117             smart.uploadUrl().toString(),
118             Matchers.equalTo(this.value(release, "upload_url"))
119         );
120     }
121 
122     /**
123      * Smart decorator returns tag name.
124      * @throws Exception If some problem inside
125      */
126     @Test
127     public void canGetTag() throws Exception {
128         final Release release = MkReleaseTest.release();
129         final Release.Smart smart = new Release.Smart(release);
130         MatcherAssert.assertThat(
131             smart.tag(),
132             Matchers.equalTo(this.value(release, "tag_name"))
133         );
134     }
135 
136     /**
137      * Smart decorator returns target commitish.
138      * @throws Exception If some problem inside
139      */
140     @Test
141     public void canGetCommitish() throws Exception {
142         final Release release = MkReleaseTest.release();
143         final Release.Smart smart = new Release.Smart(release);
144         MatcherAssert.assertThat(
145             smart.commitish(),
146             Matchers.equalTo(this.value(release, "target_commitish"))
147         );
148     }
149 
150     /**
151      * Smart decorator returns name.
152      * @throws Exception If some problem inside
153      */
154     @Test
155     public void canGetName() throws Exception {
156         final Release release = MkReleaseTest.release();
157         final Release.Smart smart = new Release.Smart(release);
158         MatcherAssert.assertThat(
159             smart.name(),
160             Matchers.equalTo(this.value(release, "name"))
161         );
162     }
163 
164     /**
165      * Smart decorator returns body.
166      * @throws Exception If some problem inside
167      */
168     @Test
169     public void canGetBody() throws Exception {
170         final Release release = MkReleaseTest.release();
171         final Release.Smart smart = new Release.Smart(release);
172         MatcherAssert.assertThat(
173             smart.body(),
174             Matchers.equalTo(this.value(release, "body"))
175         );
176     }
177 
178     /**
179      * Smart decorator returns created date.
180      * @throws Exception If some problem inside
181      */
182     @Test
183     public void canGetCreatedAt() throws Exception {
184         final Release release = MkReleaseTest.release();
185         final Release.Smart smart = new Release.Smart(release);
186         MatcherAssert.assertThat(
187             smart.createdAt(),
188             Matchers.equalTo(new Github.Time(this.value(release, "created_at"))
189                 .date()
190             )
191         );
192     }
193 
194     /**
195      * Smart decorator returns prerelease.
196      * @throws Exception If some problem inside
197      */
198     @Test
199     public void prerelease() throws Exception {
200         final Release release = MkReleaseTest.release();
201         release.patch(
202             Json.createObjectBuilder().add("prerelease", true).build()
203         );
204         MatcherAssert.assertThat(
205             new Release.Smart(release).prerelease(),
206             Matchers.is(true)
207         );
208     }
209 
210     /**
211      * Smart decorator returns published date.
212      * @throws Exception If some problem inside
213      */
214     @Test
215     public void canGetPublishedAt() throws Exception {
216         final Release release = MkReleaseTest.release();
217         final Release.Smart smart = new Release.Smart(release);
218         MatcherAssert.assertThat(
219             smart.publishedAt(),
220             Matchers.equalTo(
221                 new Github.Time(this.value(release, "published_at")).date()
222             )
223         );
224     }
225 
226     /**
227      * Returns string property value.
228      * @param release Release
229      * @param name Property name
230      * @return Value as a string
231      * @throws IOException If some problem inside
232      */
233     private String value(final Release release, final String name)
234         throws IOException {
235         final JsonValue jsonValue = release.json().get(name);
236         String result = null;
237         if (jsonValue instanceof JsonString) {
238             result = ((JsonString) jsonValue).getString();
239         }
240         return result;
241     }
242 
243     /**
244      * Create a release to work with.
245      * @return Release
246      * @throws Exception If some problem inside
247      */
248     private static Release release() throws Exception {
249         return MkReleaseTest.releases().create("v1");
250     }
251 
252     /**
253      * Creates a Releases instance set to work with.
254      * @return A test Releases instance.
255      * @throws IOException if any I/O problems occur.
256      */
257     private static Releases releases() throws IOException {
258         return new MkGithub().randomRepo().releases();
259     }
260 }