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.Immutable;
33 import com.jcabi.http.Request;
34 import com.jcabi.http.mock.MkAnswer;
35 import com.jcabi.http.mock.MkContainer;
36 import com.jcabi.http.mock.MkGrizzlyContainer;
37 import com.jcabi.http.mock.MkQuery;
38 import com.jcabi.http.request.JdkRequest;
39 import java.net.HttpURLConnection;
40 import java.util.Collections;
41 import java.util.Map;
42 import java.util.concurrent.ConcurrentHashMap;
43 import javax.json.Json;
44 import javax.json.JsonObject;
45 import javax.json.JsonObjectBuilder;
46 import org.hamcrest.MatcherAssert;
47 import org.hamcrest.Matchers;
48 import org.junit.Rule;
49 import org.junit.Test;
50 import org.mockito.Mockito;
51
52
53
54
55
56
57
58
59
60 @Immutable
61 public final class RtHooksTest {
62
63
64
65
66
67 @Rule
68 public final transient RandomPort resource = new RandomPort();
69
70
71
72
73
74 @Test
75 public void canFetchEmptyListOfHooks() throws Exception {
76 try (
77 final MkContainer container = new MkGrizzlyContainer().next(
78 new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "[]")
79 ).start(this.resource.port())
80 ) {
81 final Hooks hooks = new RtHooks(
82 new JdkRequest(container.home()),
83 RtHooksTest.repo()
84 );
85 MatcherAssert.assertThat(
86 hooks.iterate(),
87 Matchers.emptyIterable()
88 );
89 container.stop();
90 }
91 }
92
93
94
95
96
97 @Test
98 public void canFetchNonEmptyListOfHooks() throws Exception {
99 try (
100 final MkContainer container = new MkGrizzlyContainer().next(
101 new MkAnswer.Simple(
102 HttpURLConnection.HTTP_OK,
103 Json.createArrayBuilder()
104 .add(
105 hook(
106 "hook 1",
107 Collections.<String, String>emptyMap()
108 )
109 )
110 .add(
111 hook(
112 "hook 2",
113 Collections.<String, String>emptyMap()
114 )
115 )
116 .build().toString()
117 )
118 ).start(this.resource.port())
119 ) {
120 final RtHooks hooks = new RtHooks(
121 new JdkRequest(container.home()),
122 RtHooksTest.repo()
123 );
124 MatcherAssert.assertThat(
125 hooks.iterate(),
126 Matchers.<Hook>iterableWithSize(2)
127 );
128 container.stop();
129 }
130 }
131
132
133
134
135
136 @Test
137 public void canFetchSingleHook() throws Exception {
138 final String name = "hook name";
139 try (
140 final MkContainer container = new MkGrizzlyContainer().next(
141 new MkAnswer.Simple(
142 HttpURLConnection.HTTP_OK,
143 RtHooksTest.hook(
144 name,
145 Collections.<String, String>emptyMap()
146 ).toString()
147 )
148 ).start(this.resource.port())
149 ) {
150 final Hooks hooks = new RtHooks(
151 new JdkRequest(container.home()),
152 RtHooksTest.repo()
153 );
154 final Hook hook = hooks.get(1);
155 MatcherAssert.assertThat(
156 new Hook.Smart(hook).name(),
157 Matchers.equalTo(name)
158 );
159 container.stop();
160 }
161 }
162
163
164
165
166
167
168 @Test
169 public void canCreateHook() throws Exception {
170 final String name = "hook name";
171 final ConcurrentHashMap<String, String> config =
172 new ConcurrentHashMap<>(2);
173 config.put("url", "http://example.com");
174 config.put("content_type", "json");
175 final String body = RtHooksTest.hook(name, config).toString();
176 try (
177 final MkContainer container = new MkGrizzlyContainer().next(
178 new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, body)
179 ).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, body))
180 .start(this.resource.port())
181 ) {
182 final Hooks hooks = new RtHooks(
183 new JdkRequest(container.home()),
184 RtHooksTest.repo()
185 );
186 final Hook hook = hooks.create(
187 name, config, Collections.<Event>emptyList(), true
188 );
189 MatcherAssert.assertThat(
190 container.take().method(),
191 Matchers.equalTo(Request.POST)
192 );
193 MatcherAssert.assertThat(
194 new Hook.Smart(hook).name(),
195 Matchers.equalTo(name)
196 );
197 container.stop();
198 }
199 }
200
201
202
203
204
205
206 @Test
207 public void canDeleteHook() throws Exception {
208 try (
209 final MkContainer container = new MkGrizzlyContainer().next(
210 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
211 ).start(this.resource.port())
212 ) {
213 final Hooks hooks = new RtHooks(
214 new JdkRequest(container.home()),
215 RtHooksTest.repo()
216 );
217 hooks.remove(1);
218 final MkQuery query = container.take();
219 MatcherAssert.assertThat(
220 query.method(),
221 Matchers.equalTo(Request.DELETE)
222 );
223 MatcherAssert.assertThat(
224 query.body(),
225 Matchers.is(Matchers.emptyString())
226 );
227 container.stop();
228 }
229 }
230
231
232
233
234
235
236
237 private static JsonObject hook(final String name,
238 final Map<String, String> config) {
239 final JsonObjectBuilder builder = Json.createObjectBuilder();
240 for (final Map.Entry<String, String> entry : config.entrySet()) {
241 builder.add(entry.getKey(), entry.getValue());
242 }
243 return Json.createObjectBuilder()
244 .add("id", 1)
245 .add("name", name)
246 .add("config", builder)
247 .build();
248 }
249
250
251
252
253
254 private static Repo repo() {
255 final Repo repo = Mockito.mock(Repo.class);
256 Mockito.doReturn(new Coordinates.Simple("test", "hooks"))
257 .when(repo).coordinates();
258 return repo;
259 }
260 }