1
2
3
4
5 package com.jcabi.github;
6
7 import com.jcabi.github.mock.MkGitHub;
8 import com.jcabi.http.Request;
9 import com.jcabi.http.mock.MkAnswer;
10 import com.jcabi.http.mock.MkContainer;
11 import com.jcabi.http.mock.MkGrizzlyContainer;
12 import com.jcabi.http.mock.MkQuery;
13 import com.jcabi.http.request.ApacheRequest;
14 import com.jcabi.http.request.FakeRequest;
15 import jakarta.json.Json;
16 import jakarta.json.JsonObject;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.HttpURLConnection;
20 import java.nio.charset.StandardCharsets;
21 import org.apache.commons.io.IOUtils;
22 import org.hamcrest.MatcherAssert;
23 import org.hamcrest.Matchers;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mockito;
27
28
29
30
31
32
33
34 @ExtendWith(RandomPort.class)
35 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
36 final class RtReleaseAssetTest {
37
38
39
40
41
42 @Test
43 void canRepresentAsJson() throws Exception {
44 final RtReleaseAsset asset = new RtReleaseAsset(
45 new FakeRequest().withBody("{\"asset\":\"release\"}"),
46 RtReleaseAssetTest.release(),
47 1
48 );
49 MatcherAssert.assertThat(
50 "Values are not equal",
51 asset.json().getString("asset"),
52 Matchers.equalTo("release")
53 );
54 }
55
56
57
58
59
60 @Test
61 void canObtainOwnRelease() throws Exception {
62 final Release release = RtReleaseAssetTest.release();
63 final RtReleaseAsset asset = new RtReleaseAsset(
64 new FakeRequest(),
65 release,
66 1
67 );
68 MatcherAssert.assertThat(
69 "Values are not equal",
70 asset.release(),
71 Matchers.is(release)
72 );
73 }
74
75
76
77
78
79 @Test
80 void patchesAsset() throws Exception {
81 try (
82 MkContainer container = new MkGrizzlyContainer().next(
83 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
84 ).start(RandomPort.port())
85 ) {
86 final RtReleaseAsset asset = new RtReleaseAsset(
87 new ApacheRequest(container.home()),
88 RtReleaseAssetTest.release(),
89 2
90 );
91 final JsonObject json = Json.createObjectBuilder()
92 .add("name", "hello").build();
93 asset.patch(json);
94 final MkQuery query = container.take();
95 MatcherAssert.assertThat(
96 "Values are not equal",
97 query.method(),
98 Matchers.equalTo(Request.PATCH)
99 );
100 MatcherAssert.assertThat(
101 "String does not contain expected value",
102 query.body(),
103 Matchers.containsString("{\"name\":\"hello\"}")
104 );
105 MatcherAssert.assertThat(
106 "String does not end with expected value",
107 query.uri().toString(),
108 Matchers.endsWith("/repos/john/blueharvest/releases/assets/2")
109 );
110 container.stop();
111 }
112 }
113
114
115
116
117
118 @Test
119 void removesAsset() throws Exception {
120 try (
121 MkContainer container = new MkGrizzlyContainer().next(
122 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
123 ).start(RandomPort.port())
124 ) {
125 final RtReleaseAsset asset = new RtReleaseAsset(
126 new ApacheRequest(container.home()),
127 RtReleaseAssetTest.release(),
128 3
129 );
130 asset.remove();
131 final MkQuery query = container.take();
132 MatcherAssert.assertThat(
133 "Values are not equal",
134 query.method(),
135 Matchers.equalTo(Request.DELETE)
136 );
137 container.stop();
138 }
139 }
140
141
142
143
144
145 @Test
146 void rawAsset() throws Exception {
147 try (
148 MkContainer container = new MkGrizzlyContainer().next(
149 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
150 ).start(RandomPort.port())
151 ) {
152 final RtReleaseAsset asset = new RtReleaseAsset(
153 new ApacheRequest(container.home()),
154 RtReleaseAssetTest.release(),
155 4
156 );
157 try (InputStream stream = asset.raw()) {
158 final MkQuery query = container.take();
159 MatcherAssert.assertThat(
160 "Values are not equal",
161 query.method(),
162 Matchers.equalTo(Request.GET)
163 );
164 MatcherAssert.assertThat(
165 "Value is null",
166 IOUtils.toString(stream, StandardCharsets.UTF_8),
167 Matchers.notNullValue()
168 );
169 }
170 container.stop();
171 }
172 }
173
174
175
176
177
178 private static Release release() throws IOException {
179 final Release release = Mockito.mock(Release.class);
180 final Repo repo = new MkGitHub("john").repos().create(
181 new Repos.RepoCreate("blueharvest", false)
182 );
183 Mockito.doReturn(repo).when(release).repo();
184 Mockito.doReturn(1).when(release).number();
185 return release;
186 }
187 }