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.Releases;
34  import org.hamcrest.MatcherAssert;
35  import org.hamcrest.Matchers;
36  import org.junit.Test;
37  
38  /**
39   * Test case for {@link MkReleases}.
40   * @author Paul Polishchuk (ppol@ua.fm)
41   * @version $Id: 519a80367738919846b80f986a0be52134828b45 $
42   * @since 0.8
43   * @checkstyle MultipleStringLiteralsCheck (300 lines)
44   */
45  public final class MkReleasesTest {
46      /**
47       * MkReleases can fetch empty list of releases.
48       * @throws Exception if some problem inside
49       */
50      @Test
51      public void canFetchEmptyListOfReleases() throws Exception {
52          final Releases releases = new MkGithub().randomRepo().releases();
53          MatcherAssert.assertThat(
54              releases.iterate(),
55              Matchers.emptyIterable()
56          );
57      }
58  
59      /**
60       * MkReleases can fetch non-empty list of releases.
61       * @throws Exception If some problem inside
62       */
63      @Test
64      public void canFetchNonEmptyListOfReleases() throws Exception {
65          final Releases releases = new MkGithub().randomRepo().releases();
66          final String tag = "v1.0";
67          releases.create(tag);
68          MatcherAssert.assertThat(
69              // @checkstyle MultipleStringLiterals (1 line)
70              releases.iterate().iterator().next().json().getString("tag_name"),
71              Matchers.equalTo(tag)
72          );
73      }
74  
75      /**
76       * MkReleases can fetch a single release.
77       * @throws Exception If some problem inside
78       */
79      @Test
80      public void canFetchSingleRelease() throws Exception {
81          final Releases releases = new MkGithub().randomRepo().releases();
82          MatcherAssert.assertThat(releases.get(1), Matchers.notNullValue());
83      }
84  
85      /**
86       * MkReleases can create a release.
87       * @throws Exception If some problem inside
88       */
89      @Test
90      public void canCreateRelease() throws Exception {
91          final Releases releases = new MkGithub().randomRepo().releases();
92          final String tag = "v1.0.0";
93          final Release release = releases.create(tag);
94          MatcherAssert.assertThat(
95              release.json().getString("tag_name"),
96              Matchers.equalTo(tag)
97          );
98      }
99  
100     /**
101      * MkReleases can iterate through the releases.
102      * @throws Exception - if something goes wrong.
103      */
104     @Test
105     public void iteratesReleases() throws Exception {
106         final Releases releases = new MkGithub().randomRepo().releases();
107         releases.create("v1.0.1");
108         releases.create("v1.0.2");
109         MatcherAssert.assertThat(
110             releases.iterate(),
111             Matchers.<Release>iterableWithSize(2)
112         );
113     }
114 
115     /**
116      * MkReleases can be removed.
117      * @throws Exception - if something goes wrong.
118      */
119     @Test
120     public void canRemoveRelease() throws Exception {
121         final Releases releases = new MkGithub().randomRepo().releases();
122         releases.create("v1.1.1");
123         releases.create("v1.1.2");
124         MatcherAssert.assertThat(
125             releases.iterate(),
126             Matchers.<Release>iterableWithSize(2)
127         );
128         releases.remove(1);
129         MatcherAssert.assertThat(
130             releases.iterate(),
131             Matchers.<Release>iterableWithSize(1)
132         );
133     }
134 
135     /**
136      * MkReleases can find release by tag.
137      * @throws Exception If some problem inside
138      */
139     @Test
140     public void findsReleaseByTag() throws Exception {
141         final Releases releases = new MkGithub().randomRepo().releases();
142         final String tag = "v5.0";
143         releases.create(tag);
144         MatcherAssert.assertThat(
145             new Releases.Smart(releases).exists(tag),
146             Matchers.is(true)
147         );
148         MatcherAssert.assertThat(
149             new Release.Smart(new Releases.Smart(releases).find(tag)).tag(),
150             Matchers.equalTo(tag)
151         );
152     }
153 
154     /**
155      * The release's name should be empty upon initial creation.
156      *
157      * @throws Exception Unexpected.
158      */
159     @Test
160     public void releaseNameIsEmpty() throws Exception {
161         final Releases releases = new MkGithub().randomRepo().releases();
162         final String tag = "tag";
163         releases.create(tag);
164         MatcherAssert.assertThat(
165             new Release.Smart(releases.iterate().iterator().next())
166                 .name().isEmpty(),
167             Matchers.is(true)
168         );
169     }
170 
171     /**
172      * The release's body should be empty upon initial creation.
173      *
174      * @throws Exception Unexpected.
175      */
176     @Test
177     public void releaseBodyIsEmpty() throws Exception {
178         final Releases releases = new MkGithub().randomRepo().releases();
179         final String tag = "tag";
180         releases.create(tag);
181         MatcherAssert.assertThat(
182             new Release.Smart(releases.iterate().iterator().next())
183                 .body().isEmpty(),
184             Matchers.is(true)
185         );
186     }
187 }