1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
45
46
47
48
49 @OAuthScope(Scope.ADMIN_REPO_HOOK)
50 public final class RtHooksITCase {
51
52
53
54
55
56 @Rule
57 public final transient RepoRule rule = new RepoRule();
58
59
60
61
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
79
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
96
97
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
116
117
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
136
137
138 private static Repos repos() {
139 return new GithubIT().connect().repos();
140 }
141
142
143
144
145
146
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 }