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 jakarta.json.Json;
8   import java.io.IOException;
9   import org.apache.commons.lang3.RandomStringUtils;
10  import org.hamcrest.MatcherAssert;
11  import org.hamcrest.Matchers;
12  import org.junit.jupiter.api.AfterAll;
13  import org.junit.jupiter.api.BeforeAll;
14  import org.junit.jupiter.api.Test;
15  
16  /**
17   * Integration test for {@link RtReleaseAsset}.
18   *
19   * @since 0.8
20   * @checkstyle MultipleStringLiterals (300 lines)
21   */
22  @OAuthScope(OAuthScope.Scope.REPO)
23  final class RtReleaseAssetITCase {
24  
25      /**
26       * Test repos.
27       */
28      private static Repos repos;
29  
30      /**
31       * Test repo.
32       */
33      private static Repo repo;
34  
35      /**
36       * RepoRule.
37       * @checkstyle VisibilityModifierCheck (3 lines)
38       */
39      private static RepoRule rule = new RepoRule();
40  
41      /**
42       * Set up test fixtures.
43       */
44      @BeforeAll
45      static void setUp() throws IOException {
46          final GitHub github = GitHubIT.connect();
47          RtReleaseAssetITCase.repos = github.repos();
48          RtReleaseAssetITCase.repo = RtReleaseAssetITCase.rule.repo(RtReleaseAssetITCase.repos);
49          RtReleaseAssetITCase.repo.releases().create(
50              RandomStringUtils.secure().nextAlphanumeric(10)
51          );
52      }
53  
54      /**
55       * Tear down test fixtures.
56       */
57      @AfterAll
58      static void tearDown() throws IOException {
59          if (RtReleaseAssetITCase.repos != null && RtReleaseAssetITCase.repo != null) {
60              RtReleaseAssetITCase.repos.remove(RtReleaseAssetITCase.repo.coordinates());
61          }
62      }
63  
64      @Test
65      void fetchAsJson() throws IOException {
66          final String name = RandomStringUtils.secure().nextAlphanumeric(10);
67          final Release release = RtReleaseAssetITCase.repo.releases().create(name);
68          try {
69              MatcherAssert.assertThat(
70                  "Values are not equal",
71                  release.json().getInt("id"),
72                  Matchers.equalTo(release.number())
73              );
74          } finally {
75              release.delete();
76          }
77      }
78  
79      @Test
80      void executePatchRequest() throws IOException {
81          final Release release = RtReleaseAssetITCase.repo.releases().create(
82              String.format("v%s", RandomStringUtils.secure().nextAlphanumeric(10))
83          );
84          try {
85              final String desc = "Description of the release";
86              release.patch(Json.createObjectBuilder().add("body", desc).build());
87              MatcherAssert.assertThat(
88                  "String does not start with expected value",
89                  new Release.Smart(release).body(),
90                  Matchers.startsWith(desc)
91              );
92          } finally {
93              release.delete();
94          }
95      }
96  
97      @Test
98      void removesReleaseAsset() throws IOException {
99          final Releases releases = RtReleaseAssetITCase.repo.releases();
100         final String rname = RandomStringUtils.secure().nextAlphanumeric(10);
101         final Release release = releases.create(rname);
102         try {
103             MatcherAssert.assertThat(
104                 "Value is null",
105                 releases.get(release.number()),
106                 Matchers.notNullValue()
107             );
108         } finally {
109             release.delete();
110         }
111         MatcherAssert.assertThat(
112             "Assertion failed",
113             releases.iterate(),
114             Matchers.not(Matchers.contains(release))
115         );
116     }
117 
118 }