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 java.util.Collections;
9   import java.util.concurrent.ConcurrentHashMap;
10  import org.apache.commons.lang3.RandomStringUtils;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Test case for {@link RtHooks}.
17   * @since 0.8
18   */
19  @OAuthScope(OAuthScope.Scope.ADMIN_REPO_HOOK)
20  final class RtHooksITCase {
21  
22      /**
23       * RepoRule.
24       * @checkstyle VisibilityModifierCheck (3 lines)
25       */
26      public final transient RepoRule rule = new RepoRule();
27  
28      @Test
29      void canFetchAllHooks() throws IOException {
30          final Repos repos = RtHooksITCase.repos();
31          final Repo repo = this.rule.repo(repos);
32          try {
33              RtHooksITCase.createHook(repo);
34              MatcherAssert.assertThat(
35                  "Collection size is incorrect",
36                  repo.hooks().iterate(), Matchers.iterableWithSize(1)
37              );
38          } finally {
39              repos.remove(repo.coordinates());
40          }
41      }
42  
43      @Test
44      void canCreateAHook() throws IOException {
45          final Repos repos = RtHooksITCase.repos();
46          final Repo repo = this.rule.repo(repos);
47          try {
48              MatcherAssert.assertThat(
49                  "Value is null",
50                  RtHooksITCase.createHook(repo), Matchers.notNullValue()
51              );
52          } finally {
53              repos.remove(repo.coordinates());
54          }
55      }
56  
57      @Test
58      void canFetchSingleHook() throws IOException {
59          final Repos repos = RtHooksITCase.repos();
60          final Repo repo = this.rule.repo(repos);
61          try {
62              final int number = RtHooksITCase.createHook(repo).number();
63              MatcherAssert.assertThat(
64                  "Values are not equal",
65                  repo.hooks().get(number).json().getInt("id"),
66                  Matchers.equalTo(number)
67              );
68          } finally {
69              repos.remove(repo.coordinates());
70          }
71      }
72  
73      @Test
74      void canRemoveHook() throws IOException {
75          final Repos repos = RtHooksITCase.repos();
76          final Repo repo = this.rule.repo(repos);
77          try {
78              final Hook hook = RtHooksITCase.createHook(repo);
79              repo.hooks().remove(hook.number());
80              MatcherAssert.assertThat(
81                  "Collection does not contain expected item",
82                  repo.hooks().iterate(), Matchers.not(Matchers.hasItem(hook))
83              );
84          } finally {
85              repos.remove(repo.coordinates());
86          }
87      }
88  
89      /**
90       * Return repos for tests.
91       * @return Repos
92       */
93      private static Repos repos() {
94          return GitHubIT.connect().repos();
95      }
96  
97      /**
98       * Create a new hook in a repository.
99       * @param repo Repository
100      * @return Hook
101      * @throws IOException If there is any I/O problem
102      */
103     private static Hook createHook(final Repo repo) throws IOException {
104         final ConcurrentHashMap<String, String> config =
105             new ConcurrentHashMap<>();
106         config.put(
107             "url",
108             String.format(
109                 "http://github.jcabi.invalid/hooks/%s",
110                 RandomStringUtils.secure().next(20)
111             )
112         );
113         config.put("content_type", "json");
114         config.put("secret", "shibboleet");
115         config.put("insecure_ssl", "0");
116         return repo.hooks().create(
117             "web",
118             config,
119             Collections.emptyList(),
120             false
121         );
122     }
123 }