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.http.mock.MkAnswer;
33 import com.jcabi.http.mock.MkContainer;
34 import com.jcabi.http.mock.MkGrizzlyContainer;
35 import com.jcabi.http.request.ApacheRequest;
36 import java.net.HttpURLConnection;
37 import java.util.Arrays;
38 import java.util.stream.Collectors;
39 import javax.json.JsonValue;
40 import org.hamcrest.MatcherAssert;
41 import org.hamcrest.Matchers;
42 import org.hamcrest.collection.IsIterableContainingInAnyOrder;
43 import org.hamcrest.core.IsEqual;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.mockito.Mockito;
47
48
49
50
51
52
53
54
55 public final class RtHookTest {
56
57
58
59
60 @Rule
61 public final transient RandomPort resource = new RandomPort();
62
63
64
65
66
67
68 @Test
69 public void performsValidRequest() throws Exception {
70 try (
71 final MkContainer container = new MkGrizzlyContainer().next(
72 new MkAnswer.Simple(
73 HttpURLConnection.HTTP_OK,
74 "{\"test\":\"hook\"}"
75 )
76 ).start(this.resource.port())
77 ) {
78 final Hook hook = new RtHook(
79 new ApacheRequest(container.home()),
80 repo(),
81 1
82 );
83 MatcherAssert.assertThat(
84 hook.json(),
85 Matchers.notNullValue()
86 );
87 MatcherAssert.assertThat(
88 container.take().uri().toString(),
89 Matchers.endsWith("/repos/test/repo/hooks/1")
90 );
91 container.stop();
92 }
93 }
94
95
96
97
98
99
100 @Test
101 public void returnsEvents() throws Exception {
102 try (
103 final MkContainer container = new MkGrizzlyContainer().next(
104 new MkAnswer.Simple(
105 HttpURLConnection.HTTP_OK,
106 "{ \"id\": 1, \"events\": [ \"push\", \"pull_request\" ] }"
107 )
108 ).start(this.resource.port())
109 ) {
110 MatcherAssert.assertThat(
111 new RtHook(
112 new ApacheRequest(container.home()),
113 repo(),
114 1
115 ).json().getJsonArray("events")
116 .stream()
117 .map(JsonValue::toString)
118 .map(event -> event.replace("\"", ""))
119 .collect(Collectors.toList()),
120 new IsIterableContainingInAnyOrder<>(
121 Arrays.asList(
122 new IsEqual<>("push"),
123 new IsEqual<>("pull_request")
124 )
125 )
126 );
127 container.stop();
128 }
129 }
130
131
132
133
134
135 private static Repo repo() {
136 final Repo repo = Mockito.mock(Repo.class);
137 Mockito.doReturn(new Coordinates.Simple("test", "repo"))
138 .when(repo).coordinates();
139 return repo;
140 }
141
142 }