1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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.json.Json;
37 import javax.xml.bind.DatatypeConverter;
38 import org.apache.commons.io.IOUtils;
39 import org.hamcrest.MatcherAssert;
40 import org.hamcrest.Matchers;
41 import org.junit.Test;
42
43
44
45
46
47
48
49
50
51 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
52 public final class MkReleaseAssetTest {
53
54
55
56
57
58
59 @Test
60 public void fetchesRelease() throws Exception {
61 final Release rel = release();
62 MatcherAssert.assertThat(
63 rel.assets().get(1).release(),
64 Matchers.is(rel)
65 );
66 }
67
68
69
70
71
72
73 @Test
74 public void fetchesNumber() throws Exception {
75 final Release rel = release();
76 MatcherAssert.assertThat(
77 rel.assets().get(1).number(),
78 Matchers.is(1)
79 );
80 }
81
82
83
84
85
86
87 @Test
88 public void removesAsset() throws Exception {
89 final ReleaseAssets assets = release().assets();
90 final ReleaseAsset asset = assets.upload(
91 "testRemove".getBytes(), "text/plain", "remove.txt"
92 );
93 MatcherAssert.assertThat(
94 assets.iterate(),
95 Matchers.<ReleaseAsset>iterableWithSize(1)
96 );
97 asset.remove();
98 MatcherAssert.assertThat(
99 assets.iterate(),
100 Matchers.emptyIterable()
101 );
102 }
103
104
105
106
107
108
109 @Test
110 public void removesSeveralAssets() throws Exception {
111 final ReleaseAssets assets = release().assets();
112
113 final int limit = 3;
114 final ReleaseAsset[] bodies = new ReleaseAsset[limit];
115 for (int idx = 0; idx < limit; ++idx) {
116 bodies[idx] = assets.upload(
117 "testRemove".getBytes(), "text/plain", "remove.txt"
118 );
119 }
120 MatcherAssert.assertThat(
121 assets.iterate(),
122 Matchers.<ReleaseAsset>iterableWithSize(limit)
123 );
124 for (int idx = 0; idx < limit; ++idx) {
125 bodies[idx].remove();
126 }
127 MatcherAssert.assertThat(
128 assets.iterate(),
129 Matchers.emptyIterable()
130 );
131 }
132
133
134
135
136
137
138 @Test
139 public void canRepresentAsJson() throws Exception {
140 final String name = "json.txt";
141 final String type = "text/plain";
142 final ReleaseAsset asset = release().assets().upload(
143 "testJson".getBytes(), type, name
144 );
145 MatcherAssert.assertThat(
146 asset.json().getString("content_type"),
147 Matchers.is(type)
148 );
149 MatcherAssert.assertThat(
150 asset.json().getString("name"),
151 Matchers.is(name)
152 );
153 }
154
155
156
157
158
159
160 @Test
161 public void canPatchJson() throws Exception {
162 final String orig = "orig.txt";
163 final ReleaseAsset asset = release().assets().upload(
164 "testPatch".getBytes(), "text/plain", orig
165 );
166 final String attribute = "name";
167 MatcherAssert.assertThat(
168 asset.json().getString(attribute),
169 Matchers.is(orig)
170 );
171 final String patched = "patched.txt";
172 asset.patch(
173 Json.createObjectBuilder().add(attribute, patched).build()
174 );
175 MatcherAssert.assertThat(
176 asset.json().getString(attribute),
177 Matchers.is(patched)
178 );
179 }
180
181
182
183
184
185
186
187 @Test
188 public void fetchesRawRepresentation() throws Exception {
189 final String test = "This is a test asset.";
190 final ReleaseAsset asset = new MkGithub().randomRepo().releases()
191 .create("v1.0")
192 .assets()
193 .upload(test.getBytes(), "type", "name");
194 MatcherAssert.assertThat(
195 new String(
196 DatatypeConverter.parseBase64Binary(
197 IOUtils.toString(asset.raw(), StandardCharsets.UTF_8)
198 )
199 ),
200 Matchers.is(test)
201 );
202 }
203
204
205
206
207
208
209 private static Release release() throws Exception {
210 return new MkGithub().randomRepo().releases().create("v1.0");
211 }
212 }