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 com.jcabi.aspects.Tv;
33 import com.jcabi.github.mock.MkGithub;
34 import com.jcabi.http.request.FakeRequest;
35 import java.net.HttpURLConnection;
36 import org.hamcrest.MatcherAssert;
37 import org.hamcrest.Matchers;
38 import org.junit.Test;
39 import org.mockito.Mockito;
40
41 /**
42 * Test case for {@link RtReleaseAssets}.
43 * @author Carlos Miranda (miranda.cma@gmail.com)
44 * @version $Id: 3b4e16175d8911f3135f6425463ccea8fe342441 $
45 * @since 0.8
46 */
47 public final class RtReleaseAssetsTest {
48
49 /**
50 * RtRelease can list assets for a release.
51 *
52 * @throws Exception If something goes wrong.
53 */
54 @Test
55 public void listReleaseAssets() throws Exception {
56 final ReleaseAssets assets = new RtReleaseAssets(
57 new FakeRequest().withStatus(HttpURLConnection.HTTP_OK)
58 .withBody("[{\"id\":1},{\"id\":2}]"), release()
59 );
60 MatcherAssert.assertThat(
61 assets.iterate(),
62 Matchers.<ReleaseAsset>iterableWithSize(2)
63 );
64 }
65
66 /**
67 * RtRelease can upload a release asset.
68 *
69 * @throws Exception If something goes wrong
70 */
71 @Test
72 public void uploadReleaseAsset() throws Exception {
73 final String body = "{\"id\":1}";
74 final ReleaseAssets assets = new RtReleaseAssets(
75 new FakeRequest().withStatus(HttpURLConnection.HTTP_CREATED)
76 .withBody(body),
77 release()
78 );
79 MatcherAssert.assertThat(
80 assets.upload(body.getBytes(), "text/plain", "hello.txt")
81 .number(),
82 Matchers.is(1)
83 );
84 }
85
86 /**
87 * RtRelease can get a single release asset.
88 *
89 * @throws Exception if something goes wrong.
90 */
91 @Test
92 public void getReleaseAsset() throws Exception {
93 final ReleaseAssets assets = new RtReleaseAssets(
94 new FakeRequest().withStatus(HttpURLConnection.HTTP_OK)
95 .withBody("{\"id\":3}"),
96 release()
97 );
98 MatcherAssert.assertThat(
99 assets.get(Tv.THREE).number(),
100 Matchers.is(Tv.THREE)
101 );
102 }
103
104 /**
105 * This method returns a Release for testing.
106 * @return Release to be used for test.
107 * @throws Exception - if anything goes wrong.
108 */
109 private static Release release() throws Exception {
110 final Release release = Mockito.mock(Release.class);
111 final Repo repo = new MkGithub("john").randomRepo();
112 Mockito.doReturn(repo).when(release).repo();
113 Mockito.doReturn(1).when(release).number();
114 return release;
115 }
116 }