View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
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 java.io.IOException;
11  import java.nio.charset.StandardCharsets;
12  import javax.xml.bind.DatatypeConverter;
13  import org.apache.commons.io.IOUtils;
14  import org.hamcrest.MatcherAssert;
15  import org.hamcrest.Matchers;
16  import org.junit.jupiter.api.Test;
17  
18  /**
19   * Test case for {@link MkReleaseAssets}.
20   * @since 0.8
21   * @checkstyle MultipleStringLiteralsCheck (200 lines)
22   * @checkstyle MethodNameCheck (200 lines)
23   */
24  final class MkReleaseAssetsTest {
25  
26      /**
27       * MkReleaseAssets can upload a new Release Asset.
28       * @throws Exception If a problem occurs.
29       */
30      @Test
31      void uploadsNewAsset() throws Exception {
32          final ReleaseAssets assets = MkReleaseAssetsTest.release().assets();
33          final ReleaseAsset asset = assets.upload(
34              "testUpload".getBytes(), "text/plain", "upload.txt"
35          );
36          MatcherAssert.assertThat(
37              "Values are not equal",
38              asset.number(),
39              Matchers.is(1)
40          );
41      }
42  
43      /**
44       * MkReleaseAssets can fetch a single Release Asset.
45       * @throws Exception If a problem occurs.
46       */
47      @Test
48      void fetchesSingleAsset() throws Exception {
49          final ReleaseAssets assets = MkReleaseAssetsTest.release().assets();
50          final ReleaseAsset asset = assets.upload(
51              "testGet".getBytes(), "text/plain", "get.txt"
52          );
53          MatcherAssert.assertThat(
54              "Values are not equal",
55              assets.get(asset.number()),
56              Matchers.is(asset)
57          );
58      }
59  
60      /**
61       * MkReleaseAssets can iterate through Release Assets.
62       * @throws Exception If a problem occurs.
63       */
64      @Test
65      void iteratesAssets() throws Exception {
66          final ReleaseAssets assets = MkReleaseAssetsTest.release().assets();
67          assets.upload(
68              "testIterate".getBytes(), "text/plain", "iterate.txt"
69          );
70          MatcherAssert.assertThat(
71              "Collection is not empty",
72              assets.iterate(),
73              Matchers.not(Matchers.emptyIterable())
74          );
75      }
76  
77      /**
78       * MkReleaseAssets can fetch its own Release.
79       * @throws Exception If a problem occurs.
80       */
81      @Test
82      void fetchesRelease() throws Exception {
83          final Release rel = MkReleaseAssetsTest.release();
84          MatcherAssert.assertThat(
85              "Values are not equal",
86              rel.assets().release(),
87              Matchers.is(rel)
88          );
89      }
90  
91      /**
92       * Must encode the input bytes into Base64.
93       */
94      @Test
95      void encodesContentsAsBase64() throws IOException {
96          final String test = "This is a test asset.";
97          final ReleaseAsset asset = new MkGitHub().randomRepo().releases()
98              .create("v1.0")
99              .assets()
100             .upload(test.getBytes(), "type", "name");
101         MatcherAssert.assertThat(
102             "Values are not equal",
103             IOUtils.toString(asset.raw(), StandardCharsets.UTF_8),
104             Matchers.is(DatatypeConverter.printBase64Binary(test.getBytes()))
105         );
106     }
107 
108     /**
109      * Create a Release to work with.
110      * @return Repo
111      */
112     private static Release release() throws IOException {
113         return new MkGitHub().randomRepo().releases().create("v1.0");
114     }
115 }