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;
31  
32  import com.jcabi.github.mock.MkGithub;
33  import com.jcabi.http.Request;
34  import com.jcabi.http.mock.MkAnswer;
35  import com.jcabi.http.mock.MkContainer;
36  import com.jcabi.http.mock.MkGrizzlyContainer;
37  import com.jcabi.http.mock.MkQuery;
38  import com.jcabi.http.request.ApacheRequest;
39  import com.jcabi.http.request.FakeRequest;
40  import java.io.InputStream;
41  import java.net.HttpURLConnection;
42  import java.nio.charset.StandardCharsets;
43  import javax.json.Json;
44  import javax.json.JsonObject;
45  import org.apache.commons.io.IOUtils;
46  import org.hamcrest.MatcherAssert;
47  import org.hamcrest.Matchers;
48  import org.junit.Rule;
49  import org.junit.Test;
50  import org.mockito.Mockito;
51  
52  /**
53   * Test case for {@link RtReleaseAsset}.
54   * @author Carlos Miranda (miranda.cma@gmail.com)
55   * @author Paulo Lobo (pauloeduardolobo@gmail.com)
56   * @version $Id: a4f29c3d845ca561ac8f8c579f2ff1065615dc1b $
57   * @since 0.8
58   * @checkstyle MultipleStringLiteralsCheck (200 lines)
59   * @checkstyle ClassDataAbstractionCouplingCheck (3 lines)
60   */
61  public final class RtReleaseAssetTest {
62      /**
63       * The rule for skipping test if there's BindException.
64       * @checkstyle VisibilityModifierCheck (3 lines)
65       */
66      @Rule
67      public final transient RandomPort resource = new RandomPort();
68  
69      /**
70       * RtReleaseAsset can be described in JSON form.
71       * @throws Exception if a problem occurs.
72       */
73      @Test
74      public void canRepresentAsJson() throws Exception {
75          final RtReleaseAsset asset = new RtReleaseAsset(
76              new FakeRequest().withBody("{\"asset\":\"release\"}"),
77              release(),
78              1
79          );
80          MatcherAssert.assertThat(
81              asset.json().getString("asset"),
82              Matchers.equalTo("release")
83          );
84      }
85  
86      /**
87       * RtReleaseAsset can obtain its own release.
88       * @throws Exception if a problem occurs.
89       */
90      @Test
91      public void canObtainOwnRelease() throws Exception {
92          final Release release = release();
93          final RtReleaseAsset asset = new RtReleaseAsset(
94              new FakeRequest(),
95              release,
96              1
97          );
98          MatcherAssert.assertThat(
99              asset.release(),
100             Matchers.is(release)
101         );
102     }
103 
104     /**
105      * RtReleaseAsset can create a patch request.
106      * @throws Exception If a problem occurs.
107      */
108     @Test
109     public void patchesAsset() throws Exception {
110         try (
111             final MkContainer container = new MkGrizzlyContainer().next(
112                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
113             ).start(this.resource.port())
114         ) {
115             final RtReleaseAsset asset = new RtReleaseAsset(
116                 new ApacheRequest(container.home()),
117                 release(),
118                 2
119             );
120             final JsonObject json = Json.createObjectBuilder()
121                 .add("name", "hello").build();
122             asset.patch(json);
123             final MkQuery query = container.take();
124             MatcherAssert.assertThat(
125                 query.method(), Matchers.equalTo(Request.PATCH)
126             );
127             MatcherAssert.assertThat(
128                 query.body(),
129                 Matchers.containsString("{\"name\":\"hello\"}")
130             );
131             MatcherAssert.assertThat(
132                 query.uri().toString(),
133                 Matchers.endsWith("/repos/john/blueharvest/releases/assets/2")
134             );
135             container.stop();
136         }
137     }
138 
139     /**
140      * RtReleaseAsset can remove itself.
141      * @throws Exception If a problem occurs.
142      */
143     @Test
144     public void removesAsset() throws Exception {
145         try (
146             final MkContainer container = new MkGrizzlyContainer().next(
147                 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
148             ).start(this.resource.port());
149         ) {
150             final RtReleaseAsset asset = new RtReleaseAsset(
151                 new ApacheRequest(container.home()),
152                 release(),
153                 3
154             );
155             asset.remove();
156             final MkQuery query = container.take();
157             MatcherAssert.assertThat(
158                 query.method(),
159                 Matchers.equalTo(Request.DELETE)
160             );
161             container.stop();
162         }
163     }
164 
165     /**
166      * RtReleaseAsset can stream raw content.
167      * @throws Exception If a problem occurs.
168      */
169     @Test
170     public void rawAsset() throws Exception {
171         try (
172             final MkContainer container = new MkGrizzlyContainer().next(
173                 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
174             ).start(this.resource.port());
175         ) {
176             final RtReleaseAsset asset = new RtReleaseAsset(
177                 new ApacheRequest(container.home()),
178                 release(),
179                 4
180             );
181             final InputStream stream = asset.raw();
182             final MkQuery query = container.take();
183             MatcherAssert.assertThat(
184                 query.method(), Matchers.equalTo(Request.GET)
185             );
186             MatcherAssert.assertThat(
187                 IOUtils.toString(stream, StandardCharsets.UTF_8),
188                 Matchers.notNullValue()
189             );
190             container.stop();
191         }
192     }
193 
194     /**
195      * This method returns a Release for testing.
196      * @return Release to be used for test.
197      * @throws Exception - if anything goes wrong.
198      */
199     private static Release release() throws Exception {
200         final Release release = Mockito.mock(Release.class);
201         final Repo repo = new MkGithub("john").repos().create(
202             new Repos.RepoCreate("blueharvest", false)
203         );
204         Mockito.doReturn(repo).when(release).repo();
205         Mockito.doReturn(1).when(release).number();
206         return release;
207     }
208 }