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 java.io.IOException;
8   import org.apache.commons.lang3.RandomStringUtils;
9   import org.hamcrest.MatcherAssert;
10  import org.hamcrest.Matchers;
11  import org.junit.jupiter.api.AfterAll;
12  import org.junit.jupiter.api.BeforeAll;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Test case for {@link RtReferences}.
17   * @since 0.6
18   * @checkstyle MultipleStringLiterals (500 lines)
19   */
20  @OAuthScope(OAuthScope.Scope.REPO)
21  @SuppressWarnings("PMD.ConsecutiveLiteralAppends")
22  final class RtReferencesITCase {
23  
24      /**
25       * RepoRule.
26       * @checkstyle VisibilityModifierCheck (3 lines)
27       */
28      private static final RepoRule RULE = new RepoRule();
29  
30      /**
31       * Test repos.
32       */
33      private static Repos repos;
34  
35      /**
36       * Test repo.
37       */
38      private static Repo repo;
39  
40      /**
41       * Set up test fixtures.
42       */
43      @BeforeAll
44      static void setUp() throws IOException {
45          final GitHub github = GitHubIT.connect();
46          RtReferencesITCase.repos = github.repos();
47          RtReferencesITCase.repo = RtReferencesITCase.RULE.repo(RtReferencesITCase.repos);
48      }
49  
50      /**
51       * Tear down test fixtures.
52       */
53      @AfterAll
54      static void tearDown() throws IOException {
55          if (RtReferencesITCase.repos != null && RtReferencesITCase.repo != null) {
56              RtReferencesITCase.repos.remove(RtReferencesITCase.repo.coordinates());
57          }
58      }
59  
60      @Test
61      void createsReference() throws IOException {
62          final References refs = RtReferencesITCase.repo.git().references();
63          final String name = RandomStringUtils.secure().nextAlphanumeric(10);
64          final StringBuilder builder = new StringBuilder("refs/tags/")
65              .append(name);
66          final Reference reference = refs.create(
67              builder.toString(),
68              refs.get("refs/heads/master").json().getJsonObject("object")
69                  .getString("sha")
70          );
71          MatcherAssert.assertThat(
72              "Value is null",
73              reference,
74              Matchers.notNullValue()
75          );
76          builder.delete(0, builder.length());
77          builder.append("tags/").append(name);
78          refs.remove(builder.toString());
79      }
80  
81      @Test
82      void iteratesReferences() throws IOException {
83          final References refs = RtReferencesITCase.repo.git().references();
84          final String name = RandomStringUtils.secure().nextAlphanumeric(10);
85          final StringBuilder builder = new StringBuilder("refs/heads/")
86              .append(name);
87          refs.create(
88              builder.toString(),
89              refs.get("refs/heads/master").json().getJsonObject("object")
90                  .getString("sha")
91          );
92          MatcherAssert.assertThat(
93              "Value is null",
94              refs.iterate(),
95              Matchers.notNullValue()
96          );
97          builder.delete(0, builder.length());
98          builder.append("heads/").append(name);
99          refs.remove(builder.toString());
100     }
101 
102     /**
103      * RtReference can iterate over references in sub-namespace.
104      */
105     @Test
106     void iteratesReferencesInSubNamespace() throws IOException {
107         final References refs = RtReferencesITCase.repo.git().references();
108         final String name = RandomStringUtils.secure().nextAlphanumeric(10);
109         final StringBuilder builder = new StringBuilder("refs/heads/")
110             .append(name);
111         refs.create(
112             builder.toString(),
113             refs.get("refs/heads/master").json().getJsonObject("object")
114                 .getString("sha")
115         );
116         MatcherAssert.assertThat(
117             "Value is null",
118             refs.iterate("heads"),
119             Matchers.notNullValue()
120         );
121         builder.delete(0, builder.length());
122         builder.append("heads/").append(name);
123         refs.remove(builder.toString());
124     }
125 
126 }