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;
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
54
55
56
57
58
59
60
61 public final class RtReleaseAssetTest {
62
63
64
65
66 @Rule
67 public final transient RandomPort resource = new RandomPort();
68
69
70
71
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
88
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
106
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
141
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
167
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
196
197
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 }