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.ReleaseAsset;
34 import com.jcabi.github.ReleaseAssets;
35 import java.nio.charset.StandardCharsets;
36 import javax.xml.bind.DatatypeConverter;
37 import org.apache.commons.io.IOUtils;
38 import org.hamcrest.MatcherAssert;
39 import org.hamcrest.Matchers;
40 import org.junit.Test;
41
42 /**
43 * Test case for {@link MkReleaseAssets}.
44 *
45 * @author Carlos Miranda (miranda.cma@gmail.com)
46 * @version $Id: 5d4d37761f100f82cc6fbc8954bde156f67e1728 $
47 * @since 0.8
48 * @checkstyle MultipleStringLiteralsCheck (200 lines)
49 * @checkstyle MethodNameCheck (200 lines)
50 */
51 public final class MkReleaseAssetsTest {
52
53 /**
54 * MkReleaseAssets can upload a new Release Asset.
55 *
56 * @throws Exception If a problem occurs.
57 */
58 @Test
59 public void uploadsNewAsset() throws Exception {
60 final ReleaseAssets assets = release().assets();
61 final ReleaseAsset asset = assets.upload(
62 "testUpload".getBytes(), "text/plain", "upload.txt"
63 );
64 MatcherAssert.assertThat(
65 asset.number(),
66 Matchers.is(1)
67 );
68 }
69
70 /**
71 * MkReleaseAssets can fetch a single Release Asset.
72 *
73 * @throws Exception If a problem occurs.
74 */
75 @Test
76 public void fetchesSingleAsset() throws Exception {
77 final ReleaseAssets assets = release().assets();
78 final ReleaseAsset asset = assets.upload(
79 "testGet".getBytes(), "text/plain", "get.txt"
80 );
81 MatcherAssert.assertThat(
82 assets.get(asset.number()),
83 Matchers.is(asset)
84 );
85 }
86
87 /**
88 * MkReleaseAssets can iterate through Release Assets.
89 *
90 * @throws Exception If a problem occurs.
91 */
92 @Test
93 public void iteratesAssets() throws Exception {
94 final ReleaseAssets assets = release().assets();
95 assets.upload(
96 "testIterate".getBytes(), "text/plain", "iterate.txt"
97 );
98 MatcherAssert.assertThat(
99 assets.iterate(),
100 Matchers.not(Matchers.emptyIterable())
101 );
102 }
103
104 /**
105 * MkReleaseAssets can fetch its own Release.
106 *
107 * @throws Exception If a problem occurs.
108 */
109 @Test
110 public void fetchesRelease() throws Exception {
111 final Release rel = release();
112 MatcherAssert.assertThat(
113 rel.assets().release(),
114 Matchers.is(rel)
115 );
116 }
117
118 /**
119 * Must encode the input bytes into Base64.
120 * @throws Exception Unexpected.
121 */
122 @Test
123 public void encodesContentsAsBase64() throws Exception {
124 final String test = "This is a test asset.";
125 final ReleaseAsset asset = new MkGithub().randomRepo().releases()
126 .create("v1.0")
127 .assets()
128 .upload(test.getBytes(), "type", "name");
129 MatcherAssert.assertThat(
130 IOUtils.toString(asset.raw(), StandardCharsets.UTF_8),
131 Matchers.is(DatatypeConverter.printBase64Binary(test.getBytes()))
132 );
133 }
134
135 /**
136 * Create a Release to work with.
137 * @return Repo
138 * @throws Exception If some problem inside
139 */
140 private static Release release() throws Exception {
141 return new MkGithub().randomRepo().releases().create("v1.0");
142 }
143 }