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