View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
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   * Test case for {@link RtReleaseAsset}.
30   * @since 0.8
31   * @checkstyle MultipleStringLiteralsCheck (200 lines)
32   * @checkstyle ClassDataAbstractionCouplingCheck (3 lines)
33   */
34  @ExtendWith(RandomPort.class)
35  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
36  final class RtReleaseAssetTest {
37  
38      /**
39       * RtReleaseAsset can be described in JSON form.
40       * @throws Exception if a problem occurs.
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       * RtReleaseAsset can obtain its own release.
58       * @throws Exception if a problem occurs.
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       * RtReleaseAsset can create a patch request.
77       * @throws Exception If a problem occurs.
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      * RtReleaseAsset can remove itself.
116      * @throws Exception If a problem occurs.
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      * RtReleaseAsset can stream raw content.
143      * @throws Exception If a problem occurs.
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      * This method returns a Release for testing.
176      * @return Release to be used for test.
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 }