View Javadoc
1   /**
2    * Copyright (c) 2013-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github;
31  
32  import com.jcabi.aspects.Tv;
33  import com.jcabi.github.OAuthScope.Scope;
34  import java.io.IOException;
35  import java.util.Collections;
36  import java.util.concurrent.ConcurrentHashMap;
37  import org.apache.commons.lang3.RandomStringUtils;
38  import org.hamcrest.MatcherAssert;
39  import org.hamcrest.Matchers;
40  import org.junit.Rule;
41  import org.junit.Test;
42  
43  /**
44   * Test case for {@link RtHooks}.
45   * @author Paul Polishchuk (ppol@ua.fm)
46   * @version $Id: ba12618770af09b8874d956b18f4a8199020399d $
47   * @since 0.8
48   */
49  @OAuthScope(Scope.ADMIN_REPO_HOOK)
50  public final class RtHooksITCase {
51  
52      /**
53       * RepoRule.
54       * @checkstyle VisibilityModifierCheck (3 lines)
55       */
56      @Rule
57      public final transient RepoRule rule = new RepoRule();
58  
59      /**
60       * RtHooks can iterate hooks.
61       * @throws Exception If some problem inside
62       */
63      @Test
64      public void canFetchAllHooks() throws Exception {
65          final Repos repos = RtHooksITCase.repos();
66          final Repo repo = this.rule.repo(repos);
67          try {
68              RtHooksITCase.createHook(repo);
69              MatcherAssert.assertThat(
70                  repo.hooks().iterate(), Matchers.<Hook>iterableWithSize(1)
71              );
72          } finally {
73              repos.remove(repo.coordinates());
74          }
75      }
76  
77      /**
78       * RtHooks can create a hook.
79       * @throws Exception If some problem inside
80       */
81      @Test
82      public void canCreateAHook() throws Exception {
83          final Repos repos = RtHooksITCase.repos();
84          final Repo repo = this.rule.repo(repos);
85          try {
86              MatcherAssert.assertThat(
87                  RtHooksITCase.createHook(repo), Matchers.notNullValue()
88              );
89          } finally {
90              repos.remove(repo.coordinates());
91          }
92      }
93  
94      /**
95       * RtHooks can fetch a single hook.
96       *
97       * @throws Exception If some problem inside.
98       */
99      @Test
100     public void canFetchSingleHook() throws Exception {
101         final Repos repos = RtHooksITCase.repos();
102         final Repo repo = this.rule.repo(repos);
103         try {
104             final int number = RtHooksITCase.createHook(repo).number();
105             MatcherAssert.assertThat(
106                 repo.hooks().get(number).json().getInt("id"),
107                 Matchers.equalTo(number)
108             );
109         } finally {
110             repos.remove(repo.coordinates());
111         }
112     }
113 
114     /**
115      * RtHooks can remove a hook by ID.
116      *
117      * @throws Exception If something goes wrong.
118      */
119     @Test
120     public void canRemoveHook() throws Exception {
121         final Repos repos = RtHooksITCase.repos();
122         final Repo repo = this.rule.repo(repos);
123         try {
124             final Hook hook = RtHooksITCase.createHook(repo);
125             repo.hooks().remove(hook.number());
126             MatcherAssert.assertThat(
127                 repo.hooks().iterate(), Matchers.not(Matchers.hasItem(hook))
128             );
129         } finally {
130             repos.remove(repo.coordinates());
131         }
132     }
133 
134     /**
135      * Return repos for tests.
136      * @return Repos
137      */
138     private static Repos repos() {
139         return new GithubIT().connect().repos();
140     }
141 
142     /**
143      * Create a new hook in a repository.
144      * @param repo Repository
145      * @return Hook
146      * @throws IOException If there is any I/O problem
147      */
148     private static Hook createHook(final Repo repo) throws IOException {
149         final ConcurrentHashMap<String, String> config =
150             new ConcurrentHashMap<>();
151         config.put(
152             "url",
153             String.format(
154                 "http://github.jcabi.invalid/hooks/%s",
155                 RandomStringUtils.random(Tv.TWENTY)
156             )
157         );
158         config.put("content_type", "json");
159         config.put("secret", "shibboleet");
160         config.put("insecure_ssl", "0");
161         return repo.hooks().create(
162             "web",
163             config,
164             Collections.<Event>emptyList(),
165             false
166         );
167     }
168 }